summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorraysan5 <[email protected]>2020-06-07 12:16:27 +0200
committerraysan5 <[email protected]>2020-06-07 12:16:27 +0200
commit02ff68e1a336296133a6a211aa45310442e3bf0b (patch)
treebb00bee904dfb3d354073424c5b7e3552a6f9370
parent7cf1e76d538f37cc658cbb6fd82aa7a3110de004 (diff)
downloadraylib-02ff68e1a336296133a6a211aa45310442e3bf0b.tar.gz
raylib-02ff68e1a336296133a6a211aa45310442e3bf0b.zip
REDESIGNED: ImageCrop(), optimized #1218
-rw-r--r--src/textures.c37
1 files changed, 18 insertions, 19 deletions
diff --git a/src/textures.c b/src/textures.c
index 15f3f4fe..ea1a1c63 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -779,36 +779,35 @@ void ImageCrop(Image *image, Rectangle crop)
if (crop.y < 0) { crop.height += crop.y; crop.y = 0; }
if ((crop.x + crop.width) > image->width) crop.width = image->width - crop.x;
if ((crop.y + crop.height) > image->height) crop.height = image->height - crop.y;
-
- if ((crop.x < image->width) && (crop.y < image->height))
+ if ((crop.x > image->width) || (crop.y > image->height))
{
- // Start the cropping process
- Color *pixels = GetImageData(*image); // Get data as Color pixels array
- Color *cropPixels = (Color *)RL_MALLOC((int)crop.width*(int)crop.height*sizeof(Color));
+ TRACELOG(LOG_WARNING, "IMAGE: Failed to crop, rectangle out of bounds");
+ return;
+ }
- for (int j = (int)crop.y; j < (int)(crop.y + crop.height); j++)
+ if (image->mipmaps > 1) TRACELOG(LOG_WARNING, "Image manipulation only applied to base mipmap level");
+ if (image->format >= COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "Image manipulation not supported for compressed formats");
+ else
+ {
+ int dataSize = GetPixelDataSize(image->width, image->height, image->format);
+ int bytesPerPixel = dataSize/(image->width*image->height);
+
+ unsigned char *croppedData = (unsigned char *)RL_MALLOC(crop.width*crop.height*bytesPerPixel);
+
+ for (int y = (int)crop.y; y < (int)(crop.y + crop.height); y++)
{
- for (int i = (int)crop.x; i < (int)(crop.x + crop.width); i++)
+ for (int x = (int)crop.x; x < (int)(crop.x + crop.width); x++)
{
- cropPixels[(j - (int)crop.y)*(int)crop.width + (i - (int)crop.x)] = pixels[j*image->width + i];
+ //memcpy(croppedData + ((y - (int)crop.y)*(int)crop.width + (x - (int)crop.x))*bytesPerPixel, ((unsigned char *)image->data) + (y*image->width + x)*bytesPerPixel, bytesPerPixel);
+ for (int i = 0; i < bytesPerPixel; i++) croppedData[((y - (int)crop.y)*(int)crop.width + (x - (int)crop.x))*bytesPerPixel + i] = ((unsigned char *)image->data)[(y*image->width + x)*bytesPerPixel + i];
}
}
- RL_FREE(pixels);
-
- int format = image->format;
-
RL_FREE(image->data);
-
- image->data = cropPixels;
+ image->data = croppedData;
image->width = (int)crop.width;
image->height = (int)crop.height;
- image->format = UNCOMPRESSED_R8G8B8A8;
-
- // Reformat 32bit RGBA image to original format
- ImageFormat(image, format);
}
- else TRACELOG(LOG_WARNING, "IMAGE: Failed to crop, rectangle out of bounds");
}
// Convert image data to desired format