Modules | Alphabetical List | Class List | Directories | File List | GIL Home Page # png_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_PNG_DYNAMIC_IO_H
    00014 #define GIL_PNG_DYNAMIC_IO_H
    00015
    00023 //
    00024 // We are currently providing the following functions:
    00025 // template <typename Images> void png_read_image(const char*,any_image<Images>&)
    00026 // template <typename Images> void png_read_image(FILE*,any_image<Images>&,std::size_t)
    00027 // template <typename Views> void png_write_view(const char*,const any_image_view<View>&)
    00028 // template <typename Views> void png_write_view(FILE*,const any_image_view<View>&)
    00029
    00030
    00031 #include <string>
    00032 #include <stdio.h>
    00033 #include <boost/mpl/bool.hpp>
    00034 #include <boost/shared_ptr.hpp>
    00035 #include "../dynamic_image/dynamic_image_all.hpp"
    00036 #include "io_error.hpp"
    00037 #include "png_io.hpp"
    00038 #include "png_io_private.hpp"
    00039 #include "dynamic_io.hpp"
    00040
    00041 namespace boost { namespace gil {
    00042
    00043 namespace detail {
    00044
    00045 struct png_write_is_supported {
    00046     template<typename View> struct apply
    00047         : public mpl::bool_<png_write_support<View>::is_supported> {};
    00048 };
    00049
    00050 class png_writer_dynamic : public png_writer {
    00051 public:
    00052     png_writer_dynamic(FILE* file          ) : png_writer(file)    {}
    00053     png_writer_dynamic(const char* filename) : png_writer(filename){}
    00054
    00055     template <typename Views>
    00056     void write_view(const any_image_view<Views>& runtime_view) {
    00057         dynamic_io_fnobj<png_write_is_supported, png_writer> op(this);
    00058         apply_operation(runtime_view,op);
    00059     }
    00060 };
    00061
    00062 class png_type_format_checker {
    00063     int _bit_depth;
    00064     int _color_type;
    00065 public:
    00066     png_type_format_checker(int bit_depth_in,int color_type_in) :
    00067         _bit_depth(bit_depth_in),_color_type(color_type_in) {}
    00068     template <typename Image>
    00069     bool apply() {
    00070         return png_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
    00071             png_read_support<typename Image::view_t>::color_type==_color_type;
    00072     }
    00073 };
    00074
    00075 struct png_read_is_supported {
    00076     template<typename View> struct apply
    00077         : public mpl::bool_<png_read_support<View>::is_supported> {};
    00078 };
    00079
    00080 class png_reader_dynamic : public png_reader {
    00081 public:
    00082     png_reader_dynamic(FILE* file)           : png_reader(file)    {}
    00083     png_reader_dynamic(const char* filename) : png_reader(filename){}
    00084
    00085     template <typename Images>
    00086     void read_image(any_image<Images>& im) {
    00087         png_uint_32 width, height;
    00088         int bit_depth, color_type, interlace_type;
    00089         png_get_IHDR(_png_ptr, _info_ptr,
    00090                      &width, &height,&bit_depth,&color_type,&interlace_type,
    00091                      int_p_NULL, int_p_NULL);
    00092         if (!construct_matched(im,png_type_format_checker(bit_depth,color_type))) {
    00093             io_error("png_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
    00094         } else {
    00095             im.recreate(width,height);
    00096             dynamic_io_fnobj<png_read_is_supported, png_reader> op(this);
    00097             apply_operation(view(im),op);
    00098         }
    00099     }
    00100 };
    00101
    00102 } // namespace detail
    00103
    00109 template <typename Images>
    00110 inline void png_read_image(const char* filename,any_image<Images>& im) {
    00111     detail::png_reader_dynamic m(filename);
    00112     m.read_image(im);
    00113 }
    00114
    00117 template <typename Images>
    00118 inline void png_read_image(const std::string& filename,any_image<Images>& im) {
    00119     png_read_image(filename.c_str(),im);
    00120 }
    00121
    00126 template <typename Views>
    00127 inline void png_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
    00128     detail::png_writer_dynamic m(filename);
    00129     m.write_view(runtime_view);
    00130 }
    00131
    00134 template <typename Views>
    00135 inline void png_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
    00136     png_write_view(filename.c_str(),runtime_view);
    00137 }
    00138
    00139 } }  // namespace boost::gil
    00140
    00141 #endif