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;
64 BmpDecoder::~BmpDecoder()
69 void BmpDecoder::close()
74 ImageDecoder BmpDecoder::newDecoder() const
76 return makePtr<BmpDecoder>();
79 bool BmpDecoder::readHeader()
86 if( !m_strm.open( m_buf ) )
89 else if( !m_strm.open( m_filename ))
95 m_offset = m_strm.getDWord();
97 int size = m_strm.getDWord();
101 m_width = m_strm.getDWord();
102 m_height = m_strm.getDWord();
103 m_bpp = m_strm.getDWord() >> 16;
104 m_rle_code = (BmpCompression)m_strm.getDWord();
106 int clrused = m_strm.getDWord();
107 m_strm.skip( size - 36 );
109 if( m_width > 0 && m_height != 0 &&
110 (((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
111 m_bpp == 24 || m_bpp == 32 ) && m_rle_code == BMP_RGB) ||
112 ((m_bpp == 16 || m_bpp == 32) && (m_rle_code == BMP_RGB || m_rle_code == BMP_BITFIELDS)) ||
113 (m_bpp == 4 && m_rle_code == BMP_RLE4) ||
114 (m_bpp == 8 && m_rle_code == BMP_RLE8)))
121 CV_Assert(clrused <= 256);
122 memset(m_palette, 0, sizeof(m_palette));
123 m_strm.getBytes(m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );
124 iscolor = IsColorPalette( m_palette, m_bpp );
126 else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )
128 int redmask = m_strm.getDWord();
129 int greenmask = m_strm.getDWord();
130 int bluemask = m_strm.getDWord();
132 if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )
134 else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )
139 else if (m_bpp == 32 && m_rle_code == BMP_BITFIELDS)
141 // 32bit BMP not require to check something - we can simply allow it to use
144 else if( m_bpp == 16 && m_rle_code == BMP_RGB )
148 else if( size == 12 )
150 m_width = m_strm.getWord();
151 m_height = m_strm.getWord();
152 m_bpp = m_strm.getDWord() >> 16;
153 m_rle_code = BMP_RGB;
155 if( m_width > 0 && m_height != 0 &&
156 (m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
157 m_bpp == 24 || m_bpp == 32 ))
162 int j, clrused = 1 << m_bpp;
163 m_strm.getBytes( buffer, clrused*3 );
164 for( j = 0; j < clrused; j++ )
166 m_palette[j].b = buffer[3*j+0];
167 m_palette[j].g = buffer[3*j+1];
168 m_palette[j].r = buffer[3*j+2];
179 // in 32 bit case alpha channel is used - so require CV_8UC4 type
180 m_type = iscolor ? (m_bpp == 32 ? CV_8UC4 : CV_8UC3 ) : CV_8UC1;
181 m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL;
182 m_height = std::abs(m_height);
187 m_width = m_height = -1;
194 bool BmpDecoder::readData( Mat& img )
196 uchar* data = img.ptr();
197 int step = validateToInt(img.step);
198 bool color = img.channels() > 1;
199 uchar gray_palette[256] = {0};
201 int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;
202 int nch = color ? 3 : 1;
203 int y, width3 = m_width*nch;
205 if( m_offset < 0 || !m_strm.isOpened())
208 if( m_origin == IPL_ORIGIN_BL )
210 data += (m_height - 1)*(size_t)step;
214 AutoBuffer<uchar> _src, _bgr;
215 _src.allocate(src_pitch + 32);
221 CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
223 _bgr.allocate(m_width*3 + 32);
225 uchar *src = _src, *bgr = _bgr;
229 m_strm.setPos( m_offset );
233 /************************* 1 BPP ************************/
235 for( y = 0; y < m_height; y++, data += step )
237 m_strm.getBytes( src, src_pitch );
238 FillColorRow1( color ? data : bgr, src, m_width, m_palette );
240 icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );
245 /************************* 4 BPP ************************/
247 if( m_rle_code == BMP_RGB )
249 for( y = 0; y < m_height; y++, data += step )
251 m_strm.getBytes( src, src_pitch );
253 FillColorRow4( data, src, m_width, m_palette );
255 FillGrayRow4( data, src, m_width, gray_palette );
259 else if( m_rle_code == BMP_RLE4 ) // rle4 compression
261 uchar* line_end = data + width3;
266 int code = m_strm.getWord();
267 int len = code & 255;
269 if( len != 0 ) // encoded mode
275 clr[0] = m_palette[code >> 4];
276 clr[1] = m_palette[code & 15];
277 gray_clr[0] = gray_palette[code >> 4];
278 gray_clr[1] = gray_palette[code & 15];
280 uchar* end = data + len*nch;
281 if( end > line_end ) goto decode_rle4_bad;
285 WRITE_PIX( data, clr[t] );
290 while( (data += nch) < end );
292 else if( code > 2 ) // absolute mode
294 if( data + code*nch > line_end ) goto decode_rle4_bad;
295 int sz = (((code + 1)>>1) + 1) & (~1);
296 CV_Assert((size_t)sz < _src.size());
297 m_strm.getBytes(src, sz);
299 data = FillColorRow4( data, src, code, m_palette );
301 data = FillGrayRow4( data, src, code, gray_palette );
305 int x_shift3 = (int)(line_end - data);
306 int y_shift = m_height - y;
310 x_shift3 = m_strm.getByte()*nch;
311 y_shift = m_strm.getByte();
314 len = x_shift3 + ((y_shift * width3) & ((code == 0) - 1));
317 data = FillUniColor( data, line_end, step, width3,
318 y, m_height, x_shift3,
321 data = FillUniGray( data, line_end, step, width3,
322 y, m_height, x_shift3,
335 /************************* 8 BPP ************************/
337 if( m_rle_code == BMP_RGB )
339 for( y = 0; y < m_height; y++, data += step )
341 m_strm.getBytes( src, src_pitch );
343 FillColorRow8( data, src, m_width, m_palette );
345 FillGrayRow8( data, src, m_width, gray_palette );
349 else if( m_rle_code == BMP_RLE8 ) // rle8 compression
351 uchar* line_end = data + width3;
352 int line_end_flag = 0;
357 int code = m_strm.getWord();
358 int len = code & 255;
360 if( len != 0 ) // encoded mode
365 if( data + len > line_end )
366 goto decode_rle8_bad;
369 data = FillUniColor( data, line_end, step, width3,
373 data = FillUniGray( data, line_end, step, width3,
375 gray_palette[code] );
377 line_end_flag = y - prev_y;
382 else if( code > 2 ) // absolute mode
385 int code3 = code*nch;
387 if( data + code3 > line_end )
388 goto decode_rle8_bad;
389 int sz = (code + 1) & (~1);
390 CV_Assert((size_t)sz < _src.size());
391 m_strm.getBytes(src, sz);
393 data = FillColorRow8( data, src, code, m_palette );
395 data = FillGrayRow8( data, src, code, gray_palette );
397 line_end_flag = y - prev_y;
401 int x_shift3 = (int)(line_end - data);
402 int y_shift = m_height - y;
404 if( code || !line_end_flag || x_shift3 < width3 )
408 x_shift3 = m_strm.getByte()*nch;
409 y_shift = m_strm.getByte();
412 x_shift3 += (y_shift * width3) & ((code == 0) - 1);
418 data = FillUniColor( data, line_end, step, width3,
419 y, m_height, x_shift3,
422 data = FillUniGray( data, line_end, step, width3,
423 y, m_height, x_shift3,
440 /************************* 15 BPP ************************/
442 for( y = 0; y < m_height; y++, data += step )
444 m_strm.getBytes( src, src_pitch );
446 icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
448 icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
452 /************************* 16 BPP ************************/
454 for( y = 0; y < m_height; y++, data += step )
456 m_strm.getBytes( src, src_pitch );
458 icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
460 icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
464 /************************* 24 BPP ************************/
466 for( y = 0; y < m_height; y++, data += step )
468 m_strm.getBytes( src, src_pitch );
470 icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );
472 memcpy( data, src, m_width*3 );
476 /************************* 32 BPP ************************/
478 for( y = 0; y < m_height; y++, data += step )
480 m_strm.getBytes( src, src_pitch );
483 icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );
484 else if( img.channels() == 3 )
485 icvCvt_BGRA2BGR_8u_C4C3R(src, 0, data, 0, cvSize(m_width, 1));
486 else if( img.channels() == 4 )
487 memcpy(data, src, m_width * 4);
492 CV_ErrorNoReturn(cv::Error::StsError, "Invalid/unsupported mode");
504 //////////////////////////////////////////////////////////////////////////////////////////
506 BmpEncoder::BmpEncoder()
508 m_description = "Windows bitmap (*.bmp;*.dib)";
509 m_buf_supported = true;
513 BmpEncoder::~BmpEncoder()
517 ImageEncoder BmpEncoder::newEncoder() const
519 return makePtr<BmpEncoder>();
522 bool BmpEncoder::write( const Mat& img, const std::vector<int>& )
524 int width = img.cols, height = img.rows, channels = img.channels();
525 int fileStep = (width*channels + 3) & -4;
526 uchar zeropad[] = "\0\0\0\0";
531 if( !strm.open( *m_buf ) )
534 else if( !strm.open( m_filename ))
537 int bitmapHeaderSize = 40;
538 int paletteSize = channels > 1 ? 0 : 1024;
539 int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
540 size_t fileSize = (size_t)fileStep*height + headerSize;
541 PaletteEntry palette[256];
544 m_buf->reserve( alignSize(fileSize + 16, 256) );
546 // write signature 'BM'
547 strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
550 strm.putDWord( validateToInt(fileSize) ); // file size
552 strm.putDWord( headerSize );
554 // write bitmap header
555 strm.putDWord( bitmapHeaderSize );
556 strm.putDWord( width );
557 strm.putDWord( height );
559 strm.putWord( channels << 3 );
560 strm.putDWord( BMP_RGB );
569 FillGrayPalette( palette, 8 );
570 strm.putBytes( palette, sizeof(palette));
574 for( int y = height - 1; y >= 0; y-- )
576 strm.putBytes( img.ptr(y), width );
577 if( fileStep > width )
578 strm.putBytes( zeropad, fileStep - width );