Faster image masking (actual pixels, not alpha)

Hi all,

Im trying to apply a mask to an image.
By apply I mean operate on the pixels themselves rather than the alpha layer.
Im doing this as I am working with openCV downstream, and my alpha layer doesnt seem to be respected / applied.

at the moment I am doing this :

ofImage ofApp::applyMask(ofImage image, ofImage mask){
    ofPixels pixelsSource, pixelsMask;
    pixelsSource = image.getPixels();
    pixelsMask = mask.getPixels();
    
    ofImage result;
    result.allocate(image.getWidth(), image.getHeight(), image.getImageType());
    ofPixels pixelsResult;
    pixelsResult = result.getPixels();
    
    
    for (int y = 0; y < image.getHeight(); y++){
        for (int x = 0; x < image.getWidth(); x++){
            int i = (y*image.getWidth()*3)+(x*3);
            
            pixelsResult[i  ] = pixelsSource[i  ]*(pixelsMask[i  ]/255.0f);
            pixelsResult[i+1] = pixelsSource[i+1]*(pixelsMask[i+1]/255.0f);  //mask offset not nesicsary but maybe....
            pixelsResult[i+2] = pixelsSource[i+2]*(pixelsMask[i+2]/255.0f);
            
        }
    }
    
    result.setFromPixels(pixelsResult);
    return result;
}

I have two different masks, and the framerate drops to like half manually going though each frame pixel by pixel.

Is there a way to use the GPU or ofTexture to apply an alpha channel or mask to the actual pixels
ELSE
Is there a way to get openCV to apply the mask?

Thanks a mill
Theron

I would recommend you to write a simple shader to do your pixel operation.
Your code seems like multiply blend mode too.
if it is the case you can look this command ofEnableBlendMode()
Cheers

Ah! shaders! forgot they existed… haha, neat, will give it a go, thanks

you can use
ofEnableBlendMode(OF_BLENDMODE_MULTIPLY)
and draw one image on the top of another (same position) and see if you get the desired effect.