png++ 0.2.10
solid_pixel_buffer.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_SOLID_PIXEL_BUFFER_HPP_INCLUDED
32#define PNGPP_SOLID_PIXEL_BUFFER_HPP_INCLUDED
33
34#include <cassert>
35#include <cstddef>
36#include <climits>
37#include <stdexcept>
38#include <vector>
39
40#include "config.hpp"
41#include "packed_pixel.hpp"
42#include "gray_pixel.hpp"
43#include "index_pixel.hpp"
44
45namespace png
46{
47
53 template< typename pixel >
55 {
56 public:
59 {
60 typedef pixel* row_access;
61 typedef const pixel* row_const_access;
62
63 static byte* get_data(row_access row)
64 {
65 return reinterpret_cast<byte*>(row);
66 }
67 };
68
69
76
81 : m_width(0),
82 m_height(0),
83 m_stride(0)
84 {
85 }
86
91 : m_width(0),
92 m_height(0),
93 m_stride(0)
94 {
95 resize(width, height);
96 }
97
99 {
100 return m_width;
101 }
102
104 {
105 return m_height;
106 }
107
114 void resize(uint_32 width, uint_32 height)
115 {
116 m_width = width;
117 m_height = height;
119 m_bytes.resize(height * m_stride);
120 }
121
130 row_access get_row(size_t index)
131 {
132 return reinterpret_cast<row_access>(&m_bytes.at(index * m_stride));
133 }
134
141 row_const_access get_row(size_t index) const
142 {
143 return (row_const_access)(&m_bytes.at(index * m_stride));
144 }
145
149 row_access operator[](size_t index)
150 {
151 return (row_access)(&m_bytes[index * m_stride]);
152 }
153
157 row_const_access operator[](size_t index) const
158 {
159 return (row_const_access)(&m_bytes[index * m_stride]);
160 }
161
165 void put_row(size_t index, row_const_access r)
166 {
167 row_access row = get_row(index);
168 for (uint_32 i = 0; i < m_width; ++i)
169 *row++ = *r++;
170 }
171
175 pixel get_pixel(size_t x, size_t y) const
176 {
177 size_t index = (y * m_width + x) * bytes_per_pixel;
178 return *reinterpret_cast< const pixel* >(&m_bytes.at(index));
179 }
180
184 void set_pixel(size_t x, size_t y, pixel p)
185 {
186 size_t index = (y * m_width + x) * bytes_per_pixel;
187 *reinterpret_cast< pixel* >(&m_bytes.at(index)) = p;
188 }
189
193 const std::vector< byte >& get_bytes() const
194 {
195 return m_bytes;
196 }
197
198#ifdef PNGPP_HAS_STD_MOVE
202 std::vector< byte > fetch_bytes()
203 {
204 m_width = 0;
205 m_height = 0;
206 m_stride = 0;
207
208 // the buffer is moved outside without copying and leave m_bytes empty.
209 return std::move(m_bytes);
210 }
211#endif
212
213 protected:
214 static const size_t bytes_per_pixel = pixel_traits_t::channels *
215 pixel_traits_t::bit_depth / CHAR_BIT;
216
217 protected:
220 size_t m_stride;
221 std::vector< byte > m_bytes;
222
223#ifdef PNGPP_HAS_STATIC_ASSERT
224 static_assert(pixel_traits_t::bit_depth % CHAR_BIT == 0,
225 "Bit_depth should consist of integer number of bytes");
226
227 static_assert(sizeof(pixel) * CHAR_BIT ==
228 pixel_traits_t::channels * pixel_traits_t::bit_depth,
229 "pixel type should contain channels data only");
230#endif
231 };
232
238 template< int bits >
239 class solid_pixel_buffer< packed_pixel< bits > >;
240
241} // namespace png
242
243#endif // PNGPP_solid_pixel_buffer_HPP_INCLUDED
The packed pixel class template.
Definition packed_pixel.hpp:56
Pixel buffer, that stores pixels as continuous memory chunk. solid_pixel_buffer is useful when user w...
Definition solid_pixel_buffer.hpp:55
static const size_t bytes_per_pixel
Definition solid_pixel_buffer.hpp:214
row_const_access get_row(size_t index) const
Returns a const reference to the row of image data at specified index.
Definition solid_pixel_buffer.hpp:141
row_traits::row_const_access row_const_access
Definition solid_pixel_buffer.hpp:74
void set_pixel(size_t x, size_t y, pixel p)
Replaces a pixel at (x,y) position.
Definition solid_pixel_buffer.hpp:184
row_traits::row_access row_access
A row of pixel data.
Definition solid_pixel_buffer.hpp:73
uint_32 get_width() const
Definition solid_pixel_buffer.hpp:98
uint_32 m_height
Definition solid_pixel_buffer.hpp:219
size_t m_stride
Definition solid_pixel_buffer.hpp:220
row_access operator[](size_t index)
The non-checking version of get_row() method.
Definition solid_pixel_buffer.hpp:149
solid_pixel_buffer(uint_32 width, uint_32 height)
Constructs an empty pixel buffer object.
Definition solid_pixel_buffer.hpp:90
std::vector< byte > fetch_bytes()
Moves the buffer to client code (c++11 only) .
Definition solid_pixel_buffer.hpp:202
row_access row_type
Definition solid_pixel_buffer.hpp:75
pixel_traits< pixel > pixel_traits_t
Definition solid_pixel_buffer.hpp:57
uint_32 m_width
Definition solid_pixel_buffer.hpp:218
uint_32 get_height() const
Definition solid_pixel_buffer.hpp:103
const std::vector< byte > & get_bytes() const
Provides easy constant read access to underlying byte-buffer.
Definition solid_pixel_buffer.hpp:193
pixel get_pixel(size_t x, size_t y) const
Returns a pixel at (x,y) position.
Definition solid_pixel_buffer.hpp:175
solid_pixel_buffer()
Constructs an empty 0x0 pixel buffer object.
Definition solid_pixel_buffer.hpp:80
void put_row(size_t index, row_const_access r)
Replaces the row at specified index.
Definition solid_pixel_buffer.hpp:165
row_access get_row(size_t index)
Returns a reference to the row of image data at specified index.
Definition solid_pixel_buffer.hpp:130
row_const_access operator[](size_t index) const
The non-checking version of get_row() method.
Definition solid_pixel_buffer.hpp:157
std::vector< byte > m_bytes
Definition solid_pixel_buffer.hpp:221
void resize(uint_32 width, uint_32 height)
Resizes the pixel buffer.
Definition solid_pixel_buffer.hpp:114
Definition color.hpp:37
png_uint_32 uint_32
Definition types.hpp:41
Pixel traits class template.
Definition pixel_traits.hpp:48
Definition solid_pixel_buffer.hpp:59
static byte * get_data(row_access row)
Definition solid_pixel_buffer.hpp:63
const pixel * row_const_access
Definition solid_pixel_buffer.hpp:61
pixel * row_access
Definition solid_pixel_buffer.hpp:60