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:
58  typedef pixel_traits< pixel > traits;
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 
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:
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:
481  };
482 
487  : public streaming_impl< consumer< pixel,
488  pixel_consumer,
489  image_info_ref_holder,
490  /* interlacing = */ true > >
491  {
492  public:
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:
522  : streaming_impl< generator< pixel,
525  true > >(info, pixels)
526  {
527  }
528  };
529 
532  };
533 
534 } // namespace png
535 
536 #endif // PNGPP_IMAGE_HPP_INCLUDED