Added new function: ImageAlphaCrop()
This commit is contained in:
parent
c37d2d448d
commit
278d8575bd
2 changed files with 48 additions and 0 deletions
|
@ -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 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 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 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 ImageAlphaPremultiply(Image *image); // Premultiply alpha channel
|
||||||
RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle
|
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)
|
RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering)
|
||||||
|
|
|
@ -890,6 +890,51 @@ void ImageAlphaClear(Image *image, Color color, float threshold)
|
||||||
ImageFormat(image, prevFormat);
|
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
|
// Premultiply alpha channel
|
||||||
void ImageAlphaPremultiply(Image *image)
|
void ImageAlphaPremultiply(Image *image)
|
||||||
{
|
{
|
||||||
|
@ -912,6 +957,8 @@ void ImageAlphaPremultiply(Image *image)
|
||||||
ImageFormat(image, prevFormat);
|
ImageFormat(image, prevFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(SUPPORT_IMAGE_MANIPULATION)
|
#if defined(SUPPORT_IMAGE_MANIPULATION)
|
||||||
// Crop an image to area defined by a rectangle
|
// Crop an image to area defined by a rectangle
|
||||||
// NOTE: Security checks are performed in case rectangle goes out of bounds
|
// NOTE: Security checks are performed in case rectangle goes out of bounds
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue