From d99a3f4a96b754a788c4a147be27dd212d90f743 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 3 Aug 2013 23:02:14 +0300 Subject: improve software A,S dye speed. --- src/resources/dye.cpp | 96 ++++++++++++++++++++++++++-------------- src/resources/dye.h | 4 +- src/resources/dye_unittest.cc | 12 ++--- src/resources/sdlimagehelper.cpp | 20 +-------- 4 files changed, 73 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index dd6c50d63..b3ac8ea46 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -195,50 +195,80 @@ void DyePalette::getColor(double intensity, int color[3]) const color[2] = static_cast(rest * b1 + intensity * b2); } -void DyePalette::replaceSColor(uint8_t *const color) const +void DyePalette::replaceSColor(uint32_t *pixels, const int bufSize) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); - while (it != it_end) + std::vector::const_iterator it_end = mColors.end(); + const int sz = mColors.size(); + if (!sz) + return; + if (sz % 2) + -- it_end; + + for (uint32_t *p_end = pixels + bufSize; pixels != p_end; ++pixels) { - const DyeColor &col = *it; - ++ it; - if (it == it_end) - return; - const DyeColor &col2 = *it; - if (color[3] == col.value[0] && color[2] == col.value[1] - && color[1] == col.value[2]) + uint8_t *const p = reinterpret_cast(pixels); + const int alpha = *p & 255; + if (!alpha) + continue; + + std::vector::const_iterator it = mColors.begin(); + while (it != it_end) { - color[3] = col2.value[0]; - color[2] = col2.value[1]; - color[1] = col2.value[2]; - return; + const DyeColor &col = *it; + ++ it; + const DyeColor &col2 = *it; + const unsigned int data = (*pixels) & 0xffffff00; + const unsigned int coldata = (col.value[2] << 8) + | (col.value[1] << 16) | (col.value[0] << 24); + + if (data == coldata) + { + p[3] = col2.value[0]; + p[2] = col2.value[1]; + p[1] = col2.value[2]; + break; + } + + ++ it; } - ++ it; } } -void DyePalette::replaceAColor(uint8_t *const color) const +void DyePalette::replaceAColor(uint32_t *pixels, const int bufSize) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); - while (it != it_end) + std::vector::const_iterator it_end = mColors.end(); + const int sz = mColors.size(); + if (!sz) + return; + if (sz % 2) + -- it_end; + + for (uint32_t *p_end = pixels + bufSize; pixels != p_end; ++pixels) { - const DyeColor &col = *it; - ++ it; - if (it == it_end) - return; - const DyeColor &col2 = *it; - if (color[3] == col.value[0] && color[2] == col.value[1] - && color[1] == col.value[2] && color[0] == col.value[3]) + uint8_t *const p = reinterpret_cast(pixels); + + std::vector::const_iterator it = mColors.begin(); + while (it != it_end) { - color[3] = col2.value[0]; - color[2] = col2.value[1]; - color[1] = col2.value[2]; - color[0] = col2.value[3]; - return; + const DyeColor &col = *it; + ++ it; + const DyeColor &col2 = *it; + + const unsigned int data = *pixels; + const unsigned int coldata = (col.value[3]) | (col.value[2] << 8) + | (col.value[1] << 16) | (col.value[0] << 24); + + if (data == coldata) + { + p[3] = col2.value[0]; + p[2] = col2.value[1]; + p[1] = col2.value[2]; + p[0] = col2.value[3]; + break; + } + + ++ it; } - ++ it; } } diff --git a/src/resources/dye.h b/src/resources/dye.h index 95a4dbdae..2976c984d 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -63,12 +63,12 @@ class DyePalette final /** * replace colors for SDL for S dye. */ - void replaceSColor(uint8_t *const color) const; + void replaceSColor(uint32_t *pixels, const int bufSize) const; /** * replace colors for SDL for S dye. */ - void replaceAColor(uint8_t *const color) const; + void replaceAColor(uint32_t *pixels, const int bufSize) const; /** * replace colors for OpenGL for S dye. diff --git a/src/resources/dye_unittest.cc b/src/resources/dye_unittest.cc index 08120f641..10059ca34 100644 --- a/src/resources/dye_unittest.cc +++ b/src/resources/dye_unittest.cc @@ -124,7 +124,7 @@ TEST(Dye, replaceSColor1) data[1] = 0x02; data[2] = 0x03; data[3] = 0x10; - palette.replaceSColor(&data[0]); + palette.replaceSColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0x01, data[0]); EXPECT_EQ(0x02, data[1]); EXPECT_EQ(0x03, data[2]); @@ -139,7 +139,7 @@ TEST(Dye, replaceSColor2) data[1] = 0x20; data[2] = 0x30; data[3] = 0x40; - palette.replaceSColor(&data[0]); + palette.replaceSColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0x10, data[0]); EXPECT_EQ(0x50, data[1]); EXPECT_EQ(0x60, data[2]); @@ -154,7 +154,7 @@ TEST(Dye, replaceSColor3) data[1] = 0x20; data[2] = 0x30; data[3] = 0xff; - palette.replaceSColor(&data[0]); + palette.replaceSColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0x10, data[0]); EXPECT_EQ(0x50, data[1]); EXPECT_EQ(0x60, data[2]); @@ -170,7 +170,7 @@ TEST(Dye, replaceAColor1) data[1] = 0x02; data[2] = 0x03; data[3] = 0x10; - palette.replaceAColor(&data[0]); + palette.replaceAColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0x01, data[0]); EXPECT_EQ(0x02, data[1]); EXPECT_EQ(0x03, data[2]); @@ -185,7 +185,7 @@ TEST(Dye, replaceAColor2) data[1] = 0x00; data[2] = 0xff; data[3] = 0x00; - palette.replaceAColor(&data[0]); + palette.replaceAColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0xff, data[0]); EXPECT_EQ(0x11, data[1]); EXPECT_EQ(0x00, data[2]); @@ -200,7 +200,7 @@ TEST(Dye, replaceAColor3) data[1] = 0xee; data[2] = 0x00; data[3] = 0x01; - palette.replaceAColor(&data[0]); + palette.replaceAColor(reinterpret_cast(&data[0]), 1); EXPECT_EQ(0xff, data[0]); EXPECT_EQ(0x30, data[1]); EXPECT_EQ(0x20, data[2]); diff --git a/src/resources/sdlimagehelper.cpp b/src/resources/sdlimagehelper.cpp index 32fbd0f35..678835edb 100644 --- a/src/resources/sdlimagehelper.cpp +++ b/src/resources/sdlimagehelper.cpp @@ -71,30 +71,14 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const { const DyePalette *const pal = dye.getSPalete(); if (pal) - { - for (uint32_t *p_end = pixels + surf->w * surf->h; - pixels != p_end; ++pixels) - { - uint8_t *const p = reinterpret_cast(pixels); - const int alpha = *p & 255; - if (!alpha) - continue; - pal->replaceSColor(p); - } - } + pal->replaceSColor(pixels, surf->w * surf->h); break; } case 2: { const DyePalette *const pal = dye.getAPalete(); if (pal) - { - for (uint32_t *p_end = pixels + surf->w * surf->h; - pixels != p_end; ++pixels) - { - pal->replaceAColor(reinterpret_cast(pixels)); - } - } + pal->replaceAColor(pixels, surf->w * surf->h); break; } case 0: -- cgit v1.2.3-60-g2f50