Claw  1.7.3
png.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_PNG_HPP__
31 #define __CLAW_PNG_HPP__
32 
33 #include <claw/image.hpp>
34 #include <png.h>
35 #include <zlib.h>
36 #include <setjmp.h>
37 #include <iostream>
38 #include <string>
39 
40 namespace claw
41 {
42  namespace graphic
43  {
48  class png : public image
49  {
50  public:
51  /*----------------------------------------------------------------------*/
56  class reader
57  {
58  // classes that need to be accessible from png callbacks.
59  public:
60  /*--------------------------------------------------------------------*/
66  {
67  public:
68  source_manager( std::istream& is );
69 
70  void read( png_bytep data, png_size_t length );
71 
72  private:
74  std::istream& m_input;
75 
76  }; // struct source_manager
77 
78  public:
79  reader( image& img );
80  reader( image& img, std::istream& f );
81 
82  void load( std::istream& f );
83 
84  private:
85  void read_from_file( std::istream& f );
86  void check_if_png( png_structp png_ptr, std::istream& f ) const;
87 
88  void read_image( png_structp png_ptr, png_infop info_ptr );
89  void read_sequential_image( png_structp png_ptr, png_infop info_ptr );
90  void read_interlaced_image( png_structp png_ptr, png_infop info_ptr,
91  unsigned int passes );
92 
93  void copy_pixel_line( png_bytep data, unsigned int y );
94 
95  void create_read_structures( png_structp& png_ptr,
96  png_infop& info_ptr ) const;
97 
98 
99  private:
101  image& m_image;
102 
105  static const unsigned int s_rgba_pixel_size;
106 
107  }; // class reader
108 
109  /*----------------------------------------------------------------------*/
114  class writer
115  {
116  public:
120  struct options
121  {
122  public:
125  {
126  no_compression = Z_NO_COMPRESSION,
127  best_speed = Z_BEST_SPEED,
128  best_compression = Z_BEST_COMPRESSION,
129  default_compression = Z_DEFAULT_COMPRESSION
130  }; // enum compression_level
131 
134  {
136  none = PNG_INTERLACE_NONE,
137 
140  adam7 = PNG_INTERLACE_ADAM7
141  }; // enum interlace_type
142 
143  public:
144  options();
145  options( compression_level compression_level_,
146  interlace_type interlace_ );
147 
148  public:
151 
154 
155  }; // struct options
156 
157  // classes that need to be accessible from png callbacks.
158 
159  /*--------------------------------------------------------------------*/
165  {
166  public:
167  target_manager( std::ostream& os );
168 
169  void write( png_bytep data, png_size_t length );
170  void flush();
171 
172  private:
174  std::ostream& m_output;
175 
176  }; // struct target_manager
177 
178  public:
179  writer( const image& img );
180  writer( const image& img, std::ostream& f,
181  const options& opt = options() );
182 
183  void save( std::ostream& f, const options& opt = options() ) const;
184 
185  private:
186  void set_options( png_structp png_ptr, png_infop info_ptr,
187  const options& opt ) const;
188  void save_image( png_structp png_ptr, png_infop info_ptr ) const;
189 
190  void copy_pixel_line( png_bytep data, unsigned int y ) const;
191 
192  void create_write_structures( png_structp& png_ptr,
193  png_infop& info_ptr ) const;
194 
195 
196  private:
198  const image& m_image;
199 
202  static const unsigned int s_rgba_pixel_size;
203 
204  }; // class writer
205 
206  public:
207  png( unsigned int w, unsigned int h );
208  png( const image& that );
209  png( std::istream& f );
210 
211  void save( std::ostream& os,
212  const writer::options& opt = writer::options() ) const;
213 
214  }; // class png
215  } // namespace graphic
216 } // namespace claw
217 
218 #endif // __CLAW_PNG_HPP__