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_bmp.h"
45 static const char* fmtSignBmp = "BM";
50 m_signature = fmtSignBmp;
51 m_description = "Windows bitmap (*.bmp;*.dib)";
60 GrFmtReader* GrFmtBmp::NewReader( const char* filename )
62 return new GrFmtBmpReader( filename );
66 GrFmtWriter* GrFmtBmp::NewWriter( const char* filename )
68 return new GrFmtBmpWriter( filename );
72 /************************ BMP reader *****************************/
74 GrFmtBmpReader::GrFmtBmpReader( const char* filename ) : GrFmtReader( filename )
80 GrFmtBmpReader::~GrFmtBmpReader()
85 void GrFmtBmpReader::Close()
92 bool GrFmtBmpReader::ReadHeader()
96 assert( strlen(m_filename) != 0 );
97 if( !m_strm.Open( m_filename )) return false;
99 if( setjmp( m_strm.JmpBuf()) == 0 )
102 m_offset = m_strm.GetDWord();
104 int size = m_strm.GetDWord();
108 m_width = m_strm.GetDWord();
109 m_height = m_strm.GetDWord();
110 m_bpp = m_strm.GetDWord() >> 16;
111 m_rle_code = (BmpCompression)m_strm.GetDWord();
113 int clrused = m_strm.GetDWord();
114 m_strm.Skip( size - 36 );
116 if( m_width > 0 && m_height > 0 &&
117 (((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
118 m_bpp == 24 || m_bpp == 32 ) && m_rle_code == BMP_RGB) ||
119 (m_bpp == 16 && (m_rle_code == BMP_RGB || m_rle_code == BMP_BITFIELDS)) ||
120 (m_bpp == 4 && m_rle_code == BMP_RLE4) ||
121 (m_bpp == 8 && m_rle_code == BMP_RLE8)))
128 memset( m_palette, 0, sizeof(m_palette));
129 m_strm.GetBytes( m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );
130 m_iscolor = IsColorPalette( m_palette, m_bpp );
132 else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )
134 int redmask = m_strm.GetDWord();
135 int greenmask = m_strm.GetDWord();
136 int bluemask = m_strm.GetDWord();
138 if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )
140 else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )
145 else if( m_bpp == 16 && m_rle_code == BMP_RGB )
149 else if( size == 12 )
151 m_width = m_strm.GetWord();
152 m_height = m_strm.GetWord();
153 m_bpp = m_strm.GetDWord() >> 16;
154 m_rle_code = BMP_RGB;
156 if( m_width > 0 && m_height > 0 &&
157 (m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
158 m_bpp == 24 || m_bpp == 32 ))
163 int j, clrused = 1 << m_bpp;
164 m_strm.GetBytes( buffer, clrused*3 );
165 for( j = 0; j < clrused; j++ )
167 m_palette[j].b = buffer[3*j+0];
168 m_palette[j].g = buffer[3*j+1];
169 m_palette[j].r = buffer[3*j+2];
180 m_width = m_height = -1;
187 bool GrFmtBmpReader::ReadData( uchar* data, int step, int color )
189 const int buffer_size = 1 << 12;
190 uchar buffer[buffer_size];
191 uchar bgr_buffer[buffer_size];
192 uchar gray_palette[256];
195 uchar* bgr = bgr_buffer;
196 int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;
197 int nch = color ? 3 : 1;
198 int width3 = m_width*nch;
201 if( m_offset < 0 || !m_strm.IsOpened())
204 data += (m_height - 1)*step;
207 if( (m_bpp != 24 || !color) && src_pitch+32 > buffer_size )
208 src = new uchar[src_pitch+32];
214 CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
216 if( m_width*3 + 32 > buffer_size ) bgr = new uchar[m_width*3 + 32];
219 if( setjmp( m_strm.JmpBuf()) == 0 )
221 m_strm.SetPos( m_offset );
225 /************************* 1 BPP ************************/
227 for( y = 0; y < m_height; y++, data += step )
229 m_strm.GetBytes( src, src_pitch );
230 FillColorRow1( color ? data : bgr, src, m_width, m_palette );
232 icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );
237 /************************* 4 BPP ************************/
239 if( m_rle_code == BMP_RGB )
241 for( y = 0; y < m_height; y++, data += step )
243 m_strm.GetBytes( src, src_pitch );
245 FillColorRow4( data, src, m_width, m_palette );
247 FillGrayRow4( data, src, m_width, gray_palette );
251 else if( m_rle_code == BMP_RLE4 ) // rle4 compression
253 uchar* line_end = data + width3;
258 int code = m_strm.GetWord();
259 int len = code & 255;
261 if( len != 0 ) // encoded mode
267 clr[0] = m_palette[code >> 4];
268 clr[1] = m_palette[code & 15];
269 gray_clr[0] = gray_palette[code >> 4];
270 gray_clr[1] = gray_palette[code & 15];
272 uchar* end = data + len*nch;
273 if( end > line_end ) goto decode_rle4_bad;
277 WRITE_PIX( data, clr[t] );
282 while( (data += nch) < end );
284 else if( code > 2 ) // absolute mode
286 if( data + code*nch > line_end ) goto decode_rle4_bad;
287 m_strm.GetBytes( src, (((code + 1)>>1) + 1) & -2 );
289 data = FillColorRow4( data, src, code, m_palette );
291 data = FillGrayRow4( data, src, code, gray_palette );
295 int x_shift3 = (int)(line_end - data);
296 int y_shift = m_height - y;
300 x_shift3 = m_strm.GetByte()*nch;
301 y_shift = m_strm.GetByte();
304 len = x_shift3 + (y_shift * width3) & ((code == 0) - 1);
307 data = FillUniColor( data, line_end, step, width3,
308 y, m_height, x_shift3,
311 data = FillUniGray( data, line_end, step, width3,
312 y, m_height, x_shift3,
325 /************************* 8 BPP ************************/
327 if( m_rle_code == BMP_RGB )
329 for( y = 0; y < m_height; y++, data += step )
331 m_strm.GetBytes( src, src_pitch );
333 FillColorRow8( data, src, m_width, m_palette );
335 FillGrayRow8( data, src, m_width, gray_palette );
339 else if( m_rle_code == BMP_RLE8 ) // rle8 compression
341 uchar* line_end = data + width3;
342 int line_end_flag = 0;
347 int code = m_strm.GetWord();
348 int len = code & 255;
350 if( len != 0 ) // encoded mode
355 if( data + len > line_end )
356 goto decode_rle8_bad;
359 data = FillUniColor( data, line_end, step, width3,
363 data = FillUniGray( data, line_end, step, width3,
365 gray_palette[code] );
367 line_end_flag = y - prev_y;
369 else if( code > 2 ) // absolute mode
372 int code3 = code*nch;
374 if( data + code3 > line_end )
375 goto decode_rle8_bad;
376 m_strm.GetBytes( src, (code + 1) & -2 );
378 data = FillColorRow8( data, src, code, m_palette );
380 data = FillGrayRow8( data, src, code, gray_palette );
382 line_end_flag = y - prev_y;
386 int x_shift3 = (int)(line_end - data);
387 int y_shift = m_height - y;
389 if( code || !line_end_flag || x_shift3 < width3 )
393 x_shift3 = m_strm.GetByte()*nch;
394 y_shift = m_strm.GetByte();
397 x_shift3 += (y_shift * width3) & ((code == 0) - 1);
403 data = FillUniColor( data, line_end, step, width3,
404 y, m_height, x_shift3,
407 data = FillUniGray( data, line_end, step, width3,
408 y, m_height, x_shift3,
423 /************************* 15 BPP ************************/
425 for( y = 0; y < m_height; y++, data += step )
427 m_strm.GetBytes( src, src_pitch );
429 icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
431 icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
435 /************************* 16 BPP ************************/
437 for( y = 0; y < m_height; y++, data += step )
439 m_strm.GetBytes( src, src_pitch );
441 icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
443 icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
447 /************************* 24 BPP ************************/
449 for( y = 0; y < m_height; y++, data += step )
451 m_strm.GetBytes( color ? data : src, src_pitch );
453 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );
457 /************************* 32 BPP ************************/
459 for( y = 0; y < m_height; y++, data += step )
461 m_strm.GetBytes( src, src_pitch );
464 icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );
466 icvCvt_BGRA2BGR_8u_C4C3R( src, 0, data, 0, cvSize(m_width,1) );
475 if( src != buffer ) delete[] src;
476 if( bgr != bgr_buffer ) delete[] bgr;
481 //////////////////////////////////////////////////////////////////////////////////////////
483 GrFmtBmpWriter::GrFmtBmpWriter( const char* filename ) : GrFmtWriter( filename )
488 GrFmtBmpWriter::~GrFmtBmpWriter()
493 bool GrFmtBmpWriter::WriteImage( const uchar* data, int step,
494 int width, int height, int /*depth*/, int channels )
497 int fileStep = (width*channels + 3) & -4;
498 uchar zeropad[] = "\0\0\0\0";
500 assert( data && width > 0 && height > 0 && step >= fileStep );
502 if( m_strm.Open( m_filename ) )
504 int bitmapHeaderSize = 40;
505 int paletteSize = channels > 1 ? 0 : 1024;
506 int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
507 PaletteEntry palette[256];
509 // write signature 'BM'
510 m_strm.PutBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
513 m_strm.PutDWord( fileStep*height + headerSize ); // file size
514 m_strm.PutDWord( 0 );
515 m_strm.PutDWord( headerSize );
517 // write bitmap header
518 m_strm.PutDWord( bitmapHeaderSize );
519 m_strm.PutDWord( width );
520 m_strm.PutDWord( height );
522 m_strm.PutWord( channels << 3 );
523 m_strm.PutDWord( BMP_RGB );
524 m_strm.PutDWord( 0 );
525 m_strm.PutDWord( 0 );
526 m_strm.PutDWord( 0 );
527 m_strm.PutDWord( 0 );
528 m_strm.PutDWord( 0 );
532 FillGrayPalette( palette, 8 );
533 m_strm.PutBytes( palette, sizeof(palette));
537 data += step*(height - 1);
538 for( ; height--; data -= step )
540 m_strm.PutBytes( data, width );
541 if( fileStep > width )
542 m_strm.PutBytes( zeropad, fileStep - width );