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.
44 #include "grfmt_bmp.h"
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 memset( m_palette, 0, sizeof(m_palette));
119 m_strm.getBytes( m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );
120 iscolor = IsColorPalette( m_palette, m_bpp );
122 else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )
124 int redmask = m_strm.getDWord();
125 int greenmask = m_strm.getDWord();
126 int bluemask = m_strm.getDWord();
128 if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )
130 else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )
135 else if( m_bpp == 16 && m_rle_code == BMP_RGB )
139 else if( size == 12 )
141 m_width = m_strm.getWord();
142 m_height = m_strm.getWord();
143 m_bpp = m_strm.getDWord() >> 16;
144 m_rle_code = BMP_RGB;
146 if( m_width > 0 && m_height > 0 &&
147 (m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
148 m_bpp == 24 || m_bpp == 32 ))
153 int j, clrused = 1 << m_bpp;
154 m_strm.getBytes( buffer, clrused*3 );
155 for( j = 0; j < clrused; j++ )
157 m_palette[j].b = buffer[3*j+0];
158 m_palette[j].g = buffer[3*j+1];
159 m_palette[j].r = buffer[3*j+2];
170 m_type = iscolor ? CV_8UC3 : CV_8UC1;
174 m_width = m_height = -1;
181 bool BmpDecoder::readData( Mat& img )
183 uchar* data = img.data;
185 bool color = img.channels() > 1;
186 uchar gray_palette[256];
188 int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;
189 int nch = color ? 3 : 1;
190 int y, width3 = m_width*nch;
192 if( m_offset < 0 || !m_strm.isOpened())
195 data += (m_height - 1)*step;
198 AutoBuffer<uchar> _src, _bgr;
199 if( (m_bpp != 24 || !color) )
200 _src.allocate(src_pitch + 32);
206 CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
208 _bgr.allocate(m_width*3 + 32);
210 uchar *src = _src, *bgr = _bgr;
214 m_strm.setPos( m_offset );
218 /************************* 1 BPP ************************/
220 for( y = 0; y < m_height; y++, data += step )
222 m_strm.getBytes( src, src_pitch );
223 FillColorRow1( color ? data : bgr, src, m_width, m_palette );
225 icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );
230 /************************* 4 BPP ************************/
232 if( m_rle_code == BMP_RGB )
234 for( y = 0; y < m_height; y++, data += step )
236 m_strm.getBytes( src, src_pitch );
238 FillColorRow4( data, src, m_width, m_palette );
240 FillGrayRow4( data, src, m_width, gray_palette );
244 else if( m_rle_code == BMP_RLE4 ) // rle4 compression
246 uchar* line_end = data + width3;
251 int code = m_strm.getWord();
252 int len = code & 255;
254 if( len != 0 ) // encoded mode
260 clr[0] = m_palette[code >> 4];
261 clr[1] = m_palette[code & 15];
262 gray_clr[0] = gray_palette[code >> 4];
263 gray_clr[1] = gray_palette[code & 15];
265 uchar* end = data + len*nch;
266 if( end > line_end ) goto decode_rle4_bad;
270 WRITE_PIX( data, clr[t] );
275 while( (data += nch) < end );
277 else if( code > 2 ) // absolute mode
279 if( data + code*nch > line_end ) goto decode_rle4_bad;
280 m_strm.getBytes( src, (((code + 1)>>1) + 1) & -2 );
282 data = FillColorRow4( data, src, code, m_palette );
284 data = FillGrayRow4( data, src, code, gray_palette );
288 int x_shift3 = (int)(line_end - data);
289 int y_shift = m_height - y;
293 x_shift3 = m_strm.getByte()*nch;
294 y_shift = m_strm.getByte();
297 len = x_shift3 + ((y_shift * width3) & ((code == 0) - 1));
300 data = FillUniColor( data, line_end, step, width3,
301 y, m_height, x_shift3,
304 data = FillUniGray( data, line_end, step, width3,
305 y, m_height, x_shift3,
318 /************************* 8 BPP ************************/
320 if( m_rle_code == BMP_RGB )
322 for( y = 0; y < m_height; y++, data += step )
324 m_strm.getBytes( src, src_pitch );
326 FillColorRow8( data, src, m_width, m_palette );
328 FillGrayRow8( data, src, m_width, gray_palette );
332 else if( m_rle_code == BMP_RLE8 ) // rle8 compression
334 uchar* line_end = data + width3;
335 int line_end_flag = 0;
340 int code = m_strm.getWord();
341 int len = code & 255;
343 if( len != 0 ) // encoded mode
348 if( data + len > line_end )
349 goto decode_rle8_bad;
352 data = FillUniColor( data, line_end, step, width3,
356 data = FillUniGray( data, line_end, step, width3,
358 gray_palette[code] );
360 line_end_flag = y - prev_y;
362 else if( code > 2 ) // absolute mode
365 int code3 = code*nch;
367 if( data + code3 > line_end )
368 goto decode_rle8_bad;
369 m_strm.getBytes( src, (code + 1) & -2 );
371 data = FillColorRow8( data, src, code, m_palette );
373 data = FillGrayRow8( data, src, code, gray_palette );
375 line_end_flag = y - prev_y;
379 int x_shift3 = (int)(line_end - data);
380 int y_shift = m_height - y;
382 if( code || !line_end_flag || x_shift3 < width3 )
386 x_shift3 = m_strm.getByte()*nch;
387 y_shift = m_strm.getByte();
390 x_shift3 += (y_shift * width3) & ((code == 0) - 1);
396 data = FillUniColor( data, line_end, step, width3,
397 y, m_height, x_shift3,
400 data = FillUniGray( data, line_end, step, width3,
401 y, m_height, x_shift3,
416 /************************* 15 BPP ************************/
418 for( y = 0; y < m_height; y++, data += step )
420 m_strm.getBytes( src, src_pitch );
422 icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
424 icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
428 /************************* 16 BPP ************************/
430 for( y = 0; y < m_height; y++, data += step )
432 m_strm.getBytes( src, src_pitch );
434 icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
436 icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
440 /************************* 24 BPP ************************/
442 for( y = 0; y < m_height; y++, data += step )
444 m_strm.getBytes( color ? data : src, src_pitch );
446 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );
450 /************************* 32 BPP ************************/
452 for( y = 0; y < m_height; y++, data += step )
454 m_strm.getBytes( src, src_pitch );
457 icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );
459 icvCvt_BGRA2BGR_8u_C4C3R( src, 0, data, 0, cvSize(m_width,1) );
475 //////////////////////////////////////////////////////////////////////////////////////////
477 BmpEncoder::BmpEncoder()
479 m_description = "Windows bitmap (*.bmp;*.dib)";
480 m_buf_supported = true;
484 BmpEncoder::~BmpEncoder()
488 ImageEncoder BmpEncoder::newEncoder() const
490 return new BmpEncoder;
493 bool BmpEncoder::write( const Mat& img, const vector<int>& )
495 int width = img.cols, height = img.rows, channels = img.channels();
496 int fileStep = (width*channels + 3) & -4;
497 uchar zeropad[] = "\0\0\0\0";
502 if( !strm.open( *m_buf ) )
505 else if( !strm.open( m_filename ))
508 int bitmapHeaderSize = 40;
509 int paletteSize = channels > 1 ? 0 : 1024;
510 int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
511 int fileSize = fileStep*height + headerSize;
512 PaletteEntry palette[256];
515 m_buf->reserve( alignSize(fileSize + 16, 256) );
517 // write signature 'BM'
518 strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
521 strm.putDWord( fileSize ); // file size
523 strm.putDWord( headerSize );
525 // write bitmap header
526 strm.putDWord( bitmapHeaderSize );
527 strm.putDWord( width );
528 strm.putDWord( height );
530 strm.putWord( channels << 3 );
531 strm.putDWord( BMP_RGB );
540 FillGrayPalette( palette, 8 );
541 strm.putBytes( palette, sizeof(palette));
545 for( int y = height - 1; y >= 0; y-- )
547 strm.putBytes( img.data + img.step*y, width );
548 if( fileStep > width )
549 strm.putBytes( zeropad, fileStep - width );