]>
Commit | Line | Data |
---|---|---|
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_IO_H | |
14 | #define GIL_JPEG_IO_H | |
15 | ||
16 | /// \file | |
17 | /// \brief Support for reading and writing JPEG files | |
18 | /// Requires libjpeg | |
19 | /// \author Hailin Jin and Lubomir Bourdev \n | |
20 | /// Adobe Systems Incorporated | |
21 | /// \date 2005-2007 \n Last updated September 24, 2006 | |
22 | ||
23 | #include <cstdio> | |
24 | #include <algorithm> | |
25 | #include <string> | |
26 | #include <boost/static_assert.hpp> | |
27 | #include <boost/shared_ptr.hpp> | |
28 | extern "C" { | |
29 | #include <jpeglib.h> | |
30 | } | |
31 | #include "io_error.hpp" | |
32 | #include "jpeg_io_private.hpp" | |
33 | ||
34 | namespace boost { namespace gil { | |
35 | ||
36 | /// \ingroup JPEG_IO | |
37 | /// \brief Determines whether the given view type is supported for reading | |
38 | template <typename View> | |
39 | struct jpeg_read_support { | |
40 | BOOST_STATIC_CONSTANT(bool,is_supported= | |
41 | (detail::jpeg_read_support_private<typename channel_type<View>::type, | |
42 | typename color_space_type<View>::type>::is_supported)); | |
43 | BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type= | |
44 | (detail::jpeg_read_support_private<typename channel_type<View>::type, | |
45 | typename color_space_type<View>::type>::color_type)); | |
46 | BOOST_STATIC_CONSTANT(bool, value=is_supported); | |
47 | }; | |
48 | ||
49 | /// \ingroup JPEG_IO | |
50 | /// \brief Returns the width and height of the JPEG file at the specified location. | |
51 | /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file | |
52 | inline point2<std::ptrdiff_t> jpeg_read_dimensions(const char* filename) { | |
53 | detail::jpeg_reader m(filename); | |
54 | return m.get_dimensions(); | |
55 | } | |
56 | ||
57 | /// \ingroup JPEG_IO | |
58 | /// \brief Returns the width and height of the JPEG file at the specified location. | |
59 | /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file | |
60 | inline point2<std::ptrdiff_t> jpeg_read_dimensions(const std::string& filename) { | |
61 | return jpeg_read_dimensions(filename.c_str()); | |
62 | } | |
63 | ||
64 | /// \ingroup JPEG_IO | |
65 | /// \brief Loads the image specified by the given jpeg image file name into the given view. | |
66 | /// 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. | |
67 | /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not | |
68 | /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view. | |
69 | template <typename View> | |
70 | inline void jpeg_read_view(const char* filename,const View& view) { | |
71 | BOOST_STATIC_ASSERT(jpeg_read_support<View>::is_supported); | |
72 | ||
73 | detail::jpeg_reader m(filename); | |
74 | m.apply(view); | |
75 | } | |
76 | ||
77 | /// \ingroup JPEG_IO | |
78 | /// \brief Loads the image specified by the given jpeg image file name into the given view. | |
79 | template <typename View> | |
80 | inline void jpeg_read_view(const std::string& filename,const View& view) { | |
81 | jpeg_read_view(filename.c_str(),view); | |
82 | } | |
83 | ||
84 | /// \ingroup JPEG_IO | |
85 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it. | |
86 | /// 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. | |
87 | /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not | |
88 | /// compatible with the ones specified by Image | |
89 | template <typename Image> | |
90 | inline void jpeg_read_image(const char* filename,Image& im) { | |
91 | BOOST_STATIC_ASSERT(jpeg_read_support<typename Image::view_t>::is_supported); | |
92 | ||
93 | detail::jpeg_reader m(filename); | |
94 | m.read_image(im); | |
95 | } | |
96 | ||
97 | /// \ingroup JPEG_IO | |
98 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it. | |
99 | template <typename Image> | |
100 | inline void jpeg_read_image(const std::string& filename,Image& im) { | |
101 | jpeg_read_image(filename.c_str(),im); | |
102 | } | |
103 | ||
104 | /// \ingroup JPEG_IO | |
105 | /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. | |
106 | /// 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. | |
107 | template <typename View,typename CC> | |
108 | inline void jpeg_read_and_convert_view(const char* filename,const View& view,CC cc) { | |
109 | detail::jpeg_reader_color_convert<CC> m(filename,cc); | |
110 | m.apply(view); | |
111 | } | |
112 | ||
113 | /// \ingroup JPEG_IO | |
114 | /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. | |
115 | /// 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. | |
116 | template <typename View> | |
117 | inline void jpeg_read_and_convert_view(const char* filename,const View& view) { | |
118 | detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter()); | |
119 | m.apply(view); | |
120 | } | |
121 | ||
122 | /// \ingroup JPEG_IO | |
123 | /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. | |
124 | template <typename View,typename CC> | |
125 | inline void jpeg_read_and_convert_view(const std::string& filename,const View& view,CC cc) { | |
126 | jpeg_read_and_convert_view(filename.c_str(),view); | |
127 | } | |
128 | ||
129 | /// \ingroup JPEG_IO | |
130 | /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. | |
131 | template <typename View> | |
132 | inline void jpeg_read_and_convert_view(const std::string& filename,const View& view) { | |
133 | jpeg_read_and_convert_view(filename.c_str(),view); | |
134 | } | |
135 | ||
136 | /// \ingroup JPEG_IO | |
137 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. | |
138 | /// Throws std::ios_base::failure if the file is not a valid JPEG file | |
139 | template <typename Image,typename CC> | |
140 | inline void jpeg_read_and_convert_image(const char* filename,Image& im,CC cc) { | |
141 | detail::jpeg_reader_color_convert<CC> m(filename,cc); | |
142 | m.read_image(im); | |
143 | } | |
144 | ||
145 | /// \ingroup JPEG_IO | |
146 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. | |
147 | /// Throws std::ios_base::failure if the file is not a valid JPEG file | |
148 | template <typename Image> | |
149 | inline void jpeg_read_and_convert_image(const char* filename,Image& im) { | |
150 | detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter()); | |
151 | m.read_image(im); | |
152 | } | |
153 | ||
154 | /// \ingroup JPEG_IO | |
155 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. | |
156 | template <typename Image,typename CC> | |
157 | inline void jpeg_read_and_convert_image(const std::string& filename,Image& im,CC cc) { | |
158 | jpeg_read_and_convert_image(filename.c_str(),im); | |
159 | } | |
160 | ||
161 | /// \ingroup JPEG_IO | |
162 | /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. | |
163 | template <typename Image> | |
164 | inline void jpeg_read_and_convert_image(const std::string& filename,Image& im) { | |
165 | jpeg_read_and_convert_image(filename.c_str(),im); | |
166 | } | |
167 | ||
168 | /// \ingroup JPEG_IO | |
169 | /// \brief Determines whether the given view type is supported for writing | |
170 | template <typename View> | |
171 | struct jpeg_write_support { | |
172 | BOOST_STATIC_CONSTANT(bool,is_supported= | |
173 | (detail::jpeg_write_support_private<typename channel_type<View>::type, | |
174 | typename color_space_type<View>::type>::is_supported)); | |
175 | BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type= | |
176 | (detail::jpeg_write_support_private<typename channel_type<View>::type, | |
177 | typename color_space_type<View>::type>::color_type)); | |
178 | BOOST_STATIC_CONSTANT(bool, value=is_supported); | |
179 | }; | |
180 | ||
181 | /// \ingroup JPEG_IO | |
182 | /// \brief Saves the view to a jpeg file specified by the given jpeg image file name. | |
183 | /// 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. | |
184 | /// Throws std::ios_base::failure if it fails to create the file. | |
185 | template <typename View> | |
186 | inline void jpeg_write_view(const char* filename,const View& view,int quality=100) { | |
187 | BOOST_STATIC_ASSERT(jpeg_write_support<View>::is_supported); | |
188 | ||
189 | detail::jpeg_writer m(filename); | |
190 | m.apply(view,quality); | |
191 | } | |
192 | ||
193 | /// \ingroup JPEG_IO | |
194 | /// \brief Saves the view to a jpeg file specified by the given jpeg image file name. | |
195 | template <typename View> | |
196 | inline void jpeg_write_view(const std::string& filename,const View& view,int quality=100) { | |
197 | jpeg_write_view(filename.c_str(),view,quality); | |
198 | } | |
199 | ||
200 | } } // namespace boost::gil | |
201 | ||
202 | #endif |