png++  0.2.7
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 
61  template< typename pixel, typename pixel_buffer_type = pixel_buffer< pixel > >
62  class image
63  {
64  public:
69 
73  typedef pixel_buffer_type pixbuf;
74 
78  typedef typename pixbuf::row_type row_type;
79  typedef typename pixbuf::row_access row_access;
80  typedef typename pixbuf::row_const_access row_const_access;
81 
87 
92  {
93  void operator()(io_base&) const {}
94  };
95 
100  : m_info(make_image_info< pixel >())
101  {
102  }
103 
107  image(size_t width, size_t height)
108  : m_info(make_image_info< pixel >())
109  {
110  resize(width, height);
111  }
112 
117  explicit image(std::string const& filename)
118  {
119  read(filename, transform_convert());
120  }
121 
126  template< class transformation >
127  image(std::string const& filename,
128  transformation const& transform)
129  {
130  read(filename.c_str(), transform);
131  }
132 
137  explicit image(char const* filename)
138  {
139  read(filename, transform_convert());
140  }
141 
146  template< class transformation >
147  image(char const* filename, transformation const& transform)
148  {
149  read(filename, transform);
150  }
151 
156  explicit image(std::istream& stream)
157  {
158  read_stream(stream, transform_convert());
159  }
160 
165  template< class transformation >
166  image(std::istream& stream, transformation const& transform)
167  {
168  read_stream(stream, transform);
169  }
170 
175  void read(std::string const& filename)
176  {
177  read(filename, transform_convert());
178  }
179 
184  template< class transformation >
185  void read(std::string const& filename, transformation const& transform)
186  {
187  read(filename.c_str(), transform);
188  }
189 
194  void read(char const* filename)
195  {
196  read(filename, transform_convert());
197  }
198 
203  template< class transformation >
204  void read(char const* filename, transformation const& transform)
205  {
206  std::ifstream stream(filename, std::ios::binary);
207  if (!stream.is_open())
208  {
209  throw std_error(filename);
210  }
211  stream.exceptions(std::ios::badbit);
212  read_stream(stream, transform);
213  }
214 
219  void read(std::istream& stream)
220  {
221  read_stream(stream, transform_convert());
222  }
223 
228  template< class transformation >
229  void read(std::istream& stream, transformation const& transform)
230  {
231  read_stream(stream, transform);
232  }
233 
238  template< class istream >
239  void read_stream(istream& stream)
240  {
241  read_stream(stream, transform_convert());
242  }
243 
248  template< class istream, class transformation >
249  void read_stream(istream& stream, transformation const& transform)
250  {
251  pixel_consumer pixcon(m_info, m_pixbuf);
252  pixcon.read(stream, transform);
253  }
254 
258  void write(std::string const& filename)
259  {
260  write(filename.c_str());
261  }
262 
266  void write(char const* filename)
267  {
268  std::ofstream stream(filename, std::ios::binary);
269  if (!stream.is_open())
270  {
271  throw std_error(filename);
272  }
273  stream.exceptions(std::ios::badbit);
274  write_stream(stream);
275  }
276 
280  template< class ostream >
281  void write_stream(ostream& stream)
282  {
284  pixgen.write(stream);
285  }
286 
290  pixbuf& get_pixbuf()
291  {
292  return m_pixbuf;
293  }
294 
298  pixbuf const& get_pixbuf() const
299  {
300  return m_pixbuf;
301  }
302 
308  void set_pixbuf(pixbuf const& buffer)
309  {
310  m_pixbuf = buffer;
311  }
312 
313  size_t get_width() const
314  {
315  return m_pixbuf.get_width();
316  }
317 
318  size_t get_height() const
319  {
320  return m_pixbuf.get_height();
321  }
322 
326  void resize(size_t width, size_t height)
327  {
328  m_pixbuf.resize(width, height);
329  m_info.set_width(width);
330  m_info.set_height(height);
331  }
332 
339  row_access get_row(size_t index)
340  {
341  return m_pixbuf.get_row(index);
342  }
343 
350  row_const_access get_row(size_t index) const
351  {
352  return m_pixbuf.get_row(index);
353  }
354 
358  row_access operator[](size_t index)
359  {
360  return m_pixbuf[index];
361  }
362 
366  row_const_access operator[](size_t index) const
367  {
368  return m_pixbuf[index];
369  }
370 
374  pixel get_pixel(size_t x, size_t y) const
375  {
376  return m_pixbuf.get_pixel(x, y);
377  }
378 
382  void set_pixel(size_t x, size_t y, pixel p)
383  {
384  m_pixbuf.set_pixel(x, y, p);
385  }
386 
388  {
389  return m_info.get_interlace_type();
390  }
391 
393  {
394  m_info.set_interlace_type(interlace);
395  }
396 
398  {
399  return m_info.get_compression_type();
400  }
401 
403  {
404  m_info.set_compression_type(compression);
405  }
406 
408  {
409  return m_info.get_filter_type();
410  }
411 
413  {
414  m_info.set_filter_type(filter);
415  }
416 
421  {
422  return m_info.get_palette();
423  }
424 
428  palette const& get_palette() const
429  {
430  return m_info.get_palette();
431  }
432 
436  void set_palette(palette const& plte)
437  {
438  m_info.set_palette(plte);
439  }
440 
441  tRNS const& get_tRNS() const
442  {
443  return m_info.get_tRNS();
444  }
445 
447  {
448  return m_info.get_tRNS();
449  }
450 
451  void set_tRNS(tRNS const& trns)
452  {
453  m_info.set_tRNS(trns);
454  }
455 
456  protected:
461  template< typename base_impl >
463  : public base_impl
464  {
465  public:
466  streaming_impl(image_info& info, pixbuf& pixels)
467  : base_impl(info),
468  m_pixbuf(pixels)
469  {
470  }
471 
476  byte* get_next_row(size_t pos)
477  {
478  typedef typename pixbuf::row_traits row_traits;
479  return reinterpret_cast< byte* >
480  (row_traits::get_data(m_pixbuf.get_row(pos)));
481  }
482 
483  protected:
484  pixbuf& m_pixbuf;
485  };
486 
491  : public streaming_impl< consumer< pixel,
492  pixel_consumer,
493  image_info_ref_holder,
494  /* interlacing = */ true > >
495  {
496  public:
497  pixel_consumer(image_info& info, pixbuf& pixels)
498  : streaming_impl< consumer< pixel,
501  true > >(info, pixels)
502  {
503  }
504 
505  void reset(size_t pass)
506  {
507  if (pass == 0)
508  {
509  this->m_pixbuf.resize(this->get_info().get_width(),
510  this->get_info().get_height());
511  }
512  }
513  };
514 
519  : public streaming_impl< generator< pixel,
520  pixel_generator,
521  image_info_ref_holder,
522  /* interlacing = */ true > >
523  {
524  public:
525  pixel_generator(image_info& info, pixbuf& pixels)
526  : streaming_impl< generator< pixel,
529  true > >(info, pixels)
530  {
531  }
532  };
533 
535  pixbuf m_pixbuf;
536  };
537 
538 } // namespace png
539 
540 #endif // PNGPP_IMAGE_HPP_INCLUDED
filter_type get_filter_type() const
Definition: image.hpp:407
void read_stream(istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:249
convert_color_space< pixel > transform_convert
A transformation functor to convert any image to appropriate color space.
Definition: image.hpp:86
void write(ostream &stream)
Writes an image to the stream.
Definition: generator.hpp:129
pixbuf::row_type row_type
Represents a row of image pixel data.
Definition: image.hpp:78
pixel get_pixel(size_t x, size_t y) const
Returns a pixel at (x,y) position.
Definition: image.hpp:374
tRNS const & get_tRNS() const
Definition: image.hpp:441
pixel_generator(image_info &info, pixbuf &pixels)
Definition: image.hpp:525
pixbuf::row_const_access row_const_access
Definition: image.hpp:80
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:476
pixbuf & m_pixbuf
Definition: image.hpp:484
pixbuf const & get_pixbuf() const
Returns a const reference to image pixel buffer.
Definition: image.hpp:298
void resize(size_t width, size_t height)
Resizes the image pixel buffer.
Definition: image.hpp:326
interlace_type
Definition: types.hpp:79
image()
Constructs an empty image.
Definition: image.hpp:99
std::vector< byte > tRNS
The palette transparency map type. Currently implemented as std::vector of png::byte.
Definition: tRNS.hpp:44
pixbuf::row_access row_access
Definition: image.hpp:79
Base class for PNG reader/writer classes.
Definition: io_base.hpp:62
compression_type get_compression_type() const
Definition: image_info.hpp:116
void set_palette(palette const &plte)
Definition: image_info.hpp:146
void read(char const *filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:194
void set_tRNS(tRNS const &trns)
Definition: image.hpp:451
interlace_type get_interlace_type() const
Definition: image_info.hpp:106
void set_filter_type(filter_type filter)
Definition: image_info.hpp:131
image(std::istream &stream, transformation const &transform)
Constructs an image reading data from a stream using custom transformation.
Definition: image.hpp:166
filter_type
Definition: types.hpp:91
void set_interlace_type(interlace_type interlace)
Definition: image_info.hpp:111
void set_palette(palette const &plte)
Replaces the image palette.
Definition: image.hpp:436
std::vector< color > palette
The palette type. Currently implemented as std::vector of png::color.
Definition: palette.hpp:44
void write(std::string const &filename)
Writes an image to specified file.
Definition: image.hpp:258
Pixel traits class template.
Definition: pixel_traits.hpp:48
void read(char const *filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:204
size_t get_width() const
Definition: image.hpp:313
void read(std::istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:219
void read(std::string const &filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:185
Holds information about PNG image.
Definition: image_info.hpp:47
void reset(size_t pass)
Definition: image.hpp:505
void write(char const *filename)
Writes an image to specified file.
Definition: image.hpp:266
row_const_access operator[](size_t index) const
The non-checking version of get_row() method.
Definition: image.hpp:366
palette const & get_palette() const
Definition: image_info.hpp:136
image(std::istream &stream)
Constructs an image reading data from a stream using default converting transform.
Definition: image.hpp:156
void set_width(size_t width)
Definition: image_info.hpp:71
image(std::string const &filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:127
The default io transformation: does nothing.
Definition: image.hpp:91
image(size_t width, size_t height)
Constructs an empty image of specified width and height.
Definition: image.hpp:107
interlace_type get_interlace_type() const
Definition: image.hpp:387
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
Class template to represent PNG image.
Definition: image.hpp:62
A common base class template for pixel_consumer and pixel_generator classes.
Definition: image.hpp:462
png_byte byte
Definition: types.hpp:39
image(std::string const &filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:117
filter_type get_filter_type() const
Definition: image_info.hpp:126
Pixel consumer class template.
Definition: consumer.hpp:125
pixbuf m_pixbuf
Definition: image.hpp:535
void operator()(io_base &) const
Definition: image.hpp:93
image(char const *filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:147
void set_compression_type(compression_type compression)
Definition: image_info.hpp:121
void set_pixbuf(pixbuf const &buffer)
Replaces the image pixel buffer.
Definition: image.hpp:308
palette const & get_palette() const
Returns a const reference to the image palette.
Definition: image.hpp:428
compression_type get_compression_type() const
Definition: image.hpp:397
image(char const *filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:137
void set_compression_type(compression_type compression)
Definition: image.hpp:402
pixel_traits< pixel > traits
The pixel traits type for pixel.
Definition: image.hpp:68
row_const_access get_row(size_t index) const
Returns a const reference to the row of image data at specified index.
Definition: image.hpp:350
row_access get_row(size_t index)
Returns a reference to the row of image data at specified index.
Definition: image.hpp:339
image_info const & get_info() const
Definition: streaming_base.hpp:107
void set_height(size_t height)
Definition: image_info.hpp:81
tRNS & get_tRNS()
Definition: image.hpp:446
row_access operator[](size_t index)
The non-checking version of get_row() method.
Definition: image.hpp:358
The pixel row traits class template. Provides a common way to get starting address of the row for pac...
Definition: pixel_buffer.hpp:53
palette & get_palette()
Returns a reference to the image palette.
Definition: image.hpp:420
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
void set_tRNS(tRNS const &trns)
Definition: image_info.hpp:169
tRNS const & get_tRNS() const
Definition: image_info.hpp:159
pixbuf & get_pixbuf()
Returns a reference to image pixel buffer.
Definition: image.hpp:290
void set_filter_type(filter_type filter)
Definition: image.hpp:412
void write_stream(ostream &stream)
Writes an image to a stream.
Definition: image.hpp:281
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
streaming_impl(image_info &info, pixbuf &pixels)
Definition: image.hpp:466
Exception class to represent standard library errors (generally IO).
Definition: error.hpp:66
IO transformation class template. Converts image color space.
Definition: convert_color_space.hpp:256
pixel_buffer_type pixbuf
The pixel buffer type for pixel.
Definition: image.hpp:73
void read_stream(istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:239
Definition: color.hpp:36
size_t get_height() const
Definition: image.hpp:318
void read(istream &stream)
Reads an image from the stream using default io transformation.
Definition: consumer.hpp:144
void set_pixel(size_t x, size_t y, pixel p)
Replaces a pixel at (x,y) position.
Definition: image.hpp:382
void set_interlace_type(interlace_type interlace)
Definition: image.hpp:392
pixel_consumer(image_info &info, pixbuf &pixels)
Definition: image.hpp:497
void read(std::string const &filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:175
image_info m_info
Definition: image.hpp:534
Pixel generator class template.
Definition: generator.hpp:116
The pixel buffer adapter for reading pixel data.
Definition: image.hpp:490
void read(std::istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:229
The pixel buffer adapter for writing pixel data.
Definition: image.hpp:518