summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorBruno Cabral <[email protected]>2024-06-30 01:33:32 -0700
committerGitHub <[email protected]>2024-06-30 10:33:32 +0200
commit6e2661f92d38aa669047bcf63db8c27a1c1f6dec (patch)
tree80533b0fdc1065bd18b3ac89243d985e212a8813 /src
parent5b8efd68ba2d8e2c707d5811a5ce7185b2bead76 (diff)
downloadraylib-6e2661f92d38aa669047bcf63db8c27a1c1f6dec.tar.gz
raylib-6e2661f92d38aa669047bcf63db8c27a1c1f6dec.zip
[rtextures] Created `ImageFromChannel()` (#4105)
* created ImageFromChannel Adds the possibility to extract a specific channel from an image * naming convention * example window height * removed threshold * removed alpha channel * channel example organization * updated channel example image
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h1
-rw-r--r--src/rtextures.c202
2 files changed, 203 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index c3c546c3..26cf03e8 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -1334,6 +1334,7 @@ RLAPI Image ImageCopy(Image image);
RLAPI Image ImageFromImage(Image image, Rectangle rec); // Create an image from another image piece
RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font)
RLAPI Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Color tint); // Create an image from text (custom sprite font)
+RLAPI Image ImageFromChannel(Image image, int selectedChannel); // Create an image from a selected channel of another image
RLAPI void ImageFormat(Image *image, int newFormat); // Convert image data to desired format
RLAPI void ImageToPOT(Image *image, Color fill); // Convert image to POT (power-of-two)
RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle
diff --git a/src/rtextures.c b/src/rtextures.c
index e914db41..9d547cd0 100644
--- a/src/rtextures.c
+++ b/src/rtextures.c
@@ -1630,6 +1630,208 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co
return imText;
}
+// Create an image from a selected channel of another image
+Image ImageFromChannel(Image image, int selectedChannel)
+{
+ Image result = { 0 };
+
+ // Security check to avoid program crash
+ if ((image.data == NULL) || (image.width == 0) || (image.height == 0))
+ return result;
+
+ // Check selected channel
+ if (selectedChannel < 0)
+ {
+ TRACELOG(LOG_WARNING, "Channel cannot be negative. Setting channel to 0.");
+ selectedChannel = 0;
+ }
+ if (image.format == PIXELFORMAT_UNCOMPRESSED_GRAYSCALE
+ || image.format == PIXELFORMAT_UNCOMPRESSED_R32
+ || image.format == PIXELFORMAT_UNCOMPRESSED_R16
+ )
+ {
+ if (selectedChannel > 0)
+ {
+ TRACELOG(LOG_WARNING, "This image has only 1 channel. Setting channel to it.");
+ selectedChannel = 0;
+ }
+ }
+ else if (image.format == PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)
+ {
+ if (selectedChannel > 1)
+ {
+ TRACELOG(LOG_WARNING, "This image has only 2 channels. Setting channel to alpha.");
+ selectedChannel = 1;
+ }
+ }
+ else if (image.format == PIXELFORMAT_UNCOMPRESSED_R5G6B5
+ || image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8
+ || image.format == PIXELFORMAT_UNCOMPRESSED_R32G32B32
+ || image.format == PIXELFORMAT_UNCOMPRESSED_R16G16B16
+ )
+ {
+ if (selectedChannel > 2)
+ {
+ TRACELOG(LOG_WARNING, "This image has only 3 channels. Setting channel to red.");
+ selectedChannel = 0;
+ }
+ }
+
+ // formats rgba
+ if (selectedChannel > 3)
+ {
+ TRACELOG(LOG_WARNING, "ImageFromChannel supports channels 0 to 3 (rgba). Setting channel to alpha.");
+ selectedChannel = 3;
+ }
+
+ result.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE;
+ result.height = image.height;
+ result.width = image.width;
+ result.mipmaps = 1;
+
+ unsigned char *pixels = (unsigned char *)RL_CALLOC(image.width * image.height, sizeof(unsigned char)); // values 0 to 255
+
+ if (image.format >= PIXELFORMAT_COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "IMAGE: Pixel data retrieval not supported for compressed image formats");
+ else
+ {
+ for (int i = 0, k = 0; i < image.width * image.height; ++i)
+ {
+ float imageValue = -1;
+ switch (image.format)
+ {
+ case PIXELFORMAT_UNCOMPRESSED_GRAYSCALE:
+ {
+ imageValue = (float)((unsigned char *)image.data)[i + selectedChannel]/255.0f;
+
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA:
+ {
+ imageValue = (float)((unsigned char *)image.data)[k + selectedChannel]/255.0f;
+
+ k += 2;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R5G5B5A1:
+ {
+ unsigned short pixel = ((unsigned short *)image.data)[i];
+
+ if (selectedChannel == 0)
+ {
+ imageValue = (float)((pixel & 0b1111100000000000) >> 11)*(1.0f/31);
+ }
+ else if (selectedChannel == 1)
+ {
+ imageValue = (float)((pixel & 0b0000011111000000) >> 6)*(1.0f/31);
+ }
+ else if (selectedChannel == 2)
+ {
+ imageValue = (float)((pixel & 0b0000000000111110) >> 1)*(1.0f/31);
+ }
+ else if (selectedChannel == 3)
+ {
+ imageValue = ((pixel & 0b0000000000000001) == 0)? 0.0f : 1.0f;
+ }
+
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R5G6B5:
+ {
+ unsigned short pixel = ((unsigned short *)image.data)[i];
+
+ if (selectedChannel == 0)
+ {
+ imageValue = (float)((pixel & 0b1111100000000000) >> 11)*(1.0f/31);
+ }
+ else if (selectedChannel == 1)
+ {
+ imageValue = (float)((pixel & 0b0000011111100000) >> 5)*(1.0f/63);
+ }
+ else if (selectedChannel == 2)
+ {
+ imageValue = (float)(pixel & 0b0000000000011111)*(1.0f/31);
+ }
+
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R4G4B4A4:
+ {
+ unsigned short pixel = ((unsigned short *)image.data)[i];
+
+ if (selectedChannel == 0)
+ {
+ imageValue = (float)((pixel & 0b1111000000000000) >> 12)*(1.0f/15);
+ }
+ else if (selectedChannel == 1)
+ {
+ imageValue = (float)((pixel & 0b0000111100000000) >> 8)*(1.0f/15);
+ }
+ else if (selectedChannel == 2)
+ {
+ imageValue = (float)((pixel & 0b0000000011110000) >> 4)*(1.0f/15);
+ }
+ else if (selectedChannel == 3)
+ {
+ imageValue = (float)(pixel & 0b0000000000001111)*(1.0f/15);
+ }
+
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R8G8B8A8:
+ {
+ imageValue = (float)((unsigned char *)image.data)[k + selectedChannel]/255.0f;
+
+ k += 4;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R8G8B8:
+ {
+ imageValue = (float)((unsigned char *)image.data)[k + selectedChannel]/255.0f;
+
+ k += 3;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R32:
+ {
+ imageValue = ((float *)image.data)[k];
+
+ k += 1;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R32G32B32:
+ {
+ imageValue = ((float *)image.data)[k + selectedChannel];
+
+ k += 3;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R32G32B32A32:
+ {
+ imageValue = ((float *)image.data)[k + selectedChannel];
+
+ k += 4;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R16:
+ {
+ imageValue = HalfToFloat(((unsigned short *)image.data)[k]);
+
+ k += 1;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R16G16B16:
+ {
+ imageValue = HalfToFloat(((unsigned short *)image.data)[k+selectedChannel]);
+
+ k += 3;
+ } break;
+ case PIXELFORMAT_UNCOMPRESSED_R16G16B16A16:
+ {
+ imageValue = HalfToFloat(((unsigned short *)image.data)[k + selectedChannel]);
+
+ k += 4;
+ } break;
+ default: break;
+ }
+
+ pixels[i] = imageValue * 255;
+ }
+ }
+
+ result.data = pixels;
+
+ return result;
+}
+
// Resize and image to new size using Nearest-Neighbor scaling algorithm
void ImageResizeNN(Image *image,int newWidth,int newHeight)
{