Make get_non_transparent_portion return a rect again

Reverts part of 37a45c32ce and moves the actual surface handling to the relevant IPF, which was also simplified
This commit is contained in:
Charles Dang 2024-08-08 16:34:49 -04:00
parent 518e0d2050
commit c9ed3711b4
3 changed files with 25 additions and 36 deletions

View file

@ -197,7 +197,17 @@ surface gs_modification::operator()(const surface& src) const
surface crop_transparency_modification::operator()(const surface& src) const
{
return get_non_transparent_portion(src);
rect src_rect = get_non_transparent_portion(src);
if(src_rect.w == src->w && src_rect.h == src->h) {
return src;
}
if(surface cropped = get_surface_portion(src, src_rect)) {
return cropped;
} else {
ERR_DP << "Failed to either crop or scale the surface";
return nullptr;
}
}
surface bw_modification::operator()(const surface& src) const

View file

@ -1833,34 +1833,24 @@ surface get_surface_portion(const surface &src, SDL_Rect &area)
return dst;
}
namespace {
struct not_alpha
namespace
{
not_alpha() {}
// we assume neutral format
bool operator()(uint32_t pixel) const {
uint8_t alpha = pixel >> 24;
return alpha != 0x00;
constexpr bool not_alpha(uint32_t pixel)
{
return (pixel >> 24) != 0x00;
}
};
}
surface get_non_transparent_portion(const surface &surf)
rect get_non_transparent_portion(const surface &surf)
{
if(surf == nullptr)
return nullptr;
rect res {0,0,0,0};
surface nsurf = surf.clone();
if(nsurf == nullptr) {
PLAIN_LOG << "failed to make neutral surface";
return nullptr;
return res;
}
SDL_Rect res {0,0,0,0};
const not_alpha calc;
surface_lock lock(nsurf);
const uint32_t* const pixels = lock.pixels();
@ -1869,7 +1859,7 @@ surface get_non_transparent_portion(const surface &surf)
const uint32_t* const start_row = pixels + n*nsurf->w;
const uint32_t* const end_row = start_row + nsurf->w;
if(std::find_if(start_row,end_row,calc) != end_row)
if(std::find_if(start_row,end_row,not_alpha) != end_row)
break;
}
@ -1879,7 +1869,7 @@ surface get_non_transparent_portion(const surface &surf)
const uint32_t* const start_row = pixels + (nsurf->h-n-1)*surf->w;
const uint32_t* const end_row = start_row + nsurf->w;
if(std::find_if(start_row,end_row,calc) != end_row)
if(std::find_if(start_row,end_row,not_alpha) != end_row)
break;
}
@ -1892,7 +1882,7 @@ surface get_non_transparent_portion(const surface &surf)
int y;
for(y = 0; y != nsurf->h; ++y) {
const uint32_t pixel = pixels[y*nsurf->w + n];
if(calc(pixel))
if(not_alpha(pixel))
break;
}
@ -1906,7 +1896,7 @@ surface get_non_transparent_portion(const surface &surf)
int y;
for(y = 0; y != nsurf->h; ++y) {
const uint32_t pixel = pixels[y*nsurf->w + surf->w - n - 1];
if(calc(pixel))
if(not_alpha(pixel))
break;
}
@ -1915,15 +1905,5 @@ surface get_non_transparent_portion(const surface &surf)
}
res.w = nsurf->w - res.x - n;
surface cropped = get_surface_portion(nsurf, res);
if(cropped && res.w > 0 && res.h > 0) {
surface scaled = scale_surface(cropped, res.w, res.h);
if(scaled) {
return scaled;
}
}
ERR_DP << "Failed to either crop or scale the surface";
return nsurf;
return res;
}

View file

@ -254,7 +254,7 @@ surface rotate_90_surface(const surface &surf, bool clockwise);
surface flip_surface(const surface &surf);
surface flop_surface(const surface &surf);
surface get_non_transparent_portion(const surface &surf);
rect get_non_transparent_portion(const surface &surf);
/**
* Helper methods for setting/getting a single pixel in an image.
@ -268,4 +268,3 @@ surface get_non_transparent_portion(const surface &surf);
*/
void put_pixel(const surface& surf, surface_lock& surf_lock, int x, int y, uint32_t pixel);
uint32_t get_pixel(const surface& surf, const const_surface_lock& surf_lock, int x, int y);