diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-04-11 09:50:04 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-04-29 11:18:16 +0200 |
commit | c0bde6928ae13bfd4c22c2ed13313e9724a5cc18 (patch) | |
tree | 42831e15ac4165105a0706c8cf7d8ad5f3be921a | |
parent | af8e01bfc18451c7cdcf29dca4958de554ef0127 (diff) | |
download | mana-c0bde6928ae13bfd4c22c2ed13313e9724a5cc18.tar.gz mana-c0bde6928ae13bfd4c22c2ed13313e9724a5cc18.tar.bz2 mana-c0bde6928ae13bfd4c22c2ed13313e9724a5cc18.tar.xz mana-c0bde6928ae13bfd4c22c2ed13313e9724a5cc18.zip |
Dye: Cast to SDL_Color rather than shifting bits around
When we use SDL_PIXELFORMAT_RGBA32, which is actually the same as
SDL_PIXELFORMAT_ABGR8888 on little-endian systems, we can cast the pixel
data directly to SDL_Color* for easy access to the components.
This may also make applying dye more efficient on big-endian systems,
though I have no such system to test with.
Follow-up to d8b871727c363892b14f2eadfad8f6058ec6ab72.
-rw-r--r-- | src/resources/dye.cpp | 32 | ||||
-rw-r--r-- | src/resources/dye.h | 10 | ||||
-rw-r--r-- | src/resources/image.cpp | 25 |
3 files changed, 35 insertions, 32 deletions
diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index d594299c..0ecc9fd6 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -70,7 +70,7 @@ DyePalette::DyePalette(const std::string &description) v = (v << 4) | n; } - Color c = { { (unsigned char) (v >> 16), (unsigned char) (v >> 8), (unsigned char) v } }; + Color c = { (unsigned char) (v >> 16), (unsigned char) (v >> 8), (unsigned char) v }; mColors.push_back(c); pos += 6; @@ -103,9 +103,9 @@ void DyePalette::getColor(int intensity, int color[3]) const int j = t != 0 ? i : i - 1; // Get the exact color if any, the next color otherwise. - int r2 = mColors[j].value[0], - g2 = mColors[j].value[1], - b2 = mColors[j].value[2]; + int r2 = mColors[j].r, + g2 = mColors[j].g, + b2 = mColors[j].b; if (t == 0) { @@ -120,9 +120,9 @@ void DyePalette::getColor(int intensity, int color[3]) const int r1 = 0, g1 = 0, b1 = 0; if (i > 0) { - r1 = mColors[i - 1].value[0]; - g1 = mColors[i - 1].value[1]; - b1 = mColors[i - 1].value[2]; + r1 = mColors[i - 1].r; + g1 = mColors[i - 1].g; + b1 = mColors[i - 1].b; } // Perform a linear interpolation. @@ -153,9 +153,9 @@ void DyePalette::getColor(double intensity, int color[3]) const if (i == j) { // Exact color. - color[0] = mColors[i].value[0]; - color[1] = mColors[i].value[1]; - color[2] = mColors[i].value[2]; + color[0] = mColors[i].r; + color[1] = mColors[i].g; + color[2] = mColors[i].b; return; } @@ -163,12 +163,12 @@ void DyePalette::getColor(double intensity, int color[3]) const double rest = 1 - intensity; // Get the colors - int r1 = mColors[i].value[0], - g1 = mColors[i].value[1], - b1 = mColors[i].value[2], - r2 = mColors[j].value[0], - g2 = mColors[j].value[1], - b2 = mColors[j].value[2]; + int r1 = mColors[i].r, + g1 = mColors[i].g, + b1 = mColors[i].b, + r2 = mColors[j].r, + g2 = mColors[j].g, + b2 = mColors[j].b; // Perform the interpolation. color[0] = (rest * r1 + intensity * r2); diff --git a/src/resources/dye.h b/src/resources/dye.h index 61ad4df2..0fe68f07 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -30,7 +30,6 @@ class DyePalette { public: - /** * Creates a palette based on the given string. * The string is either a file name or a sequence of hexadecimal RGB @@ -50,8 +49,12 @@ class DyePalette void getColor(double intensity, int color[3]) const; private: - - struct Color { unsigned char value[3]; }; + struct Color + { + unsigned char r; + unsigned char g; + unsigned char b; + }; std::vector<Color> mColors; }; @@ -62,7 +65,6 @@ class DyePalette class Dye { public: - /** * Creates a set of palettes based on the given string. * diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 158d5956..b36aea01 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -121,12 +121,12 @@ Resource *Image::load(SDL_RWops *rw, const Dye &dye) return nullptr; } - if (surf->format->format != SDL_PIXELFORMAT_ABGR8888) + if (surf->format->format != SDL_PIXELFORMAT_RGBA32) { - logger->log("Warning: image format is %s, not SDL_PIXELFORMAT_ABGR8888. Converting...", + logger->log("Warning: image format is %s, not SDL_PIXELFORMAT_RGBA32. Converting...", SDL_GetPixelFormatName(surf->format->format)); - SDL_Surface *convertedSurf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ABGR8888, 0); + SDL_Surface *convertedSurf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA32, 0); SDL_FreeSurface(surf); if (!convertedSurf) { @@ -136,17 +136,18 @@ Resource *Image::load(SDL_RWops *rw, const Dye &dye) surf = convertedSurf; } - auto *pixels = static_cast< uint32_t * >(surf->pixels); - for (uint32_t *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) + auto *pixels = static_cast<SDL_Color *>(surf->pixels); + for (SDL_Color *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) { - const uint32_t alpha = (*pixels >> 24) & 255; - if (!alpha) continue; - int v[3]; - v[0] = (*pixels) & 255; - v[1] = (*pixels >> 8) & 255; - v[2] = (*pixels >> 16) & 255; + if (!pixels->a) + continue; + + int v[3] = { pixels->r, pixels->g, pixels->b }; dye.update(v); - *pixels = (alpha << 24) | (v[2] << 16) | (v[1] << 8) | v[0]; + + pixels->r = v[0]; + pixels->g = v[1]; + pixels->b = v[2]; } Image *image = load(surf); |