Path: blob/master/modules/imgcodecs/src/grfmt_bmp.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// 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 damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "grfmt_bmp.hpp"4445namespace cv46{4748static const char* fmtSignBmp = "BM";4950/************************ BMP decoder *****************************/5152BmpDecoder::BmpDecoder()53{54m_signature = fmtSignBmp;55m_offset = -1;56m_buf_supported = true;57m_origin = 0;58m_bpp = 0;59m_rle_code = BMP_RGB;60}616263BmpDecoder::~BmpDecoder()64{65}666768void BmpDecoder::close()69{70m_strm.close();71}7273ImageDecoder BmpDecoder::newDecoder() const74{75return makePtr<BmpDecoder>();76}7778bool BmpDecoder::readHeader()79{80bool result = false;81bool iscolor = false;8283if( !m_buf.empty() )84{85if( !m_strm.open( m_buf ) )86return false;87}88else if( !m_strm.open( m_filename ))89return false;9091CV_TRY92{93m_strm.skip( 10 );94m_offset = m_strm.getDWord();9596int size = m_strm.getDWord();97CV_Assert(size > 0); // overflow, 2Gb limit9899if( size >= 36 )100{101m_width = m_strm.getDWord();102m_height = m_strm.getDWord();103m_bpp = m_strm.getDWord() >> 16;104m_rle_code = (BmpCompression)m_strm.getDWord();105m_strm.skip(12);106int clrused = m_strm.getDWord();107m_strm.skip( size - 36 );108109if( m_width > 0 && m_height != 0 &&110(((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||111m_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)))115{116iscolor = true;117result = true;118119if( m_bpp <= 8 )120{121CV_Assert(clrused >= 0 && clrused <= 256);122memset(m_palette, 0, sizeof(m_palette));123m_strm.getBytes(m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );124iscolor = IsColorPalette( m_palette, m_bpp );125}126else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )127{128int redmask = m_strm.getDWord();129int greenmask = m_strm.getDWord();130int bluemask = m_strm.getDWord();131132if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )133m_bpp = 15;134else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )135;136else137result = false;138}139else if (m_bpp == 32 && m_rle_code == BMP_BITFIELDS)140{141// 32bit BMP not require to check something - we can simply allow it to use142;143}144else if( m_bpp == 16 && m_rle_code == BMP_RGB )145m_bpp = 15;146}147}148else if( size == 12 )149{150m_width = m_strm.getWord();151m_height = m_strm.getWord();152m_bpp = m_strm.getDWord() >> 16;153m_rle_code = BMP_RGB;154155if( m_width > 0 && m_height != 0 &&156(m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||157m_bpp == 24 || m_bpp == 32 ))158{159if( m_bpp <= 8 )160{161uchar buffer[256*3];162int j, clrused = 1 << m_bpp;163m_strm.getBytes( buffer, clrused*3 );164for( j = 0; j < clrused; j++ )165{166m_palette[j].b = buffer[3*j+0];167m_palette[j].g = buffer[3*j+1];168m_palette[j].r = buffer[3*j+2];169}170}171result = true;172}173}174}175CV_CATCH_ALL176{177CV_RETHROW();178}179// in 32 bit case alpha channel is used - so require CV_8UC4 type180m_type = iscolor ? (m_bpp == 32 ? CV_8UC4 : CV_8UC3 ) : CV_8UC1;181m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL;182m_height = std::abs(m_height);183184if( !result )185{186m_offset = -1;187m_width = m_height = -1;188m_strm.close();189}190return result;191}192193194bool BmpDecoder::readData( Mat& img )195{196uchar* data = img.ptr();197int step = validateToInt(img.step);198bool color = img.channels() > 1;199uchar gray_palette[256] = {0};200bool result = false;201int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;202int nch = color ? 3 : 1;203int y, width3 = m_width*nch;204205if( m_offset < 0 || !m_strm.isOpened())206return false;207208if( m_origin == IPL_ORIGIN_BL )209{210data += (m_height - 1)*(size_t)step;211step = -step;212}213214AutoBuffer<uchar> _src, _bgr;215_src.allocate(src_pitch + 32);216217if( !color )218{219if( m_bpp <= 8 )220{221CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );222}223_bgr.allocate(m_width*3 + 32);224}225uchar *src = _src.data(), *bgr = _bgr.data();226227CV_TRY228{229m_strm.setPos( m_offset );230231switch( m_bpp )232{233/************************* 1 BPP ************************/234case 1:235for( y = 0; y < m_height; y++, data += step )236{237m_strm.getBytes( src, src_pitch );238FillColorRow1( color ? data : bgr, src, m_width, m_palette );239if( !color )240icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );241}242result = true;243break;244245/************************* 4 BPP ************************/246case 4:247if( m_rle_code == BMP_RGB )248{249for( y = 0; y < m_height; y++, data += step )250{251m_strm.getBytes( src, src_pitch );252if( color )253FillColorRow4( data, src, m_width, m_palette );254else255FillGrayRow4( data, src, m_width, gray_palette );256}257result = true;258}259else if( m_rle_code == BMP_RLE4 ) // rle4 compression260{261uchar* line_end = data + width3;262y = 0;263264for(;;)265{266int code = m_strm.getWord();267const int len = code & 255;268code >>= 8;269if( len != 0 ) // encoded mode270{271PaletteEntry clr[2];272uchar gray_clr[2];273int t = 0;274275clr[0] = m_palette[code >> 4];276clr[1] = m_palette[code & 15];277gray_clr[0] = gray_palette[code >> 4];278gray_clr[1] = gray_palette[code & 15];279280uchar* end = data + len*nch;281if( end > line_end ) goto decode_rle4_bad;282do283{284if( color )285WRITE_PIX( data, clr[t] );286else287*data = gray_clr[t];288t ^= 1;289}290while( (data += nch) < end );291}292else if( code > 2 ) // absolute mode293{294if( data + code*nch > line_end ) goto decode_rle4_bad;295int sz = (((code + 1)>>1) + 1) & (~1);296CV_Assert((size_t)sz < _src.size());297m_strm.getBytes(src, sz);298if( color )299data = FillColorRow4( data, src, code, m_palette );300else301data = FillGrayRow4( data, src, code, gray_palette );302}303else304{305int x_shift3 = (int)(line_end - data);306307if( code == 2 )308{309x_shift3 = m_strm.getByte()*nch;310m_strm.getByte();311}312313if( color )314data = FillUniColor( data, line_end, step, width3,315y, m_height, x_shift3,316m_palette[0] );317else318data = FillUniGray( data, line_end, step, width3,319y, m_height, x_shift3,320gray_palette[0] );321322if( y >= m_height )323break;324}325}326327result = true;328decode_rle4_bad: ;329}330break;331332/************************* 8 BPP ************************/333case 8:334if( m_rle_code == BMP_RGB )335{336for( y = 0; y < m_height; y++, data += step )337{338m_strm.getBytes( src, src_pitch );339if( color )340FillColorRow8( data, src, m_width, m_palette );341else342FillGrayRow8( data, src, m_width, gray_palette );343}344result = true;345}346else if( m_rle_code == BMP_RLE8 ) // rle8 compression347{348uchar* line_end = data + width3;349int line_end_flag = 0;350y = 0;351352for(;;)353{354int code = m_strm.getWord();355int len = code & 255;356code >>= 8;357if( len != 0 ) // encoded mode358{359int prev_y = y;360len *= nch;361362if( data + len > line_end )363goto decode_rle8_bad;364365if( color )366data = FillUniColor( data, line_end, step, width3,367y, m_height, len,368m_palette[code] );369else370data = FillUniGray( data, line_end, step, width3,371y, m_height, len,372gray_palette[code] );373374line_end_flag = y - prev_y;375376if( y >= m_height )377break;378}379else if( code > 2 ) // absolute mode380{381int prev_y = y;382int code3 = code*nch;383384if( data + code3 > line_end )385goto decode_rle8_bad;386int sz = (code + 1) & (~1);387CV_Assert((size_t)sz < _src.size());388m_strm.getBytes(src, sz);389if( color )390data = FillColorRow8( data, src, code, m_palette );391else392data = FillGrayRow8( data, src, code, gray_palette );393394line_end_flag = y - prev_y;395}396else397{398int x_shift3 = (int)(line_end - data);399int y_shift = m_height - y;400401if( code || !line_end_flag || x_shift3 < width3 )402{403if( code == 2 )404{405x_shift3 = m_strm.getByte()*nch;406y_shift = m_strm.getByte();407}408409x_shift3 += (y_shift * width3) & ((code == 0) - 1);410411if( y >= m_height )412break;413414if( color )415data = FillUniColor( data, line_end, step, width3,416y, m_height, x_shift3,417m_palette[0] );418else419data = FillUniGray( data, line_end, step, width3,420y, m_height, x_shift3,421gray_palette[0] );422423if( y >= m_height )424break;425}426427line_end_flag = 0;428if( y >= m_height )429break;430}431}432433result = true;434decode_rle8_bad: ;435}436break;437/************************* 15 BPP ************************/438case 15:439for( y = 0; y < m_height; y++, data += step )440{441m_strm.getBytes( src, src_pitch );442if( !color )443icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );444else445icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );446}447result = true;448break;449/************************* 16 BPP ************************/450case 16:451for( y = 0; y < m_height; y++, data += step )452{453m_strm.getBytes( src, src_pitch );454if( !color )455icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );456else457icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );458}459result = true;460break;461/************************* 24 BPP ************************/462case 24:463for( y = 0; y < m_height; y++, data += step )464{465m_strm.getBytes( src, src_pitch );466if(!color)467icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );468else469memcpy( data, src, m_width*3 );470}471result = true;472break;473/************************* 32 BPP ************************/474case 32:475for( y = 0; y < m_height; y++, data += step )476{477m_strm.getBytes( src, src_pitch );478479if( !color )480icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );481else if( img.channels() == 3 )482icvCvt_BGRA2BGR_8u_C4C3R(src, 0, data, 0, cvSize(m_width, 1));483else if( img.channels() == 4 )484memcpy(data, src, m_width * 4);485}486result = true;487break;488default:489CV_Error(cv::Error::StsError, "Invalid/unsupported mode");490}491}492CV_CATCH_ALL493{494CV_RETHROW();495}496497return result;498}499500501//////////////////////////////////////////////////////////////////////////////////////////502503BmpEncoder::BmpEncoder()504{505m_description = "Windows bitmap (*.bmp;*.dib)";506m_buf_supported = true;507}508509510BmpEncoder::~BmpEncoder()511{512}513514ImageEncoder BmpEncoder::newEncoder() const515{516return makePtr<BmpEncoder>();517}518519bool BmpEncoder::write( const Mat& img, const std::vector<int>& )520{521int width = img.cols, height = img.rows, channels = img.channels();522int fileStep = (width*channels + 3) & -4;523uchar zeropad[] = "\0\0\0\0";524WLByteStream strm;525526if( m_buf )527{528if( !strm.open( *m_buf ) )529return false;530}531else if( !strm.open( m_filename ))532return false;533534int bitmapHeaderSize = 40;535int paletteSize = channels > 1 ? 0 : 1024;536int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;537size_t fileSize = (size_t)fileStep*height + headerSize;538PaletteEntry palette[256];539540if( m_buf )541m_buf->reserve( alignSize(fileSize + 16, 256) );542543// write signature 'BM'544strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );545546// write file header547strm.putDWord( validateToInt(fileSize) ); // file size548strm.putDWord( 0 );549strm.putDWord( headerSize );550551// write bitmap header552strm.putDWord( bitmapHeaderSize );553strm.putDWord( width );554strm.putDWord( height );555strm.putWord( 1 );556strm.putWord( channels << 3 );557strm.putDWord( BMP_RGB );558strm.putDWord( 0 );559strm.putDWord( 0 );560strm.putDWord( 0 );561strm.putDWord( 0 );562strm.putDWord( 0 );563564if( channels == 1 )565{566FillGrayPalette( palette, 8 );567strm.putBytes( palette, sizeof(palette));568}569570width *= channels;571for( int y = height - 1; y >= 0; y-- )572{573strm.putBytes( img.ptr(y), width );574if( fileStep > width )575strm.putBytes( zeropad, fileStep - width );576}577578strm.close();579return true;580}581582}583584585