00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef __CLAW_PNG_HPP__
00031 #define __CLAW_PNG_HPP__
00032
00033 #include <claw/image.hpp>
00034 #include <png.h>
00035 #include <setjmp.h>
00036 #include <iostream>
00037 #include <string>
00038
00039 namespace claw
00040 {
00041 namespace graphic
00042 {
00047 class png : public image
00048 {
00049 public:
00050
00055 class reader
00056 {
00057
00058 public:
00059
00064 struct source_manager
00065 {
00066 public:
00067 source_manager( std::istream& is );
00068
00069 void read( png_bytep data, png_size_t length );
00070
00071 private:
00073 std::istream& m_input;
00074
00075 };
00076
00077 public:
00078 reader( image& img );
00079 reader( image& img, std::istream& f );
00080
00081 void load( std::istream& f );
00082
00083 private:
00084 void read_from_file( std::istream& f );
00085 void check_if_png( png_structp png_ptr, std::istream& f ) const;
00086
00087 void read_image( png_structp png_ptr, png_infop info_ptr );
00088 void read_sequential_image( png_structp png_ptr, png_infop info_ptr );
00089 void read_interlaced_image( png_structp png_ptr, png_infop info_ptr,
00090 unsigned int passes );
00091
00092 void copy_pixel_line( png_bytep data, unsigned int y );
00093
00094 void create_read_structures( png_structp& png_ptr,
00095 png_infop& info_ptr ) const;
00096
00097
00098 private:
00100 image& m_image;
00101
00104 static const unsigned int s_rgba_pixel_size;
00105
00106 };
00107
00108
00113 class writer
00114 {
00115 public:
00119 struct options
00120 {
00121 public:
00123 enum compression_level
00124 {
00125 no_compression = Z_NO_COMPRESSION,
00126 best_speed = Z_BEST_SPEED,
00127 best_compression = Z_BEST_COMPRESSION,
00128 default_compression = Z_DEFAULT_COMPRESSION
00129 };
00130
00132 enum interlace_type
00133 {
00135 none = PNG_INTERLACE_NONE,
00136
00139 adam7 = PNG_INTERLACE_ADAM7
00140 };
00141
00142 public:
00143 options();
00144 options( compression_level compression_level_,
00145 interlace_type interlace_ );
00146
00147 public:
00149 compression_level compression;
00150
00152 interlace_type interlace;
00153
00154 };
00155
00156
00157
00158
00163 struct target_manager
00164 {
00165 public:
00166 target_manager( std::ostream& os );
00167
00168 void write( png_bytep data, png_size_t length );
00169 void flush();
00170
00171 private:
00173 std::ostream& m_output;
00174
00175 };
00176
00177 public:
00178 writer( const image& img );
00179 writer( const image& img, std::ostream& f,
00180 const options& opt = options() );
00181
00182 void save( std::ostream& f, const options& opt = options() ) const;
00183
00184 private:
00185 void set_options( png_structp png_ptr, png_infop info_ptr,
00186 const options& opt ) const;
00187 void save_image( png_structp png_ptr, png_infop info_ptr ) const;
00188
00189 void copy_pixel_line( png_bytep data, unsigned int y ) const;
00190
00191 void create_write_structures( png_structp& png_ptr,
00192 png_infop& info_ptr ) const;
00193
00194
00195 private:
00197 const image& m_image;
00198
00201 static const unsigned int s_rgba_pixel_size;
00202
00203 };
00204
00205 public:
00206 png( unsigned int w, unsigned int h );
00207 png( const image& that );
00208 png( std::istream& f );
00209
00210 void save( std::ostream& os,
00211 const writer::options& opt = writer::options() ) const;
00212
00213 };
00214 }
00215 }
00216
00217 #endif // __CLAW_PNG_HPP__