Results 1 to 4 of 4

Thread: RGB image in RGBA format

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    19

    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 640*480*3 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

  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    19

    Re: RGB image in RGBA format

    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.

  3. #3

    Re: RGB image in RGBA format

    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.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    19

    Re: RGB image in RGBA format

    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:

    Code :
    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;
    	}
    }

Similar Threads

  1. No image format 24 bit RGB?
    By user42 in forum OpenVG and VGU
    Replies: 1
    Last Post: 09-16-2008, 12:20 PM
  2. Which RGBA texture compression format will be used?&#820
    By nemesis2k in forum OpenGL ES general technical discussions
    Replies: 1
    Last Post: 01-28-2008, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •