claw  1.9.0
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@stuff-o-matic.com
24 */
30 #ifndef __CLAW_PNG_HPP__
31 #define __CLAW_PNG_HPP__
32 
33 #include <claw/graphic/image.hpp>
34 
35 #include <iostream>
36 #include <png.h>
37 #include <setjmp.h>
38 #include <string>
39 #include <zlib.h>
40 
41 namespace claw
42 {
43  namespace graphic
44  {
49  class png : public image
50  {
51  public:
56  class reader
57  {
58  // classes that need to be accessible from png callbacks.
59  public:
65  {
66  public:
67  source_manager(std::istream& is);
68 
69  void read(png_bytep data, png_size_t length);
70 
71  private:
73  std::istream& m_input;
74 
75  }; // struct source_manager
76 
77  public:
78  reader(image& img);
79  reader(image& img, std::istream& f);
80 
81  void load(std::istream& f);
82 
83  private:
84  void read_from_file(std::istream& f);
85  void check_if_png(png_structp png_ptr, std::istream& f) const;
86 
87  void read_image(png_structp png_ptr, png_infop info_ptr);
88  void read_sequential_image(png_structp png_ptr, png_infop info_ptr);
89  void read_interlaced_image(png_structp png_ptr, png_infop info_ptr,
90  unsigned int passes);
91 
92  void copy_pixel_line(png_byte color_type, png_bytep data,
93  unsigned int y);
94 
95  void create_read_structures(png_structp& png_ptr,
96  png_infop& info_ptr) const;
97 
98  private:
100  image& m_image;
101 
104  static const unsigned int s_rgba_pixel_size;
105 
106  }; // class reader
107 
112  class writer
113  {
114  public:
118  struct options
119  {
120  public:
123  {
124  no_compression = Z_NO_COMPRESSION,
125  best_speed = Z_BEST_SPEED,
126  best_compression = Z_BEST_COMPRESSION,
127  default_compression = Z_DEFAULT_COMPRESSION
128  }; // enum compression_level
129 
132  {
134  none = PNG_INTERLACE_NONE,
135 
138  adam7 = PNG_INTERLACE_ADAM7
139  }; // enum interlace_type
140 
141  public:
142  options();
143  options(compression_level compression_level_,
144  interlace_type interlace_);
145 
146  public:
149 
152 
153  }; // struct options
154 
155  // classes that need to be accessible from png callbacks.
156 
162  {
163  public:
164  target_manager(std::ostream& os);
165 
166  void write(png_bytep data, png_size_t length);
167  void flush();
168 
169  private:
171  std::ostream& m_output;
172 
173  }; // struct target_manager
174 
175  public:
176  writer(const image& img);
177  writer(const image& img, std::ostream& f,
178  const options& opt = options());
179 
180  void save(std::ostream& f, const options& opt = options()) const;
181 
182  private:
183  void set_options(png_structp png_ptr, png_infop info_ptr,
184  const options& opt) const;
185  void save_image(png_structp png_ptr, png_infop info_ptr) const;
186 
187  void copy_pixel_line(png_bytep data, unsigned int y) const;
188 
189  void create_write_structures(png_structp& png_ptr,
190  png_infop& info_ptr) const;
191 
192  private:
194  const image& m_image;
195 
198  static const unsigned int s_rgba_pixel_size;
199 
200  }; // class writer
201 
202  public:
203  png(unsigned int w, unsigned int h);
204  png(const image& that);
205  png(std::istream& f);
206 
207  void save(std::ostream& os,
208  const writer::options& opt = writer::options()) const;
209 
210  }; // class png
211  }
212 }
213 
214 #endif // __CLAW_PNG_HPP__
A class for png pictures.
Definition: png.hpp:49
compression_level
Compression level in the interlaced image.
Definition: png.hpp:122
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:100
Source manager that allow us to read from a std::istream.
Definition: png.hpp:64
options()
Default constructor.
Definition: png_writer.cpp:95
void save(std::ostream &f, const options &opt=options()) const
Save the image in a PNG file.
Definition: png_writer.cpp:139
interlace_type
The algorithm to use to interlace the saved image.
Definition: png.hpp:131
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
Definition: png.cpp:64
void write(png_bytep data, png_size_t length)
Write data in the ouput stream.
Definition: png_writer.cpp:78
Saved image won&#39;t be interaced.
Definition: png.hpp:134
Saved image will be interlaced using the Adam7 algorithm.
Definition: png.hpp:138
Target manager that allow us to write in a std::ostream.
Definition: png.hpp:161
void read(png_bytep data, png_size_t length)
Read data from the input stream.
Definition: png_reader.cpp:68
writer(const image &img)
Constructor.
Definition: png_writer.cpp:117
reader(image &img)
Constructor.
Definition: png_reader.cpp:80
source_manager(std::istream &is)
Constructor.
Definition: png_reader.cpp:57
void flush()
Flush the output stream.
Definition: png_writer.cpp:87
This class write an image in a png file.
Definition: png.hpp:112
compression_level compression
Compression level to use in the saved stream.
Definition: png.hpp:148
target_manager(std::ostream &os)
Constructor.
Definition: png_writer.cpp:67
interlace_type interlace
Interlace method to apply to the saved image.
Definition: png.hpp:151
A class to deal with images.
Definition: image.hpp:50
This is the main namespace.
Definition: application.hpp:49
Parameters of the writing algorithm.
Definition: png.hpp:118
png(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: png.cpp:38
A class to deal with images.