png++  0.2.1
image.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007,2008 Alex Shulgin
3  *
4  * This file is part of png++ the C++ wrapper for libpng. PNG++ is free
5  * software; the exact copying conditions are as follows:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef PNGPP_IMAGE_HPP_INCLUDED
32 #define PNGPP_IMAGE_HPP_INCLUDED
33 
34 #include <fstream>
35 #include "pixel_buffer.hpp"
36 #include "generator.hpp"
37 #include "consumer.hpp"
38 #include "convert_color_space.hpp"
39 
40 namespace png
41 {
42 
51  template< typename pixel >
52  class image
53  {
54  public:
59 
64 
68  typedef typename pixbuf::row_type row_type;
69 
75 
80  {
81  void operator()(io_base&) const {}
82  };
83 
88  : m_info(make_image_info< pixel >())
89  {
90  }
91 
95  image(size_t width, size_t height)
96  : m_info(make_image_info< pixel >())
97  {
98  resize(width, height);
99  }
100 
105  explicit image(std::string const& filename)
106  {
107  read(filename, transform_convert());
108  }
109 
114  template< class transformation >
115  image(std::string const& filename,
116  transformation const& transform)
117  {
118  read(filename.c_str(), transform);
119  }
120 
125  explicit image(char const* filename)
126  {
127  read(filename, transform_convert());
128  }
129 
134  template< class transformation >
135  image(char const* filename, transformation const& transform)
136  {
137  read(filename, transform);
138  }
139 
144  explicit image(std::istream& stream)
145  {
146  read_stream(stream, transform_convert());
147  }
148 
153  template< class transformation >
154  image(std::istream& stream, transformation const& transform)
155  {
156  read_stream(stream, transform);
157  }
158 
163  void read(std::string const& filename)
164  {
165  read(filename, transform_convert());
166  }
167 
172  template< class transformation >
173  void read(std::string const& filename, transformation const& transform)
174  {
175  read(filename.c_str(), transform);
176  }
177 
182  void read(char const* filename)
183  {
184  read(filename, transform_convert());
185  }
186 
191  template< class transformation >
192  void read(char const* filename, transformation const& transform)
193  {
194  std::ifstream stream(filename, std::ios::binary);
195  if (!stream.is_open())
196  {
197  throw std_error(filename);
198  }
199  stream.exceptions(std::ios::badbit);
200  read_stream(stream, transform);
201  }
202 
207  void read(std::istream& stream)
208  {
209  read_stream(stream, transform_convert());
210  }
211 
216  template< class transformation >
217  void read(std::istream& stream, transformation const& transform)
218  {
219  read_stream(stream, transform);
220  }
221 
226  template< class istream >
227  void read_stream(istream& stream)
228  {
229  read_stream(stream, transform_convert());
230  }
231 
236  template< class istream, class transformation >
237  void read_stream(istream& stream, transformation const& transform)
238  {
239  pixel_consumer pixcon(m_info, m_pixbuf);
240  pixcon.read(stream, transform);
241  }
242 
246  void write(std::string const& filename)
247  {
248  write(filename.c_str());
249  }
250 
254  void write(char const* filename)
255  {
256  std::ofstream stream(filename, std::ios::binary);
257  if (!stream.is_open())
258  {
259  throw std_error(filename);
260  }
261  stream.exceptions(std::ios::badbit);
262  write_stream(stream);
263  }
264 
268  void write_stream(std::ostream& stream)
269  {
270  write_stream(stream);
271  }
272 
276  template< class ostream >
277  void write_stream(ostream& stream)
278  {
280  pixgen.write(stream);
281  }
282 
286  pixbuf& get_pixbuf()
287  {
288  return m_pixbuf;
289  }
290 
294  pixbuf const& get_pixbuf() const
295  {
296  return m_pixbuf;
297  }
298 
304  void set_pixbuf(pixbuf const& buffer)
305  {
306  m_pixbuf = buffer;
307  }
308 
309  size_t get_width() const
310  {
311  return m_pixbuf.get_width();
312  }
313 
314  size_t get_height() const
315  {
316  return m_pixbuf.get_height();
317  }
318 
322  void resize(size_t width, size_t height)
323  {
324  m_pixbuf.resize(width, height);
325  m_info.set_width(width);
326  m_info.set_height(height);
327  }
328 
335  row_type& get_row(size_t index)
336  {
337  return m_pixbuf.get_row(index);
338  }
339 
346  row_type const& get_row(size_t index) const
347  {
348  return m_pixbuf.get_row(index);
349  }
350 
354  row_type& operator[](size_t index)
355  {
356  return m_pixbuf[index];
357  }
358 
362  row_type const& operator[](size_t index) const
363  {
364  return m_pixbuf[index];
365  }
366 
370  pixel get_pixel(size_t x, size_t y) const
371  {
372  return m_pixbuf.get_pixel(x, y);
373  }
374 
378  void set_pixel(size_t x, size_t y, pixel p)
379  {
380  m_pixbuf.set_pixel(x, y, p);
381  }
382 
384  {
385  return m_info.get_interlace_type();
386  }
387 
389  {
390  m_info.set_interlace_type(interlace);
391  }
392 
394  {
395  return m_info.get_compression_type();
396  }
397 
399  {
400  m_info.set_compression_type(compression);
401  }
402 
404  {
405  return m_info.get_filter_type();
406  }
407 
409  {
410  m_info.set_filter_type(filter);
411  }
412 
417  {
418  return m_info.get_palette();
419  }
420 
424  palette const& get_palette() const
425  {
426  return m_info.get_palette();
427  }
428 
432  void set_palette(palette const& plte)
433  {
434  m_info.set_palette(plte);
435  }
436 
437  tRNS const& get_tRNS() const
438  {
439  return m_info.get_tRNS();
440  }
441 
443  {
444  return m_info.get_tRNS();
445  }
446 
447  void set_tRNS(tRNS const& trns)
448  {
449  m_info.set_tRNS(trns);
450  }
451 
452  protected:
457  template< typename base_impl >
459  : public base_impl
460  {
461  public:
462  streaming_impl(image_info& info, pixbuf& pixels)
463  : base_impl(info),
464  m_pixbuf(pixels)
465  {
466  }
467 
472  byte* get_next_row(size_t pos)
473  {
474  typedef typename pixbuf::row_traits row_traits;
475  return reinterpret_cast< byte* >
476  (row_traits::get_data(m_pixbuf.get_row(pos)));
477  }
478 
479  protected:
480  pixbuf& m_pixbuf;
481  };
482 
487  : public streaming_impl< consumer< pixel,
488  pixel_consumer,
489  image_info_ref_holder,
490  /* interlacing = */ true > >
491  {
492  public:
493  pixel_consumer(image_info& info, pixbuf& pixels)
494  : streaming_impl< consumer< pixel,
497  true > >(info, pixels)
498  {
499  }
500 
501  void reset(size_t pass)
502  {
503  if (pass == 0)
504  {
505  this->m_pixbuf.resize(this->get_info().get_width(),
506  this->get_info().get_height());
507  }
508  }
509  };
510 
515  : public streaming_impl< generator< pixel,
516  pixel_generator,
517  image_info_ref_holder,
518  /* interlacing = */ true > >
519  {
520  public:
521  pixel_generator(image_info& info, pixbuf& pixels)
522  : streaming_impl< generator< pixel,
525  true > >(info, pixels)
526  {
527  }
528  };
529 
531  pixbuf m_pixbuf;
532  };
533 
534 } // namespace png
535 
536 #endif // PNGPP_IMAGE_HPP_INCLUDED
The pixel row traits class template. Provides a common way to get starting address of the row for pac...
Definition: pixel_buffer.hpp:53
image_info m_info
Definition: image.hpp:530
void write(ostream &stream)
Writes an image to the stream.
Definition: generator.hpp:129
void set_pixel(size_t x, size_t y, pixel p)
Replaces a pixel at (x,y) position.
Definition: pixel_buffer.hpp:178
void set_tRNS(tRNS const &trns)
Definition: image.hpp:447
filter_type get_filter_type() const
Definition: image.hpp:403
image(std::string const &filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:115
void read_stream(istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:237
image(size_t width, size_t height)
Constructs an empty image of specified width and height.
Definition: image.hpp:95
row_type & get_row(size_t index)
Returns a reference to the row of image data at specified index.
Definition: image.hpp:335
interlace_type
Definition: types.hpp:79
std::vector< byte > tRNS
The palette transparency map type. Currently implemented as std::vector of png::byte.
Definition: tRNS.hpp:44
void set_palette(palette const &plte)
Replaces the image palette.
Definition: image.hpp:432
void read(std::istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:217
Base class for PNG reader/writer classes.
Definition: io_base.hpp:62
byte * get_next_row(size_t pos)
Returns the starting address of a pos-th row in the image's pixel buffer.
Definition: image.hpp:472
compression_type get_compression_type() const
Definition: image_info.hpp:116
void write(char const *filename)
Writes an image to specified file.
Definition: image.hpp:254
void set_palette(palette const &plte)
Definition: image_info.hpp:146
Definition: pixel_buffer.hpp:211
size_t get_height() const
Definition: pixel_buffer.hpp:94
size_t get_width() const
Definition: pixel_buffer.hpp:89
pixel_buffer< pixel > pixbuf
The pixel buffer type for pixel.
Definition: image.hpp:63
interlace_type get_interlace_type() const
Definition: image_info.hpp:106
void set_filter_type(filter_type filter)
Definition: image_info.hpp:131
interlace_type get_interlace_type() const
Definition: image.hpp:383
filter_type
Definition: types.hpp:91
void set_interlace_type(interlace_type interlace)
Definition: image_info.hpp:111
std::vector< color > palette
The palette type. Currently implemented as std::vector of png::color.
Definition: palette.hpp:44
Pixel traits class template.
Definition: pixel_traits.hpp:48
image(std::string const &filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:105
Holds information about PNG image.
Definition: image_info.hpp:47
pixbuf & m_pixbuf
Definition: image.hpp:480
row_type & get_row(size_t index)
Returns a reference to the row of image data at specified index.
Definition: pixel_buffer.hpp:126
pixbuf const & get_pixbuf() const
Returns a const reference to image pixel buffer.
Definition: image.hpp:294
palette & get_palette()
Returns a reference to the image palette.
Definition: image.hpp:416
palette const & get_palette() const
Definition: image_info.hpp:136
row_type const & get_row(size_t index) const
Returns a const reference to the row of image data at specified index.
Definition: image.hpp:346
void set_width(size_t width)
Definition: image_info.hpp:71
void set_pixel(size_t x, size_t y, pixel p)
Replaces a pixel at (x,y) position.
Definition: image.hpp:378
void set_interlace_type(interlace_type interlace)
Definition: image.hpp:388
image(char const *filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:125
The default io transformation: does nothing.
Definition: image.hpp:79
compression_type
Definition: types.hpp:85
An image_info holder class. Stores a reference to the image_info object. The image_info object itself...
Definition: streaming_base.hpp:67
pixbuf m_pixbuf
Definition: image.hpp:531
void write(std::string const &filename)
Writes an image to specified file.
Definition: image.hpp:246
size_t get_height() const
Definition: image.hpp:314
palette const & get_palette() const
Returns a const reference to the image palette.
Definition: image.hpp:424
tRNS & get_tRNS()
Definition: image.hpp:442
pixel_generator(image_info &info, pixbuf &pixels)
Definition: image.hpp:521
void read_stream(istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:227
Class template to represent PNG image.
Definition: image.hpp:52
A common base class template for pixel_consumer and pixel_generator classes.
Definition: image.hpp:458
png_byte byte
Definition: types.hpp:39
void reset(size_t pass)
Definition: image.hpp:501
filter_type get_filter_type() const
Definition: image_info.hpp:126
Pixel consumer class template.
Definition: consumer.hpp:125
tRNS const & get_tRNS() const
Definition: image.hpp:437
size_t get_width() const
Definition: image.hpp:309
void set_compression_type(compression_type compression)
Definition: image.hpp:398
void set_compression_type(compression_type compression)
Definition: image_info.hpp:121
std::vector< pixel > row_type
A row of pixel data.
Definition: pixel_buffer.hpp:67
pixel_traits< pixel > traits
The pixel traits type for pixel.
Definition: image.hpp:58
pixbuf & get_pixbuf()
Returns a reference to image pixel buffer.
Definition: image.hpp:286
pixbuf::row_type row_type
Represents a row of image pixel data.
Definition: image.hpp:68
void read(std::string const &filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:173
pixel get_pixel(size_t x, size_t y) const
Returns a pixel at (x,y) position.
Definition: pixel_buffer.hpp:170
pixel_consumer(image_info &info, pixbuf &pixels)
Definition: image.hpp:493
void read(char const *filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:182
image_info const & get_info() const
Definition: streaming_base.hpp:107
void set_height(size_t height)
Definition: image_info.hpp:81
row_type const & operator[](size_t index) const
The non-checking version of get_row() method.
Definition: image.hpp:362
void resize(size_t width, size_t height)
Resizes the pixel buffer.
Definition: pixel_buffer.hpp:105
void resize(size_t width, size_t height)
Resizes the image pixel buffer.
Definition: image.hpp:322
streaming_impl(image_info &info, pixbuf &pixels)
Definition: image.hpp:462
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
image(std::istream &stream)
Constructs an image reading data from a stream using default converting transform.
Definition: image.hpp:144
void set_tRNS(tRNS const &trns)
Definition: image_info.hpp:169
void set_pixbuf(pixbuf const &buffer)
Replaces the image pixel buffer.
Definition: image.hpp:304
tRNS const & get_tRNS() const
Definition: image_info.hpp:159
image_info make_image_info()
Returns an image_info object with color_type and bit_depth fields setup appropriate for the pixel typ...
Definition: image_info.hpp:192
image(char const *filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:135
Exception class to represent standard library errors (generally IO).
Definition: error.hpp:64
convert_color_space< pixel > transform_convert
A transformation functor to convert any image to appropriate color space.
Definition: image.hpp:74
image(std::istream &stream, transformation const &transform)
Constructs an image reading data from a stream using custom transformation.
Definition: image.hpp:154
IO transformation class template. Converts image color space.
Definition: convert_color_space.hpp:252
void write_stream(std::ostream &stream)
Writes an image to a stream.
Definition: image.hpp:268
void read(char const *filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:192
void read(std::istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:207
Definition: color.hpp:36
The row_traits specialization for unpacked pixel rows.
Definition: pixel_buffer.hpp:194
image()
Constructs an empty image.
Definition: image.hpp:87
void read(std::string const &filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:163
void read(istream &stream)
Reads an image from the stream using default io transformation.
Definition: consumer.hpp:144
row_type & operator[](size_t index)
The non-checking version of get_row() method.
Definition: image.hpp:354
void set_filter_type(filter_type filter)
Definition: image.hpp:408
void operator()(io_base &) const
Definition: image.hpp:81
compression_type get_compression_type() const
Definition: image.hpp:393
Pixel generator class template.
Definition: generator.hpp:116
The pixel buffer adapter for reading pixel data.
Definition: image.hpp:486
void write_stream(ostream &stream)
Writes an image to a stream.
Definition: image.hpp:277
pixel get_pixel(size_t x, size_t y) const
Returns a pixel at (x,y) position.
Definition: image.hpp:370
The pixel buffer adapter for writing pixel data.
Definition: image.hpp:514