1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

no code duplication

This commit is contained in:
Laserlicht
2025-05-19 09:10:28 +02:00
committed by GitHub
parent d102f660a0
commit 507cdb5c99

View File

@@ -543,7 +543,7 @@ void CSDL_Ext::putPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x,
}
template<int bpp>
void CSDL_Ext::convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect )
void loopOverPixel(SDL_Surface * surf, const Rect & rect, std::function<void(int &r, int &g, int &b)> func)
{
uint8_t * pixels = static_cast<uint8_t*>(surf->pixels);
@@ -560,16 +560,27 @@ void CSDL_Ext::convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect )
int g = Channels::px<bpp>::g.get(pixel);
int b = Channels::px<bpp>::b.get(pixel);
int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 *b);
func(r, g, b);
Channels::px<bpp>::r.set(pixel, gray);
Channels::px<bpp>::g.set(pixel, gray);
Channels::px<bpp>::b.set(pixel, gray);
Channels::px<bpp>::r.set(pixel, r);
Channels::px<bpp>::g.set(pixel, g);
Channels::px<bpp>::b.set(pixel, b);
}
}
});
}
template<int bpp>
void CSDL_Ext::convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect )
{
loopOverPixel<bpp>(surf, rect, [](int &r, int &g, int &b){
int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 * b);
r = gray;
g = gray;
b = gray;
});
}
void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
{
switch(surf->format->BytesPerPixel)
@@ -582,38 +593,18 @@ void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
template<int bpp>
void CSDL_Ext::convertToH2SchemeBpp(SDL_Surface * surf, const Rect & rect )
{
uint8_t * pixels = static_cast<uint8_t*>(surf->pixels);
loopOverPixel<bpp>(surf, rect, [](int &r, int &g, int &b){
double gray = 0.3 * r + 0.59 * g + 0.11 * b;
double factor = 2.0;
tbb::parallel_for(tbb::blocked_range<size_t>(rect.top(), rect.bottom()), [&](const tbb::blocked_range<size_t>& r)
{
for(int yp = r.begin(); yp != r.end(); ++yp)
{
uint8_t * pixel_from = pixels + yp * surf->pitch + rect.left() * surf->format->BytesPerPixel;
uint8_t * pixel_dest = pixels + yp * surf->pitch + rect.right() * surf->format->BytesPerPixel;
//fast approximation instead of colorspace conversion
r = static_cast<int>(gray + (r - gray) * factor);
g = static_cast<int>(gray + (g - gray) * factor);
b = static_cast<int>(gray + (b - gray) * factor);
for (uint8_t * pixel = pixel_from; pixel < pixel_dest; pixel += surf->format->BytesPerPixel)
{
int r = Channels::px<bpp>::r.get(pixel);
int g = Channels::px<bpp>::g.get(pixel);
int b = Channels::px<bpp>::b.get(pixel);
double gray = 0.3 * r + 0.59 * g + 0.11 * b;
double factor = 2.0;
//fast approximation instead of colorspace conversion
r = static_cast<int>(gray + (r - gray) * factor);
g = static_cast<int>(gray + (g - gray) * factor);
b = static_cast<int>(gray + (b - gray) * factor);
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
Channels::px<bpp>::r.set(pixel, r);
Channels::px<bpp>::g.set(pixel, g);
Channels::px<bpp>::b.set(pixel, b);
}
}
r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255);
b = std::clamp(b, 0, 255);
});
}