Convert to alpha


I’m dealing with a little piece of code:

    (pixels is "unsigned char * pixels" who comes from a grab from a camera)      
    ofImage vidBuffer;  
    vidBuffer.setFromPixels(pixels, w, h, OF_IMAGE_COLOR, true);  

The problem is that, if I use OF_IMAGE_COLOR, I can not make alpha modifications to my “vidBuffer” object. Obviously, if I say that is an OF_IMAGE_COLOR_ALPHA type of image, it scrambles the image (because it suppose that is a 4-bits image -while converting- but is actually 3-bits image).

To simplify the question:
Does anybody knows a way to convert a non-Alpha-image-object to an Alpha-image-object?

Thanks a lot, F.

Hmmm… this spring is really hot in Buenos Aires: I forgot to…


Thanks :slight_smile:

you might just create a separate RGBA image and loop through the RGB souce image’s pixels and grab the RGB components. it’s not the most optimized method, but it should do the trick.


ofImage vidBuffer;

unsigned char * vidPixels = videoCamera.getPixels();

int pixelCount = w * h;

unsigned char * newPixels = new unsigned char[pixelCount * 4];

for(int i=0; i<pixelCount; i++){

int srcPos = i * 3;
int dstPos = i * 4;

// grab the r/g/b components from the source
// set the alpha manually
newPixels[dstPos] = vidPixels[srcPos];
newPixels[dstPos+1] = vidPixels[srcPos + 1];
newPixels[dstPos+2] = vidPixels[srcPos + 2];
newPixels[dstPos+3] = 255;

ofImage vidBuffer;
vidBuffer.setFromPixels(newPixels, w, h, OF_IMAGE_COLOR_ALPHA, true);