00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef GIL_PNG_DYNAMIC_IO_H
00010 #define GIL_PNG_DYNAMIC_IO_H
00011
00015
00016
00017
00018
00019
00020
00021
00025
00026 #include <string>
00027 #include <stdio.h>
00028 #include <boost/mpl/bool.hpp>
00029 #include <boost/shared_ptr.hpp>
00030 #include "../../core/gil_config.hpp"
00031 #include "../../core/utilities.hpp"
00032 #include "../dynamic_image/any_image.hpp"
00033 #include "../dynamic_image/apply_operation.hpp"
00034 #include "io_error.hpp"
00035 #include "png_io.hpp"
00036 #include "png_io_private.hpp"
00037 #include "dynamic_io.hpp"
00038
00039 ADOBE_GIL_NAMESPACE_BEGIN
00040
00041 namespace detail {
00042
00043 struct png_write_is_supported {
00044 template<typename VIEW> struct apply
00045 : public boost::mpl::bool_<png_write_support<VIEW>::is_supported> {};
00046 };
00047
00048 class png_writer_dynamic : public png_writer {
00049 public:
00050 png_writer_dynamic(FILE* file ) : png_writer(file) {}
00051 png_writer_dynamic(const char* filename) : png_writer(filename){}
00052
00053 template <typename VIEWS>
00054 void write_view(const any_image_view<VIEWS>& runtime_view) {
00055 dynamic_io_fnobj<png_write_is_supported, png_writer> op(this);
00056 apply_operation(runtime_view,op);
00057 }
00058 };
00059
00060 class png_type_format_checker {
00061 int _bit_depth;
00062 int _color_type;
00063 public:
00064 png_type_format_checker(int bit_depth_in,int color_type_in) :
00065 _bit_depth(bit_depth_in),_color_type(color_type_in) {}
00066 template <typename IMAGE>
00067 bool apply() {
00068 return png_read_support<typename IMAGE::view_t>::bit_depth==_bit_depth &&
00069 png_read_support<typename IMAGE::view_t>::color_type==_color_type;
00070 }
00071 };
00072
00073 struct png_read_is_supported {
00074 template<typename VIEW> struct apply
00075 : public boost::mpl::bool_<png_read_support<VIEW>::is_supported> {};
00076 };
00077
00078 class png_reader_dynamic : public png_reader {
00079 public:
00080 png_reader_dynamic(FILE* file) : png_reader(file) {}
00081 png_reader_dynamic(const char* filename) : png_reader(filename){}
00082
00083 template <typename IMAGES>
00084 void read_image(any_image<IMAGES>& im) {
00085 png_uint_32 width, height;
00086 int bit_depth, color_type, interlace_type;
00087 png_get_IHDR(_png_ptr, _info_ptr,
00088 &width, &height,&bit_depth,&color_type,&interlace_type,
00089 int_p_NULL, int_p_NULL);
00090 if (!construct_matched(im,png_type_format_checker(bit_depth,color_type))) {
00091 io_error("png_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
00092 } else {
00093 resize_clobber_image(im,point2<int>(width,height));
00094 dynamic_io_fnobj<png_read_is_supported, png_reader> op(this);
00095 apply_operation(view(im),op);
00096 }
00097 }
00098 };
00099
00100 }
00101
00106 template <typename IMAGES>
00107 inline void png_read_image(const char* filename,any_image<IMAGES>& im) {
00108 detail::png_reader_dynamic m(filename);
00109 m.read_image(im);
00110 }
00111
00114 template <typename IMAGES>
00115 inline void png_read_image(const std::string& filename,any_image<IMAGES>& im) {
00116 png_read_image(filename.c_str(),im);
00117 }
00118
00123 template <typename VIEWS>
00124 inline void png_write_view(const char* filename,const any_image_view<VIEWS>& runtime_view) {
00125 detail::png_writer_dynamic m(filename);
00126 m.write_view(runtime_view);
00127 }
00128
00131 template <typename VIEWS>
00132 inline void png_write_view(const std::string& filename,const any_image_view<VIEWS>& runtime_view) {
00133 png_write_view(filename.c_str(),runtime_view);
00134 }
00135
00136 ADOBE_GIL_NAMESPACE_END
00137
00138 #endif