Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/src/grfmt_bmp.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
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.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
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.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
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.
26
//
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.
29
//
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.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "grfmt_bmp.hpp"
45
46
namespace cv
47
{
48
49
static const char* fmtSignBmp = "BM";
50
51
/************************ BMP decoder *****************************/
52
53
BmpDecoder::BmpDecoder()
54
{
55
m_signature = fmtSignBmp;
56
m_offset = -1;
57
m_buf_supported = true;
58
m_origin = 0;
59
m_bpp = 0;
60
m_rle_code = BMP_RGB;
61
}
62
63
64
BmpDecoder::~BmpDecoder()
65
{
66
}
67
68
69
void BmpDecoder::close()
70
{
71
m_strm.close();
72
}
73
74
ImageDecoder BmpDecoder::newDecoder() const
75
{
76
return makePtr<BmpDecoder>();
77
}
78
79
bool BmpDecoder::readHeader()
80
{
81
bool result = false;
82
bool iscolor = false;
83
84
if( !m_buf.empty() )
85
{
86
if( !m_strm.open( m_buf ) )
87
return false;
88
}
89
else if( !m_strm.open( m_filename ))
90
return false;
91
92
CV_TRY
93
{
94
m_strm.skip( 10 );
95
m_offset = m_strm.getDWord();
96
97
int size = m_strm.getDWord();
98
CV_Assert(size > 0); // overflow, 2Gb limit
99
100
if( size >= 36 )
101
{
102
m_width = m_strm.getDWord();
103
m_height = m_strm.getDWord();
104
m_bpp = m_strm.getDWord() >> 16;
105
m_rle_code = (BmpCompression)m_strm.getDWord();
106
m_strm.skip(12);
107
int clrused = m_strm.getDWord();
108
m_strm.skip( size - 36 );
109
110
if( m_width > 0 && m_height != 0 &&
111
(((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 ||
112
m_bpp == 24 || m_bpp == 32 ) && m_rle_code == BMP_RGB) ||
113
((m_bpp == 16 || m_bpp == 32) && (m_rle_code == BMP_RGB || m_rle_code == BMP_BITFIELDS)) ||
114
(m_bpp == 4 && m_rle_code == BMP_RLE4) ||
115
(m_bpp == 8 && m_rle_code == BMP_RLE8)))
116
{
117
iscolor = true;
118
result = true;
119
120
if( m_bpp <= 8 )
121
{
122
CV_Assert(clrused >= 0 && clrused <= 256);
123
memset(m_palette, 0, sizeof(m_palette));
124
m_strm.getBytes(m_palette, (clrused == 0? 1<<m_bpp : clrused)*4 );
125
iscolor = IsColorPalette( m_palette, m_bpp );
126
}
127
else if( m_bpp == 16 && m_rle_code == BMP_BITFIELDS )
128
{
129
int redmask = m_strm.getDWord();
130
int greenmask = m_strm.getDWord();
131
int bluemask = m_strm.getDWord();
132
133
if( bluemask == 0x1f && greenmask == 0x3e0 && redmask == 0x7c00 )
134
m_bpp = 15;
135
else if( bluemask == 0x1f && greenmask == 0x7e0 && redmask == 0xf800 )
136
;
137
else
138
result = false;
139
}
140
else if (m_bpp == 32 && m_rle_code == BMP_BITFIELDS)
141
{
142
// 32bit BMP not require to check something - we can simply allow it to use
143
;
144
}
145
else if( m_bpp == 16 && m_rle_code == BMP_RGB )
146
m_bpp = 15;
147
}
148
}
149
else if( size == 12 )
150
{
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;
155
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 ))
159
{
160
if( m_bpp <= 8 )
161
{
162
uchar buffer[256*3];
163
int j, clrused = 1 << m_bpp;
164
m_strm.getBytes( buffer, clrused*3 );
165
for( j = 0; j < clrused; j++ )
166
{
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];
170
}
171
}
172
result = true;
173
}
174
}
175
}
176
CV_CATCH_ALL
177
{
178
CV_RETHROW();
179
}
180
// in 32 bit case alpha channel is used - so require CV_8UC4 type
181
m_type = iscolor ? (m_bpp == 32 ? CV_8UC4 : CV_8UC3 ) : CV_8UC1;
182
m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL;
183
m_height = std::abs(m_height);
184
185
if( !result )
186
{
187
m_offset = -1;
188
m_width = m_height = -1;
189
m_strm.close();
190
}
191
return result;
192
}
193
194
195
bool BmpDecoder::readData( Mat& img )
196
{
197
uchar* data = img.ptr();
198
int step = validateToInt(img.step);
199
bool color = img.channels() > 1;
200
uchar gray_palette[256] = {0};
201
bool result = false;
202
int src_pitch = ((m_width*(m_bpp != 15 ? m_bpp : 16) + 7)/8 + 3) & -4;
203
int nch = color ? 3 : 1;
204
int y, width3 = m_width*nch;
205
206
if( m_offset < 0 || !m_strm.isOpened())
207
return false;
208
209
if( m_origin == IPL_ORIGIN_BL )
210
{
211
data += (m_height - 1)*(size_t)step;
212
step = -step;
213
}
214
215
AutoBuffer<uchar> _src, _bgr;
216
_src.allocate(src_pitch + 32);
217
218
if( !color )
219
{
220
if( m_bpp <= 8 )
221
{
222
CvtPaletteToGray( m_palette, gray_palette, 1 << m_bpp );
223
}
224
_bgr.allocate(m_width*3 + 32);
225
}
226
uchar *src = _src.data(), *bgr = _bgr.data();
227
228
CV_TRY
229
{
230
m_strm.setPos( m_offset );
231
232
switch( m_bpp )
233
{
234
/************************* 1 BPP ************************/
235
case 1:
236
for( y = 0; y < m_height; y++, data += step )
237
{
238
m_strm.getBytes( src, src_pitch );
239
FillColorRow1( color ? data : bgr, src, m_width, m_palette );
240
if( !color )
241
icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) );
242
}
243
result = true;
244
break;
245
246
/************************* 4 BPP ************************/
247
case 4:
248
if( m_rle_code == BMP_RGB )
249
{
250
for( y = 0; y < m_height; y++, data += step )
251
{
252
m_strm.getBytes( src, src_pitch );
253
if( color )
254
FillColorRow4( data, src, m_width, m_palette );
255
else
256
FillGrayRow4( data, src, m_width, gray_palette );
257
}
258
result = true;
259
}
260
else if( m_rle_code == BMP_RLE4 ) // rle4 compression
261
{
262
uchar* line_end = data + width3;
263
y = 0;
264
265
for(;;)
266
{
267
int code = m_strm.getWord();
268
const int len = code & 255;
269
code >>= 8;
270
if( len != 0 ) // encoded mode
271
{
272
PaletteEntry clr[2];
273
uchar gray_clr[2];
274
int t = 0;
275
276
clr[0] = m_palette[code >> 4];
277
clr[1] = m_palette[code & 15];
278
gray_clr[0] = gray_palette[code >> 4];
279
gray_clr[1] = gray_palette[code & 15];
280
281
uchar* end = data + len*nch;
282
if( end > line_end ) goto decode_rle4_bad;
283
do
284
{
285
if( color )
286
WRITE_PIX( data, clr[t] );
287
else
288
*data = gray_clr[t];
289
t ^= 1;
290
}
291
while( (data += nch) < end );
292
}
293
else if( code > 2 ) // absolute mode
294
{
295
if( data + code*nch > line_end ) goto decode_rle4_bad;
296
int sz = (((code + 1)>>1) + 1) & (~1);
297
CV_Assert((size_t)sz < _src.size());
298
m_strm.getBytes(src, sz);
299
if( color )
300
data = FillColorRow4( data, src, code, m_palette );
301
else
302
data = FillGrayRow4( data, src, code, gray_palette );
303
}
304
else
305
{
306
int x_shift3 = (int)(line_end - data);
307
308
if( code == 2 )
309
{
310
x_shift3 = m_strm.getByte()*nch;
311
m_strm.getByte();
312
}
313
314
if( color )
315
data = FillUniColor( data, line_end, step, width3,
316
y, m_height, x_shift3,
317
m_palette[0] );
318
else
319
data = FillUniGray( data, line_end, step, width3,
320
y, m_height, x_shift3,
321
gray_palette[0] );
322
323
if( y >= m_height )
324
break;
325
}
326
}
327
328
result = true;
329
decode_rle4_bad: ;
330
}
331
break;
332
333
/************************* 8 BPP ************************/
334
case 8:
335
if( m_rle_code == BMP_RGB )
336
{
337
for( y = 0; y < m_height; y++, data += step )
338
{
339
m_strm.getBytes( src, src_pitch );
340
if( color )
341
FillColorRow8( data, src, m_width, m_palette );
342
else
343
FillGrayRow8( data, src, m_width, gray_palette );
344
}
345
result = true;
346
}
347
else if( m_rle_code == BMP_RLE8 ) // rle8 compression
348
{
349
uchar* line_end = data + width3;
350
int line_end_flag = 0;
351
y = 0;
352
353
for(;;)
354
{
355
int code = m_strm.getWord();
356
int len = code & 255;
357
code >>= 8;
358
if( len != 0 ) // encoded mode
359
{
360
int prev_y = y;
361
len *= nch;
362
363
if( data + len > line_end )
364
goto decode_rle8_bad;
365
366
if( color )
367
data = FillUniColor( data, line_end, step, width3,
368
y, m_height, len,
369
m_palette[code] );
370
else
371
data = FillUniGray( data, line_end, step, width3,
372
y, m_height, len,
373
gray_palette[code] );
374
375
line_end_flag = y - prev_y;
376
377
if( y >= m_height )
378
break;
379
}
380
else if( code > 2 ) // absolute mode
381
{
382
int prev_y = y;
383
int code3 = code*nch;
384
385
if( data + code3 > line_end )
386
goto decode_rle8_bad;
387
int sz = (code + 1) & (~1);
388
CV_Assert((size_t)sz < _src.size());
389
m_strm.getBytes(src, sz);
390
if( color )
391
data = FillColorRow8( data, src, code, m_palette );
392
else
393
data = FillGrayRow8( data, src, code, gray_palette );
394
395
line_end_flag = y - prev_y;
396
}
397
else
398
{
399
int x_shift3 = (int)(line_end - data);
400
int y_shift = m_height - y;
401
402
if( code || !line_end_flag || x_shift3 < width3 )
403
{
404
if( code == 2 )
405
{
406
x_shift3 = m_strm.getByte()*nch;
407
y_shift = m_strm.getByte();
408
}
409
410
x_shift3 += (y_shift * width3) & ((code == 0) - 1);
411
412
if( y >= m_height )
413
break;
414
415
if( color )
416
data = FillUniColor( data, line_end, step, width3,
417
y, m_height, x_shift3,
418
m_palette[0] );
419
else
420
data = FillUniGray( data, line_end, step, width3,
421
y, m_height, x_shift3,
422
gray_palette[0] );
423
424
if( y >= m_height )
425
break;
426
}
427
428
line_end_flag = 0;
429
if( y >= m_height )
430
break;
431
}
432
}
433
434
result = true;
435
decode_rle8_bad: ;
436
}
437
break;
438
/************************* 15 BPP ************************/
439
case 15:
440
for( y = 0; y < m_height; y++, data += step )
441
{
442
m_strm.getBytes( src, src_pitch );
443
if( !color )
444
icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
445
else
446
icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
447
}
448
result = true;
449
break;
450
/************************* 16 BPP ************************/
451
case 16:
452
for( y = 0; y < m_height; y++, data += step )
453
{
454
m_strm.getBytes( src, src_pitch );
455
if( !color )
456
icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) );
457
else
458
icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) );
459
}
460
result = true;
461
break;
462
/************************* 24 BPP ************************/
463
case 24:
464
for( y = 0; y < m_height; y++, data += step )
465
{
466
m_strm.getBytes( src, src_pitch );
467
if(!color)
468
icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) );
469
else
470
memcpy( data, src, m_width*3 );
471
}
472
result = true;
473
break;
474
/************************* 32 BPP ************************/
475
case 32:
476
for( y = 0; y < m_height; y++, data += step )
477
{
478
m_strm.getBytes( src, src_pitch );
479
480
if( !color )
481
icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) );
482
else if( img.channels() == 3 )
483
icvCvt_BGRA2BGR_8u_C4C3R(src, 0, data, 0, cvSize(m_width, 1));
484
else if( img.channels() == 4 )
485
memcpy(data, src, m_width * 4);
486
}
487
result = true;
488
break;
489
default:
490
CV_Error(cv::Error::StsError, "Invalid/unsupported mode");
491
}
492
}
493
CV_CATCH_ALL
494
{
495
CV_RETHROW();
496
}
497
498
return result;
499
}
500
501
502
//////////////////////////////////////////////////////////////////////////////////////////
503
504
BmpEncoder::BmpEncoder()
505
{
506
m_description = "Windows bitmap (*.bmp;*.dib)";
507
m_buf_supported = true;
508
}
509
510
511
BmpEncoder::~BmpEncoder()
512
{
513
}
514
515
ImageEncoder BmpEncoder::newEncoder() const
516
{
517
return makePtr<BmpEncoder>();
518
}
519
520
bool BmpEncoder::write( const Mat& img, const std::vector<int>& )
521
{
522
int width = img.cols, height = img.rows, channels = img.channels();
523
int fileStep = (width*channels + 3) & -4;
524
uchar zeropad[] = "\0\0\0\0";
525
WLByteStream strm;
526
527
if( m_buf )
528
{
529
if( !strm.open( *m_buf ) )
530
return false;
531
}
532
else if( !strm.open( m_filename ))
533
return false;
534
535
int bitmapHeaderSize = 40;
536
int paletteSize = channels > 1 ? 0 : 1024;
537
int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
538
size_t fileSize = (size_t)fileStep*height + headerSize;
539
PaletteEntry palette[256];
540
541
if( m_buf )
542
m_buf->reserve( alignSize(fileSize + 16, 256) );
543
544
// write signature 'BM'
545
strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
546
547
// write file header
548
strm.putDWord( validateToInt(fileSize) ); // file size
549
strm.putDWord( 0 );
550
strm.putDWord( headerSize );
551
552
// write bitmap header
553
strm.putDWord( bitmapHeaderSize );
554
strm.putDWord( width );
555
strm.putDWord( height );
556
strm.putWord( 1 );
557
strm.putWord( channels << 3 );
558
strm.putDWord( BMP_RGB );
559
strm.putDWord( 0 );
560
strm.putDWord( 0 );
561
strm.putDWord( 0 );
562
strm.putDWord( 0 );
563
strm.putDWord( 0 );
564
565
if( channels == 1 )
566
{
567
FillGrayPalette( palette, 8 );
568
strm.putBytes( palette, sizeof(palette));
569
}
570
571
width *= channels;
572
for( int y = height - 1; y >= 0; y-- )
573
{
574
strm.putBytes( img.ptr(y), width );
575
if( fileStep > width )
576
strm.putBytes( zeropad, fileStep - width );
577
}
578
579
strm.close();
580
return true;
581
}
582
583
}
584
585