Merge pull request #2042 from brendandahl/color-key-mask

Adds support for color key mask.
This commit is contained in:
Yury Delendik 2012-08-29 11:40:09 -07:00
commit cfca36c535
3 changed files with 55 additions and 18 deletions

View File

@ -97,7 +97,12 @@ var PDFImage = (function PDFImageClosure() {
if (smask) {
this.smask = new PDFImage(xref, res, smask, false);
} else if (mask) {
this.mask = new PDFImage(xref, res, mask, false);
if (isStream(mask)) {
this.mask = new PDFImage(xref, res, mask, false);
} else {
// Color key mask (just an array).
this.mask = mask;
}
}
}
/**
@ -129,11 +134,15 @@ var PDFImage = (function PDFImageClosure() {
maskPromise.resolve(null);
} else {
smaskPromise.resolve(null);
if (mask && isStream(mask)) {
handleImageData(handler, xref, res, mask, maskPromise);
} else if (mask) {
TODO('handle color key masking');
maskPromise.resolve(null);
if (mask) {
if (isStream(mask)) {
handleImageData(handler, xref, res, mask, maskPromise);
} else if (isArray(mask)) {
maskPromise.resolve(mask);
} else {
warn('Unsupported mask format.');
maskPromise.resolve(null);
}
} else {
maskPromise.resolve(null);
}
@ -279,7 +288,7 @@ var PDFImage = (function PDFImageClosure() {
}
return output;
},
getOpacity: function PDFImage_getOpacity(width, height) {
getOpacity: function PDFImage_getOpacity(width, height, image) {
var smask = this.smask;
var mask = this.mask;
var originalWidth = this.width;
@ -294,18 +303,40 @@ var PDFImage = (function PDFImageClosure() {
if (sw != width || sh != height)
buf = PDFImage.resize(buf, smask.bpc, 1, sw, sh, width, height);
} else if (mask) {
var sw = mask.width;
var sh = mask.height;
buf = new Uint8Array(sw * sh);
mask.numComps = 1;
mask.fillGrayBuffer(buf);
if (mask instanceof PDFImage) {
var sw = mask.width;
var sh = mask.height;
buf = new Uint8Array(sw * sh);
mask.numComps = 1;
mask.fillGrayBuffer(buf);
// Need to invert values in buffer
for (var i = 0, ii = sw * sh; i < ii; ++i)
buf[i] = 255 - buf[i];
// Need to invert values in buffer
for (var i = 0, ii = sw * sh; i < ii; ++i)
buf[i] = 255 - buf[i];
if (sw != width || sh != height)
buf = PDFImage.resize(buf, mask.bpc, 1, sw, sh, width, height);
if (sw != width || sh != height)
buf = PDFImage.resize(buf, mask.bpc, 1, sw, sh, width, height);
} else if (isArray(mask)) {
// Color key mask: if any of the compontents are outside the range
// then they should be painted.
buf = new Uint8Array(width * height);
var numComps = this.numComps;
for (var i = 0, ii = width * height; i < ii; ++i) {
var opacity = 0;
var imageOffset = i * numComps;
for (var j = 0; j < numComps; ++j) {
var color = image[imageOffset + j];
var maskOffset = j * 2;
if (color < mask[maskOffset] || color > mask[maskOffset + 1]) {
opacity = 255;
break;
}
}
buf[i] = opacity;
}
} else {
error('Unknown mask format.');
}
} else {
buf = new Uint8Array(width * height);
for (var i = 0, ii = width * height; i < ii; ++i)
@ -357,7 +388,7 @@ var PDFImage = (function PDFImageClosure() {
comps = PDFImage.resize(comps, this.bpc, 3, originalWidth,
originalHeight, width, height);
var compsPos = 0;
var opacity = this.getOpacity(width, height);
var opacity = this.getOpacity(width, height, imgArray);
var opacityPos = 0;
var length = width * actualHeight * 4;

BIN
test/pdfs/colorkeymask.pdf Normal file

Binary file not shown.

View File

@ -673,5 +673,11 @@
"md5": "2ac7c68e26a8ef797aead15e4875cc6d",
"rounds": 1,
"type": "load"
},
{ "id": "colorkeymask",
"file": "pdfs/colorkeymask.pdf",
"md5": "9f11e815b485f7f0e1fa5c116c636cf9",
"rounds": 1,
"type": "eq"
}
]