6#include "fifechan/backends/opengl/image.hpp"
12#include "fifechan/exception.hpp"
31 unsigned int const magicPink = 0xff00ffff;
33 unsigned int const magicPink = 0xffff00ff;
38 unsigned int c = pixels[
static_cast<size_t>(x + (y *
mWidth))];
57 Image::Image(GLuint textureHandle,
int width,
int height,
bool autoFree) :
134 auto const r =
static_cast<unsigned char>((c >> 24) & 0xff);
135 auto const g =
static_cast<unsigned char>((c >> 16) & 0xff);
136 auto const b =
static_cast<unsigned char>((c >> 8) & 0xff);
137 auto const a =
static_cast<unsigned char>(c & 0xff);
139 auto const a =
static_cast<unsigned char>((c >> 24) & 0xff);
140 auto const b =
static_cast<unsigned char>((c >> 16) & 0xff);
141 auto const g =
static_cast<unsigned char>((c >> 8) & 0xff);
142 auto const r =
static_cast<unsigned char>(c & 0xff);
159 unsigned int const c = color.
a | color.
b << 8 | color.
g << 16 | color.
r << 24;
161 unsigned int const c = color.
r | color.
g << 8 | color.
b << 16 | color.
a << 24;
170 throwException(
"Image has already been converted to display format");
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
183 GLenum
const error = glGetError();
184 if (error != GL_NO_ERROR) {
187 case GL_INVALID_ENUM:
188 errmsg =
"GL_INVALID_ENUM";
191 case GL_INVALID_VALUE:
192 errmsg =
"GL_INVALID_VALUE";
195 case GL_INVALID_OPERATION:
196 errmsg =
"GL_INVALID_OPERATION";
199 case GL_STACK_OVERFLOW:
200 errmsg =
"GL_STACK_OVERFLOW";
203 case GL_STACK_UNDERFLOW:
204 errmsg =
"GL_STACK_UNDERFLOW";
207 case GL_OUT_OF_MEMORY:
208 errmsg =
"GL_OUT_OF_MEMORY";
211 errmsg =
"UNKNOWN_ERROR";
214 throwException(std::string(
"Unable to convert to OpenGL display format, glGetError said: ") + errmsg);
uint8_t a
Alpha color component (0-255).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
int getWidth() const override
Gets the width of the image.
bool mAutoFree
Whether the texture handle should be freed on destruction.
int getHeight() const override
Gets the height of the image.
void free() override
Frees an image.
void convertToDisplayFormat() override
Converts the image, if possible, to display format.
int mWidth
Logical image width in pixels.
virtual GLuint getTextureHandle() const
Gets the OpenGL texture handle for the image.
virtual int getTextureWidth() const
Gets the width of texture.
void putPixel(int x, int y, Color const &color) override
Puts a pixel with a certain color at coordinate (x, y).
int mTextureHeight
Actual texture height (power-of-two) used by GL.
Color getPixel(int x, int y) override
Gets the color of a pixel at coordinate (x, y) in the image.
GLuint mTextureHandle
OpenGL texture handle backing this image (if any).
int mTextureWidth
Actual texture width (power-of-two) used by GL.
Image(std::span< unsigned int const > pixels, int width, int height, bool convertToDisplayFormat=true)
Constructor.
virtual int getTextureHeight() const
Gets the height of the texture.
std::vector< unsigned int > mPixels
Pixel buffer copy for the image (RGBA packed).
int mHeight
Logical image height in pixels.
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.