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.
43 #include "grfmt_sunras.h"
45 static const char* fmtSignSunRas = "\x59\xA6\x6A\x95";
47 // Sun Raster filter factory
49 GrFmtSunRaster::GrFmtSunRaster()
52 m_signature = fmtSignSunRas;
53 m_description = "Sun raster files (*.sr;*.ras)";
57 GrFmtSunRaster::~GrFmtSunRaster()
62 GrFmtReader* GrFmtSunRaster::NewReader( const char* filename )
64 return new GrFmtSunRasterReader( filename );
68 GrFmtWriter* GrFmtSunRaster::NewWriter( const char* filename )
70 return new GrFmtSunRasterWriter( filename );
74 /************************ Sun Raster reader *****************************/
76 GrFmtSunRasterReader::GrFmtSunRasterReader( const char* filename ) : GrFmtReader( filename )
82 GrFmtSunRasterReader::~GrFmtSunRasterReader()
87 void GrFmtSunRasterReader::Close()
93 bool GrFmtSunRasterReader::ReadHeader()
97 assert( strlen(m_filename) != 0 );
98 if( !m_strm.Open( m_filename )) return false;
100 if( setjmp( m_strm.JmpBuf()) == 0 )
103 m_width = m_strm.GetDWord();
104 m_height = m_strm.GetDWord();
105 m_bpp = m_strm.GetDWord();
106 int palSize = 3*(1 << m_bpp);
109 m_type = (SunRasType)m_strm.GetDWord();
110 m_maptype = (SunRasMapType)m_strm.GetDWord();
111 m_maplength = m_strm.GetDWord();
113 if( m_width > 0 && m_height > 0 &&
114 (m_bpp == 1 || m_bpp == 8 || m_bpp == 24 || m_bpp == 32) &&
115 (m_type == RAS_OLD || m_type == RAS_STANDARD ||
116 (m_type == RAS_BYTE_ENCODED && m_bpp == 8) || m_type == RAS_FORMAT_RGB) &&
117 (m_maptype == RMT_NONE && m_maplength == 0 ||
118 m_maptype == RMT_EQUAL_RGB && m_maplength <= palSize && m_bpp <= 8))
120 memset( m_palette, 0, sizeof(m_palette));
122 if( m_maplength != 0 )
127 m_strm.GetBytes( buffer, m_maplength, &readed );
128 if( readed == m_maplength )
131 palSize = m_maplength/3;
133 for( i = 0; i < palSize; i++ )
135 m_palette[i].b = buffer[i + 2*palSize];
136 m_palette[i].g = buffer[i + palSize];
137 m_palette[i].r = buffer[i];
141 m_iscolor = IsColorPalette( m_palette, m_bpp );
142 m_offset = m_strm.GetPos();
144 assert( m_offset == 32 + m_maplength );
150 m_iscolor = m_bpp > 8;
153 FillGrayPalette( m_palette, m_bpp );
155 m_offset = m_strm.GetPos();
157 assert( m_offset == 32 + m_maplength );
166 m_width = m_height = -1;
173 bool GrFmtSunRasterReader::ReadData( uchar* data, int step, int color )
175 const int buffer_size = 1 << 12;
176 uchar buffer[buffer_size];
177 uchar bgr_buffer[buffer_size];
178 uchar gray_palette[256];
181 uchar* bgr = bgr_buffer;
182 int src_pitch = ((m_width*m_bpp + 7)/8 + 1) & -2;
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[src_pitch+32];
193 if( m_width*3 + 32 > buffer_size )
194 bgr = new uchar[m_width*3 + 32];
196 if( !color && m_maptype == RMT_EQUAL_RGB )
197 CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
199 if( setjmp( m_strm.JmpBuf()) == 0 )
201 m_strm.SetPos( m_offset );
205 /************************* 1 BPP ************************/
207 if( m_type != RAS_BYTE_ENCODED )
209 for( y = 0; y < m_height; y++, data += step )
211 m_strm.GetBytes( src, src_pitch );
213 FillColorRow1( data, src, m_width, m_palette );
215 FillGrayRow1( data, src, m_width, gray_palette );
221 uchar* line_end = src + (m_width*m_bpp + 7)/8;
227 int max_count = (int)(line_end - tsrc);
228 int code = 0, len = 0, len1 = 0;
232 code = m_strm.GetByte();
235 len = m_strm.GetByte();
236 if( len != 0 ) break;
238 tsrc[len1] = (uchar)code;
240 while( ++len1 < max_count );
244 if( len > 0 ) // encoded mode
247 code = m_strm.GetByte();
248 if( len > line_end - tsrc )
251 goto bad_decoding_1bpp;
254 memset( tsrc, code, len );
258 if( tsrc >= line_end )
262 FillColorRow1( data, src, m_width, m_palette );
264 FillGrayRow1( data, src, m_width, gray_palette );
266 if( ++y >= m_height ) break;
274 /************************* 8 BPP ************************/
276 if( m_type != RAS_BYTE_ENCODED )
278 for( y = 0; y < m_height; y++, data += step )
280 m_strm.GetBytes( src, src_pitch );
282 FillColorRow8( data, src, m_width, m_palette );
284 FillGrayRow8( data, src, m_width, gray_palette );
290 uchar* line_end = data + width3;
295 int max_count = (int)(line_end - data);
296 int code = 0, len = 0, len1;
301 code = m_strm.GetByte();
304 len = m_strm.GetByte();
305 if( len != 0 ) break;
307 *tsrc++ = (uchar)code;
309 while( (max_count -= nch) > 0 );
311 len1 = (int)(tsrc - src);
316 FillColorRow8( data, src, len1, m_palette );
318 FillGrayRow8( data, src, len1, gray_palette );
322 if( len > 0 ) // encoded mode
325 code = m_strm.GetByte();
328 data = FillUniColor( data, line_end, step, width3,
332 data = FillUniGray( data, line_end, step, width3,
334 gray_palette[code] );
339 if( data == line_end )
341 if( m_strm.GetByte() != 0 )
342 goto bad_decoding_end;
344 data = line_end - width3;
345 if( ++y >= m_height ) break;
354 /************************* 24 BPP ************************/
356 for( y = 0; y < m_height; y++, data += step )
358 m_strm.GetBytes( color ? data : bgr, src_pitch );
362 if( m_type == RAS_FORMAT_RGB )
363 icvCvt_RGB2BGR_8u_C3R( data, 0, data, 0, cvSize(m_width,1) );
367 icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1),
368 m_type == RAS_FORMAT_RGB ? 2 : 0 );
373 /************************* 32 BPP ************************/
375 for( y = 0; y < m_height; y++, data += step )
377 /* hack: a0 b0 g0 r0 a1 b1 g1 r1 ... are written to src + 3,
378 so when we look at src + 4, we see b0 g0 r0 x b1 g1 g1 x ... */
379 m_strm.GetBytes( src + 3, src_pitch );
382 icvCvt_BGRA2BGR_8u_C4C3R( src + 4, 0, data, 0, cvSize(m_width,1),
383 m_type == RAS_FORMAT_RGB ? 2 : 0 );
385 icvCvt_BGRA2Gray_8u_C4C1R( src + 4, 0, data, 0, cvSize(m_width,1),
386 m_type == RAS_FORMAT_RGB ? 2 : 0 );
395 if( src != buffer ) delete[] src;
396 if( bgr != bgr_buffer ) delete[] bgr;
402 //////////////////////////////////////////////////////////////////////////////////////////
404 GrFmtSunRasterWriter::GrFmtSunRasterWriter( const char* filename ) : GrFmtWriter( filename )
409 GrFmtSunRasterWriter::~GrFmtSunRasterWriter()
414 bool GrFmtSunRasterWriter::WriteImage( const uchar* data, int step,
415 int width, int height, int /*depth*/, int channels )
418 int fileStep = (width*channels + 1) & -2;
421 assert( data && width > 0 && height > 0 && step >= fileStep);
423 if( m_strm.Open( m_filename ) )
425 m_strm.PutBytes( fmtSignSunRas, (int)strlen(fmtSignSunRas) );
426 m_strm.PutDWord( width );
427 m_strm.PutDWord( height );
428 m_strm.PutDWord( channels*8 );
429 m_strm.PutDWord( fileStep*height );
430 m_strm.PutDWord( RAS_STANDARD );
431 m_strm.PutDWord( RMT_NONE );
432 m_strm.PutDWord( 0 );
434 for( y = 0; y < height; y++, data += step )
435 m_strm.PutBytes( data, fileStep );