Claw 1.7.0
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #ifndef __CLAW_PNG_HPP__ 00031 #define __CLAW_PNG_HPP__ 00032 00033 #include <claw/image.hpp> 00034 #include <png.h> 00035 #include <zlib.h> 00036 #include <setjmp.h> 00037 #include <iostream> 00038 #include <string> 00039 00040 namespace claw 00041 { 00042 namespace graphic 00043 { 00048 class png : public image 00049 { 00050 public: 00051 /*----------------------------------------------------------------------*/ 00056 class reader 00057 { 00058 // classes that need to be accessible from png callbacks. 00059 public: 00060 /*--------------------------------------------------------------------*/ 00065 struct source_manager 00066 { 00067 public: 00068 source_manager( std::istream& is ); 00069 00070 void read( png_bytep data, png_size_t length ); 00071 00072 private: 00074 std::istream& m_input; 00075 00076 }; // struct source_manager 00077 00078 public: 00079 reader( image& img ); 00080 reader( image& img, std::istream& f ); 00081 00082 void load( std::istream& f ); 00083 00084 private: 00085 void read_from_file( std::istream& f ); 00086 void check_if_png( png_structp png_ptr, std::istream& f ) const; 00087 00088 void read_image( png_structp png_ptr, png_infop info_ptr ); 00089 void read_sequential_image( png_structp png_ptr, png_infop info_ptr ); 00090 void read_interlaced_image( png_structp png_ptr, png_infop info_ptr, 00091 unsigned int passes ); 00092 00093 void copy_pixel_line( png_bytep data, unsigned int y ); 00094 00095 void create_read_structures( png_structp& png_ptr, 00096 png_infop& info_ptr ) const; 00097 00098 00099 private: 00101 image& m_image; 00102 00105 static const unsigned int s_rgba_pixel_size; 00106 00107 }; // class reader 00108 00109 /*----------------------------------------------------------------------*/ 00114 class writer 00115 { 00116 public: 00120 struct options 00121 { 00122 public: 00124 enum compression_level 00125 { 00126 no_compression = Z_NO_COMPRESSION, 00127 best_speed = Z_BEST_SPEED, 00128 best_compression = Z_BEST_COMPRESSION, 00129 default_compression = Z_DEFAULT_COMPRESSION 00130 }; // enum compression_level 00131 00133 enum interlace_type 00134 { 00136 none = PNG_INTERLACE_NONE, 00137 00140 adam7 = PNG_INTERLACE_ADAM7 00141 }; // enum interlace_type 00142 00143 public: 00144 options(); 00145 options( compression_level compression_level_, 00146 interlace_type interlace_ ); 00147 00148 public: 00150 compression_level compression; 00151 00153 interlace_type interlace; 00154 00155 }; // struct options 00156 00157 // classes that need to be accessible from png callbacks. 00158 00159 /*--------------------------------------------------------------------*/ 00164 struct target_manager 00165 { 00166 public: 00167 target_manager( std::ostream& os ); 00168 00169 void write( png_bytep data, png_size_t length ); 00170 void flush(); 00171 00172 private: 00174 std::ostream& m_output; 00175 00176 }; // struct target_manager 00177 00178 public: 00179 writer( const image& img ); 00180 writer( const image& img, std::ostream& f, 00181 const options& opt = options() ); 00182 00183 void save( std::ostream& f, const options& opt = options() ) const; 00184 00185 private: 00186 void set_options( png_structp png_ptr, png_infop info_ptr, 00187 const options& opt ) const; 00188 void save_image( png_structp png_ptr, png_infop info_ptr ) const; 00189 00190 void copy_pixel_line( png_bytep data, unsigned int y ) const; 00191 00192 void create_write_structures( png_structp& png_ptr, 00193 png_infop& info_ptr ) const; 00194 00195 00196 private: 00198 const image& m_image; 00199 00202 static const unsigned int s_rgba_pixel_size; 00203 00204 }; // class writer 00205 00206 public: 00207 png( unsigned int w, unsigned int h ); 00208 png( const image& that ); 00209 png( std::istream& f ); 00210 00211 void save( std::ostream& os, 00212 const writer::options& opt = writer::options() ) const; 00213 00214 }; // class png 00215 } // namespace graphic 00216 } // namespace claw 00217 00218 #endif // __CLAW_PNG_HPP__