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>
15 #include "scanline_read_test.hpp"
18 using namespace boost::gil
;
20 using tag_t
= bmp_tag
;
22 BOOST_AUTO_TEST_SUITE( gil_io_bmp_tests
)
24 #ifdef BOOST_GIL_IO_USE_BMP_TEST_SUITE_IMAGES
26 template< typename Image
>
27 void write( Image
& img
28 , const string
& file_name
31 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
32 write_view( bmp_out
+ file_name
36 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
39 BOOST_AUTO_TEST_CASE( read_header_test
)
42 using backend_t
= get_reader_backend
<std::string
const, tag_t
>::type
;
44 backend_t backend
= read_image_info( bmp_filename
48 BOOST_CHECK_EQUAL( backend
._info
._offset
, 54u );
49 BOOST_CHECK_EQUAL( backend
._info
._header_size
, 40u );
50 BOOST_CHECK_EQUAL( backend
._info
._width
, 1000 );
51 BOOST_CHECK_EQUAL( backend
._info
._height
, 600 );
52 BOOST_CHECK_EQUAL( backend
._info
._bits_per_pixel
, 24 );
53 BOOST_CHECK_EQUAL( backend
._info
._compression
, 0u );
54 BOOST_CHECK_EQUAL( backend
._info
._image_size
, 1800000u );
55 BOOST_CHECK_EQUAL( backend
._info
._horizontal_resolution
, 0 );
56 BOOST_CHECK_EQUAL( backend
._info
._vertical_resolution
, 0 );
57 BOOST_CHECK_EQUAL( backend
._info
._num_colors
, 0u );
58 BOOST_CHECK_EQUAL( backend
._info
._num_important_colors
, 0u );
59 BOOST_CHECK_EQUAL( backend
._info
._valid
, true );
63 BOOST_AUTO_TEST_CASE( read_reference_images_test
)
65 // comments are taken from http://entropymine.com/jason/bmpsuite/reference/reference.html
67 // g01bw.bmp - black and white palette (#000000,#FFFFFF)
70 read_image( bmp_in
+ "g01bw.bmp", img
, tag_t() );
73 // g01wb.bmp - white and black palette (#FFFFFF,#000000).
74 // Should look the same as g01bw, not inverted.
78 read_image( bmp_in
+ "g01wb.bmp", img
, tag_t() );
79 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
80 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
82 write( img
, "g01wb.bmp" );
85 // g01bg.bmp - blue and green palette (#4040FF,#40FF40)
89 read_image( bmp_in
+ "g01bg.bmp", img
, tag_t() );
90 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
91 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
93 write( img
, "g01bg.bmp" );
96 // g01p1.bmp - 1-color (blue) palette (#4040FF)
100 read_image( bmp_in
+ "g01p1.bmp", img
, tag_t() );
101 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
102 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
104 write( img
, "g01p1.bmp" );
107 // g04.bmp - basic 4bpp (16 color) image
111 read_image( bmp_in
+ "g04.bmp", img
, tag_t() );
112 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
113 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
115 write( img
, "g04.bmp" );
118 // g04rle.bmp - RLE compressed.
122 read_image( bmp_in
+ "g04rle.bmp", img
, tag_t() );
123 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
124 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
126 write( img
, "g04rle.bmp" );
129 // g04p4.bmp - 4-color grayscale palette
133 read_image( bmp_in
+ "g04p4.bmp", img
, tag_t() );
134 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
135 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
137 write( img
, "g04p4.bmp" );
140 // g08.bmp - basic 8bpp (256 color) image
144 read_image( bmp_in
+ "g08.bmp", img
, tag_t() );
145 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
146 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
148 write( img
, "g08.bmp" );
151 // g08p256.bmp - biClrUsed=256, biClrImportant=0 [=256]
155 read_image( bmp_in
+ "g08p256.bmp", img
, tag_t() );
156 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
157 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
159 write( img
, "g08p256.bmp" );
162 // g08pi256.bmp - biClrUsed=256, biClrImportant=256
166 read_image( bmp_in
+ "g08pi256.bmp", img
, tag_t() );
167 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
168 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
170 write( img
, "g08pi256.bmp" );
173 // g08pi64.bmp - biClrUsed=256, biClrImportant=64. It's barely possible that some
174 // sophisticated viewers may display this image in grayscale, if there are a
175 // limited number of colors available.
179 read_image( bmp_in
+ "g08pi64.bmp", img
, tag_t() );
180 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
181 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
183 write( img
, "g08pi64.bmp" );
186 // g08rle.bmp - RLE compressed.
190 read_image( bmp_in
+ "g08rle.bmp", img
, tag_t() );
191 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
192 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
194 write( img
, "g08rle.bmp" );
197 // g08os2.bmp - OS/2-style bitmap. This is an obsolete variety of BMP
198 // that is still encountered sometimes. It has 3-byte palette
199 // entries (instead of 4), and 16-bit width/height fields (instead of 32).
203 read_image( bmp_in
+ "g08os2.bmp", img
, tag_t() );
204 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
205 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
207 write( img
, "g08os2.bmp" );
210 // g08res22.bmp - resolution 7874x7874 pixels/meter (200x200 dpi)
214 read_image( bmp_in
+ "g08res22.bmp", img
, tag_t() );
215 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
216 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
218 write( img
, "g08res22.bmp" );
221 // g08res11.bmp - resolution 3937x3937 pixels/meter (100x100 dpi)
225 read_image( bmp_in
+ "g08res11.bmp", img
, tag_t() );
226 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
227 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
229 write( img
, "g08res11.bmp" );
232 // g08res21.bmp resolution 7874x3937 pixels/meter (200x100 dpi).
233 // Some programs (e.g. Imaging for Windows) may display this image
234 // stretched vertically, which is the optimal thing to do if the
235 // program is primarily a viewer, rather than an editor.
239 read_image( bmp_in
+ "g08res21.bmp", img
, tag_t() );
240 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
241 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
243 write( img
, "g08res21.bmp" );
246 // g08s0.bmp - bits size not given (set to 0). This is legal for uncompressed bitmaps.
250 read_image( bmp_in
+ "g08s0.bmp", img
, tag_t() );
251 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
252 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
254 write( img
, "g08s0.bmp" );
257 // g08offs.bmp - bfOffBits in header not set to the usual value.
258 // There are 100 extra unused bytes between palette and bits.
262 read_image( bmp_in
+ "g08offs.bmp", img
, tag_t() );
263 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
264 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
266 write( img
, "g08offs.bmp" );
269 // g08w126.bmp - size 126x63 (right and bottom slightly clipped)
273 read_image( bmp_in
+ "g08w126.bmp", img
, tag_t() );
274 BOOST_CHECK_EQUAL( view( img
).width() , 126u );
275 BOOST_CHECK_EQUAL( view( img
).height(), 63u );
277 write( img
, "g08w126.bmp" );
280 // g08w125.bmp - size 125x62
284 read_image( bmp_in
+ "g08w125.bmp", img
, tag_t() );
285 BOOST_CHECK_EQUAL( view( img
).width() , 125u );
286 BOOST_CHECK_EQUAL( view( img
).height(), 62u );
288 write( img
, "g08w125.bmp" );
291 // g08w124.bmp - size 124x61
295 read_image( bmp_in
+ "g08w124.bmp", img
, tag_t() );
296 BOOST_CHECK_EQUAL( view( img
).width() , 124u );
297 BOOST_CHECK_EQUAL( view( img
).height(), 61u );
299 write( img
, "g08w124.bmp" );
302 // g08p64.bmp - 64-color grayscale palette
306 read_image( bmp_in
+ "g08p64.bmp", img
, tag_t() );
307 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
308 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
310 write( img
, "g08p64.bmp" );
313 // g16def555.bmp - 15-bit color (1 bit wasted), biCompression=BI_RGB (no bitfields, defaults to 5-5-5)
317 read_image( bmp_in
+ "g16def555.bmp", img
, tag_t() );
318 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
319 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
321 write( img
, "g16def555.bmp" );
324 // g16bf555.bmp - 15-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-5-5)
328 read_image( bmp_in
+ "g16bf555.bmp", img
, tag_t() );
329 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
330 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
332 write( img
, "g16bf555.bmp" );
335 // g16bf565.bmp - 16-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-6-5)
339 read_image( bmp_in
+ "g16bf565.bmp", img
, tag_t() );
340 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
341 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
343 write( img
, "g16bf565.bmp" );
346 // g24.bmp - 24-bit color (BGR)
350 read_image( bmp_in
+ "g24.bmp", img
, tag_t() );
351 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
352 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
354 write( img
, "g24.bmp" );
357 // g32def.bmp - 24-bit color (8 bits wasted), biCompression=BI_RGB (no bitfields, defaults to BGRx)
361 read_image( bmp_in
+ "g32def.bmp", img
, tag_t() );
362 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
363 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
365 write( img
, "g32def.bmp" );
368 // g32bf.bmp - 24-bit color (8 bits wasted), biCompression=BI_BITFIELDS (bitfields indicate BGRx)
372 read_image( bmp_in
+ "g32bf.bmp", img
, tag_t() );
373 BOOST_CHECK_EQUAL( view( img
).width() , 127u );
374 BOOST_CHECK_EQUAL( view( img
).height(), 64u );
376 write( img
, "g32bf.bmp" );
381 BOOST_AUTO_TEST_CASE( read_reference_images_image_iterator_test
)
383 // comments are taken from http://entropymine.com/jason/bmpsuite/reference/reference.html
385 // g01bw.bmp - black and white palette (#000000,#FFFFFF)
386 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g01bw.bmp" ).c_str() );
388 // g01wb.bmp - white and black palette (#FFFFFF,#000000).
389 // Should look the same as g01bw, not inverted.
390 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g01wb.bmp" ).c_str() );
392 // g01bg.bmp - blue and green palette (#4040FF,#40FF40)
393 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g01bg.bmp" ).c_str() );
395 // g01p1.bmp - 1-color (blue) palette (#4040FF)
396 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g01p1.bmp" ).c_str() );
398 // g04.bmp - basic 4bpp (16 color) image
399 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g04.bmp" ).c_str() );
402 // g04rle.bmp - RLE compressed.
403 //test_scanline_reader< bgra8_image_t, bmp_tag >( string( bmp_in + "g01bg.bmp" ).c_str() );
405 // g04p4.bmp - 4-color grayscale palette
406 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g04p4.bmp" ).c_str() );
408 // g08.bmp - basic 8bpp (256 color) image
409 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08.bmp" ).c_str() );
411 // g08p256.bmp - biClrUsed=256, biClrImportant=0 [=256]
412 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08p256.bmp" ).c_str() );
414 // g08pi256.bmp - biClrUsed=256, biClrImportant=256
415 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08pi256.bmp" ).c_str() );
417 // g08pi64.bmp - biClrUsed=256, biClrImportant=64. It's barely possible that some
418 // sophisticated viewers may display this image in grayscale, if there are a
419 // limited number of colors available.
420 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08pi64.bmp" ).c_str() );
423 // g08rle.bmp - RLE compressed.
425 // g08os2.bmp - OS/2-style bitmap. This is an obsolete variety of BMP
426 // that is still encountered sometimes. It has 3-byte palette
427 // entries (instead of 4), and 16-bit width/height fields (instead of 32).
428 test_scanline_reader
< rgb8_image_t
, bmp_tag
>( string( bmp_in
+ "g08os2.bmp" ).c_str() );
430 // g08res22.bmp - resolution 7874x7874 pixels/meter (200x200 dpi)
431 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08res22.bmp" ).c_str() );
433 // g08res11.bmp - resolution 3937x3937 pixels/meter (100x100 dpi)
434 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08res11.bmp" ).c_str() );
436 // g08res21.bmp resolution 7874x3937 pixels/meter (200x100 dpi).
437 // Some programs (e.g. Imaging for Windows) may display this image
438 // stretched vertically, which is the optimal thing to do if the
439 // program is primarily a viewer, rather than an editor.
440 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08res21.bmp" ).c_str() );
442 // g08s0.bmp - bits size not given (set to 0). This is legal for uncompressed bitmaps.
443 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08s0.bmp" ).c_str() );
445 // g08offs.bmp - bfOffBits in header not set to the usual value.
446 // There are 100 extra unused bytes between palette and bits.
447 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08offs.bmp" ).c_str() );
449 // g08w126.bmp - size 126x63 (right and bottom slightly clipped)
450 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08w126.bmp" ).c_str() );
452 // g08w125.bmp - size 125x62
453 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08w125.bmp" ).c_str() );
455 // g08w124.bmp - size 124x61
456 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08w124.bmp" ).c_str() );
458 // g08p64.bmp - 64-color grayscale palette
459 test_scanline_reader
< rgba8_image_t
, bmp_tag
>( string( bmp_in
+ "g08p64.bmp" ).c_str() );
461 // g16def555.bmp - 15-bit color (1 bit wasted), biCompression=BI_RGB (no bitfields, defaults to 5-5-5)
462 test_scanline_reader
< rgb8_image_t
, bmp_tag
>( string( bmp_in
+ "g16def555.bmp" ).c_str() );
464 // g16bf555.bmp - 15-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-5-5)
465 test_scanline_reader
< rgb8_image_t
, bmp_tag
>( string( bmp_in
+ "g16bf555.bmp" ).c_str() );
467 // g16bf565.bmp - 16-bit color, biCompression=BI_BITFIELDS (bitfields indicate 5-6-5)
468 test_scanline_reader
< rgb8_image_t
, bmp_tag
>( string( bmp_in
+ "g16bf565.bmp" ).c_str() );
470 // g24.bmp - 24-bit color (BGR)
471 test_scanline_reader
< bgr8_image_t
, bmp_tag
>( string( bmp_in
+ "g24.bmp" ).c_str() );
473 // g32def.bmp - 24-bit color (8 bits wasted), biCompression=BI_RGB (no bitfields, defaults to BGRx)
474 test_scanline_reader
< bgra8_image_t
, bmp_tag
>( string( bmp_in
+ "g32def.bmp" ).c_str() );
476 // g32bf.bmp - 24-bit color (8 bits wasted), biCompression=BI_BITFIELDS (bitfields indicate BGRx)
477 test_scanline_reader
< bgra8_image_t
, bmp_tag
>( string( bmp_in
+ "g32bf.bmp" ).c_str() );
480 BOOST_AUTO_TEST_CASE( partial_image_test
)
482 const std::string
filename( bmp_in
+ "rgb.bmp" );
488 , image_read_settings
< bmp_tag
>( point_t( 0, 0 ), point_t( 50, 50 ) )
491 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
492 write_view( bmp_out
+ "rgb_partial.bmp"
496 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
500 #endif // BOOST_GIL_IO_USE_BMP_TEST_SUITE_IMAGES
502 BOOST_AUTO_TEST_SUITE_END()