2 // Copyright 2012 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 #ifndef BOOST_GIL_EXTENSION_IO_BMP_DETAIL_READER_BACKEND_HPP
9 #define BOOST_GIL_EXTENSION_IO_BMP_DETAIL_READER_BACKEND_HPP
11 #include <boost/gil/extension/io/bmp/tags.hpp>
13 namespace boost { namespace gil {
15 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
17 #pragma warning(disable:4512) //assignment operator could not be generated
20 /// Color channel mask
23 unsigned int mask; // Bit mask at corresponding position
24 unsigned int width; // Bit width of the mask
25 unsigned int shift; // Bit position from right to left
31 bit_field red; // Red bits
32 bit_field green; // Green bits
33 bit_field blue; // Blue bits
40 template< typename Device >
41 struct reader_backend< Device
47 using format_tag_t = bmp_tag;
51 reader_backend( const Device& io_dev
52 , const image_read_settings< bmp_tag >& settings
55 , _settings( settings )
57 , _scanline_length( 0 )
62 if( _settings._dim.x == 0 )
64 _settings._dim.x = _info._width;
67 if( _settings._dim.y == 0 )
69 _settings._dim.y = _info._height;
75 // the magic number used to identify the BMP file:
76 // 0x42 0x4D (ASCII code points for B and M)
77 if( _io_dev.read_uint16() == 0x424D )
79 io_error( "Wrong magic number for bmp file." );
82 // the size of the BMP file in bytes
83 _io_dev.read_uint32();
85 // reserved; actual value depends on the application that creates the image
86 _io_dev.read_uint16();
87 // reserved; actual value depends on the application that creates the image
88 _io_dev.read_uint16();
90 _info._offset = _io_dev.read_uint32();
95 // the size of this header ( 40 bytes )
96 _info._header_size = _io_dev.read_uint32();
98 if( _info._header_size == bmp_header_size::_win32_info_size )
100 _info._width = _io_dev.read_uint32();
101 _info._height = _io_dev.read_uint32();
103 if (_info._height < 0)
105 _info._height = -_info._height;
106 _info._top_down = true;
109 // the number of color planes being used. Must be set to 1.
110 _io_dev.read_uint16();
112 _info._bits_per_pixel = _io_dev.read_uint16();
114 _info._compression = _io_dev.read_uint32();
116 _info._image_size = _io_dev.read_uint32();
118 _info._horizontal_resolution = _io_dev.read_uint32();
119 _info._vertical_resolution = _io_dev.read_uint32();
121 _info._num_colors = _io_dev.read_uint32();
122 _info._num_important_colors = _io_dev.read_uint32();
125 else if( _info._header_size == bmp_header_size::_os2_info_size )
127 _info._width = static_cast< bmp_image_width::type >( _io_dev.read_uint16() );
128 _info._height = static_cast< bmp_image_height::type >( _io_dev.read_uint16() );
130 // the number of color planes being used. Must be set to 1.
131 _io_dev.read_uint16();
133 _info._bits_per_pixel = _io_dev.read_uint16();
135 _info._compression = bmp_compression::_rgb;
138 _info._image_size = 0;
139 _info._horizontal_resolution = 0;
140 _info._vertical_resolution = 0;
141 _info._num_colors = 0;
142 _info._num_important_colors = 0;
144 else if (_info._header_size > bmp_header_size::_win32_info_size)
147 // see MSDN: Bitmap Header Types ( BITMAPV4HEADER or BITMAPV5HEADER )
149 _info._width = _io_dev.read_uint32();
150 _info._height = _io_dev.read_uint32();
152 // the number of color planes being used. Must be set to 1.
153 _io_dev.read_uint16();
155 _info._bits_per_pixel = _io_dev.read_uint16();
157 _info._compression = _io_dev.read_uint32();
159 _info._image_size = _io_dev.read_uint32();
161 _info._horizontal_resolution = _io_dev.read_uint32();
162 _info._vertical_resolution = _io_dev.read_uint32();
164 _info._num_colors = _io_dev.read_uint32();
165 _info._num_important_colors = _io_dev.read_uint32();
169 io_error( "Invalid BMP info header." );
177 int entries = this->_info._num_colors;
181 entries = 1u << this->_info._bits_per_pixel;
184 _palette.resize( entries, rgba8_pixel_t(0, 0, 0, 0));
186 for( int i = 0; i < entries; ++i )
188 get_color( _palette[i], blue_t() ) = _io_dev.read_uint8();
189 get_color( _palette[i], green_t() ) = _io_dev.read_uint8();
190 get_color( _palette[i], red_t() ) = _io_dev.read_uint8();
192 // there are 4 entries when windows header
193 // but 3 for os2 header
194 if( _info._header_size == bmp_header_size::_win32_info_size )
196 _io_dev.read_uint8();
202 /// Check if image is large enough.
203 void check_image_size( const point_t& img_dim )
205 if( _settings._dim.x > 0 )
207 if( img_dim.x < _settings._dim.x ) { io_error( "Supplied image is too small" ); }
211 if( img_dim.x < _info._width ) { io_error( "Supplied image is too small" ); }
215 if( _settings._dim.y > 0 )
217 if( img_dim.y < _settings._dim.y ) { io_error( "Supplied image is too small" ); }
221 if( img_dim.y < _info._height ) { io_error( "Supplied image is too small" ); }
229 image_read_settings< bmp_tag > _settings;
230 image_read_info< bmp_tag > _info;
232 std::size_t _scanline_length;
234 ///@todo make it an image.
235 std::vector< rgba8_pixel_t > _palette;
240 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)