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
94  ( png_byte color_type, png_bytep data, unsigned int y );
95 
96  void create_read_structures( png_structp& png_ptr,
97  png_infop& info_ptr ) const;
98 
99 
100  private:
102  image& m_image;
103 
106  static const unsigned int s_rgba_pixel_size;
107 
108  }; // class reader
109 
110  /*----------------------------------------------------------------------*/
115  class writer
116  {
117  public:
121  struct options
122  {
123  public:
126  {
127  no_compression = Z_NO_COMPRESSION,
128  best_speed = Z_BEST_SPEED,
129  best_compression = Z_BEST_COMPRESSION,
130  default_compression = Z_DEFAULT_COMPRESSION
131  }; // enum compression_level
132 
135  {
137  none = PNG_INTERLACE_NONE,
138 
141  adam7 = PNG_INTERLACE_ADAM7
142  }; // enum interlace_type
143 
144  public:
145  options();
146  options( compression_level compression_level_,
147  interlace_type interlace_ );
148 
149  public:
152 
155 
156  }; // struct options
157 
158  // classes that need to be accessible from png callbacks.
159 
160  /*--------------------------------------------------------------------*/
166  {
167  public:
168  target_manager( std::ostream& os );
169 
170  void write( png_bytep data, png_size_t length );
171  void flush();
172 
173  private:
175  std::ostream& m_output;
176 
177  }; // struct target_manager
178 
179  public:
180  writer( const image& img );
181  writer( const image& img, std::ostream& f,
182  const options& opt = options() );
183 
184  void save( std::ostream& f, const options& opt = options() ) const;
185 
186  private:
187  void set_options( png_structp png_ptr, png_infop info_ptr,
188  const options& opt ) const;
189  void save_image( png_structp png_ptr, png_infop info_ptr ) const;
190 
191  void copy_pixel_line( png_bytep data, unsigned int y ) const;
192 
193  void create_write_structures( png_structp& png_ptr,
194  png_infop& info_ptr ) const;
195 
196 
197  private:
199  const image& m_image;
200 
203  static const unsigned int s_rgba_pixel_size;
204 
205  }; // class writer
206 
207  public:
208  png( unsigned int w, unsigned int h );
209  png( const image& that );
210  png( std::istream& f );
211 
212  void save( std::ostream& os,
213  const writer::options& opt = writer::options() ) const;
214 
215  }; // class png
216  } // namespace graphic
217 } // namespace claw
218 
219 #endif // __CLAW_PNG_HPP__
A class for png pictures.
Definition: png.hpp:48
compression_level
Compression level in the interlaced image.
Definition: png.hpp:125
This class read data from a png file and store it in an image.
Definition: png.hpp:56
void load(std::istream &f)
Load an image from a png file.
Definition: png_reader.cpp:114
Source manager that allow us to read from a std::istream.
Definition: png.hpp:65
interlace_type
The algorithm to use to interlace the saved image.
Definition: png.hpp:134
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
Definition: png.cpp:73
Target manager that allow us to write in a std::ostream.
Definition: png.hpp:165
void read(png_bytep data, png_size_t length)
Read data from the input stream.
Definition: png_reader.cpp:74
reader(image &img)
Constructor.
Definition: png_reader.cpp:90
source_manager(std::istream &is)
Constructor.
Definition: png_reader.cpp:61
This class write an image in a png file.
Definition: png.hpp:115
compression_level compression
Compression level to use in the saved stream.
Definition: png.hpp:151
interlace_type interlace
Interlace method to apply to the saved image.
Definition: png.hpp:154
A class to deal with images.
Definition: image.hpp:49
This is the main namespace.
Definition: algorithm.hpp:33
Parameters of the writing algorithm.
Definition: png.hpp:121
png(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: png.cpp:39
A class to deal with images.