Skip to content

Commit

Permalink
Enable/disable image smoothing based on image interpolate value. (bug…
Browse files Browse the repository at this point in the history
… 1722191)

While some of the output looks worse to my eye, this behavior more
closely matches what I see when I open the PDFs in Adobe acrobat.

Fixes: #4706, #9713
  • Loading branch information
brendandahl committed Sep 10, 2021
1 parent 8a79f13 commit 1ebb595
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ class PartialEvaluator {
}

const imageMask = dict.get("ImageMask", "IM") || false;
const interpolate = dict.get("Interpolate", "I");
let imgData, args;
if (imageMask) {
// This depends on a tmpCanvas being filled with the
Expand All @@ -612,6 +613,7 @@ class PartialEvaluator {
height,
imageIsFromDecodeStream: image instanceof DecodeStream,
inverseDecode: !!decode && decode[0] > 0,
interpolate,
});
imgData.cached = !!cacheKey;
args = [imgData];
Expand Down
6 changes: 4 additions & 2 deletions src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class PDFImage {
this.width = width;
this.height = height;

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

Expand Down Expand Up @@ -294,6 +294,7 @@ class PDFImage {
height,
imageIsFromDecodeStream,
inverseDecode,
interpolate,
}) {
if (
typeof PDFJSDev === "undefined" ||
Expand Down Expand Up @@ -339,7 +340,7 @@ class PDFImage {
}
}

return { data, width, height };
return { data, width, height, interpolate };
}

get drawWidth() {
Expand Down Expand Up @@ -593,6 +594,7 @@ class PDFImage {
const imgData = {
width: drawWidth,
height: drawHeight,
interpolate: this.interpolate,
kind: 0,
data: null,
// Other fields are filled in below.
Expand Down
21 changes: 21 additions & 0 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,19 @@ function composeSMask(ctx, smask, layerCtx) {
ctx.drawImage(mask, 0, 0);
}

function getImageSmoothingEnabled(transform, interpolate) {
const scale = Util.singularValueDecompose2dScale(transform);
if (interpolate !== undefined) {
// If the value is explicitly set use it.
return interpolate;
} else if (scale[0] < 1 || scale[1] < 1) {
// Smooth when downscaling.
return true;
}
// Don't smooth when upscaling.
return false;
}

const LINE_CAP_STYLES = ["butt", "round", "square"];
const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
const NORMAL_CLIP = {};
Expand Down Expand Up @@ -1183,6 +1196,10 @@ class CanvasGraphics {
maskCanvas.canvas,
fillCtx.mozCurrentTransformInverse
);
fillCtx.imageSmoothingEnabled = getImageSmoothingEnabled(
fillCtx.mozCurrentTransform,
img.interpolate
);
fillCtx.drawImage(
scaled.img,
0,
Expand Down Expand Up @@ -2663,6 +2680,10 @@ class CanvasGraphics {
}

const scaled = this._scaleImage(imgToPaint, ctx.mozCurrentTransformInverse);
ctx.imageSmoothingEnabled = getImageSmoothingEnabled(
ctx.mozCurrentTransform,
imgData.interpolate
);
ctx.drawImage(
scaled.img,
0,
Expand Down
2 changes: 2 additions & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
!issue8229.pdf
!issue8276_reduced.pdf
!issue8372.pdf
!issue9713.pdf
!xfa_filled_imm1344e.pdf
!issue8424.pdf
!issue8480.pdf
Expand Down Expand Up @@ -150,6 +151,7 @@
!complex_ttf_font.pdf
!issue3694_reduced.pdf
!extgstate.pdf
!issue4706.pdf
!rotation.pdf
!simpletype3font.pdf
!sizes.pdf
Expand Down
Binary file added test/pdfs/issue4706.pdf
Binary file not shown.
Binary file added test/pdfs/issue9713.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3110,6 +3110,12 @@
"rounds": 1,
"type": "eq"
},
{ "id": "issue4706",
"file": "pdfs/issue4706.pdf",
"md5": "f3e90a3cf52550583fa2a07a138b8660",
"rounds": 1,
"type": "eq"
},
{ "id": "issue11242",
"file": "pdfs/issue11242_reduced.pdf",
"md5": "ba50b6ee537f3e815ccfe0c99e598e05",
Expand Down Expand Up @@ -4421,6 +4427,12 @@
"link": true,
"type": "eq"
},
{ "id": "issue9713",
"file": "pdfs/issue9713.pdf",
"md5": "a62bd42d12271105b26a68c8eae5ea5f",
"rounds": 1,
"type": "eq"
},
{ "id": "issue1936-text",
"file": "pdfs/issue1936.pdf",
"md5": "7302eb9b6a626308e2a933aaed9e1756",
Expand Down

0 comments on commit 1ebb595

Please sign in to comment.