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> 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); 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 g = Channels::px<bpp>::g.get(pixel);
int b = Channels::px<bpp>::b.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>::r.set(pixel, r);
Channels::px<bpp>::g.set(pixel, gray); Channels::px<bpp>::g.set(pixel, g);
Channels::px<bpp>::b.set(pixel, gray); 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 ) void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
{ {
switch(surf->format->BytesPerPixel) switch(surf->format->BytesPerPixel)
@@ -582,21 +593,7 @@ void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
template<int bpp> template<int bpp>
void CSDL_Ext::convertToH2SchemeBpp(SDL_Surface * surf, const Rect & rect ) 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){
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;
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 gray = 0.3 * r + 0.59 * g + 0.11 * b;
double factor = 2.0; double factor = 2.0;
@@ -608,12 +605,6 @@ void CSDL_Ext::convertToH2SchemeBpp(SDL_Surface * surf, const Rect & rect )
r = std::clamp(r, 0, 255); r = std::clamp(r, 0, 255);
g = std::clamp(g, 0, 255); g = std::clamp(g, 0, 255);
b = std::clamp(b, 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);
}
}
}); });
} }