1 #ifndef INCLUDE_AL_IMAGE_HPP
2 #define INCLUDE_AL_IMAGE_HPP
67 unsigned int mWidth = 0, mHeight = 0;
68 std::vector<uint8_t> mArray;
69 std::string mFilename =
"";
70 int mCompression = 50;
80 Image(
const std::string &filename);
88 bool load(
const std::string &filePath);
96 bool save(
const std::string &filePath);
99 const std::string &
filepath()
const {
return mFilename; }
105 std::vector<uint8_t> &
array() {
return mArray; }
108 const std::vector<uint8_t> &
array()
const {
return mArray; }
111 template <
typename T = RGBAPix> T *
pixels() {
return (T *)(mArray.data()); }
114 template <
typename T = RGBAPix>
const T *
pixels()
const {
115 return (
const T *)(mArray.data());
129 int height,
bool flipVertically =
false,
130 int numComponents = 3);
140 unsigned width()
const {
return mWidth; }
143 unsigned height()
const {
return mHeight; }
153 mCompression = flags;
161 template <
typename Pix = RGBAPix>
162 const Pix &
at(
unsigned x,
unsigned y)
const {
163 auto pixel_index = x + y *
width();
164 return *
reinterpret_cast<const RGBAPix *
>(mArray.data() + 4 * pixel_index);
172 template <
typename Pix = RGBAPix> Pix &
at(
unsigned x,
unsigned y) {
173 auto pixel_index = x + y *
width();
174 return *
reinterpret_cast<RGBAPix *
>(mArray.data() + 4 * pixel_index);
182 template <
typename Pix = RGBAPix>
183 void write(
const Pix &pix,
unsigned x,
unsigned y) {
184 auto pixel_index = x + y *
width();
185 mArray[4 * pixel_index + 0] = pix.r;
186 mArray[4 * pixel_index + 1] = pix.g;
187 mArray[4 * pixel_index + 2] = pix.b;
188 mArray[4 * pixel_index + 3] = pix.a;
196 template <
typename Pix = RGBAPix>
197 void read(Pix &pix,
unsigned x,
unsigned y)
const {
198 auto pixel_index = x + y *
width();
199 pix.r = mArray[4 * pixel_index + 0];
200 pix.g = mArray[4 * pixel_index + 1];
201 pix.b = mArray[4 * pixel_index + 2];
202 pix.a = mArray[4 * pixel_index + 3];
212 template <
typename T>
bool resize(
int dimX,
int dimY ) {
213 mArray.resize(4 * dimX * dimY);
Interface for loading image files.
void read(Pix &pix, unsigned x, unsigned y) const
Read a pixel from an Image.
Image & compression(int flags)
Set compression flags for saving.
const std::string & filepath() const
File path to image.
bool resize(int dimX, int dimY)
Resize internal pixel buffer. Erases any existing data.
T * pixels()
Get pointer to pixels.
bool save(const std::string &filePath)
Save image to disk.
std::vector< uint8_t > & array()
Get pixels as an Array.
const Pix & at(unsigned x, unsigned y) const
Get read-only reference to a pixel.
bool loaded() const
Whether image was loaded from file.
int compression() const
Get compression flags for saving.
const std::vector< uint8_t > & array() const
Get pixels as an Array (read-only)
Pix & at(unsigned x, unsigned y)
Get mutable reference to a pixel.
unsigned height() const
Get height, in pixels.
bool load(const std::string &filePath)
Load image from disk.
unsigned width() const
Get number of bytes per pixel.
const T * pixels() const
Get pointer to pixels (read-only)
static bool saveImage(std::string fileName, unsigned char *pixs, int width, int height, bool flipVertically=false, int numComponents=3)
saveImage
void write(const Pix &pix, unsigned x, unsigned y)
Write a pixel to an Image.