From 57a81ed6cf1c02d7ec8bfe1e6f0a1d3232309534 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 3 Jun 2012 17:26:04 +0300 Subject: Improve dye speed. --- src/resources/dye.cpp | 12 +++--------- src/resources/dye.h | 16 +++++++++++++++- src/resources/image.cpp | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index a4e101ff8..50972c6d6 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -205,7 +205,7 @@ void DyePalette::getColor(double intensity, int color[3]) const color[2] = static_cast(rest * b1 + intensity * b2); } -void DyePalette::replaceColor(int color[3]) const +void DyePalette::replaceColor(Uint8 *color) const { std::vector::const_iterator it = mColors.begin(); std::vector::const_iterator it_end = mColors.end(); @@ -219,9 +219,9 @@ void DyePalette::replaceColor(int color[3]) const if (color[0] == col.value[0] && color[1] == col.value[1] && color[2] == col.value[2]) { - color[0] = col2.value[0]; + color[2] = col2.value[0]; color[1] = col2.value[1]; - color[2] = col2.value[2]; + color[0] = col2.value[2]; return; } ++ it; @@ -285,12 +285,6 @@ Dye::~Dye() void Dye::update(int color[3]) const { - if (mDyePalettes[dyePalateSize - 1]) - { - mDyePalettes[dyePalateSize - 1]->replaceColor(color); - return; - } - int cmax = std::max(color[0], std::max(color[1], color[2])); if (cmax == 0) return; diff --git a/src/resources/dye.h b/src/resources/dye.h index 34afcf8ed..03aec71cb 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -26,6 +26,8 @@ #include #include +#include + const int dyePalateSize = 8; /** @@ -59,7 +61,7 @@ class DyePalette */ void getColor(double intensity, int color[3]) const; - void replaceColor(int color[3]) const; + void replaceColor(Uint8 *color) const; private: struct Color @@ -99,6 +101,18 @@ class Dye static void instantiate(std::string &target, const std::string &palettes); + /** + * Check if dye is special dye (S) + */ + bool isSpecialDye() const + { return mDyePalettes[dyePalateSize - 1]; } + + /** + * Return special dye palete (S) + */ + DyePalette *getSPalete() const + { return mDyePalettes[dyePalateSize - 1]; } + private: /** diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 34a2e22b7..da20719ac 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -164,19 +164,34 @@ Resource *Image::load(SDL_RWops *rw, Dye const &dye) SDL_FreeSurface(tmpImage); Uint32 *pixels = static_cast(surf->pixels); - for (Uint32 *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) - { - const Uint32 p = *pixels; - - int alpha = p & 255; - if (!alpha) - continue; - int v[3]; - v[0] = (p >> 24) & 255; - v[1] = (p >> 16) & 255; - v[2] = (p >> 8 ) & 255; - dye.update(v); - *pixels = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | alpha; + DyePalette *pal = dye.getSPalete(); + + if (pal) + { + for (Uint32 *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) + { + Uint8 *p = (Uint8 *)pixels; + const int alpha = *p & 255; + if (!alpha) + continue; + pal->replaceColor(p + 1); + } + } + else + { + for (Uint32 *p_end = pixels + surf->w * surf->h; pixels != p_end; ++pixels) + { + const Uint32 p = *pixels; + const int alpha = p & 255; + if (!alpha) + continue; + int v[3]; + v[0] = (p >> 24) & 255; + v[1] = (p >> 16) & 255; + v[2] = (p >> 8 ) & 255; + dye.update(v); + *pixels = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | alpha; + } } Image *image = load(surf); -- cgit v1.2.3-60-g2f50