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_TIFF_DYNAMIC_IO_H Chris@16: #define GIL_TIFF_DYNAMIC_IO_H Chris@16: Chris@16: /// \file Chris@16: /// \brief Support for reading and writing TIFF files Chris@16: /// Requires libtiff! 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: // We are currently providing the following functions: Chris@16: // template void tiff_read_image(const char*,any_image) Chris@16: // template void tiff_write_view(const char*,any_image_view) Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include "../dynamic_image/dynamic_image_all.hpp" Chris@16: #include "io_error.hpp" Chris@16: #include "tiff_io.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 tiff_write_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class tiff_writer_dynamic : public tiff_writer { Chris@16: public: Chris@16: typedef void result_type; Chris@16: tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {} 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 tiff_type_format_checker { Chris@16: int _bit_depth; Chris@16: int _color_type; Chris@16: public: Chris@16: tiff_type_format_checker(int bit_depth_in,int color_type_in) : Chris@16: _bit_depth(bit_depth_in),_color_type(color_type_in) {} Chris@16: template Chris@16: bool apply() { Chris@16: return tiff_read_support::bit_depth==_bit_depth && Chris@16: tiff_read_support::color_type==_color_type; Chris@16: } Chris@16: }; Chris@16: Chris@16: struct tiff_read_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class tiff_reader_dynamic : public tiff_reader { Chris@16: public: Chris@16: tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {} Chris@16: Chris@16: template Chris@16: void read_image(any_image& im) { Chris@16: int width,height; Chris@16: unsigned short bps,photometric; Chris@16: TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width); Chris@16: TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height); Chris@16: TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps); Chris@16: TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric); Chris@16: if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) { Chris@16: io_error("tiff_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(width,height); 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: /// \ingroup TIFF_IO Chris@16: /// \brief reads a TIFF image into a run-time instantiated image Chris@16: /// Opens the given tiff 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 tiff_read_image(const char* filename,any_image& im) { Chris@16: detail::tiff_reader_dynamic m(filename); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup TIFF_IO Chris@16: /// \brief reads a TIFF image into a run-time instantiated image Chris@16: template Chris@16: inline void tiff_read_image(const std::string& filename,any_image& im) { Chris@16: tiff_read_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup TIFF_IO Chris@16: /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff 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 tiff_write_view(const char* filename,const any_image_view& runtime_view) { Chris@16: detail::tiff_writer_dynamic m(filename); Chris@16: m.write_view(runtime_view); Chris@16: } Chris@16: Chris@16: /// \ingroup TIFF_IO Chris@16: /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name. Chris@16: template Chris@16: inline void tiff_write_view(const std::string& filename,const any_image_view& runtime_view) { Chris@16: tiff_write_view(filename.c_str(),runtime_view); Chris@16: } Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: #endif