1 /*
2     Copyright 2005-2007 Adobe Systems Incorporated
3 
4     Use, modification and distribution are subject to the Boost Software License,
5     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt).
7 
8     See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_JPEG_DYNAMIC_IO_H
14 #define GIL_JPEG_DYNAMIC_IO_H
15 
16 /// \file
17 /// \brief  Support for reading and writing JPEG files
18 ///         Requires libjpeg
19 ///
20 /// \author Hailin Jin and Lubomir Bourdev \n
21 ///         Adobe Systems Incorporated
22 /// \date   2005-2007 \n Last updated June 10, 2006
23 
24 #include <stdio.h>
25 #include <string>
26 #include <boost/mpl/bool.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include "../dynamic_image/dynamic_image_all.hpp"
29 #include "io_error.hpp"
30 
31 #include "jpeg_io.hpp"
32 #include "jpeg_io_private.hpp"
33 #include "dynamic_io.hpp"
34 
35 namespace boost { namespace gil {
36 
37 namespace detail {
38 
39 struct jpeg_write_is_supported {
40     template<typename View> struct apply
41         : public mpl::bool_<jpeg_write_support<View>::is_supported> {};
42 };
43 
44 class jpeg_writer_dynamic : public jpeg_writer {
45     int _quality;
46 public:
jpeg_writer_dynamic(FILE * file,int quality=100)47     jpeg_writer_dynamic(FILE* file,           int quality=100) : jpeg_writer(file)    , _quality(quality) {}
jpeg_writer_dynamic(const char * filename,int quality=100)48     jpeg_writer_dynamic(const char* filename, int quality=100) : jpeg_writer(filename), _quality(quality) {}
49 
50     template <typename Views>
write_view(const any_image_view<Views> & runtime_view)51     void write_view(const any_image_view<Views>& runtime_view) {
52         dynamic_io_fnobj<jpeg_write_is_supported, jpeg_writer> op(this);
53         apply_operation(runtime_view,op);
54     }
55 };
56 
57 class jpeg_type_format_checker {
58     J_COLOR_SPACE _color_type;
59 public:
jpeg_type_format_checker(J_COLOR_SPACE color_type_in)60     jpeg_type_format_checker(J_COLOR_SPACE color_type_in) :
61         _color_type(color_type_in) {}
62     template <typename Image>
apply()63     bool apply() {
64         return jpeg_read_support<typename Image::view_t>::color_type==_color_type;
65     }
66 };
67 
68 struct jpeg_read_is_supported {
69     template<typename View> struct apply
70         : public mpl::bool_<jpeg_read_support<View>::is_supported> {};
71 };
72 
73 class jpeg_reader_dynamic : public jpeg_reader {
74 public:
jpeg_reader_dynamic(FILE * file)75     jpeg_reader_dynamic(FILE* file)           : jpeg_reader(file)    {}
jpeg_reader_dynamic(const char * filename)76     jpeg_reader_dynamic(const char* filename) : jpeg_reader(filename){}
77 
78     template <typename Images>
read_image(any_image<Images> & im)79     void read_image(any_image<Images>& im) {
80         if (!construct_matched(im,detail::jpeg_type_format_checker(_cinfo.out_color_space))) {
81             io_error("jpeg_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
82         } else {
83             im.recreate(get_dimensions());
84             dynamic_io_fnobj<jpeg_read_is_supported, jpeg_reader> op(this);
85             apply_operation(view(im),op);
86         }
87     }
88 };
89 
90 } // namespace detail
91 
92 
93 /// \ingroup JPEG_IO
94 /// \brief reads a JPEG image into a run-time instantiated image
95 /// 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
96 /// and creates a new image of that type with the dimensions specified by the image file.
97 /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
98 template <typename Images>
jpeg_read_image(const char * filename,any_image<Images> & im)99 inline void jpeg_read_image(const char* filename,any_image<Images>& im) {
100     detail::jpeg_reader_dynamic m(filename);
101     m.read_image(im);
102 }
103 
104 /// \ingroup JPEG_IO
105 /// \brief reads a JPEG image into a run-time instantiated image
106 template <typename Images>
jpeg_read_image(const std::string & filename,any_image<Images> & im)107 inline void jpeg_read_image(const std::string& filename,any_image<Images>& im) {
108     jpeg_read_image(filename.c_str(),im);
109 }
110 
111 /// \ingroup JPEG_IO
112 /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name.
113 /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
114 /// or if it fails to create the file.
115 template <typename Views>
jpeg_write_view(const char * filename,const any_image_view<Views> & runtime_view)116 inline void jpeg_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
117     detail::jpeg_writer_dynamic m(filename);
118     m.write_view(runtime_view);
119 }
120 
121 /// \ingroup JPEG_IO
122 /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name.
123 template <typename Views>
jpeg_write_view(const std::string & filename,const any_image_view<Views> & runtime_view)124 inline void jpeg_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
125     jpeg_write_view(filename.c_str(),runtime_view);
126 }
127 
128 } }  // namespace boost::gil
129 
130 #endif
131