| 3168 | // Based on: R. Dura, 2009, http://mouaif.wordpress.com/2009/01/05/photoshop-math-with-glsl-shaders/ |
| 3169 | |
| 3170 | function composite(img1, img2, dx, dy, operator) { |
| 3171 | /* Returns a new Image by mixing img1 (the destination) with blend image img2 (the source). |
| 3172 | * The given operator is a function(pixel1, pixel2) that returns a new pixel (RGBA-array). |
| 3173 | * This is used to implement alpha compositing and blend modes. |
| 3174 | * - dx: horizontal offset (in pixels) of the blend layer. |
| 3175 | * - dy: vertical offset (in pixels) of the blend layer. |
| 3176 | */ |
| 3177 | //var t = new Date().getTime(); |
| 3178 | dx = dx || 0; |
| 3179 | dy = dy || 0; |
| 3180 | var pixels1 = new Pixels(img1); |
| 3181 | var pixels2 = new Pixels(img2); |
| 3182 | for (var j=0; j < pixels1.height; j++) { |
| 3183 | for (var i=0; i < pixels1.width; i++) { |
| 3184 | if (0 <= i-dx && i-dx < pixels2.width) { |
| 3185 | if (0 <= j-dy && j-dy < pixels2.height) { |
| 3186 | var p1 = pixels1.get(i + j * pixels1.width); |
| 3187 | var p2 = pixels2.get((i-dx) + (j-dy) * pixels2.width); |
| 3188 | pixels1.set(i + j * pixels1.width, operator(p1, p2)); |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | pixels1.update(); |
| 3194 | //console.log(new Date().getTime() - t); |
| 3195 | return pixels1.image(); |
| 3196 | } |
| 3197 | |
| 3198 | function transparent(img, alpha) { |
| 3199 | /* Returns a transparent version of the image. |