5#include "fifechan/backends/opengl/image.hpp"
9#include "fifechan/exception.hpp"
28 unsigned int const magicPink = 0xff00ffff;
30 unsigned int const magicPink = 0xffff00ff;
35 unsigned int c = pixels[x + (y *
mWidth)];
54 Image::Image(GLuint textureHandle,
int width,
int height,
bool autoFree) :
121 throwException(
"Image has been converted to display format");
125 throwException(
"Coordinates outside of the image");
131 auto const r =
static_cast<unsigned char>((c >> 24) & 0xff);
132 auto const g =
static_cast<unsigned char>((c >> 16) & 0xff);
133 auto const b =
static_cast<unsigned char>((c >> 8) & 0xff);
134 auto const a =
static_cast<unsigned char>(c & 0xff);
136 auto const a =
static_cast<unsigned char>((c >> 24) & 0xff);
137 auto const b =
static_cast<unsigned char>((c >> 16) & 0xff);
138 auto const g =
static_cast<unsigned char>((c >> 8) & 0xff);
139 auto const r =
static_cast<unsigned char>(c & 0xff);
148 throwException(
"Image has been converted to display format");
152 throwException((
"Coordinates outside of the image"));
156 const unsigned int c = color.
a | color.
b << 8 | color.
g << 16 | color.
r << 24;
158 const unsigned int c = color.
r | color.
g << 8 | color.
b << 16 | color.
a << 24;
167 throwException(
"Image has already been converted to display format");
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
180 GLenum
const error = glGetError();
181 if (error != GL_NO_ERROR) {
184 case GL_INVALID_ENUM:
185 errmsg =
"GL_INVALID_ENUM";
188 case GL_INVALID_VALUE:
189 errmsg =
"GL_INVALID_VALUE";
192 case GL_INVALID_OPERATION:
193 errmsg =
"GL_INVALID_OPERATION";
196 case GL_STACK_OVERFLOW:
197 errmsg =
"GL_STACK_OVERFLOW";
200 case GL_STACK_UNDERFLOW:
201 errmsg =
"GL_STACK_UNDERFLOW";
204 case GL_OUT_OF_MEMORY:
205 errmsg =
"GL_OUT_OF_MEMORY";
208 errmsg =
"UNKNOWN_ERROR";
211 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.