tags.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
  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_JPEG_TAGS_HPP
  9. #define BOOST_GIL_EXTENSION_IO_JPEG_TAGS_HPP
  10. // taken from jpegxx - https://bitbucket.org/edd/jpegxx/src/ea2492a1a4a6/src/ijg_headers.hpp
  11. #ifndef BOOST_GIL_EXTENSION_IO_JPEG_C_LIB_COMPILED_AS_CPLUSPLUS
  12. extern "C" {
  13. #else
  14. // DONT_USE_EXTERN_C introduced in v7 of the IJG library.
  15. // By default the v7 IJG headers check for __cplusplus being defined and
  16. // wrap the content in an 'extern "C"' block if it's present.
  17. // When DONT_USE_EXTERN_C is defined, this wrapping is not performed.
  18. #ifndef DONT_USE_EXTERN_C
  19. #define DONT_USE_EXTERN_C 1
  20. #endif
  21. #endif
  22. #include <cstdio> // jpeglib doesn't know about FILE
  23. #include <jerror.h>
  24. #include <jpeglib.h>
  25. #ifndef BOOST_GIL_EXTENSION_IO_JPEG_C_LIB_COMPILED_AS_CPLUSPLUS
  26. }
  27. #endif
  28. #include <boost/gil/io/base.hpp>
  29. namespace boost { namespace gil {
  30. /// Defines jpeg tag.
  31. struct jpeg_tag : format_tag {};
  32. /// see http://en.wikipedia.org/wiki/JPEG for reference
  33. /// Defines type for image width property.
  34. struct jpeg_image_width : property_base< JDIMENSION > {};
  35. /// Defines type for image height property.
  36. struct jpeg_image_height : property_base< JDIMENSION > {};
  37. /// Defines type for number of components property.
  38. struct jpeg_num_components : property_base< int > {};
  39. /// Defines type for color space property.
  40. struct jpeg_color_space : property_base< J_COLOR_SPACE > {};
  41. /// Defines type for jpeg quality property.
  42. struct jpeg_quality : property_base< int >
  43. {
  44. static const type default_value = 100;
  45. };
  46. /// Defines type for data precision property.
  47. struct jpeg_data_precision : property_base< int > {};
  48. /// JFIF code for pixel size units
  49. struct jpeg_density_unit : property_base< UINT8 >
  50. {
  51. static const type default_value = 0;
  52. };
  53. /// pixel density
  54. struct jpeg_pixel_density : property_base< UINT16 >
  55. {
  56. static const type default_value = 0;
  57. };
  58. /// Defines type for dct ( discrete cosine transformation ) method property.
  59. struct jpeg_dct_method : property_base< J_DCT_METHOD >
  60. {
  61. static const type slow = JDCT_ISLOW;
  62. static const type fast = JDCT_IFAST;
  63. static const type floating_pt = JDCT_FLOAT;
  64. static const type fastest = JDCT_FASTEST;
  65. static const type default_value = slow;
  66. };
  67. /// Read information for jpeg images.
  68. ///
  69. /// The structure is returned when using read_image_info.
  70. template<>
  71. struct image_read_info< jpeg_tag >
  72. {
  73. image_read_info()
  74. : _width ( 0 )
  75. , _height( 0 )
  76. , _num_components( 0 )
  77. , _color_space( J_COLOR_SPACE( 0 ))
  78. , _data_precision( 0 )
  79. , _density_unit ( 0 )
  80. , _x_density ( 0 )
  81. , _y_density ( 0 )
  82. , _pixel_width_mm ( 0.0 )
  83. , _pixel_height_mm( 0.0 )
  84. {}
  85. /// The image width.
  86. jpeg_image_width::type _width;
  87. /// The image height.
  88. jpeg_image_height::type _height;
  89. /// The number of channels.
  90. jpeg_num_components::type _num_components;
  91. /// The color space.
  92. jpeg_color_space::type _color_space;
  93. /// The width of channel.
  94. /// I believe this number is always 8 in the case libjpeg is built with 8.
  95. /// see: http://www.asmail.be/msg0055405033.html
  96. jpeg_data_precision::type _data_precision;
  97. /// Density conversion unit.
  98. jpeg_density_unit::type _density_unit;
  99. jpeg_pixel_density::type _x_density;
  100. jpeg_pixel_density::type _y_density;
  101. /// Real-world dimensions
  102. double _pixel_width_mm;
  103. double _pixel_height_mm;
  104. };
  105. /// Read settings for jpeg images.
  106. ///
  107. /// The structure can be used for all read_xxx functions, except read_image_info.
  108. template<>
  109. struct image_read_settings< jpeg_tag > : public image_read_settings_base
  110. {
  111. /// Default constructor
  112. image_read_settings<jpeg_tag>()
  113. : image_read_settings_base()
  114. , _dct_method( jpeg_dct_method::default_value )
  115. {}
  116. /// Constructor
  117. /// \param top_left Top left coordinate for reading partial image.
  118. /// \param dim Dimensions for reading partial image.
  119. /// \param dct_method Specifies dct method.
  120. image_read_settings( const point_t& top_left
  121. , const point_t& dim
  122. , jpeg_dct_method::type dct_method = jpeg_dct_method::default_value
  123. )
  124. : image_read_settings_base( top_left
  125. , dim
  126. )
  127. , _dct_method( dct_method )
  128. {}
  129. /// The dct ( discrete cosine transformation ) method.
  130. jpeg_dct_method::type _dct_method;
  131. };
  132. /// Write information for jpeg images.
  133. ///
  134. /// The structure can be used for write_view() function.
  135. template<>
  136. struct image_write_info< jpeg_tag >
  137. {
  138. /// Constructor
  139. /// \param quality Defines the jpeg quality.
  140. /// \param dct_method Defines the DCT method.
  141. /// \param density_unit Defines the density unit.
  142. /// \param x_density Defines the x density.
  143. /// \param y_density Defines the y density.
  144. image_write_info( const jpeg_quality::type quality = jpeg_quality::default_value
  145. , const jpeg_dct_method::type dct_method = jpeg_dct_method::default_value
  146. , const jpeg_density_unit::type density_unit = jpeg_density_unit::default_value
  147. , const jpeg_pixel_density::type x_density = jpeg_pixel_density::default_value
  148. , const jpeg_pixel_density::type y_density = jpeg_pixel_density::default_value
  149. )
  150. : _quality ( quality )
  151. , _dct_method( dct_method )
  152. , _density_unit( density_unit )
  153. , _x_density ( x_density )
  154. , _y_density ( y_density )
  155. {}
  156. /// The jpeg quality.
  157. jpeg_quality::type _quality;
  158. /// The dct ( discrete cosine transformation ) method.
  159. jpeg_dct_method::type _dct_method;
  160. /// Density conversion unit.
  161. jpeg_density_unit::type _density_unit;
  162. /// Pixel density dimensions.
  163. jpeg_pixel_density::type _x_density;
  164. jpeg_pixel_density::type _y_density;
  165. /// Sets the pixel dimensions.
  166. void set_pixel_dimensions( int image_width // in pixels
  167. , int image_height // in pixels
  168. , double pixel_width // in mm
  169. , double pixel_height // in mm
  170. )
  171. {
  172. _density_unit = 2; // dots per cm
  173. _x_density = round( image_width / ( pixel_width / 10 ));
  174. _y_density = round( image_height / ( pixel_height / 10 ));
  175. }
  176. private:
  177. UINT16 round( double d )
  178. {
  179. return static_cast< UINT16 >( d + 0.5 );
  180. }
  181. };
  182. } // namespace gil
  183. } // namespace boost
  184. #endif