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