RGB image in RGBA format

Just a quick question - I have a 640x480 24bit RGB webcam feed I’m trying to process with some OpenCL. I’m trying to use RGBA OpenCL 2DImages, but I’m a bit confused how to go about this. Let’s say I copy my 6404803 byte image to an opencl RGBA 2DImage with a RGBA, CL_UNSIGNED_INT8 format. Isn’t the indexing messed up now? (The fourth byte is technically the red channel of the second pixel, but it’s indexed as the alpha channel of the first pixel.) Then, if I take the normal approach and get global x and y IDs in my kernel to do pixel-by-pixel processing, I’m actually grabbing a pixel and a third, right? I feel like this must be a common problem, so has anybody had to deal with this before?

Thanks in advance,
Spencer

So, I figured this out myself. The answer is YES, if you just arbitrarily copy an RGB image into an RGBA opencl 2DImage, your indexing will be messed up. I just went in an added an alpha byte for ever three color bytes in the original RGB image before copying. Problem solved!

A friend recommended a neat trick to doing this that makes your code extra readable…define RGB and RGBA structs, casting your original image data into the RGB struct. You can then allocate new memory for the 2DImage and loop through, manually copying the color channels member by member and then setting the alpha channel to 0.

Nice. Can this be applied to 3D images as well? Or even moving gifs? i am just wondering since I’m having a roadblock and I am not sure how to bypass it.

I’m not sure on gifs - literally just never messed with one before. No problem on 3D images, though - the method I used just loops through raw host memory - it doesn’t matter of you’re going to use that memory to create a buffer, 2D image, 3D image or whatever. I’m including my code below:


struct RGB
{
	char red;
	char green;
	char blue;
};

struct RGBA
{
	char red;
	char green;
	char blue;
	char alpha;
};

/* Adds alpha channel to RGB image - takes in pointers to rgb data and the allocated space for rgba data. Also takes in the size (in pixels) of both images - behavior is undefined for images of different size */
void addAlpha(char* rgb, char* rgba, int size)
{
	RGB* rgbPtr = (RGB*)rgb;
	RGBA* rgbaPtr= (RGBA*)rgba;
	for (int i=0; i < size; i++)
	{
		rgbaPtr[i].red = rgbPtr[i].red;
		rgbaPtr[i].green = rgbPtr[i].green;
		rgbaPtr[i].blue = rgbPtr[i].blue;
		rgbaPtr[i].alpha = 0;
	}
}

/* Removes alpha channel from RGBA image - takes in pointers to rgba data and the allocated space for rgb data. Also takes in the size (in pixels) of both images - behavior is undefined for images of different size. */
void removeAlpha(char* rgb, char* rgba, int size)
{
	RGB* rgbPtr = (RGB*)rgb;
	RGBA* rgbaPtr = (RGBA*)rgba;
	for (int i=0; i < size; i++)
	{
		rgbPtr[i].red = rgbaPtr[i].red;
		rgbPtr[i].green = rgbaPtr[i].green;
		rgbPtr[i].blue = rgbaPtr[i].blue;
	}
}