2 // Copyright 2013 Christian Henning
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 //#define BOOST_TEST_MODULE bmp_read_test_module
9 #include <boost/gil.hpp>
10 #include <boost/gil/extension/io/bmp.hpp>
12 #include <boost/test/unit_test.hpp>
13 #include <boost/type_traits/is_same.hpp>
16 #include "scanline_read_test.hpp"
19 using namespace boost::gil;
21 using tag_t = bmp_tag;
23 BOOST_AUTO_TEST_SUITE( gil_io_bmp_tests )
25 #ifdef BOOST_GIL_IO_USE_BMP_TEST_SUITE_IMAGES
27 template< typename Image >
28 void write( Image& img
29 , const string& file_name
32 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
33 write_view( bmp_out + file_name
37 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
40 BOOST_AUTO_TEST_CASE( read_header_test )
43 using backend_t = get_reader_backend<std::string const, tag_t>::type;
45 backend_t backend = read_image_info( bmp_filename
49 BOOST_CHECK_EQUAL( backend._info._offset , 54u );
50 BOOST_CHECK_EQUAL( backend._info._header_size , 40u );
51 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
52 BOOST_CHECK_EQUAL( backend._info._height , 600 );
53 BOOST_CHECK_EQUAL( backend._info._bits_per_pixel , 24 );
54 BOOST_CHECK_EQUAL( backend._info._compression , 0u );
55 BOOST_CHECK_EQUAL( backend._info._image_size , 1800000u );
56 BOOST_CHECK_EQUAL( backend._info._horizontal_resolution, 0 );
57 BOOST_CHECK_EQUAL( backend._info._vertical_resolution , 0 );
58 BOOST_CHECK_EQUAL( backend._info._num_colors , 0u );
59 BOOST_CHECK_EQUAL( backend._info._num_important_colors , 0u );
60 BOOST_CHECK_EQUAL( backend._info._valid , true );
64 BOOST_AUTO_TEST_CASE( read_reference_images_test )
66 // comments are taken from http://entropymine.com/jason/bmpsuite/reference/reference.html
68 // g01bw.bmp - black and white palette (#000000,#FFFFFF)
71 read_image( bmp_in + "g01bw.bmp", img, tag_t() );
74 // g01wb.bmp - white and black palette (#FFFFFF,#000000).
75 // Should look the same as g01bw, not inverted.
79 read_image( bmp_in + "g01wb.bmp", img, tag_t() );
80 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
81 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
83 write( img, "g01wb.bmp" );
86 // g01bg.bmp - blue and green palette (#4040FF,#40FF40)
90 read_image( bmp_in + "g01bg.bmp", img, tag_t() );
91 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
92 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
94 write( img, "g01bg.bmp" );
97 // g01p1.bmp - 1-color (blue) palette (#4040FF)
101 read_image( bmp_in + "g01p1.bmp", img, tag_t() );
102 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
103 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
105 write( img, "g01p1.bmp" );
108 // g04.bmp - basic 4bpp (16 color) image
112 read_image( bmp_in + "g04.bmp", img, tag_t() );
113 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
114 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
116 write( img, "g04.bmp" );
119 // g04rle.bmp - RLE compressed.
123 read_image( bmp_in + "g04rle.bmp", img, tag_t() );
124 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
125 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
127 write( img, "g04rle.bmp" );
130 // g04p4.bmp - 4-color grayscale palette
134 read_image( bmp_in + "g04p4.bmp", img, tag_t() );
135 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
136 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
138 write( img, "g04p4.bmp" );
141 // g08.bmp - basic 8bpp (256 color) image
145 read_image( bmp_in + "g08.bmp", img, tag_t() );
146 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
147 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
149 write( img, "g08.bmp" );
152 // g08p256.bmp - biClrUsed=256, biClrImportant=0 [=256]
156 read_image( bmp_in + "g08p256.bmp", img, tag_t() );
157 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
158 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
160 write( img, "g08p256.bmp" );
163 // g08pi256.bmp - biClrUsed=256, biClrImportant=256
167 read_image( bmp_in + "g08pi256.bmp", img, tag_t() );
168 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
169 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
171 write( img, "g08pi256.bmp" );
174 // g08pi64.bmp - biClrUsed=256, biClrImportant=64. It's barely possible that some
175 // sophisticated viewers may display this image in grayscale, if there are a
176 // limited number of colors available.
180 read_image( bmp_in + "g08pi64.bmp", img, tag_t() );
181 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
182 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
184 write( img, "g08pi64.bmp" );
187 // g08rle.bmp - RLE compressed.
191 read_image( bmp_in + "g08rle.bmp", img, tag_t() );
192 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
193 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
195 write( img, "g08rle.bmp" );
198 // g08os2.bmp - OS/2-style bitmap. This is an obsolete variety of BMP
199 // that is still encountered sometimes. It has 3-byte palette
200 // entries (instead of 4), and 16-bit width/height fields (instead of 32).
204 read_image( bmp_in + "g08os2.bmp", img, tag_t() );
205 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
206 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
208 write( img, "g08os2.bmp" );
211 // g08res22.bmp - resolution 7874x7874 pixels/meter (200x200 dpi)
215 read_image( bmp_in + "g08res22.bmp", img, tag_t() );
216 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
217 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
219 write( img, "g08res22.bmp" );
222 // g08res11.bmp - resolution 3937x3937 pixels/meter (100x100 dpi)
226 read_image( bmp_in + "g08res11.bmp", img, tag_t() );
227 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
228 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
230 write( img, "g08res11.bmp" );
233 // g08res21.bmp resolution 7874x3937 pixels/meter (200x100 dpi).
234 // Some programs (e.g. Imaging for Windows) may display this image
235 // stretched vertically, which is the optimal thing to do if the
236 // program is primarily a viewer, rather than an editor.
240 read_image( bmp_in + "g08res21.bmp", img, tag_t() );
241 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
242 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
244 write( img, "g08res21.bmp" );
247 // g08s0.bmp - bits size not given (set to 0). This is legal for uncompressed bitmaps.
251 read_image( bmp_in + "g08s0.bmp", img, tag_t() );
252 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
253 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
255 write( img, "g08s0.bmp" );
258 // g08offs.bmp - bfOffBits in header not set to the usual value.
259 // There are 100 extra unused bytes between palette and bits.
263 read_image( bmp_in + "g08offs.bmp", img, tag_t() );
264 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
265 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
267 write( img, "g08offs.bmp" );
270 // g08w126.bmp - size 126x63 (right and bottom slightly clipped)
274 read_image( bmp_in + "g08w126.bmp", img, tag_t() );
275 BOOST_CHECK_EQUAL( view( img ).width() , 126u );
276 BOOST_CHECK_EQUAL( view( img ).height(), 63u );
278 write( img, "g08w126.bmp" );
281 // g08w125.bmp - size 125x62
285 read_image( bmp_in + "g08w125.bmp", img, tag_t() );
286 BOOST_CHECK_EQUAL( view( img ).width() , 125u );
287 BOOST_CHECK_EQUAL( view( img ).height(), 62u );
289 write( img, "g08w125.bmp" );
292 // g08w124.bmp - size 124x61
296 read_image( bmp_in + "g08w124.bmp", img, tag_t() );
297 BOOST_CHECK_EQUAL( view( img ).width() , 124u );
298 BOOST_CHECK_EQUAL( view( img ).height(), 61u );
300 write( img, "g08w124.bmp" );
303 // g08p64.bmp - 64-color grayscale palette
307 read_image( bmp_in + "g08p64.bmp", img, tag_t() );
308 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
309 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
311 write( img, "g08p64.bmp" );
314 // g16def555.bmp - 15-bit color (1 bit wasted), biCompression=BI_RGB (no bitfields, defaults to 5-5-5)
318 read_image( bmp_in + "g16def555.bmp", img, tag_t() );
319 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
320 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
322 write( img, "g16def555.bmp" );
325 // g16bf555.bmp - 15-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-5-5)
329 read_image( bmp_in + "g16bf555.bmp", img, tag_t() );
330 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
331 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
333 write( img, "g16bf555.bmp" );
336 // g16bf565.bmp - 16-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-6-5)
340 read_image( bmp_in + "g16bf565.bmp", img, tag_t() );
341 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
342 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
344 write( img, "g16bf565.bmp" );
347 // g24.bmp - 24-bit color (BGR)
351 read_image( bmp_in + "g24.bmp", img, tag_t() );
352 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
353 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
355 write( img, "g24.bmp" );
358 // g32def.bmp - 24-bit color (8 bits wasted), biCompression=BI_RGB (no bitfields, defaults to BGRx)
362 read_image( bmp_in + "g32def.bmp", img, tag_t() );
363 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
364 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
366 write( img, "g32def.bmp" );
369 // g32bf.bmp - 24-bit color (8 bits wasted), biCompression=BI_BITFIELDS (bitfields indicate BGRx)
373 read_image( bmp_in + "g32bf.bmp", img, tag_t() );
374 BOOST_CHECK_EQUAL( view( img ).width() , 127u );
375 BOOST_CHECK_EQUAL( view( img ).height(), 64u );
377 write( img, "g32bf.bmp" );
382 BOOST_AUTO_TEST_CASE( read_reference_images_image_iterator_test )
384 // comments are taken from http://entropymine.com/jason/bmpsuite/reference/reference.html
386 // g01bw.bmp - black and white palette (#000000,#FFFFFF)
387 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g01bw.bmp" ).c_str() );
389 // g01wb.bmp - white and black palette (#FFFFFF,#000000).
390 // Should look the same as g01bw, not inverted.
391 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g01wb.bmp" ).c_str() );
393 // g01bg.bmp - blue and green palette (#4040FF,#40FF40)
394 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g01bg.bmp" ).c_str() );
396 // g01p1.bmp - 1-color (blue) palette (#4040FF)
397 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g01p1.bmp" ).c_str() );
399 // g04.bmp - basic 4bpp (16 color) image
400 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g04.bmp" ).c_str() );
403 // g04rle.bmp - RLE compressed.
404 //test_scanline_reader< bgra8_image_t, bmp_tag >( string( bmp_in + "g01bg.bmp" ).c_str() );
406 // g04p4.bmp - 4-color grayscale palette
407 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g04p4.bmp" ).c_str() );
409 // g08.bmp - basic 8bpp (256 color) image
410 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08.bmp" ).c_str() );
412 // g08p256.bmp - biClrUsed=256, biClrImportant=0 [=256]
413 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08p256.bmp" ).c_str() );
415 // g08pi256.bmp - biClrUsed=256, biClrImportant=256
416 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08pi256.bmp" ).c_str() );
418 // g08pi64.bmp - biClrUsed=256, biClrImportant=64. It's barely possible that some
419 // sophisticated viewers may display this image in grayscale, if there are a
420 // limited number of colors available.
421 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08pi64.bmp" ).c_str() );
424 // g08rle.bmp - RLE compressed.
426 // g08os2.bmp - OS/2-style bitmap. This is an obsolete variety of BMP
427 // that is still encountered sometimes. It has 3-byte palette
428 // entries (instead of 4), and 16-bit width/height fields (instead of 32).
429 test_scanline_reader< rgb8_image_t, bmp_tag >( string( bmp_in + "g08os2.bmp" ).c_str() );
431 // g08res22.bmp - resolution 7874x7874 pixels/meter (200x200 dpi)
432 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08res22.bmp" ).c_str() );
434 // g08res11.bmp - resolution 3937x3937 pixels/meter (100x100 dpi)
435 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08res11.bmp" ).c_str() );
437 // g08res21.bmp resolution 7874x3937 pixels/meter (200x100 dpi).
438 // Some programs (e.g. Imaging for Windows) may display this image
439 // stretched vertically, which is the optimal thing to do if the
440 // program is primarily a viewer, rather than an editor.
441 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08res21.bmp" ).c_str() );
443 // g08s0.bmp - bits size not given (set to 0). This is legal for uncompressed bitmaps.
444 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08s0.bmp" ).c_str() );
446 // g08offs.bmp - bfOffBits in header not set to the usual value.
447 // There are 100 extra unused bytes between palette and bits.
448 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08offs.bmp" ).c_str() );
450 // g08w126.bmp - size 126x63 (right and bottom slightly clipped)
451 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08w126.bmp" ).c_str() );
453 // g08w125.bmp - size 125x62
454 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08w125.bmp" ).c_str() );
456 // g08w124.bmp - size 124x61
457 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08w124.bmp" ).c_str() );
459 // g08p64.bmp - 64-color grayscale palette
460 test_scanline_reader< rgba8_image_t, bmp_tag >( string( bmp_in + "g08p64.bmp" ).c_str() );
462 // g16def555.bmp - 15-bit color (1 bit wasted), biCompression=BI_RGB (no bitfields, defaults to 5-5-5)
463 test_scanline_reader< rgb8_image_t, bmp_tag >( string( bmp_in + "g16def555.bmp" ).c_str() );
465 // g16bf555.bmp - 15-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-5-5)
466 test_scanline_reader< rgb8_image_t, bmp_tag >( string( bmp_in + "g16bf555.bmp" ).c_str() );
468 // g16bf565.bmp - 16-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-6-5)
469 test_scanline_reader< rgb8_image_t, bmp_tag >( string( bmp_in + "g16bf565.bmp" ).c_str() );
471 // g24.bmp - 24-bit color (BGR)
472 test_scanline_reader< bgr8_image_t, bmp_tag >( string( bmp_in + "g24.bmp" ).c_str() );
474 // g32def.bmp - 24-bit color (8 bits wasted), biCompression=BI_RGB (no bitfields, defaults to BGRx)
475 test_scanline_reader< bgra8_image_t, bmp_tag >( string( bmp_in + "g32def.bmp" ).c_str() );
477 // g32bf.bmp - 24-bit color (8 bits wasted), biCompression=BI_BITFIELDS (bitfields indicate BGRx)
478 test_scanline_reader< bgra8_image_t, bmp_tag >( string( bmp_in + "g32bf.bmp" ).c_str() );
481 BOOST_AUTO_TEST_CASE( partial_image_test )
483 const std::string filename( bmp_in + "rgb.bmp" );
489 , image_read_settings< bmp_tag >( point_t( 0, 0 ), point_t( 50, 50 ) )
492 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
493 write_view( bmp_out + "rgb_partial.bmp"
497 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
501 #endif // BOOST_GIL_IO_USE_BMP_TEST_SUITE_IMAGES
503 BOOST_AUTO_TEST_SUITE_END()