Chris@16: /* Chris@16: Copyright 2005-2007 Adobe Systems Incorporated Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: Chris@16: See http://opensource.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_JPEG_DYNAMIC_IO_H Chris@16: #define GIL_JPEG_DYNAMIC_IO_H Chris@16: Chris@16: /// \file Chris@16: /// \brief Support for reading and writing JPEG files Chris@16: /// Requires libjpeg Chris@16: /// Chris@16: /// \author Hailin Jin and Lubomir Bourdev \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated June 10, 2006 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include "../dynamic_image/dynamic_image_all.hpp" Chris@16: #include "io_error.hpp" Chris@16: Chris@16: #include "jpeg_io.hpp" Chris@16: #include "jpeg_io_private.hpp" Chris@16: #include "dynamic_io.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: struct jpeg_write_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class jpeg_writer_dynamic : public jpeg_writer { Chris@16: int _quality; Chris@16: public: Chris@16: jpeg_writer_dynamic(FILE* file, int quality=100) : jpeg_writer(file) , _quality(quality) {} Chris@16: jpeg_writer_dynamic(const char* filename, int quality=100) : jpeg_writer(filename), _quality(quality) {} Chris@16: Chris@16: template Chris@16: void write_view(const any_image_view& runtime_view) { Chris@16: dynamic_io_fnobj op(this); Chris@16: apply_operation(runtime_view,op); Chris@16: } Chris@16: }; Chris@16: Chris@16: class jpeg_type_format_checker { Chris@16: J_COLOR_SPACE _color_type; Chris@16: public: Chris@16: jpeg_type_format_checker(J_COLOR_SPACE color_type_in) : Chris@16: _color_type(color_type_in) {} Chris@16: template Chris@16: bool apply() { Chris@16: return jpeg_read_support::color_type==_color_type; Chris@16: } Chris@16: }; Chris@16: Chris@16: struct jpeg_read_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class jpeg_reader_dynamic : public jpeg_reader { Chris@16: public: Chris@16: jpeg_reader_dynamic(FILE* file) : jpeg_reader(file) {} Chris@16: jpeg_reader_dynamic(const char* filename) : jpeg_reader(filename){} Chris@16: Chris@16: template Chris@16: void read_image(any_image& im) { Chris@16: if (!construct_matched(im,detail::jpeg_type_format_checker(_cinfo.out_color_space))) { Chris@16: 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: } else { Chris@16: im.recreate(get_dimensions()); Chris@16: dynamic_io_fnobj op(this); Chris@16: apply_operation(view(im),op); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief reads a JPEG image into a run-time instantiated image Chris@16: /// 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: /// and creates a new image of that type with the dimensions specified by the image file. Chris@16: /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk. Chris@16: template Chris@16: inline void jpeg_read_image(const char* filename,any_image& im) { Chris@16: detail::jpeg_reader_dynamic m(filename); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief reads a JPEG image into a run-time instantiated image Chris@16: template Chris@16: inline void jpeg_read_image(const std::string& filename,any_image& im) { Chris@16: jpeg_read_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name. Chris@16: /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension Chris@16: /// or if it fails to create the file. Chris@16: template Chris@16: inline void jpeg_write_view(const char* filename,const any_image_view& runtime_view) { Chris@16: detail::jpeg_writer_dynamic m(filename); Chris@16: m.write_view(runtime_view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name. Chris@16: template Chris@16: inline void jpeg_write_view(const std::string& filename,const any_image_view& runtime_view) { Chris@16: jpeg_write_view(filename.c_str(),runtime_view); Chris@16: } Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: #endif