Skip to content

Commit

Permalink
Takes SMask's preblending in account
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Jul 2, 2013
1 parent 81fa4a0 commit 419bee1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/image.js
Expand Up @@ -70,6 +70,7 @@ var PDFImage = (function PDFImageClosure() {

this.interpolate = dict.get('Interpolate', 'I') || false;
this.imageMask = dict.get('ImageMask', 'IM') || false;
this.matte = dict.get('Matte') || false;

var bitsPerComponent = image.bitsPerComponent;
if (!bitsPerComponent) {
Expand Down Expand Up @@ -386,6 +387,34 @@ var PDFImage = (function PDFImageClosure() {
}
return buf;
},
undoPreblend: function PDFImage_undoPreblend(buffer, width, height) {
var matte = this.smask && this.smask.matte;
if (!matte) {
return;
}

function clamp(value) {
return (value < 0 ? 0 : value > 255 ? 255 : value) | 0;
}

var matteRgb = this.colorSpace.getRgb(matte, 0);
var length = width * height * 4;
for (var i = 0; i < length; i += 4) {
var alpha = buffer[i + 3];
if (alpha === 0) {
// according formula we have to get Infinity in all components
// making it as white (tipical paper color) should be okay
buffer[i] = 255;
buffer[i + 1] = 255;
buffer[i + 2] = 255;
continue;
}
var k = 255 / alpha;
buffer[i] = clamp((buffer[i] - matteRgb[0]) * k + matteRgb[0]);
buffer[i + 1] = clamp((buffer[i + 1] - matteRgb[1]) * k + matteRgb[1]);
buffer[i + 2] = clamp((buffer[i + 2] - matteRgb[2]) * k + matteRgb[2]);
}
},
fillRgbaBuffer: function PDFImage_fillRgbaBuffer(buffer, width, height) {
var numComps = this.numComps;
var originalWidth = this.width;
Expand Down Expand Up @@ -422,6 +451,8 @@ var PDFImage = (function PDFImageClosure() {
buffer[i + 2] = rgbBuf[compsPos++];
buffer[i + 3] = opacity[opacityPos++];
}

this.undoPreblend(buffer, width, actualHeight);
},
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) {
var numComps = this.numComps;
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/bug888437.pdf.link
@@ -0,0 +1 @@
https://bug888437.bugzilla.mozilla.org/attachment.cgi?id=770341
7 changes: 7 additions & 0 deletions test/test_manifest.json
Expand Up @@ -1015,6 +1015,13 @@
"link": true,
"type": "eq"
},
{ "id": "bug888437",
"file": "pdfs/bug888437.pdf",
"md5": "93370cd589a08732a05601441c899bcb",
"rounds": 1,
"link": true,
"type": "eq"
},
{ "id": "annotation-tx",
"file": "pdfs/annotation-tx.pdf",
"md5": "56321ea830be9c4f8437ca17ac535b2d",
Expand Down

0 comments on commit 419bee1

Please sign in to comment.