1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
45 #include "grfmt_pxm.h"
50 ///////////////////////// P?M reader //////////////////////////////
52 static int ReadNumber( RLByteStream& strm, int maxdigits )
58 code = strm.getByte();
68 code = strm.getByte();
70 while( code != '\n' && code != '\r' );
73 code = strm.getByte();
76 code = strm.getByte();
78 while( !isdigit( code ));
83 val = val*10 + code - '0';
84 if( ++digits >= maxdigits ) break;
85 code = strm.getByte();
87 while( isdigit(code));
93 PxMDecoder::PxMDecoder()
96 m_buf_supported = true;
100 PxMDecoder::~PxMDecoder()
105 size_t PxMDecoder::signatureLength() const
110 bool PxMDecoder::checkSignature( const string& signature ) const
112 return signature.size() >= 3 && signature[0] == 'P' &&
113 '1' <= signature[1] && signature[1] <= '6' &&
114 isspace(signature[2]);
117 ImageDecoder PxMDecoder::newDecoder() const
119 return new PxMDecoder;
122 void PxMDecoder::close()
128 bool PxMDecoder::readHeader()
134 if( !m_strm.open(m_buf) )
137 else if( !m_strm.open( m_filename ))
142 int code = m_strm.getByte();
144 throw RBS_BAD_HEADER;
146 code = m_strm.getByte();
149 case '1': case '4': m_bpp = 1; break;
150 case '2': case '5': m_bpp = 8; break;
151 case '3': case '6': m_bpp = 24; break;
152 default: throw RBS_BAD_HEADER;
155 m_binary = code >= '4';
156 m_type = m_bpp > 8 ? CV_8UC3 : CV_8UC1;
158 m_width = ReadNumber( m_strm, INT_MAX );
159 m_height = ReadNumber( m_strm, INT_MAX );
161 m_maxval = m_bpp == 1 ? 1 : ReadNumber( m_strm, INT_MAX );
162 if( m_maxval > 65535 )
163 throw RBS_BAD_HEADER;
165 //if( m_maxval > 255 ) m_binary = false; nonsense
167 m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));
169 if( m_width > 0 && m_height > 0 && m_maxval > 0 && m_maxval < (1 << 16))
171 m_offset = m_strm.getPos();
182 m_width = m_height = -1;
189 bool PxMDecoder::readData( Mat& img )
191 int color = img.channels() > 1;
192 uchar* data = img.data;
194 PaletteEntry palette[256];
196 int bit_depth = CV_ELEM_SIZE1(m_type)*8;
197 int src_pitch = (m_width*m_bpp*bit_depth/8 + 7)/8;
198 int nch = CV_MAT_CN(m_type);
199 int width3 = m_width*nch;
202 if( m_offset < 0 || !m_strm.isOpened())
205 AutoBuffer<uchar,1024> _src(src_pitch + 32);
207 AutoBuffer<uchar,1024> _gray_palette;
208 uchar* gray_palette = _gray_palette;
210 // create LUT for converting colors
213 _gray_palette.allocate(m_maxval + 1);
214 gray_palette = _gray_palette;
216 for( i = 0; i <= m_maxval; i++ )
217 gray_palette[i] = (uchar)((i*255/m_maxval)^(m_bpp == 1 ? 255 : 0));
219 FillGrayPalette( palette, m_bpp==1 ? 1 : 8 , m_bpp == 1 );
224 m_strm.setPos( m_offset );
228 ////////////////////////// 1 BPP /////////////////////////
232 for( y = 0; y < m_height; y++, data += step )
234 for( x = 0; x < m_width; x++ )
235 src[x] = ReadNumber( m_strm, 1 ) != 0;
238 FillColorRow8( data, src, m_width, palette );
240 FillGrayRow8( data, src, m_width, gray_palette );
245 for( y = 0; y < m_height; y++, data += step )
247 m_strm.getBytes( src, src_pitch );
250 FillColorRow1( data, src, m_width, palette );
252 FillGrayRow1( data, src, m_width, gray_palette );
258 ////////////////////////// 8 BPP /////////////////////////
261 for( y = 0; y < m_height; y++, data += step )
265 for( x = 0; x < width3; x++ )
267 int code = ReadNumber( m_strm, INT_MAX );
268 if( (unsigned)code > (unsigned)m_maxval ) code = m_maxval;
270 src[x] = gray_palette[code];
272 ((ushort *)src)[x] = (ushort)code;
277 m_strm.getBytes( src, src_pitch );
278 if( bit_depth == 16 && !isBigEndian() )
280 for( x = 0; x < width3; x++ )
282 uchar v = src[x * 2];
283 src[x * 2] = src[x * 2 + 1];
289 if( img.depth() == CV_8U && bit_depth == 16 )
291 for( x = 0; x < width3; x++ )
293 int v = ((ushort *)src)[x];
294 src[x] = (uchar)(v >> 8);
298 if( m_bpp == 8 ) // image has one channel
302 if( img.depth() == CV_8U ) {
303 uchar *d = data, *s = src, *end = src + m_width;
304 for( ; s < end; d += 3, s++)
305 d[0] = d[1] = d[2] = *s;
307 ushort *d = (ushort *)data, *s = (ushort *)src, *end = ((ushort *)src) + m_width;
308 for( ; s < end; s++, d += 3)
309 d[0] = d[1] = d[2] = *s;
313 memcpy( data, src, m_width*(bit_depth/8) );
319 if( img.depth() == CV_8U )
320 icvCvt_RGB2BGR_8u_C3R( src, 0, data, 0, cvSize(m_width,1) );
322 icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1) );
324 else if( img.depth() == CV_8U )
325 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1), 2 );
327 icvCvt_BGR2Gray_16u_C3C1R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1), 2 );
344 //////////////////////////////////////////////////////////////////////////////////////////
346 PxMEncoder::PxMEncoder()
348 m_description = "Portable image format (*.pbm;*.pgm;*.ppm;*.pxm;*.pnm)";
349 m_buf_supported = true;
353 PxMEncoder::~PxMEncoder()
358 ImageEncoder PxMEncoder::newEncoder() const
360 return new PxMEncoder;
364 bool PxMEncoder::isFormatSupported( int depth ) const
366 return depth == CV_8U || depth == CV_16U;
370 bool PxMEncoder::write( const Mat& img, const vector<int>& params )
372 bool isBinary = true;
374 int width = img.cols, height = img.rows;
375 int _channels = img.channels(), depth = img.elemSize1()*8;
376 int channels = _channels > 1 ? 3 : 1;
377 int fileStep = width*img.elemSize();
380 for( size_t i = 0; i < params.size(); i += 2 )
381 if( params[i] == CV_IMWRITE_PXM_BINARY )
382 isBinary = params[i+1] != 0;
388 if( !strm.open(*m_buf) )
390 int t = CV_MAKETYPE(img.depth(), channels);
391 m_buf->reserve( alignSize(256 + (isBinary ? fileStep*height :
392 ((t == CV_8UC1 ? 4 : t == CV_8UC3 ? 4*3+2 :
393 t == CV_16UC1 ? 6 : 6*3+2)*width+1)*height), 256));
395 else if( !strm.open(m_filename) )
399 int bufferSize = 128; // buffer that should fit a header
402 lineLength = width * img.elemSize();
404 lineLength = (6 * channels + (channels > 1 ? 2 : 0)) * width + 32;
406 if( bufferSize < lineLength )
407 bufferSize = lineLength;
409 AutoBuffer<char> _buffer(bufferSize);
410 char* buffer = _buffer;
413 sprintf( buffer, "P%c\n%d %d\n%d\n",
414 '2' + (channels > 1 ? 1 : 0) + (isBinary ? 3 : 0),
415 width, height, (1 << depth) - 1 );
417 strm.putBytes( buffer, (int)strlen(buffer) );
419 for( y = 0; y < height; y++ )
421 uchar* data = img.data + img.step*y;
427 icvCvt_BGR2RGB_8u_C3R( (uchar*)data, 0,
428 (uchar*)buffer, 0, cvSize(width,1) );
430 icvCvt_BGR2RGB_16u_C3R( (ushort*)data, 0,
431 (ushort*)buffer, 0, cvSize(width,1) );
434 // swap endianness if necessary
435 if( depth == 16 && !isBigEndian() )
438 memcpy( buffer, data, fileStep );
439 for( x = 0; x < width*channels*2; x += 2 )
442 buffer[x] = buffer[x + 1];
446 strm.putBytes( (channels > 1 || depth > 8) ? buffer : (char*)data, fileStep );
456 for( x = 0; x < width*channels; x += channels )
458 sprintf( ptr, "% 4d", data[x + 2] );
460 sprintf( ptr, "% 4d", data[x + 1] );
462 sprintf( ptr, "% 4d", data[x] );
470 for( x = 0; x < width*channels; x += channels )
472 sprintf( ptr, "% 6d", ((ushort *)data)[x + 2] );
474 sprintf( ptr, "% 6d", ((ushort *)data)[x + 1] );
476 sprintf( ptr, "% 6d", ((ushort *)data)[x] );
487 for( x = 0; x < width; x++ )
489 sprintf( ptr, "% 4d", data[x] );
495 for( x = 0; x < width; x++ )
497 sprintf( ptr, "% 6d", ((ushort *)data)[x] );
505 strm.putBytes( buffer, (int)(ptr - buffer) );