Browse Source

REDESIGNED: ImageRotateCCW(), optimized #1218

raysan5 5 years ago
parent
commit
776e4a37ef
1 changed files with 22 additions and 20 deletions
  1. 22 20
      src/textures.c

+ 22 - 20
src/textures.c

@@ -1686,30 +1686,32 @@ void ImageRotateCCW(Image *image)
     // Security check to avoid program crash
     if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
 
-    Color *srcPixels = GetImageData(*image);
-    Color *rotPixels = (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 *rotatedData = (unsigned char *)RL_MALLOC(dataSize);
+        
+        for (int y = 0; y < image->height; y++)
         {
-            rotPixels[x*image->height + y] = srcPixels[y*image->width + (image->width - x - 1)];
+            for (int x = 0; x < image->width; x++)
+            {
+                //memcpy(rotatedData + (x*image->height + y))*bytesPerPixel, ((unsigned char *)image->data) + (y*image->width + (image->width - x - 1))*bytesPerPixel, bytesPerPixel);
+                for (int i = 0; i < bytesPerPixel; i++) rotatedData[(x*image->height + y)*bytesPerPixel + i] = ((unsigned char *)image->data)[(y*image->width + (image->width - x - 1))*bytesPerPixel + i];
+            }
         }
+        
+        RL_FREE(image->data);
+        image->data = rotatedData;
+        int width = image->width;
+        int height = image-> height;
+        
+        image->width = height;
+        image->height = width;
     }
-
-    int format = image->format;
-    int width = image->width;
-    int height = image-> height;
-    RL_FREE(image->data);
-    
-    image->data = rotPixels;
-    image->width = height;
-    image->height = width;
-    image->format = UNCOMPRESSED_R8G8B8A8;
-    
-    ImageFormat(image, format);
-    
-    RL_FREE(srcPixels);
 }
 
 // Modify image color: tint