2 Copyright 2005-2007 Adobe Systems Incorporated
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).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
11 /*************************************************************************************************/
17 /// \brief Support for reading and writing JPEG files
19 /// \author Hailin Jin and Lubomir Bourdev \n
20 /// Adobe Systems Incorporated
21 /// \date 2005-2007 \n Last updated September 24, 2006
26 #include <boost/static_assert.hpp>
27 #include <boost/shared_ptr.hpp>
31 #include "io_error.hpp"
32 #include "jpeg_io_private.hpp"
34 namespace boost { namespace gil {
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);
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();
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());
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);
73 detail::jpeg_reader m(filename);
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);
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);
93 detail::jpeg_reader m(filename);
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);
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);
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());
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);
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);
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);
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());
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);
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);
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);
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);
189 detail::jpeg_writer m(filename);
190 m.apply(view,quality);
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);
200 } } // namespace boost::gil