blend()

Image / Pixels

Description

Copies a region of pixels from one image into another with full alpha channel blending. Blend modes include BLEND, ADD, SUBTRACT, MULTIPLY, SCREEN, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION.

Syntax

void blend(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, int mode)

Parameters

NameTypeDescription
sxintx-coordinate of source region
syinty-coordinate of source region
swintsource region width
shintsource region height
dxintx-coordinate of destination
dyinty-coordinate of destination
dwintdestination width
dhintdestination height
modeintblend mode constant

Returns

void

Related

Under the Hood

From Processing.h:

static PColor blend(const PColor& src, const PColor& dst) { float sa = src.a/255.f; return PColor(src.r*sa+dst.r*(1-sa), src.g*sa+dst.g*(1-sa), src.b*sa+dst.b*(1-sa), 255); } void blend(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, int mode);

Under the Hood

From Processing.cpp:

void PApplet::blend(int sx,int sy,int sw,int sh,int dx,int dy,int dw,int dh,int mode){ std::vector<unsigned char> src(sw*sh*4); glReadPixels(sx,winHeight-(sy+sh),sw,sh,GL_RGBA,GL_UNSIGNED_BYTE,src.data()); std::vector<unsigned char> dst(dw*dh*4); for(int y=0;y<dh;y++) for(int x=0;x<dw;x++){ int srcX=(int)(x*(float)sw/dw), srcY=(int)(y*(float)sh/dh); for(int c=0;c<4;c++) dst[(y*dw+x)*4+c]=src[(srcY*sw+srcX)*4+c]; } GLenum sfact=GL_SRC_ALPHA,dfact=GL_ONE_MINUS_SRC_ALPHA; switch(mode){ case ADD: sfact=GL_SRC_ALPHA;dfact=GL_ONE;break; case MULTIPLY:sfact=GL_DST_COLOR;dfact=GL_ZERO;break; default: break; } glEnable(GL_BLEND);glBlendFunc(sfact,dfact); glWindowPos2i(dx,winHeight-(dy+dh)); glDrawPixels(dw,dh,GL_RGBA,GL_UNSIGNED_BYTE,dst.data()); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); } blend(sx,sy,sw,sh,dx,dy,dw,dh,BLEND);