Processing: AlphaBlend

Date
2007-04-05 (Thu)
Category
Processing

Processing を久々にやっているので、ピクセル処理でいつも忘れるのでメモ。

image() っていつも思うが、なぜキャンバスに直接しかかけないのだろうか。 PImage 同士の合成ができてもいいじゃん!という事で書いたのがこれです。ポイントは AlphaBlend でしょうか…

void compositeImage (PImage top, int x, int y, PImage base) {
  int curX = 0;
  int curY = 0;
  for (int i = 0; i < top.pixels.length; i++) {
    curX = x + iToX(i, top.width);
    curY = y + iToY(i, top.width);
    if ((curX < base.width) && (curY < base.height)) {
      base.pixels[xyToI(curX, curY, base.width)] = blendByPixel(top.pixels[i], base.pixels[xyToI(curX, curY, base.width)]);
    }
  }
}
int blendByPixel (int top, int bottom) { int result = 0; int tA = (0xFF000000 & top) >>> 24;
if (tA == 255) { result = top; } else if (tA != 0) { result = alphaBlend(top, bottom, tA); } else { result = bottom; } return result; }
int alphaBlend(int fg, int bg, int a) {
int fR = (0x00FF0000 & fg) >>> 16; int fG = (0x0000FF00 & fg) >>> 8; int fB = 0x000000FF & fg;
int bR = (0x00FF0000 & bg) >>> 16; int bG = (0x0000FF00 & bg) >>> 8; int bB = 0x000000FF & bg;
int rR = (((fR - bR) * a) >>> 8 ) + bR; int rG = (((fG - bG) * a) >>> 8 ) + bG; int rB = (((fB - bB) * a) >>> 8 ) + bB;
return 0xFF000000 | (rR << 16) | (rG << 8) | rB; }
int iToX (int i, int w) { return i % w; }
int iToY (int i, int w) { return i / w; }
int xyToI (int x, int y, int w) { return (w * y) + x; }

Comment:0

Comment Form

Remember Me?


Trackback:0

TrackBack URL for this entry
http://blogs.grf-design.com/mt/mt-tb.cgi/202
Listed below are links to weblogs that reference
Processing: AlphaBlend from The Croton

Return to Page Top