Generic Image Library: tiff_dynamic_io.hpp Source File

Modules | Alphabetical List | Class List | Directories | File List | GIL Home Page

# tiff_dynamic_io.hpp

Go to the documentation of this file.

    00001 /*
    00002     Copyright 2005-2007 Adobe Systems Incorporated
    00003
    00004     Use, modification and distribution are subject to the Boost Software License,
    00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    00006     http://www.boost.org/LICENSE_1_0.txt).
    00007
    00008     See http://opensource.adobe.com/gil for most recent version including documentation.
    00009 */
    00010
    00011 /*************************************************************************************************/
    00012
    00013 #ifndef GIL_TIFF_DYNAMIC_IO_H
    00014 #define GIL_TIFF_DYNAMIC_IO_H
    00015
    00022 //
    00023 // We are currently providing the following functions:
    00024 // template <typename Images> void tiff_read_image(const char*,any_image<Images>)
    00025 // template <typename Views> void tiff_write_view(const char*,any_image_view<Views>)
    00026 //
    00027
    00028 #include <string>
    00029 #include <boost/mpl/bool.hpp>
    00030 #include "../dynamic_image/dynamic_image_all.hpp"
    00031 #include "io_error.hpp"
    00032 #include "tiff_io.hpp"
    00033 #include "dynamic_io.hpp"
    00034
    00035 namespace boost { namespace gil {
    00036
    00037 namespace detail {
    00038
    00039 struct tiff_write_is_supported {
    00040     template<typename View> struct apply
    00041         : public mpl::bool_<tiff_write_support<View>::is_supported> {};
    00042 };
    00043
    00044 class tiff_writer_dynamic : public tiff_writer {
    00045 public:
    00046     typedef void result_type;
    00047     tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {}
    00048
    00049     template <typename Views>
    00050     void write_view(const any_image_view<Views>& runtime_view) {
    00051         dynamic_io_fnobj<tiff_write_is_supported, tiff_writer> op(this);
    00052         apply_operation(runtime_view,op);
    00053     }
    00054 };
    00055
    00056 class tiff_type_format_checker {
    00057     int _bit_depth;
    00058     int _color_type;
    00059 public:
    00060     tiff_type_format_checker(int bit_depth_in,int color_type_in) :
    00061         _bit_depth(bit_depth_in),_color_type(color_type_in) {}
    00062     template <typename Image>
    00063     bool apply() {
    00064         return tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
    00065                tiff_read_support<typename Image::view_t>::color_type==_color_type;
    00066     }
    00067 };
    00068
    00069 struct tiff_read_is_supported {
    00070     template<typename View> struct apply
    00071         : public mpl::bool_<tiff_read_support<View>::is_supported> {};
    00072 };
    00073
    00074 class tiff_reader_dynamic : public tiff_reader {
    00075 public:
    00076     tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {}
    00077
    00078     template <typename Images>
    00079     void read_image(any_image<Images>& im) {
    00080         int width,height;
    00081         unsigned short bps,photometric;
    00082         TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
    00083         TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
    00084         TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
    00085         TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
    00086         if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
    00087             io_error("tiff_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
    00088         } else {
    00089             im.recreate(width,height);
    00090             dynamic_io_fnobj<tiff_read_is_supported, tiff_reader> op(this);
    00091             apply_operation(view(im),op);
    00092         }
    00093     }
    00094 };
    00095
    00096 } // namespace detail
    00097
    00103 template <typename Images>
    00104 inline void tiff_read_image(const char* filename,any_image<Images>& im) {
    00105     detail::tiff_reader_dynamic m(filename);
    00106     m.read_image(im);
    00107 }
    00108
    00111 template <typename Images>
    00112 inline void tiff_read_image(const std::string& filename,any_image<Images>& im) {
    00113     tiff_read_image(filename.c_str(),im);
    00114 }
    00115
    00120 template <typename Views>
    00121 inline void tiff_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
    00122     detail::tiff_writer_dynamic m(filename);
    00123     m.write_view(runtime_view);
    00124 }
    00125
    00128 template <typename Views>
    00129 inline void tiff_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
    00130     tiff_write_view(filename.c_str(),runtime_view);
    00131 }
    00132
    00133 } }  // namespace boost::gil
    00134
    00135 #endif