summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorraysan5 <[email protected]>2020-06-07 11:52:21 +0200
committerraysan5 <[email protected]>2020-06-07 11:52:21 +0200
commitd278eae4a3dec65dd3593aba37746e9f672b7378 (patch)
tree0950526f964ac728c8e108526939f191bd39ef64
parent93d6dd30236a4339bc0855e091d475f328c3b6f0 (diff)
downloadraylib-d278eae4a3dec65dd3593aba37746e9f672b7378.tar.gz
raylib-d278eae4a3dec65dd3593aba37746e9f672b7378.zip
REDESIGNED: ImageFlipHorizontal(), optimized #1218
Added several optimized options for future reference (if required)
-rw-r--r--src/textures.c51
1 files changed, 35 insertions, 16 deletions
diff --git a/src/textures.c b/src/textures.c
index 846b7a70..c98c857d 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -1605,26 +1605,45 @@ void ImageFlipHorizontal(Image *image)
// Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
- Color *srcPixels = GetImageData(*image);
- Color *dstPixels = (Color *)RL_MALLOC(image->width*image->height*sizeof(Color));
-
- for (int y = 0; y < image->height; y++)
+ 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
{
- for (int x = 0; x < image->width; x++)
+ int dataSize = GetPixelDataSize(image->width, image->height, image->format);
+ int bytesPerPixel = dataSize/(image->width*image->height);
+
+ unsigned char *flippedData = (unsigned char *)RL_MALLOC(dataSize);
+
+ for (int y = 0; y < image->height; y++)
{
- dstPixels[y*image->width + x] = srcPixels[y*image->width + (image->width - 1 - x)];
+ for (int x = 0; x < image->width; x++)
+ {
+ // OPTION 1: Move pixels with memcopy()
+ //memcpy(flippedData + (y*image->width + x)*bytesPerPixel, ((unsigned char *)image->data) + (y*image->width + (image->width - 1 - x))*bytesPerPixel, bytesPerPixel);
+
+ // OPTION 2: Just copy data pixel by pixel
+ for (int i = 0; i < bytesPerPixel; i++) flippedData[(y*image->width + x)*bytesPerPixel + i] = ((unsigned char *)image->data)[(y*image->width + (image->width - 1 - x))*bytesPerPixel + i];
+ }
}
- }
-
- int format = image->format;
- RL_FREE(image->data);
-
- image->data = dstPixels;
- image->format = UNCOMPRESSED_R8G8B8A8;
-
- ImageFormat(image, format);
+
+ RL_FREE(image->data);
+ image->data = flippedData;
- RL_FREE(srcPixels);
+ /*
+ // OPTION 3: Faster implementation (specific for 32bit pixels)
+ // NOTE: It does not require additional allocations
+ uint32_t *ptr = (uint32_t *)image->data;
+ for (int y = 0; y < image->height; y++)
+ {
+ for (int x = 0; x < image->width/2; x++)
+ {
+ uint32_t backup = ptr[y*image->width + x];
+ ptr[y*image->width + x] = ptr[y*image->width + (image->width - 1 - x)];
+ ptr[y*image->width + (image->width - 1 - x)] = backup;
+ }
+ }
+ */
+ }
}
// Rotate image clockwise 90deg