diff options
| author | raysan5 <[email protected]> | 2020-06-15 12:04:18 +0200 |
|---|---|---|
| committer | raysan5 <[email protected]> | 2020-06-15 12:04:18 +0200 |
| commit | bfa654403069307f1d828f86fe798893b4e869a3 (patch) | |
| tree | a0cc381ef8986e54fbac774b9e903c8417cebe01 /src/textures.c | |
| parent | 691c1f9391119506c4bd7a92cf1b88e4046b0d3a (diff) | |
| download | raylib-bfa654403069307f1d828f86fe798893b4e869a3.tar.gz raylib-bfa654403069307f1d828f86fe798893b4e869a3.zip | |
REDESIGNED: ImageResize(), optimized #1218
Diffstat (limited to 'src/textures.c')
| -rw-r--r-- | src/textures.c | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/src/textures.c b/src/textures.c index e98efc59..eccddfa6 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1313,25 +1313,50 @@ void ImageResize(Image *image, int newWidth, int newHeight) { // Security check to avoid program crash 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; + } - // Get data as Color pixels array to work with it - Color *pixels = GetImageData(*image); - Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color)); + RL_FREE(image->data); + image->data = output; + image->width = newWidth; + image->height = newHeight; + } + else + { + // Get data as Color pixels array to work with it + Color *pixels = GetImageData(*image); + Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color)); - // NOTE: Color data is casted to (unsigned char *), there shouldn't been any problem... - stbir_resize_uint8((unsigned char *)pixels, image->width, image->height, 0, (unsigned char *)output, newWidth, newHeight, 0, 4); + // NOTE: Color data is casted to (unsigned char *), there shouldn't been any problem... + stbir_resize_uint8((unsigned char *)pixels, image->width, image->height, 0, (unsigned char *)output, newWidth, newHeight, 0, 4); - int format = image->format; - - RL_FREE(pixels); - RL_FREE(image->data); + int format = image->format; + + RL_FREE(pixels); + RL_FREE(image->data); - image->data = output; - image->width = newWidth; - image->height = newHeight; - image->format = UNCOMPRESSED_R8G8B8A8; + image->data = output; + image->width = newWidth; + image->height = newHeight; + image->format = UNCOMPRESSED_R8G8B8A8; - 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 |
