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 <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 // classes that need to be accessible from png callbacks. 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 }; // struct source_manager 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 }; // class reader 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 }; // enum compression_level 00130 00132 enum interlace_type 00133 { 00135 none = PNG_INTERLACE_NONE, 00136 00139 adam7 = PNG_INTERLACE_ADAM7 00140 }; // enum interlace_type 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 }; // struct options 00155 00156 // classes that need to be accessible from png callbacks. 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 }; // struct target_manager 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 }; // class writer 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 }; // class png 00214 } // namespace graphic 00215 } // namespace claw 00216 00217 #endif // __CLAW_PNG_HPP__