read.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //
  2. // Copyright 2012 Christian Henning
  3. //
  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
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_IO_BMP_DETAIL_READ_HPP
  9. #define BOOST_GIL_EXTENSION_IO_BMP_DETAIL_READ_HPP
  10. #include <boost/gil/extension/io/bmp/detail/is_allowed.hpp>
  11. #include <boost/gil/extension/io/bmp/detail/reader_backend.hpp>
  12. #include <boost/gil/io/base.hpp>
  13. #include <boost/gil/io/bit_operations.hpp>
  14. #include <boost/gil/io/conversion_policies.hpp>
  15. #include <boost/gil/io/device.hpp>
  16. #include <boost/gil/io/dynamic_io_new.hpp>
  17. #include <boost/gil/io/reader_base.hpp>
  18. #include <boost/gil/io/row_buffer_helper.hpp>
  19. #include <boost/gil/io/typedefs.hpp>
  20. #include <boost/assert.hpp>
  21. #include <type_traits>
  22. #include <vector>
  23. namespace boost { namespace gil {
  24. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  25. #pragma warning(push)
  26. #pragma warning(disable:4512) //assignment operator could not be generated
  27. #endif
  28. ///
  29. /// BMP Reader
  30. ///
  31. template< typename Device
  32. , typename ConversionPolicy
  33. >
  34. class reader< Device
  35. , bmp_tag
  36. , ConversionPolicy
  37. >
  38. : public reader_base< bmp_tag
  39. , ConversionPolicy
  40. >
  41. , public reader_backend< Device
  42. , bmp_tag
  43. >
  44. {
  45. private:
  46. using this_t = reader<Device, bmp_tag, ConversionPolicy>;
  47. using cc_t = typename ConversionPolicy::color_converter_type;
  48. public:
  49. using backend_t = reader_backend< Device, bmp_tag>;
  50. public:
  51. //
  52. // Constructor
  53. //
  54. reader( const Device& io_dev
  55. , const image_read_settings< bmp_tag >& settings
  56. )
  57. : backend_t( io_dev
  58. , settings
  59. )
  60. , _pitch( 0 )
  61. {}
  62. //
  63. // Constructor
  64. //
  65. reader( const Device& io_dev
  66. , const ConversionPolicy& cc
  67. , const image_read_settings< bmp_tag >& settings
  68. )
  69. : reader_base< bmp_tag
  70. , ConversionPolicy
  71. >( cc )
  72. , backend_t( io_dev
  73. , settings
  74. )
  75. , _pitch( 0 )
  76. {}
  77. /// Read image.
  78. template< typename View >
  79. void apply( const View& dst_view )
  80. {
  81. if( this->_info._valid == false )
  82. {
  83. io_error( "Image header was not read." );
  84. }
  85. using is_read_and_convert_t = typename std::is_same
  86. <
  87. ConversionPolicy,
  88. detail::read_and_no_convert
  89. >::type;
  90. io_error_if( !detail::is_allowed< View >( this->_info
  91. , is_read_and_convert_t()
  92. )
  93. , "Image types aren't compatible."
  94. );
  95. // the row pitch must be multiple 4 bytes
  96. if( this->_info._bits_per_pixel < 8 )
  97. {
  98. _pitch = static_cast<long>((( this->_info._width * this->_info._bits_per_pixel ) + 7 ) >> 3 );
  99. }
  100. else
  101. {
  102. _pitch = static_cast<long>( this->_info._width * (( this->_info._bits_per_pixel + 7 ) >> 3 ));
  103. }
  104. _pitch = (_pitch + 3) & ~3;
  105. switch( this->_info._bits_per_pixel )
  106. {
  107. case 1:
  108. {
  109. this->_scanline_length = ( this->_info._width * num_channels< rgba8_view_t >::value + 3 ) & ~3;
  110. read_palette_image
  111. <
  112. gray1_image_t::view_t,
  113. detail::mirror_bits<byte_vector_t, std::true_type>
  114. >(dst_view);
  115. break;
  116. }
  117. case 4:
  118. {
  119. switch ( this->_info._compression )
  120. {
  121. case bmp_compression::_rle4:
  122. {
  123. ///@todo How can we determine that?
  124. this->_scanline_length = 0;
  125. read_palette_image_rle( dst_view );
  126. break;
  127. }
  128. case bmp_compression::_rgb:
  129. {
  130. this->_scanline_length = ( this->_info._width * num_channels< rgba8_view_t >::value + 3 ) & ~3;
  131. read_palette_image
  132. <
  133. gray4_image_t::view_t,
  134. detail::swap_half_bytes<byte_vector_t, std::true_type>
  135. >(dst_view);
  136. break;
  137. }
  138. default:
  139. {
  140. io_error( "Unsupported compression mode in BMP file." );
  141. break;
  142. }
  143. }
  144. break;
  145. }
  146. case 8:
  147. {
  148. switch ( this->_info._compression )
  149. {
  150. case bmp_compression::_rle8:
  151. {
  152. ///@todo How can we determine that?
  153. this->_scanline_length = 0;
  154. read_palette_image_rle( dst_view );
  155. break;
  156. }
  157. case bmp_compression::_rgb:
  158. {
  159. this->_scanline_length = ( this->_info._width * num_channels< rgba8_view_t >::value + 3 ) & ~3;
  160. read_palette_image< gray8_image_t::view_t
  161. , detail::do_nothing< std::vector< gray8_pixel_t > >
  162. > ( dst_view );
  163. break;
  164. }
  165. default:
  166. {
  167. io_error( "Unsupported compression mode in BMP file." );
  168. break;
  169. }
  170. }
  171. break;
  172. }
  173. case 15: case 16:
  174. {
  175. this->_scanline_length = ( this->_info._width * num_channels< rgb8_view_t >::value + 3 ) & ~3;
  176. read_data_15( dst_view );
  177. break;
  178. }
  179. case 24:
  180. {
  181. this->_scanline_length = ( this->_info._width * num_channels< rgb8_view_t >::value + 3 ) & ~3;
  182. read_data< bgr8_view_t >( dst_view );
  183. break;
  184. }
  185. case 32:
  186. {
  187. this->_scanline_length = ( this->_info._width * num_channels< rgba8_view_t >::value + 3 ) & ~3;
  188. read_data< bgra8_view_t >( dst_view );
  189. break;
  190. }
  191. }
  192. }
  193. private:
  194. long get_offset( std::ptrdiff_t pos )
  195. {
  196. if( this->_info._height > 0 )
  197. {
  198. // the image is upside down
  199. return static_cast<long>( ( this->_info._offset
  200. + ( this->_info._height - 1 - pos ) * _pitch
  201. ));
  202. }
  203. else
  204. {
  205. return static_cast<long>( ( this->_info._offset
  206. + pos * _pitch
  207. ));
  208. }
  209. }
  210. template< typename View_Src
  211. , typename Byte_Manipulator
  212. , typename View_Dst
  213. >
  214. void read_palette_image( const View_Dst& view )
  215. {
  216. this->read_palette();
  217. using rh_t = detail::row_buffer_helper_view<View_Src>;
  218. using it_t = typename rh_t::iterator_t;
  219. rh_t rh( _pitch, true );
  220. // we have to swap bits
  221. Byte_Manipulator byte_manipulator;
  222. for( std::ptrdiff_t y = 0
  223. ; y < this->_settings._dim.y
  224. ; ++y
  225. )
  226. {
  227. this->_io_dev.seek( get_offset( y + this->_settings._top_left.y ));
  228. this->_io_dev.read( reinterpret_cast< byte_t* >( rh.data() )
  229. , _pitch
  230. );
  231. byte_manipulator( rh.buffer() );
  232. typename View_Dst::x_iterator dst_it = view.row_begin( y );
  233. it_t it = rh.begin() + this->_settings._top_left.x;
  234. it_t end = it + this->_settings._dim.x;
  235. for( ; it != end; ++it, ++dst_it )
  236. {
  237. unsigned char c = get_color( *it, gray_color_t() );
  238. *dst_it = this->_palette[ c ];
  239. }
  240. }
  241. }
  242. template< typename View >
  243. void read_data_15( const View& view )
  244. {
  245. byte_vector_t row( _pitch );
  246. // read the color masks
  247. if( this->_info._compression == bmp_compression::_bitfield )
  248. {
  249. this->_mask.red.mask = this->_io_dev.read_uint32();
  250. this->_mask.green.mask = this->_io_dev.read_uint32();
  251. this->_mask.blue.mask = this->_io_dev.read_uint32();
  252. this->_mask.red.width = detail::count_ones( this->_mask.red.mask );
  253. this->_mask.green.width = detail::count_ones( this->_mask.green.mask );
  254. this->_mask.blue.width = detail::count_ones( this->_mask.blue.mask );
  255. this->_mask.red.shift = detail::trailing_zeros( this->_mask.red.mask );
  256. this->_mask.green.shift = detail::trailing_zeros( this->_mask.green.mask );
  257. this->_mask.blue.shift = detail::trailing_zeros( this->_mask.blue.mask );
  258. }
  259. else if( this->_info._compression == bmp_compression::_rgb )
  260. {
  261. switch( this->_info._bits_per_pixel )
  262. {
  263. case 15:
  264. case 16:
  265. {
  266. this->_mask.red.mask = 0x007C00; this->_mask.red.width = 5; this->_mask.red.shift = 10;
  267. this->_mask.green.mask = 0x0003E0; this->_mask.green.width = 5; this->_mask.green.shift = 5;
  268. this->_mask.blue.mask = 0x00001F; this->_mask.blue.width = 5; this->_mask.blue.shift = 0;
  269. break;
  270. }
  271. case 24:
  272. case 32:
  273. {
  274. this->_mask.red.mask = 0xFF0000; this->_mask.red.width = 8; this->_mask.red.shift = 16;
  275. this->_mask.green.mask = 0x00FF00; this->_mask.green.width = 8; this->_mask.green.shift = 8;
  276. this->_mask.blue.mask = 0x0000FF; this->_mask.blue.width = 8; this->_mask.blue.shift = 0;
  277. break;
  278. }
  279. }
  280. }
  281. else
  282. {
  283. io_error( "bmp_reader::apply(): unsupported BMP compression" );
  284. }
  285. using image_t = rgb8_image_t;
  286. using it_t = typename image_t::view_t::x_iterator;
  287. for( std::ptrdiff_t y = 0
  288. ; y < this->_settings._dim.y
  289. ; ++y
  290. )
  291. {
  292. this->_io_dev.seek( get_offset( y + this->_settings._top_left.y ));
  293. this->_io_dev.read( &row.front()
  294. , row.size()
  295. );
  296. image_t img_row( this->_info._width, 1 );
  297. image_t::view_t v = gil::view( img_row );
  298. it_t it = v.row_begin( 0 );
  299. it_t beg = v.row_begin( 0 ) + this->_settings._top_left.x;
  300. it_t end = beg + this->_settings._dim.x;
  301. byte_t* src = &row.front();
  302. for( int32_t i = 0 ; i < this->_info._width; ++i, src += 2 )
  303. {
  304. int p = ( src[1] << 8 ) | src[0];
  305. int r = ((p & this->_mask.red.mask) >> this->_mask.red.shift) << (8 - this->_mask.red.width);
  306. int g = ((p & this->_mask.green.mask) >> this->_mask.green.shift) << (8 - this->_mask.green.width);
  307. int b = ((p & this->_mask.blue.mask) >> this->_mask.blue.shift) << (8 - this->_mask.blue.width);
  308. get_color( it[i], red_t() ) = static_cast< byte_t >( r );
  309. get_color( it[i], green_t() ) = static_cast< byte_t >( g );
  310. get_color( it[i], blue_t() ) = static_cast< byte_t >( b );
  311. }
  312. this->_cc_policy.read( beg
  313. , end
  314. , view.row_begin( y )
  315. );
  316. }
  317. }
  318. // 8-8-8 BGR
  319. // 8-8-8-8 BGRA
  320. template< typename View_Src
  321. , typename View_Dst
  322. >
  323. void read_data( const View_Dst& view )
  324. {
  325. byte_vector_t row( _pitch );
  326. View_Src v = interleaved_view( this->_info._width
  327. , 1
  328. , (typename View_Src::value_type*) &row.front()
  329. , this->_info._width * num_channels< View_Src >::value
  330. );
  331. typename View_Src::x_iterator beg = v.row_begin( 0 ) + this->_settings._top_left.x;
  332. typename View_Src::x_iterator end = beg + this->_settings._dim.x;
  333. for( std::ptrdiff_t y = 0
  334. ; y < this->_settings._dim.y
  335. ; ++y
  336. )
  337. {
  338. this->_io_dev.seek( get_offset( y + this->_settings._top_left.y ));
  339. this->_io_dev.read( &row.front()
  340. , row.size()
  341. );
  342. this->_cc_policy.read( beg
  343. , end
  344. , view.row_begin( y )
  345. );
  346. }
  347. }
  348. template< typename Buffer
  349. , typename View
  350. >
  351. void copy_row_if_needed( const Buffer& buf
  352. , const View& view
  353. , std::ptrdiff_t y
  354. )
  355. {
  356. if( y >= this->_settings._top_left.y
  357. && y < this->_settings._dim.y
  358. )
  359. {
  360. typename Buffer::const_iterator beg = buf.begin() + this->_settings._top_left.x;
  361. typename Buffer::const_iterator end = beg + this->_settings._dim.x;
  362. std::copy( beg
  363. , end
  364. , view.row_begin( y )
  365. );
  366. }
  367. }
  368. template< typename View_Dst >
  369. void read_palette_image_rle( const View_Dst& view )
  370. {
  371. BOOST_ASSERT(
  372. this->_info._compression == bmp_compression::_rle4 ||
  373. this->_info._compression == bmp_compression::_rle8);
  374. this->read_palette();
  375. // jump to start of rle4 data
  376. this->_io_dev.seek( this->_info._offset );
  377. // we need to know the stream position for padding purposes
  378. std::size_t stream_pos = this->_info._offset;
  379. using Buf_type = std::vector<rgba8_pixel_t>;
  380. Buf_type buf( this->_settings._dim.x );
  381. Buf_type::iterator dst_it = buf.begin();
  382. Buf_type::iterator dst_end = buf.end();
  383. // If height is positive, the bitmap is a bottom-up DIB.
  384. // If height is negative, the bitmap is a top-down DIB.
  385. // The origin of a bottom-up DIB is the bottom left corner of the bitmap image,
  386. // which is the first pixel of the first row of bitmap data.
  387. // The origin of a top-down DIB is also the bottom left corner of the bitmap image,
  388. // but in this case the bottom left corner is the first pixel of the last row of bitmap data.
  389. // - "Programming Windows", 5th Ed. by Charles Petzold explains Windows docs ambiguities.
  390. std::ptrdiff_t ybeg = 0;
  391. std::ptrdiff_t yend = this->_settings._dim.y;
  392. std::ptrdiff_t yinc = 1;
  393. if( this->_info._height > 0 )
  394. {
  395. ybeg = this->_settings._dim.y - 1;
  396. yend = -1;
  397. yinc = -1;
  398. }
  399. std::ptrdiff_t y = ybeg;
  400. bool finished = false;
  401. while ( !finished )
  402. {
  403. std::ptrdiff_t count = this->_io_dev.read_uint8();
  404. std::ptrdiff_t second = this->_io_dev.read_uint8();
  405. stream_pos += 2;
  406. if ( count )
  407. {
  408. // encoded mode
  409. // clamp to boundary
  410. if( count > dst_end - dst_it )
  411. {
  412. count = dst_end - dst_it;
  413. }
  414. if( this->_info._compression == bmp_compression::_rle4 )
  415. {
  416. std::ptrdiff_t cs[2] = { second >> 4, second & 0x0f };
  417. for( int i = 0; i < count; ++i )
  418. {
  419. *dst_it++ = this->_palette[ cs[i & 1] ];
  420. }
  421. }
  422. else
  423. {
  424. for( int i = 0; i < count; ++i )
  425. {
  426. *dst_it++ = this->_palette[ second ];
  427. }
  428. }
  429. }
  430. else
  431. {
  432. switch( second )
  433. {
  434. case 0: // end of row
  435. {
  436. copy_row_if_needed( buf, view, y );
  437. y += yinc;
  438. if( y == yend )
  439. {
  440. finished = true;
  441. }
  442. else
  443. {
  444. dst_it = buf.begin();
  445. dst_end = buf.end();
  446. }
  447. break;
  448. }
  449. case 1: // end of bitmap
  450. {
  451. copy_row_if_needed( buf, view, y );
  452. finished = true;
  453. break;
  454. }
  455. case 2: // offset coordinates
  456. {
  457. std::ptrdiff_t dx = this->_io_dev.read_uint8();
  458. std::ptrdiff_t dy = this->_io_dev.read_uint8() * yinc;
  459. stream_pos += 2;
  460. if( dy )
  461. {
  462. copy_row_if_needed( buf, view, y );
  463. }
  464. std::ptrdiff_t x = dst_it - buf.begin();
  465. x += dx;
  466. if( x > this->_info._width )
  467. {
  468. io_error( "Mangled BMP file." );
  469. }
  470. y += dy;
  471. if( yinc > 0 ? y > yend : y < yend )
  472. {
  473. io_error( "Mangled BMP file." );
  474. }
  475. dst_it = buf.begin() + x;
  476. dst_end = buf.end();
  477. break;
  478. }
  479. default: // absolute mode
  480. {
  481. count = second;
  482. // clamp to boundary
  483. if( count > dst_end - dst_it )
  484. {
  485. count = dst_end - dst_it;
  486. }
  487. if ( this->_info._compression == bmp_compression::_rle4 )
  488. {
  489. for( int i = 0; i < count; ++i )
  490. {
  491. uint8_t packed_indices = this->_io_dev.read_uint8();
  492. ++stream_pos;
  493. *dst_it++ = this->_palette[ packed_indices >> 4 ];
  494. if( ++i == second )
  495. break;
  496. *dst_it++ = this->_palette[ packed_indices & 0x0f ];
  497. }
  498. }
  499. else
  500. {
  501. for( int i = 0; i < count; ++i )
  502. {
  503. uint8_t c = this->_io_dev.read_uint8();
  504. ++stream_pos;
  505. *dst_it++ = this->_palette[ c ];
  506. }
  507. }
  508. // pad to word boundary
  509. if( ( stream_pos - get_offset( 0 )) & 1 )
  510. {
  511. this->_io_dev.seek( 1, SEEK_CUR );
  512. ++stream_pos;
  513. }
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. private:
  521. std::size_t _pitch;
  522. };
  523. namespace detail {
  524. class bmp_type_format_checker
  525. {
  526. public:
  527. bmp_type_format_checker( const bmp_bits_per_pixel::type& bpp )
  528. : _bpp( bpp )
  529. {}
  530. template< typename Image >
  531. bool apply()
  532. {
  533. if( _bpp < 32 )
  534. {
  535. return pixels_are_compatible< typename Image::value_type, rgb8_pixel_t >::value
  536. ? true
  537. : false;
  538. }
  539. else
  540. {
  541. return pixels_are_compatible< typename Image::value_type, rgba8_pixel_t >::value
  542. ? true
  543. : false;
  544. }
  545. }
  546. private:
  547. // to avoid C4512
  548. bmp_type_format_checker& operator=( const bmp_type_format_checker& ) { return *this; }
  549. private:
  550. const bmp_bits_per_pixel::type _bpp;
  551. };
  552. struct bmp_read_is_supported
  553. {
  554. template< typename View >
  555. struct apply : public is_read_supported< typename get_pixel_type< View >::type
  556. , bmp_tag
  557. >
  558. {};
  559. };
  560. } // namespace detail
  561. ///
  562. /// BMP Dynamic Reader
  563. ///
  564. template< typename Device >
  565. class dynamic_image_reader< Device
  566. , bmp_tag
  567. >
  568. : public reader< Device
  569. , bmp_tag
  570. , detail::read_and_no_convert
  571. >
  572. {
  573. using parent_t = reader<Device, bmp_tag, detail::read_and_no_convert>;
  574. public:
  575. dynamic_image_reader( const Device& io_dev
  576. , const image_read_settings< bmp_tag >& settings
  577. )
  578. : parent_t( io_dev
  579. , settings
  580. )
  581. {}
  582. template< typename Images >
  583. void apply( any_image< Images >& images )
  584. {
  585. detail::bmp_type_format_checker format_checker( this->_info._bits_per_pixel );
  586. if( !construct_matched( images
  587. , format_checker
  588. ))
  589. {
  590. io_error( "No matching image type between those of the given any_image and that of the file" );
  591. }
  592. else
  593. {
  594. this->init_image( images
  595. , this->_settings
  596. );
  597. detail::dynamic_io_fnobj< detail::bmp_read_is_supported
  598. , parent_t
  599. > op( this );
  600. apply_operation( view( images )
  601. , op
  602. );
  603. }
  604. }
  605. };
  606. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  607. #pragma warning(pop)
  608. #endif
  609. } // gil
  610. } // boost
  611. #endif