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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
44 #include "grfmt_pxm.h"
52 m_description = "Portable image format (*.pbm;*.pgm;*.ppm;*.pxm;*.pnm)";
61 bool GrFmtPxM::CheckSignature( const char* signature )
63 return signature[0] == 'P' &&
64 '1' <= signature[1] && signature[1] <= '6' &&
65 isspace(signature[2]);
69 GrFmtReader* GrFmtPxM::NewReader( const char* filename )
71 return new GrFmtPxMReader( filename );
75 GrFmtWriter* GrFmtPxM::NewWriter( const char* filename )
77 return new GrFmtPxMWriter( filename );
81 ///////////////////////// P?M reader //////////////////////////////
83 static int ReadNumber( RLByteStream& strm, int maxdigits )
89 code = strm.GetByte();
99 code = strm.GetByte();
101 while( code != '\n' && code != '\r' );
104 code = strm.GetByte();
106 while( isspace(code))
107 code = strm.GetByte();
109 while( !isdigit( code ));
114 val = val*10 + code - '0';
115 if( ++digits >= maxdigits ) break;
116 code = strm.GetByte();
118 while( isdigit(code));
124 GrFmtPxMReader::GrFmtPxMReader( const char* filename ) : GrFmtReader( filename )
130 GrFmtPxMReader::~GrFmtPxMReader()
135 void GrFmtPxMReader::Close()
141 bool GrFmtPxMReader::ReadHeader()
145 assert( strlen(m_filename) != 0 );
146 if( !m_strm.Open( m_filename )) return false;
148 if( setjmp( m_strm.JmpBuf()) == 0 )
150 int code = m_strm.GetByte();
154 code = m_strm.GetByte();
157 case '1': case '4': m_bpp = 1; break;
158 case '2': case '5': m_bpp = 8; break;
159 case '3': case '6': m_bpp = 24; break;
160 default: BAD_HEADER_ERR();
163 m_binary = code >= '4';
164 m_iscolor = m_bpp > 8;
166 m_width = ReadNumber( m_strm, INT_MAX );
167 m_height = ReadNumber( m_strm, INT_MAX );
169 m_maxval = m_bpp == 1 ? 1 : ReadNumber( m_strm, INT_MAX );
170 if( m_maxval > 65535 )
173 //if( m_maxval > 255 ) m_binary = false; nonsense
177 if( m_width > 0 && m_height > 0 && m_maxval > 0 && m_maxval < (1 << 16))
179 m_offset = m_strm.GetPos();
189 m_width = m_height = -1;
196 bool GrFmtPxMReader::ReadData( uchar* data, int step, int color )
198 const int buffer_size = 1 << 12;
199 uchar buffer[buffer_size];
200 uchar pal_buffer[buffer_size];
201 PaletteEntry palette[256];
204 uchar* gray_palette = pal_buffer;
205 int src_pitch = (m_width*m_bpp*m_bit_depth/8 + 7)/8;
206 int nch = m_iscolor ? 3 : 1;
207 int width3 = m_width*nch;
210 if( m_offset < 0 || !m_strm.IsOpened())
213 if( src_pitch+32 > buffer_size )
214 src = new uchar[width3*m_bit_depth/8 + 32];
216 // create LUT for converting colors
217 if( m_bit_depth == 8 )
219 if( m_maxval + 1 > buffer_size )
220 gray_palette = new uchar[m_maxval + 1];
222 for( i = 0; i <= m_maxval; i++ )
224 gray_palette[i] = (uchar)((i*255/m_maxval)^(m_bpp == 1 ? 255 : 0));
227 FillGrayPalette( palette, m_bpp==1 ? 1 : 8 , m_bpp == 1 );
230 if( setjmp( m_strm.JmpBuf()) == 0 )
232 m_strm.SetPos( m_offset );
236 ////////////////////////// 1 BPP /////////////////////////
240 for( y = 0; y < m_height; y++, data += step )
242 for( x = 0; x < m_width; x++ )
243 src[x] = ReadNumber( m_strm, 1 ) != 0;
246 FillColorRow8( data, src, m_width, palette );
248 FillGrayRow8( data, src, m_width, gray_palette );
253 for( y = 0; y < m_height; y++, data += step )
255 m_strm.GetBytes( src, src_pitch );
258 FillColorRow1( data, src, m_width, palette );
260 FillGrayRow1( data, src, m_width, gray_palette );
266 ////////////////////////// 8 BPP /////////////////////////
269 for( y = 0; y < m_height; y++, data += step )
273 for( x = 0; x < width3; x++ )
275 int code = ReadNumber( m_strm, INT_MAX );
276 if( (unsigned)code > (unsigned)m_maxval ) code = m_maxval;
277 if( m_bit_depth == 8 )
278 src[x] = gray_palette[code];
280 ((ushort *)src)[x] = (ushort)code;
285 m_strm.GetBytes( src, src_pitch );
286 if( m_bit_depth == 16 && !isBigEndian() )
288 for( x = 0; x < width3; x++ )
290 uchar v = src[x * 2];
291 src[x * 2] = src[x * 2 + 1];
297 if( !m_native_depth && m_bit_depth == 16 )
299 for( x = 0; x < width3; x++ )
301 int v = ((ushort *)src)[x];
302 src[x] = (uchar)(v >> 8);
306 if( m_bpp == 8 ) // image has one channel
310 if( m_bit_depth == 8 || !m_native_depth ) {
311 uchar *d = data, *s = src, *end = src + m_width;
312 for( ; s < end; d += 3, s++)
313 d[0] = d[1] = d[2] = *s;
315 ushort *d = (ushort *)data, *s = (ushort *)src, *end = ((ushort *)src) + m_width;
316 for( ; s < end; s++, d += 3)
317 d[0] = d[1] = d[2] = *s;
320 else if( m_native_depth )
321 memcpy( data, src, m_width*m_bit_depth/8 );
323 memcpy( data, src, m_width );
329 if( m_bit_depth == 8 || !m_native_depth )
330 icvCvt_RGB2BGR_8u_C3R( src, 0, data, 0, cvSize(m_width,1) );
332 icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1) );
334 else if( m_bit_depth == 8 || !m_native_depth )
335 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1), 2 );
337 icvCvt_BGR2Gray_16u_C3C1R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1), 2 );
350 if( gray_palette != pal_buffer )
351 delete[] gray_palette;
357 //////////////////////////////////////////////////////////////////////////////////////////
359 GrFmtPxMWriter::GrFmtPxMWriter( const char* filename ) : GrFmtWriter( filename )
364 GrFmtPxMWriter::~GrFmtPxMWriter()
369 bool GrFmtPxMWriter::IsFormatSupported( int depth )
371 return depth == IPL_DEPTH_8U || depth == IPL_DEPTH_16U;
375 bool GrFmtPxMWriter::WriteImage( const uchar* data, int step,
376 int width, int height, int depth, int _channels )
378 bool isBinary = true;
381 int channels = _channels > 1 ? 3 : 1;
382 int fileStep = width*channels*(depth/8);
385 assert( data && width > 0 && height > 0 && step >= fileStep );
387 if( m_strm.Open( m_filename ) )
390 int bufferSize = 128; // buffer that should fit a header
394 lineLength = channels * width * depth / 8;
396 lineLength = (6 * channels + (channels > 1 ? 2 : 0)) * width + 32;
398 if( bufferSize < lineLength )
399 bufferSize = lineLength;
401 buffer = new char[bufferSize];
409 sprintf( buffer, "P%c\n%d %d\n%d\n",
410 '2' + (channels > 1 ? 1 : 0) + (isBinary ? 3 : 0),
411 width, height, (1 << depth) - 1 );
413 m_strm.PutBytes( buffer, (int)strlen(buffer) );
415 for( y = 0; y < height; y++, data += step )
422 icvCvt_BGR2RGB_8u_C3R( (uchar*)data, 0,
423 (uchar*)buffer, 0, cvSize(width,1) );
425 icvCvt_BGR2RGB_16u_C3R( (ushort*)data, 0,
426 (ushort*)buffer, 0, cvSize(width,1) );
429 // swap endianness if necessary
430 if( depth == 16 && !isBigEndian() )
433 memcpy( buffer, data, fileStep );
434 for( x = 0; x < width*channels*2; x += 2 )
437 buffer[x] = buffer[x + 1];
441 m_strm.PutBytes( (channels > 1 || depth > 8) ? buffer : (char*)data, fileStep );
451 for( x = 0; x < width*channels; x += channels )
453 sprintf( ptr, "% 4d", data[x + 2] );
455 sprintf( ptr, "% 4d", data[x + 1] );
457 sprintf( ptr, "% 4d", data[x] );
465 for( x = 0; x < width*channels; x += channels )
467 sprintf( ptr, "% 6d", ((ushort *)data)[x + 2] );
469 sprintf( ptr, "% 6d", ((ushort *)data)[x + 1] );
471 sprintf( ptr, "% 6d", ((ushort *)data)[x] );
482 for( x = 0; x < width; x++ )
484 sprintf( ptr, "% 4d", data[x] );
490 for( x = 0; x < width; x++ )
492 sprintf( ptr, "% 6d", ((ushort *)data)[x] );
500 m_strm.PutBytes( buffer, (int)(ptr - buffer) );