Browse Source

ADDED: GetImageAlphaBorder()

Ray 6 years ago
parent
commit
2a913b6587
2 changed files with 34 additions and 0 deletions
  1. 1 0
      src/raylib.h
  2. 33 0
      src/textures.c

+ 1 - 0
src/raylib.h

@@ -1095,6 +1095,7 @@ RLAPI void UnloadTexture(Texture2D texture);
 RLAPI void UnloadRenderTexture(RenderTexture2D target);                                                  // Unload render texture from GPU memory (VRAM)
 RLAPI void UnloadRenderTexture(RenderTexture2D target);                                                  // Unload render texture from GPU memory (VRAM)
 RLAPI Color *GetImageData(Image image);                                                                  // Get pixel data from image as a Color struct array
 RLAPI Color *GetImageData(Image image);                                                                  // Get pixel data from image as a Color struct array
 RLAPI Vector4 *GetImageDataNormalized(Image image);                                                      // Get pixel data from image as Vector4 array (float normalized)
 RLAPI Vector4 *GetImageDataNormalized(Image image);                                                      // Get pixel data from image as Vector4 array (float normalized)
+RLAPI Rectangle GetImageAlphaBorder(Image image);                                                        // Get image alpha border rectangle
 RLAPI int GetPixelDataSize(int width, int height, int format);                                           // Get pixel data size in bytes (image or texture)
 RLAPI int GetPixelDataSize(int width, int height, int format);                                           // Get pixel data size in bytes (image or texture)
 RLAPI Image GetTextureData(Texture2D texture);                                                           // Get pixel data from GPU texture and return an Image
 RLAPI Image GetTextureData(Texture2D texture);                                                           // Get pixel data from GPU texture and return an Image
 RLAPI Image GetScreenData(void);                                                                         // Get pixel data from screen buffer and return an Image (screenshot)
 RLAPI Image GetScreenData(void);                                                                         // Get pixel data from screen buffer and return an Image (screenshot)

+ 33 - 0
src/textures.c

@@ -680,6 +680,37 @@ Vector4 *GetImageDataNormalized(Image image)
     return pixels;
     return pixels;
 }
 }
 
 
+// Get image alpha border rectangle
+Rectangle GetImageAlphaBorder(Image image)
+{
+    Color *pixels = GetImageData(image);
+
+    int xMin = 65536;   // Define a big enough number
+    int xMax = 0;
+    int yMin = 65536;
+    int yMax = 0;
+
+    for (int y = 0; y < image->height; y++)
+    {
+        for (int x = 0; x < image->width; x++)
+        {
+            if (pixels[y*image->width + x].a > (unsigned char)(threshold*255.0f))
+            {
+                if (x < xMin) xMin = x;
+                if (x > xMax) xMax = x;
+                if (y < yMin) yMin = y;
+                if (y > yMax) yMax = y;
+            }
+        }
+    }
+
+    Rectangle crop = { xMin, yMin, (xMax + 1) - xMin, (yMax + 1) - yMin };
+
+    RL_FREE(pixels);
+
+    return crop;
+}
+
 // Get pixel data size in bytes (image or texture)
 // Get pixel data size in bytes (image or texture)
 // NOTE: Size depends on pixel format
 // NOTE: Size depends on pixel format
 int GetPixelDataSize(int width, int height, int format)
 int GetPixelDataSize(int width, int height, int format)
@@ -894,7 +925,9 @@ Image ImageFromImage(Image image, Rectangle rec)
 {
 {
     Image result = ImageCopy(image);
     Image result = ImageCopy(image);
     
     
+#if defined(SUPPORT_IMAGE_MANIPULATION)
     ImageCrop(&result, rec);
     ImageCrop(&result, rec);
+#endif
     
     
     return result;
     return result;
 }
 }