9 #include <utils/ColorRgb.h> 12 template <
typename Pixel_T>
17 typedef Pixel_T pixel_type;
25 _pixels(new Pixel_T[2]),
26 _endOfPixels(_pixels + 1)
28 memset(_pixels, 0, 2*
sizeof(Pixel_T));
40 _pixels(new Pixel_T[width * height + 1]),
41 _endOfPixels(_pixels + width * height)
43 memset(_pixels, 0, (_width*_height+1)*
sizeof(Pixel_T));
56 _pixels(new Pixel_T[width * height + 1]),
57 _endOfPixels(_pixels + width * height)
59 std::fill(_pixels, _endOfPixels, background);
67 _height(other._height),
68 _pixels(new Pixel_T[other._width * other._height + 1]),
69 _endOfPixels(_pixels + other._width * other._height)
71 memcpy(_pixels, other._pixels, (
long) other._width * other._height *
sizeof(Pixel_T));
82 void swap(
Image& s) noexcept
85 swap(this->_width, s._width);
86 swap(this->_height, s._height);
87 swap(this->_pixels, s._pixels);
88 swap(this->_endOfPixels, s._endOfPixels);
134 uint8_t red(
const unsigned pixel)
const 136 return (_pixels + pixel)->red;
139 uint8_t green(
const unsigned pixel)
const 141 return (_pixels + pixel)->green;
144 uint8_t blue(
const unsigned pixel)
const 146 return (_pixels + pixel)->blue;
157 const Pixel_T&
operator()(
const unsigned x,
const unsigned y)
const 159 return _pixels[toIndex(x,y)];
172 return _pixels[toIndex(x,y)];
180 if ((width*height) >
unsigned((_endOfPixels-_pixels)))
183 _pixels =
new Pixel_T[width*height + 1];
184 _endOfPixels = _pixels + width*
height;
198 assert(other._width == _width);
199 assert(other._height == _height);
201 memcpy(_pixels, other._pixels, _width*_height*
sizeof(Pixel_T));
230 image.
resize(_width, _height);
231 const unsigned imageSize = _width * _height;
233 for (
unsigned idx=0; idx<imageSize; idx++)
235 const Pixel_T color =
memptr()[idx];
245 return (ssize_t) _width * _height *
sizeof(Pixel_T);
258 inline unsigned toIndex(
const unsigned x,
const unsigned y)
const 273 Pixel_T* _endOfPixels;
const Pixel_T & operator()(const unsigned x, const unsigned y) const
Returns a const reference to a specified pixel in the image.
Definition: Image.h:157
unsigned height() const
Returns the height of the image.
Definition: Image.h:129
Image(const unsigned width, const unsigned height, const Pixel_T background)
Constructor for an image with specified width and height.
Definition: Image.h:53
unsigned width() const
Returns the width of the image.
Definition: Image.h:119
ssize_t size() const
get size of buffer
Definition: Image.h:243
Pixel_T * memptr()
Returns a memory pointer to the first pixel in the image.
Definition: Image.h:208
Image()
Default constructor for an image.
Definition: Image.h:22
Image(const unsigned width, const unsigned height)
Constructor for an image with specified width and height.
Definition: Image.h:37
~Image()
Destructor.
Definition: Image.h:109
void resize(const unsigned width, const unsigned height)
Resize the image.
Definition: Image.h:178
void copy(const Image< Pixel_T > &other)
Copies another image into this image.
Definition: Image.h:196
Pixel_T & operator()(const unsigned x, const unsigned y)
Returns a reference to a specified pixel in the image.
Definition: Image.h:170
Image(const Image &other)
Copy constructor for an image.
Definition: Image.h:65
void toRgb(Image< ColorRgb > &image)
Convert image of any color order to a RGB image.
Definition: Image.h:228
const Pixel_T * memptr() const
Returns a const memory pointer to the first pixel in the image.
Definition: Image.h:217
uint8_t red
The red color channel.
Definition: ColorRgb.h:16
Plain-Old-Data structure containing the red-green-blue color specification.
Definition: ColorRgb.h:13