aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2018-11-27 10:20:41 +0100
committerRay <raysan5@gmail.com>2018-11-27 10:20:41 +0100
commitb5c6736592d0b1f11d91122d8be11ae94240e448 (patch)
treef9c7c4ab0927f9ea715487718be2cc4af26f5a42
parent69ae79446530697601ac14a69b8c2ba227e47544 (diff)
downloadraylib-b5c6736592d0b1f11d91122d8be11ae94240e448.tar.gz
raylib-b5c6736592d0b1f11d91122d8be11ae94240e448.zip
Reimplement ImageAlphaCrop()
-rw-r--r--src/textures.c51
1 files changed, 19 insertions, 32 deletions
diff --git a/src/textures.c b/src/textures.c
index fce1092a..fbb95863 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -1101,46 +1101,33 @@ void ImageAlphaClear(Image *image, Color color, float threshold)
// Crop image depending on alpha value
void ImageAlphaCrop(Image *image, float threshold)
{
- Rectangle crop = { 0 };
-
Color *pixels = GetImageData(*image);
- int minx = 0;
- int miny = 0;
-
- for (int i = 0; i < image->width*image->height; i++)
+ int xMin = 65536; // Define a big enough number
+ int xMax = 0;
+ int yMin = 65536;
+ 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;
- miny = -(-((i/image->width) + 1) + 1);
-
- if (crop.y == 0.0f) crop.y = (float)miny;
-
- if (crop.x == 0.0f) crop.x = (float)minx;
- 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;
+ if (pixels[y*image->width + x].a > (unsigned char)(threshold*255.0f))
+ {
+ if (x < xMin) xMin = x;
+ if (x > xMax) xMax = x;
+ if (y < yMin) yMin = y;
+ if (y > yMax) yMax = y;
+ }
}
}
-
- crop.width -= (crop.x - 1);
- crop.height -= (crop.y - 1);
-
- TraceLog(LOG_INFO, "Crop rectangle: (%i, %i, %i, %i)", crop.x, crop.y, crop.width, crop.height);
+
+ Rectangle crop = { xMin, yMin, (xMax + 1) - xMin, (yMax + 1) - yMin };
free(pixels);
-
- // NOTE: Added this weird check to avoid additional 1px crop to
- // image data that has already been cropped...
- if ((crop.x != 1) &&
- (crop.y != 1) &&
- (crop.width != image->width - 1) &&
- (crop.height != image->height - 1)) ImageCrop(image, crop);
+
+ // Check for not empty image brefore cropping
+ if (!((xMax < xMin) || (yMax < yMin))) ImageCrop(image, crop);
}
// Premultiply alpha channel