[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
This commit is contained in:
Bruno Cabral
2024-06-30 01:33:32 -07:00
committed by GitHub
parent 5b8efd68ba
commit 6e2661f92d
4 changed files with 309 additions and 0 deletions

View File

@@ -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)
{