Reimplement ImageAlphaCrop()

This commit is contained in:
Ray 2018-11-27 10:20:41 +01:00
parent 69ae794465
commit b5c6736592

View file

@ -1101,46 +1101,33 @@ void ImageAlphaClear(Image *image, Color color, float threshold)
// Crop image depending on alpha value // Crop image depending on alpha value
void ImageAlphaCrop(Image *image, float threshold) void ImageAlphaCrop(Image *image, float threshold)
{ {
Rectangle crop = { 0 };
Color *pixels = GetImageData(*image); Color *pixels = GetImageData(*image);
int minx = 0; int xMin = 65536; // Define a big enough number
int miny = 0; int xMax = 0;
int yMin = 65536;
for (int i = 0; i < image->width*image->height; i++) int yMax = 0;
for (int y = 0; y < image->height; y++)
{ {
if (pixels[i].a > (unsigned char)(threshold*255.0f)) for (int x = 0; x < image->width; x++)
{ {
minx = i%image->width; if (pixels[y*image->width + x].a > (unsigned char)(threshold*255.0f))
miny = -(-((i/image->width) + 1) + 1); {
if (x < xMin) xMin = x;
if (crop.y == 0.0f) crop.y = (float)miny; if (x > xMax) xMax = x;
if (y < yMin) yMin = y;
if (crop.x == 0.0f) crop.x = (float)minx; if (y > yMax) yMax = y;
else if (minx < crop.x) crop.x = (float)minx; }
if (crop.width == 0.0f) crop.width = (float)minx;
else if (crop.width < minx) crop.width = (float)minx;
if (crop.height == 0.0f) crop.height = (float)miny;
else if (crop.height < (float)miny) crop.height = (float)miny;
} }
} }
crop.width -= (crop.x - 1); Rectangle crop = { xMin, yMin, (xMax + 1) - xMin, (yMax + 1) - yMin };
crop.height -= (crop.y - 1);
TraceLog(LOG_INFO, "Crop rectangle: (%i, %i, %i, %i)", crop.x, crop.y, crop.width, crop.height);
free(pixels); free(pixels);
// NOTE: Added this weird check to avoid additional 1px crop to // Check for not empty image brefore cropping
// image data that has already been cropped... if (!((xMax < xMin) || (yMax < yMin))) ImageCrop(image, crop);
if ((crop.x != 1) &&
(crop.y != 1) &&
(crop.width != image->width - 1) &&
(crop.height != image->height - 1)) ImageCrop(image, crop);
} }
// Premultiply alpha channel // Premultiply alpha channel