Last active
August 29, 2015 13:58
-
-
Save badgeek/9947476 to your computer and use it in GitHub Desktop.
alpha blend image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void imgBlend::blendImage(int posX, int posY, ofImage &imgfg, ofImage &imgbg, ofImage &imgdst) | |
{ | |
int imgFgWidth = imgfg.getWidth(); | |
int imgFgHeight = imgfg.getHeight(); | |
int imgBgWidth = imgbg.getWidth(); | |
int imgBgHeight = imgbg.getHeight(); | |
unsigned char * pix_bg = imgbg.getPixelsRef().getPixels(); | |
unsigned char * pix_fg = imgfg.getPixelsRef().getPixels(); | |
imgdst.getPixelsRef().setFromPixels(pix_bg,imgBgWidth,imgBgHeight, OF_IMAGE_COLOR ); //copy bg image to dst, auto allocate | |
imgdst.setImageType(OF_IMAGE_COLOR_ALPHA); //add alpha channel to imgdst | |
unsigned char * pix_dst = imgdst.getPixelsRef().getPixels(); | |
int channel_dst = imgdst.getPixelsRef().getBytesPerPixel(); | |
int channel_bg = imgbg.getPixelsRef().getBytesPerPixel(); | |
int channel_fg = imgfg.getPixelsRef().getBytesPerPixel(); | |
for (int x = 0 ; x < imgFgWidth; x++) | |
{ | |
for (int y = 0 ; y < imgFgHeight; y++) | |
{ | |
int arrpos_fg = (y*channel_fg*imgFgWidth) + x*channel_fg; | |
int arrpos_dst = ((y+posY)*channel_dst*imgBgWidth) + (x+posX)*channel_dst; | |
//output = alpha * foreground + (1-alpha) * background | |
float alpha = pix_fg[arrpos_fg+3]/255.0; | |
float Rp = alpha * pix_fg[arrpos_fg+0] + (1.0-alpha) * pix_dst[arrpos_dst+0]; | |
float Gp = alpha * pix_fg[arrpos_fg+1] + (1.0-alpha) * pix_dst[arrpos_dst+1]; | |
float Bp = alpha * pix_fg[arrpos_fg+2] + (1.0-alpha) * pix_dst[arrpos_dst+2]; | |
pix_dst[arrpos_dst] = (int) Rp; | |
pix_dst[arrpos_dst+1] = (int) Gp; | |
pix_dst[arrpos_dst+2] = (int) Bp; | |
pix_dst[arrpos_dst+3] = 255; | |
} | |
} | |
imgdst.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment