mirror of
https://github.com/raysan5/raylib.git
synced 2026-01-31 03:09:17 -05:00
REDESIGNED: ImageResize(), optimized #1218
This commit is contained in:
@ -1314,6 +1314,30 @@ void ImageResize(Image *image, int newWidth, int newHeight)
|
|||||||
// Security check to avoid program crash
|
// Security check to avoid program crash
|
||||||
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
|
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
|
||||||
|
|
||||||
|
bool fastPath = true;
|
||||||
|
if ((image->format != UNCOMPRESSED_GRAYSCALE) && (image->format != UNCOMPRESSED_GRAY_ALPHA) && (image->format != UNCOMPRESSED_R8G8B8) && (image->format != UNCOMPRESSED_R8G8B8A8)) fastPath = true;
|
||||||
|
|
||||||
|
if (fastPath)
|
||||||
|
{
|
||||||
|
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||||
|
unsigned char *output = RL_MALLOC(newWidth*newHeight*bytesPerPixel);
|
||||||
|
|
||||||
|
switch (image->format)
|
||||||
|
{
|
||||||
|
case UNCOMPRESSED_GRAYSCALE: stbir_resize_uint8((unsigned char *)image->data, image->width, image->height, 0, output, newWidth, newHeight, 0, 1); break;
|
||||||
|
case UNCOMPRESSED_GRAY_ALPHA: stbir_resize_uint8((unsigned char *)image->data, image->width, image->height, 0, output, newWidth, newHeight, 0, 2); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8: stbir_resize_uint8((unsigned char *)image->data, image->width, image->height, 0, output, newWidth, newHeight, 0, 3); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8A8: stbir_resize_uint8((unsigned char *)image->data, image->width, image->height, 0, output, newWidth, newHeight, 0, 4); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
RL_FREE(image->data);
|
||||||
|
image->data = output;
|
||||||
|
image->width = newWidth;
|
||||||
|
image->height = newHeight;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// Get data as Color pixels array to work with it
|
// Get data as Color pixels array to work with it
|
||||||
Color *pixels = GetImageData(*image);
|
Color *pixels = GetImageData(*image);
|
||||||
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
|
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
|
||||||
@ -1333,6 +1357,7 @@ void ImageResize(Image *image, int newWidth, int newHeight)
|
|||||||
|
|
||||||
ImageFormat(image, format); // Reformat 32bit RGBA image to original format
|
ImageFormat(image, format); // Reformat 32bit RGBA image to original format
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Resize and image to new size using Nearest-Neighbor scaling algorithm
|
// Resize and image to new size using Nearest-Neighbor scaling algorithm
|
||||||
void ImageResizeNN(Image *image,int newWidth,int newHeight)
|
void ImageResizeNN(Image *image,int newWidth,int newHeight)
|
||||||
|
|||||||
Reference in New Issue
Block a user