From 6172803012c679ec345c2e91207874d72ec62a89 Mon Sep 17 00:00:00 2001 From: Dimitrios Katsaros Date: Thu, 14 Jul 2016 15:27:36 +0200 Subject: [PATCH] imgcodecs: Implementation of PAM encoder/decoder This patch implements the PAM image format as defined at: http://netpbm.sourceforge.net/doc/pam.html The PAM format provides a generic means for storing 2 dimensional information. This is useful for opencv since there are cases where data gets translated into non standardized formats, which makes it difficult to store and load this information. --- modules/imgcodecs/include/opencv2/imgcodecs.hpp | 13 +- .../include/opencv2/imgcodecs/imgcodecs_c.h | 11 +- modules/imgcodecs/src/grfmt_pam.cpp | 719 +++++++++++++++++++++ modules/imgcodecs/src/grfmt_pam.hpp | 99 +++ modules/imgcodecs/src/grfmts.hpp | 1 + modules/imgcodecs/src/loadsave.cpp | 2 + 6 files changed, 843 insertions(+), 2 deletions(-) create mode 100644 modules/imgcodecs/src/grfmt_pam.cpp create mode 100644 modules/imgcodecs/src/grfmt_pam.hpp diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index ce827a7..d797737 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -88,7 +88,8 @@ enum ImwriteFlags { IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_DEFAULT. IMWRITE_PNG_BILEVEL = 18, //!< Binary level PNG, 0 or 1, default is 0. IMWRITE_PXM_BINARY = 32, //!< For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1. - IMWRITE_WEBP_QUALITY = 64 //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used. + IMWRITE_WEBP_QUALITY = 64, //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used. + IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format }; //! Imwrite PNG specific flags used to tune the compression algorithm. @@ -107,6 +108,16 @@ enum ImwritePNGFlags { IMWRITE_PNG_STRATEGY_FIXED = 4 //!< Using this value prevents the use of dynamic Huffman codes, allowing for a simpler decoder for special applications. }; +//! Imwrite PAM specific tupletype flags used to define the 'TUPETYPE' field of a PAM file. +enum ImwritePAMFlags { + IMWRITE_PAM_FORMAT_NULL = 0, + IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, + IMWRITE_PAM_FORMAT_GRAYSCALE = 2, + IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, + IMWRITE_PAM_FORMAT_RGB = 4, + IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, + }; + /** @brief Loads an image from a file. @anchor imread diff --git a/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h b/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h index ad793cc..27ab35d 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h +++ b/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h @@ -92,9 +92,18 @@ enum CV_IMWRITE_PNG_STRATEGY_RLE =3, CV_IMWRITE_PNG_STRATEGY_FIXED =4, CV_IMWRITE_PXM_BINARY =32, - CV_IMWRITE_WEBP_QUALITY =64 + CV_IMWRITE_WEBP_QUALITY =64, + CV_IMWRITE_PAM_TUPLETYPE = 128, + CV_IMWRITE_PAM_FORMAT_NULL = 0, + CV_IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, + CV_IMWRITE_PAM_FORMAT_GRAYSCALE = 2, + CV_IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, + CV_IMWRITE_PAM_FORMAT_RGB = 4, + CV_IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, }; + + /* save image to file */ CVAPI(int) cvSaveImage( const char* filename, const CvArr* image, const int* params CV_DEFAULT(0) ); diff --git a/modules/imgcodecs/src/grfmt_pam.cpp b/modules/imgcodecs/src/grfmt_pam.cpp new file mode 100644 index 0000000..ac7198a --- /dev/null +++ b/modules/imgcodecs/src/grfmt_pam.cpp @@ -0,0 +1,719 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// (3-clause BSD License) +// +// Copyright (C) 2000-2016, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. +// Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. +// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. +// Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. +// Copyright (C) 2015-2016, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the names of the copyright holders nor the names of the contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall copyright holders or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + + +#include + +#include "precomp.hpp" +#include "utils.hpp" +#include "grfmt_pam.hpp" + +/* the PAM related fields */ +#define MAX_PAM_HEADER_IDENITFIER_LENGTH 8 +#define MAX_PAM_HEADER_VALUE_LENGTH 255 + +/* PAM header fileds */ +typedef enum { + PAM_HEADER_NONE, + PAM_HEADER_COMMENT, + PAM_HEADER_ENDHDR, + PAM_HEADER_HEIGHT, + PAM_HEADER_WIDTH, + PAM_HEADER_DEPTH, + PAM_HEADER_MAXVAL, + PAM_HEADER_TUPLTYPE, +} PamHeaderFieldType; + +struct pam_header_field { + PamHeaderFieldType type; + char identifier[MAX_PAM_HEADER_IDENITFIER_LENGTH+1]; +}; + +const static struct pam_header_field fields[] = { + {PAM_HEADER_ENDHDR, "ENDHDR"}, + {PAM_HEADER_HEIGHT, "HEIGHT"}, + {PAM_HEADER_WIDTH, "WIDTH"}, + {PAM_HEADER_DEPTH, "DEPTH"}, + {PAM_HEADER_MAXVAL, "MAXVAL"}, + {PAM_HEADER_TUPLTYPE, "TUPLTYPE"}, +}; +#define PAM_FIELDS_NO (sizeof (fields) / sizeof ((fields)[0])) + +typedef bool (*cvtFunc) (void *src, void *target, int width, int target_channels, + int target_depth); + +struct channel_layout { + uint rchan, gchan, bchan, graychan; +}; + +struct pam_format { + uint fmt; + char name[MAX_PAM_HEADER_VALUE_LENGTH+1]; + cvtFunc cvt_func; + /* the channel layout that should be used when + * imread_ creates a 3 channel or 1 channel image + * used when no conversion function is available + */ + struct channel_layout layout; +}; + +static bool rgb_convert (void *src, void *target, int width, int target_channels, + int target_depth); + +const static struct pam_format formats[] = { + {CV_IMWRITE_PAM_FORMAT_NULL, "", NULL, {0, 0, 0, 0} }, + {CV_IMWRITE_PAM_FORMAT_BLACKANDWHITE, "BLACKANDWHITE", NULL, {0, 0, 0, 0} }, + {CV_IMWRITE_PAM_FORMAT_GRAYSCALE, "GRAYSCALE", NULL, {0, 0, 0, 0} }, + {CV_IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA, "GRAYSCALE_ALPHA", NULL, {0, 0, 0, 0} }, + {CV_IMWRITE_PAM_FORMAT_RGB, "RGB", rgb_convert, {0, 1, 2, 0} }, + {CV_IMWRITE_PAM_FORMAT_RGB_ALPHA, "RGB_ALPHA", NULL, {0, 1, 2, 0} }, +}; +#define PAM_FORMATS_NO (sizeof (fields) / sizeof ((fields)[0])) + +/* + * conversion functions + */ + +static bool +rgb_convert (void *src, void *target, int width, int target_channels, int target_depth) +{ + bool ret = false; + if (target_channels == 3) { + switch (target_depth) { + case CV_8U: + icvCvt_RGB2BGR_8u_C3R( (uchar*) src, 0, (uchar*) target, 0, + cvSize(width,1) ); + ret = true; + break; + case CV_16U: + icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)target, 0, + cvSize(width,1) ); + ret = true; + break; + default: + break; + } + } else if (target_channels == 1) { + switch (target_depth) { + case CV_8U: + icvCvt_BGR2Gray_8u_C3C1R( (uchar*) src, 0, (uchar*) target, 0, + cvSize(width,1), 2 ); + ret = true; + break; + case CV_16U: + icvCvt_BGRA2Gray_16u_CnC1R( (ushort *)src, 0, (ushort *)target, 0, + cvSize(width,1), 3, 2 ); + ret = true; + break; + default: + break; + } + } + return ret; +} + +/* + * copy functions used as a fall back for undefined formats + * or simpler conversion options + */ + +static void +basic_conversion (void *src, const struct channel_layout *layout, int src_sampe_size, + int src_width, void *target, int target_channels, int target_depth) +{ + switch (target_depth) { + case CV_8U: + { + uchar *d = (uchar *)target, *s = (uchar *)src, + *end = ((uchar *)src) + src_width; + switch (target_channels) { + case 1: + for( ; s < end; d += 3, s += src_sampe_size ) + d[0] = d[1] = d[2] = s[layout->graychan]; + break; + case 3: + for( ; s < end; d += 3, s += src_sampe_size ) { + d[0] = s[layout->bchan]; + d[1] = s[layout->gchan]; + d[2] = s[layout->rchan]; + } + break; + default: + assert (0); + } + break; + } + case CV_16U: + { + ushort *d = (ushort *)target, *s = (ushort *)src, + *end = ((ushort *)src) + src_width; + switch (target_channels) { + case 1: + for( ; s < end; d += 3, s += src_sampe_size ) + d[0] = d[1] = d[2] = s[layout->graychan]; + break; + case 3: + for( ; s < end; d += 3, s += src_sampe_size ) { + d[0] = s[layout->bchan]; + d[1] = s[layout->gchan]; + d[2] = s[layout->rchan]; + } + break; + default: + assert (0); + } + break; + } + default: + assert (0); + } +} + + +static bool ReadPAMHeaderLine (cv::RLByteStream& strm, + PamHeaderFieldType &fieldtype, + char value[MAX_PAM_HEADER_VALUE_LENGTH+1]) +{ + int code, pos; + bool ident_found = false; + uint i; + char ident[MAX_PAM_HEADER_IDENITFIER_LENGTH+1] = { 0 }; + + do { + code = strm.getByte(); + } while ( isspace(code) ); + + if (code == '#') { + /* we are in a comment, eat characters until linebreak */ + do + { + code = strm.getByte(); + } while( code != '\n' && code != '\r' ); + fieldtype = PAM_HEADER_COMMENT; + return true; + } else if (code == '\n' || code == '\r' ) { + fieldtype = PAM_HEADER_NONE; + return true; + } + + /* nul-ify buffers before writing to them */ + memset (ident, '\0', sizeof(char) * MAX_PAM_HEADER_IDENITFIER_LENGTH); + for (i=0; i= 0 && isspace(value[pos])) + value[pos--] = '\0'; + + return true; +} + +static bool ParseNumber (char *str, int *retval) +{ + char *endptr; + long lval = strtol (str, &endptr, 0); + + if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) + || (errno != 0 && lval == 0)) { + return false; + } + if (endptr == str) { + return false; + } + + *retval = (int) lval; + + return true; +} + +namespace cv +{ + +PAMDecoder::PAMDecoder() +{ + m_offset = -1; + m_buf_supported = true; + bit_mode = false; + selected_fmt = CV_IMWRITE_PAM_FORMAT_NULL; +} + + +PAMDecoder::~PAMDecoder() +{ + m_strm.close(); +} + +size_t PAMDecoder::signatureLength() const +{ + return 3; +} + +bool PAMDecoder::checkSignature( const String& signature ) const +{ + return signature.size() >= 3 && signature[0] == 'P' && + signature[1] == '7' && + isspace(signature[2]); +} + +ImageDecoder PAMDecoder::newDecoder() const +{ + return makePtr(); +} + +struct parsed_fields +{ + bool endhdr, height, width, depth, maxval; +}; + +#define HEADER_READ_CORRECT(pf) (pf.endhdr && pf.height && pf.width \ + && pf.depth && pf.maxval) + + +bool PAMDecoder::readHeader() +{ + PamHeaderFieldType fieldtype = PAM_HEADER_NONE; + char value[MAX_PAM_HEADER_VALUE_LENGTH+1]; + int byte; + struct parsed_fields flds; + if( !m_buf.empty() ) + { + if( !m_strm.open(m_buf) ) + return false; + } + else if( !m_strm.open( m_filename )) + return false; + try + { + byte = m_strm.getByte(); + if( byte != 'P' ) + throw RBS_BAD_HEADER; + + byte = m_strm.getByte(); + if (byte != '7') + throw RBS_BAD_HEADER; + + byte = m_strm.getByte(); + if (byte != '\n' && byte != '\r') + throw RBS_BAD_HEADER; + + uint i; + memset (&flds, 0x00, sizeof (struct parsed_fields)); + do { + if (!ReadPAMHeaderLine(m_strm, fieldtype, value)) + throw RBS_BAD_HEADER; + switch (fieldtype) { + case PAM_HEADER_NONE: + case PAM_HEADER_COMMENT: + continue; + case PAM_HEADER_ENDHDR: + flds.endhdr = true; + break; + case PAM_HEADER_HEIGHT: + if (flds.height) + throw RBS_BAD_HEADER; + if (!ParseNumber (value, &m_height)) + throw RBS_BAD_HEADER; + flds.height = true; + break; + case PAM_HEADER_WIDTH: + if (flds.width) + throw RBS_BAD_HEADER; + if (!ParseNumber (value, &m_width)) + throw RBS_BAD_HEADER; + flds.width = true; + break; + case PAM_HEADER_DEPTH: + if (flds.depth) + throw RBS_BAD_HEADER; + if (!ParseNumber (value, &m_channels)) + throw RBS_BAD_HEADER; + flds.depth = true; + break; + case PAM_HEADER_MAXVAL: + if (flds.maxval) + throw RBS_BAD_HEADER; + if (!ParseNumber (value, &m_maxval)) + throw RBS_BAD_HEADER; + if ( m_maxval > 65535 ) + throw RBS_BAD_HEADER; + if ( m_maxval > 255 ) { + m_sampledepth = CV_16U; + } + else + m_sampledepth = CV_8U; + if (m_maxval == 1) + bit_mode = true; + flds.maxval = true; + break; + case PAM_HEADER_TUPLTYPE: + for (i=0; i _src(src_elems_per_row * 2); + uchar* src = _src; + AutoBuffer _gray_palette; + uchar* gray_palette = _gray_palette; + + if( m_offset < 0 || !m_strm.isOpened()) + return false; + + if (selected_fmt != CV_IMWRITE_PAM_FORMAT_NULL) + fmt = &formats[selected_fmt]; + else { + /* default layout handling */ + if (m_channels >= 3) { + layout.bchan = 0; + layout.gchan = 1; + layout.rchan = 2; + } else + layout.bchan = layout.gchan = layout.rchan = 0; + layout.graychan = 0; + } + + try + { + m_strm.setPos( m_offset ); + + /* the case where data fits the opencv matrix */ + if (m_sampledepth == img.depth() && target_channels == m_channels && !bit_mode) { + /* special case for 16bit images with wrong endianess */ + if (m_sampledepth == CV_16U && !isBigEndian()) + { + for (y = 0; y < m_height; y++, data += imp_stride ) + { + m_strm.getBytes( src, src_stride ); + for( x = 0; x < src_elems_per_row; x++ ) + { + uchar v = src[x * 2]; + data[x * 2] = src[x * 2 + 1]; + data[x * 2 + 1] = v; + } + } + } + else { + m_strm.getBytes( data, src_stride * m_height ); + } + + } + else { + /* black and white mode */ + if (bit_mode) { + if( target_channels == 1 ) + { + _gray_palette.allocate(2); + gray_palette = _gray_palette; + gray_palette[0] = 0; + gray_palette[1] = 255; + for( y = 0; y < m_height; y++, data += imp_stride ) + { + m_strm.getBytes( src, src_stride ); + FillGrayRow1( data, src, m_width, gray_palette ); + } + } else if ( target_channels == 3 ) + { + FillGrayPalette( palette, 1 , false ); + for( y = 0; y < m_height; y++, data += imp_stride ) + { + m_strm.getBytes( src, src_stride ); + FillColorRow1( data, src, m_width, palette ); + } + } + } else { + for (y = 0; y < m_height; y++, data += imp_stride ) + { + m_strm.getBytes( src, src_stride ); + + /* endianess correction */ + if( m_sampledepth == CV_16U && !isBigEndian() ) + { + for( x = 0; x < src_elems_per_row; x++ ) + { + uchar v = src[x * 2]; + src[x * 2] = src[x * 2 + 1]; + src[x * 2 + 1] = v; + } + } + + /* scale down */ + if( img.depth() == CV_8U && m_sampledepth == CV_16U ) + { + for( x = 0; x < src_elems_per_row; x++ ) + { + int v = ((ushort *)src)[x]; + src[x] = (uchar)(v >> 8); + } + } + + /* if we are only scaling up/down then we can then copy the data */ + if (target_channels == m_channels) { + memcpy (data, src, imp_stride); + } + /* perform correct conversion based on format */ + else if (fmt) { + funcout = false; + if (fmt->cvt_func) + funcout = fmt->cvt_func (src, data, m_width, target_channels, + img.depth()); + /* fall back to default if there is no conversion function or it + * can't handle the specified characteristics + */ + if (!funcout) + basic_conversion (src, &fmt->layout, m_channels, + m_width, data, target_channels, img.depth()); + + /* default to selecting the first available channels */ + } else { + basic_conversion (src, &layout, m_channels, + m_width, data, target_channels, img.depth()); + } + } + } + } + + res = true; + } catch(...) + { + } + + return res; +} + + +////////////////////////////////////////////////////////////////////////////////////////// + +PAMEncoder::PAMEncoder() +{ + m_description = "Portable arbitrary format (*.pam)"; + m_buf_supported = true; +} + + +PAMEncoder::~PAMEncoder() +{ +} + + +ImageEncoder PAMEncoder::newEncoder() const +{ + return makePtr(); +} + + +bool PAMEncoder::isFormatSupported( int depth ) const +{ + return depth == CV_8U || depth == CV_16U; +} + + +bool PAMEncoder::write( const Mat& img, const std::vector& params ) +{ + + WLByteStream strm; + + int width = img.cols, height = img.rows; + int stride = width*(int)img.elemSize(); + const uchar* data = img.ptr(); + const struct pam_format *fmt = NULL; + int x, y, tmp, bufsize = 256; + + /* parse save file type */ + for( size_t i = 0; i < params.size(); i += 2 ) + if( params[i] == CV_IMWRITE_PAM_TUPLETYPE ) { + if ( params[i+1] > CV_IMWRITE_PAM_FORMAT_NULL && + params[i+1] < (int) PAM_FORMATS_NO) + fmt = &formats[params[i+1]]; + } + + if( m_buf ) + { + if( !strm.open(*m_buf) ) + return false; + m_buf->reserve( alignSize(256 + stride*height, 256)); + } + else if( !strm.open(m_filename) ) + return false; + + tmp = width * (int)img.elemSize(); + + if (bufsize < tmp) + bufsize = tmp; + + AutoBuffer _buffer(bufsize); + char* buffer = _buffer; + + /* write header */ + tmp = 0; + tmp += sprintf( buffer, "P7\n"); + tmp += sprintf( buffer + tmp, "WIDTH %d\n", width); + tmp += sprintf( buffer + tmp, "HEIGHT %d\n", height); + tmp += sprintf( buffer + tmp, "DEPTH %d\n", img.channels()); + tmp += sprintf( buffer + tmp, "MAXVAL %d\n", (1 << img.elemSize1()*8) - 1); + if (fmt) + tmp += sprintf( buffer + tmp, "TUPLTYPE %s\n", fmt->name ); + tmp += sprintf( buffer + tmp, "ENDHDR\n" ); + + strm.putBytes( buffer, (int)strlen(buffer) ); + /* write data */ + if (img.depth() == CV_8U) + strm.putBytes( data, stride*height ); + else if (img.depth() == CV_16U) { + /* fix endianess */ + if (!isBigEndian()) { + for( y = 0; y < height; y++ ) { + memcpy( buffer, img.ptr(y), stride ); + for( x = 0; x < stride; x += 2 ) + { + uchar v = buffer[x]; + buffer[x] = buffer[x + 1]; + buffer[x + 1] = v; + } + strm.putBytes( buffer, stride ); + } + } else + strm.putBytes( data, stride*height ); + } else + assert (0); + + strm.close(); + return true; +} + +} diff --git a/modules/imgcodecs/src/grfmt_pam.hpp b/modules/imgcodecs/src/grfmt_pam.hpp new file mode 100644 index 0000000..8b3b1f1 --- /dev/null +++ b/modules/imgcodecs/src/grfmt_pam.hpp @@ -0,0 +1,99 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// (3-clause BSD License) +// +// Copyright (C) 2000-2016, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. +// Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. +// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. +// Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. +// Copyright (C) 2015-2016, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the names of the copyright holders nor the names of the contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall copyright holders or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +// +//M*/ + +//Based on "imgcodecs/src/grfmt_pxm.hpp" +//Written by Dimitrios Katsaros + +#ifndef _OPENCV_PAM_HPP_ +#define _OPENCV_PAM_HPP_ + +#include "grfmt_base.hpp" +#include "bitstrm.hpp" + +namespace cv +{ + +class PAMDecoder : public BaseImageDecoder +{ +public: + + PAMDecoder(); + virtual ~PAMDecoder(); + + bool readData( Mat& img ); + bool readHeader(); + + size_t signatureLength() const; + bool checkSignature( const String& signature ) const; + ImageDecoder newDecoder() const; + +protected: + + RLByteStream m_strm; + int m_maxval, m_channels, m_sampledepth, m_offset, + selected_fmt; + bool bit_mode; +}; + + +class PAMEncoder : public BaseImageEncoder +{ +public: + PAMEncoder(); + virtual ~PAMEncoder(); + + bool isFormatSupported( int depth ) const; + bool write( const Mat& img, const std::vector& params ); + + ImageEncoder newEncoder() const; +}; + +} + +#endif /* _OPENCV_PAM_HPP_ */ \ No newline at end of file diff --git a/modules/imgcodecs/src/grfmts.hpp b/modules/imgcodecs/src/grfmts.hpp index 7db1ac9..10bd882 100644 --- a/modules/imgcodecs/src/grfmts.hpp +++ b/modules/imgcodecs/src/grfmts.hpp @@ -55,5 +55,6 @@ #include "grfmt_hdr.hpp" #include "grfmt_gdal.hpp" #include "grfmt_gdcm.hpp" +#include "grfmt_pam.hpp" #endif/*_GRFMTS_H_*/ diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index 1c0b794..36e6349 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -109,6 +109,8 @@ struct ImageCodecInitializer /// Attach the GDAL Decoder decoders.push_back( makePtr() ); #endif/*HAVE_GDAL*/ + decoders.push_back( makePtr() ); + encoders.push_back( makePtr() ); } std::vector decoders; -- 2.7.4