00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CVD_INTERNAL_IO_PNG_H
00022 #define CVD_INTERNAL_IO_PNG_H
00023 #include <iostream>
00024 #include <vector>
00025 #include <string>
00026
00027 #include <cvd/image.h>
00028 #include <cvd/internal/load_and_save.h>
00029 #include <cvd/internal/convert_pixel_types.h>
00030
00031 struct png_struct_def;
00032 struct png_info_struct;
00033
00034
00035 namespace CVD{
00036 namespace PNG{
00037
00038 using CVD::Internal::TypeList;
00039 using CVD::Internal::Head;
00040
00041
00042
00043 class png_reader
00044 {
00045 public:
00046 png_reader(std::istream&);
00047 ~png_reader();
00048
00049 ImageRef size();
00050
00051 void get_raw_pixel_line(bool*);
00052 void get_raw_pixel_line(unsigned char*);
00053 void get_raw_pixel_line(unsigned short*);
00054 void get_raw_pixel_line(Rgb<unsigned char>*);
00055 void get_raw_pixel_line(Rgb<unsigned short>*);
00056 void get_raw_pixel_line(Rgba<unsigned char>*);
00057 void get_raw_pixel_line(Rgba<unsigned short>*);
00058
00059 std::string datatype();
00060 std::string name();
00061
00062 typedef TypeList<bool,
00063 TypeList<byte,
00064 TypeList<unsigned short,
00065 TypeList<Rgb<byte>,
00066 TypeList<Rgb<unsigned short>,
00067 TypeList<Rgba<byte>,
00068 TypeList<Rgba<unsigned short>,
00069 Head> > > > > > > Types;
00070
00071 private:
00072
00073 std::istream& i;
00074 std::string type;
00075 unsigned long row;
00076 png_struct_def* png_ptr;
00077 png_info_struct* info_ptr, *end_info;
00078
00079 std::string error_string;
00080 ImageRef my_size;
00081
00082 template<class C> void read_pixels(C*);
00083
00084 };
00085
00086
00088
00089
00090
00091
00092
00093
00094
00095 template<int g1, int g8> struct IntMapper { typedef unsigned short type;};
00096 template<> struct IntMapper<1, 0> { typedef unsigned char type; };
00097 template<> struct IntMapper<0, 0> { typedef bool type; };
00098
00099
00100
00101 template<class ComponentIn, int is_integral> struct ComponentMapper_
00102 {
00103 typedef typename IntMapper<
00104 (Pixel::traits<ComponentIn>::bits_used > 1),
00105 (Pixel::traits<ComponentIn>::bits_used > 8)
00106 >::type type;
00107 };
00108
00109
00110
00111 template<class ComponentIn> struct ComponentMapper_<ComponentIn, 0> { typedef unsigned short type; };
00112
00113 template<class ComponentIn> struct ComponentMapper
00114 {
00115 typedef typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type type;
00116 };
00117
00118
00119
00120 template<class ComponentIn> struct ComponentMapper<Rgb<ComponentIn> >
00121 {
00122 typedef Rgb<typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type> type;
00123 };
00124
00125 template<class ComponentIn> struct ComponentMapper<Rgba<ComponentIn> >
00126 {
00127 typedef Rgba<typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type> type;
00128 };
00129
00130 template<> struct ComponentMapper<Rgb8>
00131 {
00132 typedef Rgb8 type;
00133 };
00134
00135
00136
00137
00138 class png_writer
00139 {
00140 public:
00141 png_writer(std::ostream&, ImageRef size, const std::string& type, const std::map<std::string, Parameter<> >& p);
00142 ~png_writer();
00143
00144 void write_raw_pixel_line(const bool*);
00145 void write_raw_pixel_line(const unsigned char*);
00146 void write_raw_pixel_line(const unsigned short*);
00147 void write_raw_pixel_line(const Rgb<unsigned char>*);
00148 void write_raw_pixel_line(const Rgb8*);
00149 void write_raw_pixel_line(const Rgb<unsigned short>*);
00150 void write_raw_pixel_line(const Rgba<unsigned char>*);
00151 void write_raw_pixel_line(const Rgba<unsigned short>*);
00152
00153 template<class Incoming> struct Outgoing
00154 {
00155 typedef typename ComponentMapper<Incoming>::type type;
00156 };
00157 private:
00158
00159 template<class P> void write_line(const P*);
00160
00161 long row;
00162 std::ostream& o;
00163 ImageRef size;
00164 std::string type;
00165 std::string error_string;
00166
00167 png_struct_def* png_ptr;
00168 png_info_struct* info_ptr, *end_info;
00169
00170 };
00171
00172
00173 }}
00174 #endif