Allolib  1.0
C++ Components For Interactive Multimedia
al_Image.hpp
1 #ifndef INCLUDE_AL_IMAGE_HPP
2 #define INCLUDE_AL_IMAGE_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above copyright notice,
19  this list of conditions and the following disclaimer in the documentation
20  and/or other materials provided with the distribution.
21 
22  Neither the name of the University of California nor the names
23  of its contributors may be used to endorse or promote products derived from
24  this software without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 
39  File description:
40  Loads and saves images
41 
42  File author(s):
43  Graham Wakefield, 2010, grrrwaaa@gmail.com
44  Keehong Youn, 2019, younkeehong@gmail.com
45 */
46 
47 #include <cstdint>
48 #include <string>
49 #include <vector>
50 
51 namespace al {
52 
62 struct Image {
63  struct RGBAPix {
64  uint8_t r, g, b, a;
65  };
66 
67  unsigned int mWidth = 0, mHeight = 0;
68  std::vector<uint8_t> mArray;
69  std::string mFilename = "";
70  int mCompression = 50;
71  bool mLoaded = false;
72 
73  Image() = default;
74  Image(const Image &) = default;
75  Image(Image &&) = default;
76  Image &operator=(const Image &) = default;
77  Image &operator=(Image &&) = default;
78  ~Image() = default;
79 
80  Image(const std::string &filename);
81 
83 
88  bool load(const std::string &filePath);
89 
91 
96  bool save(const std::string &filePath);
97 
99  const std::string &filepath() const { return mFilename; }
100 
102  bool loaded() const { return mLoaded; }
103 
105  std::vector<uint8_t> &array() { return mArray; }
106 
108  const std::vector<uint8_t> &array() const { return mArray; }
109 
111  template <typename T = RGBAPix> T *pixels() { return (T *)(mArray.data()); }
112 
114  template <typename T = RGBAPix> const T *pixels() const {
115  return (const T *)(mArray.data());
116  }
117 
128  static bool saveImage(std::string fileName, unsigned char *pixs, int width,
129  int height, bool flipVertically = false,
130  int numComponents = 3);
131 
133  // unsigned bytesPerPixel() const { return allo_type_size(array().type()) *
134  // array().components(); }
135 
137  // Format format() const;
138 
140  unsigned width() const { return mWidth; }
141 
143  unsigned height() const { return mHeight; }
144 
146  int compression() const { return mCompression; }
147 
149 
152  Image &compression(int flags) {
153  mCompression = flags;
154  return *this;
155  }
156 
158 
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);
165  // return *array().cell<Pix>(x, y);
166  }
167 
169 
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);
175  // return *array().cell<Pix>(x, y);
176  }
177 
179 
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;
189  // array().write(&pix.r, x, y);
190  }
191 
193 
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];
203  // array().read(&pix.r, x, y);
204  }
205 
207 
212  template <typename T> bool resize(int dimX, int dimY /*, Format format */) {
213  mArray.resize(4 * dimX * dimY);
214  // mArray.formatAligned(components(format), Array::type<T>(), dimX, dimY,
215  // 1);
216  return true;
217  }
218 
219  // void sendToTexture(Texture& tex, Texture::Internal i, Texture::Format f,
220  // Texture::DataType d) {
221  // tex.create2D(width(), height(), i, f, d);
222  // tex.submit(pixels<uint8_t>(), f, Texture::UBYTE);
223  //}
224 
225  // void sendToTexture(Texture& tex) {
226  // auto to_texture_internalformat = [](Format f) {
227  // switch (f) {
228  // case LUMINANCE: return Texture::R8;
229  // case LUMALPHA: return Texture::RG8;
230  // case RGB: return Texture::RGB8;
231  // case RGBA: return Texture::RGBA8;
232  // default: return Texture::RGBA8;
233  // }
234  // };
235  // auto to_texture_format = [](Format f) {
236  // switch (f) {
237  // case LUMINANCE: return Texture::RED;
238  // case LUMALPHA: return Texture::RG;
239  // case RGB: return Texture::RGB;
240  // case RGBA: return Texture::RGBA;
241  // default: return Texture::RGBA;
242  // }
243  // };
244  // sendToTexture(tex, to_texture_internalformat(format()),
245  // to_texture_format(format()), Texture::UBYTE);
246  //}
247 
249  // static int components(Format v);
250 
251  // static Format getFormat(int planes);
252 
253  // class Impl {
254  // public:
255  // virtual ~Impl() {};
256  // virtual bool load(const std::string& filename, Array& lat) = 0;
257  // virtual bool save(const std::string& filename, const Array& lat, int
258  // compressFlags) = 0;
259  //};
260 };
261 
262 } // namespace al
263 #endif
Definition: al_App.hpp:23
Interface for loading image files.
Definition: al_Image.hpp:62
void read(Pix &pix, unsigned x, unsigned y) const
Read a pixel from an Image.
Definition: al_Image.hpp:197
Image & compression(int flags)
Set compression flags for saving.
Definition: al_Image.hpp:152
const std::string & filepath() const
File path to image.
Definition: al_Image.hpp:99
bool resize(int dimX, int dimY)
Resize internal pixel buffer. Erases any existing data.
Definition: al_Image.hpp:212
T * pixels()
Get pointer to pixels.
Definition: al_Image.hpp:111
bool save(const std::string &filePath)
Save image to disk.
std::vector< uint8_t > & array()
Get pixels as an Array.
Definition: al_Image.hpp:105
const Pix & at(unsigned x, unsigned y) const
Get read-only reference to a pixel.
Definition: al_Image.hpp:162
bool loaded() const
Whether image was loaded from file.
Definition: al_Image.hpp:102
int compression() const
Get compression flags for saving.
Definition: al_Image.hpp:146
const std::vector< uint8_t > & array() const
Get pixels as an Array (read-only)
Definition: al_Image.hpp:108
Pix & at(unsigned x, unsigned y)
Get mutable reference to a pixel.
Definition: al_Image.hpp:172
unsigned height() const
Get height, in pixels.
Definition: al_Image.hpp:143
bool load(const std::string &filePath)
Load image from disk.
unsigned width() const
Get number of bytes per pixel.
Definition: al_Image.hpp:140
const T * pixels() const
Get pointer to pixels (read-only)
Definition: al_Image.hpp:114
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.
Definition: al_Image.hpp:183