aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2018-01-11 10:22:32 +0100
committerRay <raysan5@gmail.com>2018-01-11 10:22:32 +0100
commit278d8575bdd09ba6536291e1b66dc9d32224fb2d (patch)
tree163f92e97d825ca086249212586df47f26c7757b /src
parentc37d2d448d373ee6aaf6b202fbfacc8283d7e682 (diff)
downloadraylib-278d8575bdd09ba6536291e1b66dc9d32224fb2d.tar.gz
raylib-278d8575bdd09ba6536291e1b66dc9d32224fb2d.zip
Added new function: ImageAlphaCrop()
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h1
-rw-r--r--src/textures.c47
2 files changed, 48 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index b482ab3b..dc02370d 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -881,6 +881,7 @@ RLAPI void ImageToPOT(Image *image, Color fillColor);
RLAPI void ImageFormat(Image *image, int newFormat); // Convert image data to desired format
RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image
RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); // Clear alpha channel to desired color
+RLAPI void ImageAlphaCrop(Image *image, float threshold); // Crop image depending on alpha value
RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel
RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle
RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering)
diff --git a/src/textures.c b/src/textures.c
index 44a9fef2..665e5447 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -890,6 +890,51 @@ void ImageAlphaClear(Image *image, Color color, float threshold)
ImageFormat(image, prevFormat);
}
+// 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++)
+ {
+ if (pixels[i].a > (unsigned char)(threshold*255.0f))
+ {
+ minx = i%image->width;
+ miny = -(-((i/image->width) + 1) + 1);
+
+ if (crop.y == 0) crop.y = miny;
+
+ if (crop.x == 0) crop.x = minx;
+ else if (minx < crop.x) crop.x = minx;
+
+ if (crop.width == 0) crop.width = minx;
+ else if (crop.width < minx) crop.width = minx;
+
+ if (crop.height == 0) crop.height = miny;
+ else if (crop.height < miny) crop.height = miny;
+ }
+ }
+
+ 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);
+
+ 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);
+}
+
// Premultiply alpha channel
void ImageAlphaPremultiply(Image *image)
{
@@ -912,6 +957,8 @@ void ImageAlphaPremultiply(Image *image)
ImageFormat(image, prevFormat);
}
+
+
#if defined(SUPPORT_IMAGE_MANIPULATION)
// Crop an image to area defined by a rectangle
// NOTE: Security checks are performed in case rectangle goes out of bounds