jpeg_dynamic_io.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_DYNAMIC_IO_H
  10. #define GIL_JPEG_DYNAMIC_IO_H
  11. /// \file
  12. /// \brief Support for reading and writing JPEG files
  13. /// Requires libjpeg
  14. ///
  15. /// \author Hailin Jin and Lubomir Bourdev \n
  16. /// Adobe Systems Incorporated
  17. /// \date 2005-2007 \n Last updated June 10, 2006
  18. #include <stdio.h>
  19. #include <string>
  20. #include <boost/mpl/bool.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include "../dynamic_image/dynamic_image_all.hpp"
  23. #include "io_error.hpp"
  24. #include "jpeg_io.hpp"
  25. #include "jpeg_io_private.hpp"
  26. #include "dynamic_io.hpp"
  27. namespace boost { namespace gil {
  28. namespace detail {
  29. struct jpeg_write_is_supported {
  30. template<typename View> struct apply
  31. : public mpl::bool_<jpeg_write_support<View>::is_supported> {};
  32. };
  33. class jpeg_writer_dynamic : public jpeg_writer {
  34. int _quality;
  35. public:
  36. jpeg_writer_dynamic(FILE* file, int quality=100) : jpeg_writer(file) , _quality(quality) {}
  37. jpeg_writer_dynamic(const char* filename, int quality=100) : jpeg_writer(filename), _quality(quality) {}
  38. template <typename Views>
  39. void write_view(const any_image_view<Views>& runtime_view) {
  40. dynamic_io_fnobj<jpeg_write_is_supported, jpeg_writer> op(this);
  41. apply_operation(runtime_view,op);
  42. }
  43. };
  44. class jpeg_type_format_checker {
  45. J_COLOR_SPACE _color_type;
  46. public:
  47. jpeg_type_format_checker(J_COLOR_SPACE color_type_in) :
  48. _color_type(color_type_in) {}
  49. template <typename Image>
  50. bool apply() {
  51. return jpeg_read_support<typename Image::view_t>::color_type==_color_type;
  52. }
  53. };
  54. struct jpeg_read_is_supported {
  55. template<typename View> struct apply
  56. : public mpl::bool_<jpeg_read_support<View>::is_supported> {};
  57. };
  58. class jpeg_reader_dynamic : public jpeg_reader {
  59. public:
  60. jpeg_reader_dynamic(FILE* file) : jpeg_reader(file) {}
  61. jpeg_reader_dynamic(const char* filename) : jpeg_reader(filename){}
  62. template <typename Images>
  63. void read_image(any_image<Images>& im) {
  64. if (!construct_matched(im,detail::jpeg_type_format_checker(_cinfo.out_color_space))) {
  65. io_error("jpeg_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
  66. } else {
  67. im.recreate(get_dimensions());
  68. dynamic_io_fnobj<jpeg_read_is_supported, jpeg_reader> op(this);
  69. apply_operation(view(im),op);
  70. }
  71. }
  72. };
  73. } // namespace detail
  74. /// \ingroup JPEG_IO
  75. /// \brief reads a JPEG image into a run-time instantiated image
  76. /// Opens the given JPEG file name, selects the first type in Images whose color space and channel are compatible to those of the image file
  77. /// and creates a new image of that type with the dimensions specified by the image file.
  78. /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
  79. template <typename Images>
  80. inline void jpeg_read_image(const char* filename,any_image<Images>& im) {
  81. detail::jpeg_reader_dynamic m(filename);
  82. m.read_image(im);
  83. }
  84. /// \ingroup JPEG_IO
  85. /// \brief reads a JPEG image into a run-time instantiated image
  86. template <typename Images>
  87. inline void jpeg_read_image(const std::string& filename,any_image<Images>& im) {
  88. jpeg_read_image(filename.c_str(),im);
  89. }
  90. /// \ingroup JPEG_IO
  91. /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name.
  92. /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
  93. /// or if it fails to create the file.
  94. template <typename Views>
  95. inline void jpeg_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
  96. detail::jpeg_writer_dynamic m(filename);
  97. m.write_view(runtime_view);
  98. }
  99. /// \ingroup JPEG_IO
  100. /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name.
  101. template <typename Views>
  102. inline void jpeg_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
  103. jpeg_write_view(filename.c_str(),runtime_view);
  104. }
  105. } } // namespace boost::gil
  106. #endif