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.
42 #include "grfmt_pxm.h"
48 /************************ P*M reader *****************************/
50 static int ReadNumber( RLByteStream& strm, int maxdigits )
56 code = strm.GetByte();
63 code = strm.GetByte();
69 code = strm.GetByte();
71 while( code != '\n' && code != '\r' );
74 code = strm.GetByte();
76 while( !isdigit( code ));
81 val = val*10 + code - '0';
82 if( ++digits >= maxdigits ) break;
83 code = strm.GetByte();
85 while( isdigit(code));
91 GrFmtPXMReader::GrFmtPXMReader()
95 m_description = "Portable image format (*.pbm;*.pgm;*.ppm)";
100 bool GrFmtPXMReader::CheckFormat( const char* signature )
102 return strlen(signature) >= 3 && signature[0] == 'P' &&
103 '1' <= signature[1] && signature[1] <= '6' &&
104 isspace(signature[2]);
108 int GrFmtPXMReader::GetColor()
110 return m_bpp > 8 ? 1 : 0;
114 void GrFmtPXMReader::Close()
120 bool GrFmtPXMReader::ReadHeader()
124 assert( strlen(m_filename) != 0 );
125 if( !m_strm.Open( m_filename )) return false;
129 int code = m_strm.GetByte();
133 code = m_strm.GetByte();
136 case '1': case '4': m_bpp = 1; break;
137 case '2': case '5': m_bpp = 8; break;
138 case '3': case '6': m_bpp = 24; break;
139 default: BAD_HEADER_ERR();
142 m_binary = code >= '4';
144 m_width = ReadNumber( m_strm, INT_MAX );
145 m_height = ReadNumber( m_strm, INT_MAX );
147 m_maxval = m_bpp == 1 ? 1 : ReadNumber( m_strm, INT_MAX );
149 if( m_maxval > 255 ) m_binary = false;
151 if( m_width > 0 && m_height > 0 && m_maxval > 0 && m_maxval < (1 << 16))
153 m_offset = m_strm.GetPos();
164 m_width = m_height = -1;
171 bool GrFmtPXMReader::ReadData( uchar* data, int step, int color )
173 const int buffer_size = 1 << 12;
174 uchar buffer[buffer_size];
175 uchar pal_buffer[buffer_size];
176 uchar bgr_buffer[buffer_size];
177 PaletteEntry palette[256];
180 uchar* gray_palette = pal_buffer;
181 uchar* bgr = bgr_buffer;
182 int src_pitch = (m_width*m_bpp + 7)/8;
183 int nch = color ? 3 : 1;
184 int width3 = m_width*nch;
187 if( m_offset < 0 || !m_strm.IsOpened())
190 if( src_pitch+32 > buffer_size )
191 src = new uchar[width3 + 32];
193 if( m_maxval + 1 > buffer_size )
194 gray_palette = new uchar[m_maxval + 1];
196 if( m_width*3 + 32 > buffer_size )
197 bgr = new uchar[m_width*3 + 32];
199 // create LUT for converting colors
200 for( i = 0; i <= m_maxval; i++ )
202 gray_palette[i] = (uchar)(i*255/m_maxval);
205 FillGrayPalette( palette, 8 );
209 m_strm.SetPos( m_offset );
213 /************************* 1 BPP ************************/
217 for( y = 0; y < m_height; y++, data += step )
219 for( x = 0; x < m_width; x++ )
221 src[x] = gray_palette[ReadNumber( m_strm, 1 ) != 0];
225 FillColorRow8( data, src, m_width, palette );
227 memcpy( data, src, m_width );
232 for( y = 0; y < m_height; y++, data += step )
234 m_strm.GetBytes( src, src_pitch );
237 FillColorRow1( data, src, m_width, palette );
239 FillGrayRow1( data, src, m_width, gray_palette );
245 /************************* 8 BPP ************************/
248 for( y = 0; y < m_height; y++, data += step )
252 for( x = 0; x < width3; x++ )
254 int code = ReadNumber( m_strm, INT_MAX );
255 if( (unsigned)code > (unsigned)m_maxval ) code = m_maxval;
256 src[x] = gray_palette[code];
261 m_strm.GetBytes( src, width3 );
267 FillColorRow8( src, data, m_width, palette );
269 memcpy( data, src, m_width );
274 CvtRGBToGray( src, data, m_width );
276 CvtRGBToBGR( src, data, m_width );
292 if( bgr != bgr_buffer )
295 if( gray_palette != pal_buffer )