Merge pull request #4201 from yurydelendik/smask-backdrop

Fixes smask backdrop
This commit is contained in:
Brendan Dahl 2014-01-27 11:16:37 -08:00
commit c2421c2897

View File

@ -531,29 +531,28 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var maskCtx = smask.context; var maskCtx = smask.context;
var width = mask.width, height = mask.height; var width = mask.width, height = mask.height;
var removeBackdropFn; var addBackdropFn;
if (smask.backdrop) { if (smask.backdrop) {
var cs = smask.colorSpace || ColorSpace.singletons.rgb; var cs = smask.colorSpace || ColorSpace.singletons.rgb;
var backdrop = cs.getRgb(smask.backdrop, 0); var backdrop = cs.getRgb(smask.backdrop, 0);
removeBackdropFn = function (r0, g0, b0, layerDataBytes) { addBackdropFn = function (r0, g0, b0, bytes) {
var length = layerDataBytes.length; var length = bytes.length;
for (var i = 3; i < length; i += 4) { for (var i = 3; i < length; i += 4) {
var alpha = layerDataBytes[i]; var alpha = bytes[i] / 255;
if (alpha !== 0 && alpha !== 255) { if (alpha === 0) {
var r = ((layerDataBytes[i - 3] * 255 - bytes[i - 3] = r0;
r0 * (255 - alpha)) / alpha) | 0; bytes[i - 2] = g0;
layerDataBytes[i - 3] = r < 0 ? 0 : r > 255 ? 255 : r; bytes[i - 1] = b0;
var g = ((layerDataBytes[i - 2] * 255 - } else if (alpha < 1) {
g0 * (255 - alpha)) / alpha) | 0; var alpha_ = 1 - alpha;
layerDataBytes[i - 2] = g < 0 ? 0 : g > 255 ? 255 : g; bytes[i - 3] = (bytes[i - 3] * alpha + r0 * alpha_) | 0;
var b = ((layerDataBytes[i - 1] * 255 - bytes[i - 2] = (bytes[i - 2] * alpha + g0 * alpha_) | 0;
b0 * (255 - alpha)) / alpha) | 0; bytes[i - 1] = (bytes[i - 1] * alpha + b0 * alpha_) | 0;
layerDataBytes[i - 1] = b < 0 ? 0 : b > 255 ? 255 : b;
} }
} }
}.bind(null, backdrop[0], backdrop[1], backdrop[2]); }.bind(null, backdrop[0], backdrop[1], backdrop[2]);
} else { } else {
removeBackdropFn = function () {}; addBackdropFn = function () {};
} }
var composeFn; var composeFn;
@ -584,7 +583,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var maskData = maskCtx.getImageData(0, row, width, chunkHeight); var maskData = maskCtx.getImageData(0, row, width, chunkHeight);
var layerData = layerCtx.getImageData(0, row, width, chunkHeight); var layerData = layerCtx.getImageData(0, row, width, chunkHeight);
removeBackdropFn(layerData.data); addBackdropFn(maskData.data);
composeFn(maskData.data, layerData.data); composeFn(maskData.data, layerData.data);
maskCtx.putImageData(layerData, 0, row); maskCtx.putImageData(layerData, 0, row);