Claw  1.7.3
png.hpp
Go to the documentation of this file.
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
00094         ( png_byte color_type, png_bytep data, unsigned int y );
00095 
00096         void create_read_structures( png_structp& png_ptr,
00097                                      png_infop& info_ptr ) const;
00098 
00099 
00100       private:
00102         image& m_image;
00103 
00106         static const unsigned int s_rgba_pixel_size;
00107 
00108       }; // class reader
00109 
00110       /*----------------------------------------------------------------------*/
00115       class writer
00116       {
00117       public:
00121         struct options
00122         {
00123         public:
00125           enum compression_level
00126             {
00127               no_compression = Z_NO_COMPRESSION,
00128               best_speed = Z_BEST_SPEED,
00129               best_compression = Z_BEST_COMPRESSION,
00130               default_compression = Z_DEFAULT_COMPRESSION
00131             }; // enum compression_level
00132 
00134           enum interlace_type
00135             {
00137               none = PNG_INTERLACE_NONE,
00138 
00141               adam7 = PNG_INTERLACE_ADAM7
00142             }; // enum interlace_type
00143 
00144         public:
00145           options();
00146           options( compression_level compression_level_,
00147                    interlace_type interlace_ );
00148 
00149         public:
00151           compression_level compression;
00152 
00154           interlace_type interlace;
00155 
00156         }; // struct options
00157 
00158         // classes that need to be accessible from png callbacks.
00159 
00160         /*--------------------------------------------------------------------*/
00165         struct target_manager
00166         {
00167         public:
00168           target_manager( std::ostream& os );
00169 
00170           void write( png_bytep data, png_size_t length );
00171           void flush();
00172 
00173         private:
00175           std::ostream& m_output;
00176 
00177         }; // struct target_manager
00178 
00179       public:
00180         writer( const image& img );
00181         writer( const image& img, std::ostream& f,
00182                 const options& opt = options() );
00183 
00184         void save( std::ostream& f, const options& opt = options() ) const;
00185 
00186       private:
00187         void set_options( png_structp png_ptr, png_infop info_ptr,
00188                           const options& opt ) const;
00189         void save_image( png_structp png_ptr, png_infop info_ptr ) const;
00190         
00191         void copy_pixel_line( png_bytep data, unsigned int y ) const;
00192 
00193         void create_write_structures( png_structp& png_ptr,
00194                                       png_infop& info_ptr ) const;
00195 
00196 
00197       private:
00199         const image& m_image;
00200 
00203         static const unsigned int s_rgba_pixel_size;
00204 
00205       }; // class writer
00206 
00207     public:
00208       png( unsigned int w, unsigned int h );
00209       png( const image& that );
00210       png( std::istream& f );
00211 
00212       void save( std::ostream& os,
00213                  const writer::options& opt = writer::options() ) const;
00214 
00215     }; // class png
00216   } // namespace graphic
00217 } // namespace claw
00218 
00219 #endif // __CLAW_PNG_HPP__