jpeg_io.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_JPEG_IO_H
  10. #define GIL_JPEG_IO_H
  11. /// \file
  12. /// \brief Support for reading and writing JPEG files
  13. /// Requires libjpeg
  14. /// \author Hailin Jin and Lubomir Bourdev \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated September 24, 2006
  17. #include <cstdio>
  18. #include <algorithm>
  19. #include <string>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. extern "C" {
  23. #include <jpeglib.h>
  24. }
  25. #include "io_error.hpp"
  26. #include "jpeg_io_private.hpp"
  27. namespace boost { namespace gil {
  28. /// \ingroup JPEG_IO
  29. /// \brief Determines whether the given view type is supported for reading
  30. template <typename View>
  31. struct jpeg_read_support {
  32. BOOST_STATIC_CONSTANT(bool,is_supported=
  33. (detail::jpeg_read_support_private<typename channel_type<View>::type,
  34. typename color_space_type<View>::type>::is_supported));
  35. BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
  36. (detail::jpeg_read_support_private<typename channel_type<View>::type,
  37. typename color_space_type<View>::type>::color_type));
  38. BOOST_STATIC_CONSTANT(bool, value=is_supported);
  39. };
  40. /// \ingroup JPEG_IO
  41. /// \brief Returns the width and height of the JPEG file at the specified location.
  42. /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
  43. inline point2<std::ptrdiff_t> jpeg_read_dimensions(const char* filename) {
  44. detail::jpeg_reader m(filename);
  45. return m.get_dimensions();
  46. }
  47. /// \ingroup JPEG_IO
  48. /// \brief Returns the width and height of the JPEG file at the specified location.
  49. /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
  50. inline point2<std::ptrdiff_t> jpeg_read_dimensions(const std::string& filename) {
  51. return jpeg_read_dimensions(filename.c_str());
  52. }
  53. /// \ingroup JPEG_IO
  54. /// \brief Loads the image specified by the given jpeg image file name into the given view.
  55. /// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
  56. /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not
  57. /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
  58. template <typename View>
  59. inline void jpeg_read_view(const char* filename,const View& view) {
  60. BOOST_STATIC_ASSERT(jpeg_read_support<View>::is_supported);
  61. detail::jpeg_reader m(filename);
  62. m.apply(view);
  63. }
  64. /// \ingroup JPEG_IO
  65. /// \brief Loads the image specified by the given jpeg image file name into the given view.
  66. template <typename View>
  67. inline void jpeg_read_view(const std::string& filename,const View& view) {
  68. jpeg_read_view(filename.c_str(),view);
  69. }
  70. /// \ingroup JPEG_IO
  71. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it.
  72. /// Triggers a compile assert if the image color space or channel depth are not supported by the JPEG library or by the I/O extension.
  73. /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not
  74. /// compatible with the ones specified by Image
  75. template <typename Image>
  76. inline void jpeg_read_image(const char* filename,Image& im) {
  77. BOOST_STATIC_ASSERT(jpeg_read_support<typename Image::view_t>::is_supported);
  78. detail::jpeg_reader m(filename);
  79. m.read_image(im);
  80. }
  81. /// \ingroup JPEG_IO
  82. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it.
  83. template <typename Image>
  84. inline void jpeg_read_image(const std::string& filename,Image& im) {
  85. jpeg_read_image(filename.c_str(),im);
  86. }
  87. /// \ingroup JPEG_IO
  88. /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
  89. /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
  90. template <typename View,typename CC>
  91. inline void jpeg_read_and_convert_view(const char* filename,const View& view,CC cc) {
  92. detail::jpeg_reader_color_convert<CC> m(filename,cc);
  93. m.apply(view);
  94. }
  95. /// \ingroup JPEG_IO
  96. /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
  97. /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
  98. template <typename View>
  99. inline void jpeg_read_and_convert_view(const char* filename,const View& view) {
  100. detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
  101. m.apply(view);
  102. }
  103. /// \ingroup JPEG_IO
  104. /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
  105. template <typename View,typename CC>
  106. inline void jpeg_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
  107. jpeg_read_and_convert_view(filename.c_str(),view);
  108. }
  109. /// \ingroup JPEG_IO
  110. /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
  111. template <typename View>
  112. inline void jpeg_read_and_convert_view(const std::string& filename,const View& view) {
  113. jpeg_read_and_convert_view(filename.c_str(),view);
  114. }
  115. /// \ingroup JPEG_IO
  116. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
  117. /// Throws std::ios_base::failure if the file is not a valid JPEG file
  118. template <typename Image,typename CC>
  119. inline void jpeg_read_and_convert_image(const char* filename,Image& im,CC cc) {
  120. detail::jpeg_reader_color_convert<CC> m(filename,cc);
  121. m.read_image(im);
  122. }
  123. /// \ingroup JPEG_IO
  124. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
  125. /// Throws std::ios_base::failure if the file is not a valid JPEG file
  126. template <typename Image>
  127. inline void jpeg_read_and_convert_image(const char* filename,Image& im) {
  128. detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
  129. m.read_image(im);
  130. }
  131. /// \ingroup JPEG_IO
  132. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
  133. template <typename Image,typename CC>
  134. inline void jpeg_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
  135. jpeg_read_and_convert_image(filename.c_str(),im);
  136. }
  137. /// \ingroup JPEG_IO
  138. /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
  139. template <typename Image>
  140. inline void jpeg_read_and_convert_image(const std::string& filename,Image& im) {
  141. jpeg_read_and_convert_image(filename.c_str(),im);
  142. }
  143. /// \ingroup JPEG_IO
  144. /// \brief Determines whether the given view type is supported for writing
  145. template <typename View>
  146. struct jpeg_write_support {
  147. BOOST_STATIC_CONSTANT(bool,is_supported=
  148. (detail::jpeg_write_support_private<typename channel_type<View>::type,
  149. typename color_space_type<View>::type>::is_supported));
  150. BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
  151. (detail::jpeg_write_support_private<typename channel_type<View>::type,
  152. typename color_space_type<View>::type>::color_type));
  153. BOOST_STATIC_CONSTANT(bool, value=is_supported);
  154. };
  155. /// \ingroup JPEG_IO
  156. /// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
  157. /// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
  158. /// Throws std::ios_base::failure if it fails to create the file.
  159. template <typename View>
  160. inline void jpeg_write_view(const char* filename,const View& view,int quality=100) {
  161. BOOST_STATIC_ASSERT(jpeg_write_support<View>::is_supported);
  162. detail::jpeg_writer m(filename);
  163. m.apply(view,quality);
  164. }
  165. /// \ingroup JPEG_IO
  166. /// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
  167. template <typename View>
  168. inline void jpeg_write_view(const std::string& filename,const View& view,int quality=100) {
  169. jpeg_write_view(filename.c_str(),view,quality);
  170. }
  171. } } // namespace boost::gil
  172. #endif