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_IO_H Chris@16: #define GIL_JPEG_IO_H Chris@16: Chris@16: /// \file Chris@16: /// \brief Support for reading and writing JPEG files Chris@16: /// Requires libjpeg Chris@16: /// \author Hailin Jin and Lubomir Bourdev \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated September 24, 2006 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: extern "C" { Chris@16: #include Chris@16: } Chris@16: #include "io_error.hpp" Chris@16: #include "jpeg_io_private.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Determines whether the given view type is supported for reading Chris@16: template Chris@16: struct jpeg_read_support { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported= Chris@16: (detail::jpeg_read_support_private::type, Chris@16: typename color_space_type::type>::is_supported)); Chris@16: BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type= Chris@16: (detail::jpeg_read_support_private::type, Chris@16: typename color_space_type::type>::color_type)); Chris@16: BOOST_STATIC_CONSTANT(bool, value=is_supported); Chris@16: }; Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Returns the width and height of the JPEG file at the specified location. Chris@16: /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file Chris@16: inline point2 jpeg_read_dimensions(const char* filename) { Chris@16: detail::jpeg_reader m(filename); Chris@16: return m.get_dimensions(); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Returns the width and height of the JPEG file at the specified location. Chris@16: /// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file Chris@16: inline point2 jpeg_read_dimensions(const std::string& filename) { Chris@16: return jpeg_read_dimensions(filename.c_str()); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads the image specified by the given jpeg image file name into the given view. Chris@16: /// 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. Chris@16: /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not Chris@16: /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view. Chris@16: template Chris@16: inline void jpeg_read_view(const char* filename,const View& view) { Chris@16: BOOST_STATIC_ASSERT(jpeg_read_support::is_supported); Chris@16: Chris@16: detail::jpeg_reader m(filename); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads the image specified by the given jpeg image file name into the given view. Chris@16: template Chris@16: inline void jpeg_read_view(const std::string& filename,const View& view) { Chris@16: jpeg_read_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it. Chris@16: /// 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. Chris@16: /// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not Chris@16: /// compatible with the ones specified by Image Chris@16: template Chris@16: inline void jpeg_read_image(const char* filename,Image& im) { Chris@16: BOOST_STATIC_ASSERT(jpeg_read_support::is_supported); Chris@16: Chris@16: detail::jpeg_reader m(filename); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it. Chris@16: template Chris@16: inline void jpeg_read_image(const std::string& filename,Image& im) { Chris@16: jpeg_read_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. Chris@16: /// 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. Chris@16: template Chris@16: inline void jpeg_read_and_convert_view(const char* filename,const View& view,CC cc) { Chris@16: detail::jpeg_reader_color_convert m(filename,cc); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. Chris@16: /// 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. Chris@16: template Chris@16: inline void jpeg_read_and_convert_view(const char* filename,const View& view) { Chris@16: detail::jpeg_reader_color_convert m(filename,default_color_converter()); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. Chris@16: template Chris@16: inline void jpeg_read_and_convert_view(const std::string& filename,const View& view,CC cc) { Chris@16: jpeg_read_and_convert_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view. Chris@16: template Chris@16: inline void jpeg_read_and_convert_view(const std::string& filename,const View& view) { Chris@16: jpeg_read_and_convert_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. Chris@16: /// Throws std::ios_base::failure if the file is not a valid JPEG file Chris@16: template Chris@16: inline void jpeg_read_and_convert_image(const char* filename,Image& im,CC cc) { Chris@16: detail::jpeg_reader_color_convert m(filename,cc); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. Chris@16: /// Throws std::ios_base::failure if the file is not a valid JPEG file Chris@16: template Chris@16: inline void jpeg_read_and_convert_image(const char* filename,Image& im) { Chris@16: detail::jpeg_reader_color_convert m(filename,default_color_converter()); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. Chris@16: template Chris@16: inline void jpeg_read_and_convert_image(const std::string& filename,Image& im,CC cc) { Chris@16: jpeg_read_and_convert_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it. Chris@16: template Chris@16: inline void jpeg_read_and_convert_image(const std::string& filename,Image& im) { Chris@16: jpeg_read_and_convert_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Determines whether the given view type is supported for writing Chris@16: template Chris@16: struct jpeg_write_support { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported= Chris@16: (detail::jpeg_write_support_private::type, Chris@16: typename color_space_type::type>::is_supported)); Chris@16: BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type= Chris@16: (detail::jpeg_write_support_private::type, Chris@16: typename color_space_type::type>::color_type)); Chris@16: BOOST_STATIC_CONSTANT(bool, value=is_supported); Chris@16: }; Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Saves the view to a jpeg file specified by the given jpeg image file name. Chris@16: /// 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. Chris@16: /// Throws std::ios_base::failure if it fails to create the file. Chris@16: template Chris@16: inline void jpeg_write_view(const char* filename,const View& view,int quality=100) { Chris@16: BOOST_STATIC_ASSERT(jpeg_write_support::is_supported); Chris@16: Chris@16: detail::jpeg_writer m(filename); Chris@16: m.apply(view,quality); Chris@16: } Chris@16: Chris@16: /// \ingroup JPEG_IO Chris@16: /// \brief Saves the 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 View& view,int quality=100) { Chris@16: jpeg_write_view(filename.c_str(),view,quality); Chris@16: } Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: #endif