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.
43 #include "precomp.hpp"
44 #include "grfmt_bmp.hpp"
49 static const char* fmtSignBmp = "BM";
51 /************************ BMP decoder *****************************/
53 BmpDecoder::BmpDecoder()
55 m_signature = fmtSignBmp;
57 m_buf_supported = true;
61 BmpDecoder::~BmpDecoder()
66 void BmpDecoder::close()
71 ImageDecoder BmpDecoder::newDecoder() const
73 return new BmpDecoder;
76 bool BmpDecoder::readHeader()
83 if( !m_strm.open( m_buf ) )
86 else if( !m_strm.open( m_filename ))
92 m_offset = m_strm.getDWord();
94 int size = m_strm.getDWord();
98 m_width = m_strm.getDWord();
99 m_height = m_strm.getDWord();
100 m_bpp = m_strm.getDWord() >> 16;
101 m_rle_code = (BmpCompression)m_strm.getDWord();
103 int clrused = m_strm.getDWord();
104 m_strm.skip( size - 36 );
106 if( m_width > 0 && m_height != 0 &&
107 (((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
108 m_bpp == 24 || m_bpp == 32 ) && m_rle_code == BMP_RGB) ||
109 (m_bpp == 16 && (m_rle_code == BMP_RGB || m_rle_code == BMP_BITFIELDS)) ||
110 (m_bpp == 4 && m_rle_code == BMP_RLE4) ||
111 (m_bpp == 8 && m_rle_code == BMP_RLE8)))
118 CV_Assert(clrused < 256);
119 memset(m_palette, 0, sizeof(m_palette));
120 m_strm.getBytes(m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );
121 iscolor = IsColorPalette( m_palette, m_bpp );
123 else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )
125 int redmask = m_strm.getDWord();
126 int greenmask = m_strm.getDWord();
127 int bluemask = m_strm.getDWord();
129 if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )
131 else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )
136 else if( m_bpp == 16 && m_rle_code == BMP_RGB )
140 else if( size == 12 )
142 m_width = m_strm.getWord();
143 m_height = m_strm.getWord();
144 m_bpp = m_strm.getDWord() >> 16;
145 m_rle_code = BMP_RGB;
147 if( m_width > 0 && m_height != 0 &&
148 (m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
149 m_bpp == 24 || m_bpp == 32 ))
154 int j, clrused = 1 << m_bpp;
155 m_strm.getBytes( buffer, clrused*3 );
156 for( j = 0; j < clrused; j++ )
158 m_palette[j].b = buffer[3*j+0];
159 m_palette[j].g = buffer[3*j+1];
160 m_palette[j].r = buffer[3*j+2];
171 m_type = iscolor ? CV_8UC3 : CV_8UC1;
172 m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL;
173 m_height = std::abs(m_height);
178 m_width = m_height = -1;
185 bool BmpDecoder::readData( Mat& img )
187 uchar* data = img.data;
188 int step = (int)img.step;
189 bool color = img.channels() > 1;
190 uchar gray_palette[256];
192 int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;
193 int nch = color ? 3 : 1;
194 int y, width3 = m_width*nch;
196 if( m_offset < 0 || !m_strm.isOpened())
199 if( m_origin == IPL_ORIGIN_BL )
201 data += (m_height - 1)*step;
205 AutoBuffer<uchar> _src, _bgr;
206 _src.allocate(src_pitch + 32);
212 CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
214 _bgr.allocate(m_width*3 + 32);
216 uchar *src = _src, *bgr = _bgr;
220 m_strm.setPos( m_offset );
224 /************************* 1 BPP ************************/
226 for( y = 0; y < m_height; y++, data += step )
228 m_strm.getBytes( src, src_pitch );
229 FillColorRow1( color ? data : bgr, src, m_width, m_palette );
231 icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );
236 /************************* 4 BPP ************************/
238 if( m_rle_code == BMP_RGB )
240 for( y = 0; y < m_height; y++, data += step )
242 m_strm.getBytes( src, src_pitch );
244 FillColorRow4( data, src, m_width, m_palette );
246 FillGrayRow4( data, src, m_width, gray_palette );
250 else if( m_rle_code == BMP_RLE4 ) // rle4 compression
252 uchar* line_end = data + width3;
257 int code = m_strm.getWord();
258 int len = code & 255;
260 if( len != 0 ) // encoded mode
266 clr[0] = m_palette[code >> 4];
267 clr[1] = m_palette[code & 15];
268 gray_clr[0] = gray_palette[code >> 4];
269 gray_clr[1] = gray_palette[code & 15];
271 uchar* end = data + len*nch;
272 if( end > line_end ) goto decode_rle4_bad;
276 WRITE_PIX( data, clr[t] );
281 while( (data += nch) < end );
283 else if( code > 2 ) // absolute mode
285 if( data + code*nch > line_end ) goto decode_rle4_bad;
286 int sz = (((code + 1)>>1) + 1) & (~1);
287 CV_Assert((size_t)sz < _src.getSize());
288 m_strm.getBytes(src, sz);
290 data = FillColorRow4( data, src, code, m_palette );
292 data = FillGrayRow4( data, src, code, gray_palette );
296 int x_shift3 = (int)(line_end - data);
297 int y_shift = m_height - y;
301 x_shift3 = m_strm.getByte()*nch;
302 y_shift = m_strm.getByte();
305 len = x_shift3 + ((y_shift * width3) & ((code == 0) - 1));
308 data = FillUniColor( data, line_end, step, width3,
309 y, m_height, x_shift3,
312 data = FillUniGray( data, line_end, step, width3,
313 y, m_height, x_shift3,
326 /************************* 8 BPP ************************/
328 if( m_rle_code == BMP_RGB )
330 for( y = 0; y < m_height; y++, data += step )
332 m_strm.getBytes( src, src_pitch );
334 FillColorRow8( data, src, m_width, m_palette );
336 FillGrayRow8( data, src, m_width, gray_palette );
340 else if( m_rle_code == BMP_RLE8 ) // rle8 compression
342 uchar* line_end = data + width3;
343 int line_end_flag = 0;
348 int code = m_strm.getWord();
349 int len = code & 255;
351 if( len != 0 ) // encoded mode
356 if( data + len > line_end )
357 goto decode_rle8_bad;
360 data = FillUniColor( data, line_end, step, width3,
364 data = FillUniGray( data, line_end, step, width3,
366 gray_palette[code] );
368 line_end_flag = y - prev_y;
370 else if( code > 2 ) // absolute mode
373 int code3 = code*nch;
375 if( data + code3 > line_end )
376 goto decode_rle8_bad;
377 int sz = (code + 1) & (~1);
378 CV_Assert((size_t)sz < _src.getSize());
379 m_strm.getBytes(src, sz);
381 data = FillColorRow8( data, src, code, m_palette );
383 data = FillGrayRow8( data, src, code, gray_palette );
385 line_end_flag = y - prev_y;
389 int x_shift3 = (int)(line_end - data);
390 int y_shift = m_height - y;
392 if( code || !line_end_flag || x_shift3 < width3 )
396 x_shift3 = m_strm.getByte()*nch;
397 y_shift = m_strm.getByte();
400 x_shift3 += (y_shift * width3) & ((code == 0) - 1);
406 data = FillUniColor( data, line_end, step, width3,
407 y, m_height, x_shift3,
410 data = FillUniGray( data, line_end, step, width3,
411 y, m_height, x_shift3,
428 /************************* 15 BPP ************************/
430 for( y = 0; y < m_height; y++, data += step )
432 m_strm.getBytes( src, src_pitch );
434 icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
436 icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
440 /************************* 16 BPP ************************/
442 for( y = 0; y < m_height; y++, data += step )
444 m_strm.getBytes( src, src_pitch );
446 icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
448 icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
452 /************************* 24 BPP ************************/
454 for( y = 0; y < m_height; y++, data += step )
456 m_strm.getBytes( src, src_pitch );
458 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );
460 memcpy( data, src, m_width*3 );
464 /************************* 32 BPP ************************/
466 for( y = 0; y < m_height; y++, data += step )
468 m_strm.getBytes( src, src_pitch );
471 icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );
473 icvCvt_BGRA2BGR_8u_C4C3R( src, 0, data, 0, cvSize(m_width,1) );
489 //////////////////////////////////////////////////////////////////////////////////////////
491 BmpEncoder::BmpEncoder()
493 m_description = "Windows bitmap (*.bmp;*.dib)";
494 m_buf_supported = true;
498 BmpEncoder::~BmpEncoder()
502 ImageEncoder BmpEncoder::newEncoder() const
504 return new BmpEncoder;
507 bool BmpEncoder::write( const Mat& img, const vector<int>& )
509 int width = img.cols, height = img.rows, channels = img.channels();
510 int fileStep = (width*channels + 3) & -4;
511 uchar zeropad[] = "\0\0\0\0";
516 if( !strm.open( *m_buf ) )
519 else if( !strm.open( m_filename ))
522 int bitmapHeaderSize = 40;
523 int paletteSize = channels > 1 ? 0 : 1024;
524 int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
525 int fileSize = fileStep*height + headerSize;
526 PaletteEntry palette[256];
529 m_buf->reserve( alignSize(fileSize + 16, 256) );
531 // write signature 'BM'
532 strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
535 strm.putDWord( fileSize ); // file size
537 strm.putDWord( headerSize );
539 // write bitmap header
540 strm.putDWord( bitmapHeaderSize );
541 strm.putDWord( width );
542 strm.putDWord( height );
544 strm.putWord( channels << 3 );
545 strm.putDWord( BMP_RGB );
554 FillGrayPalette( palette, 8 );
555 strm.putBytes( palette, sizeof(palette));
559 for( int y = height - 1; y >= 0; y-- )
561 strm.putBytes( img.data + img.step*y, width );
562 if( fileStep > width )
563 strm.putBytes( zeropad, fileStep - width );