Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type3 smoothing: pre-scale image in the paintImageMaskXObject #1763

Merged
merged 3 commits into from
Jun 6, 2012
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,40 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
}
}
}
function rescaleImage(pixels, widthScale, heightScale) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a resize in image.js too. Do we want to try and share code between the two?
https://github.com/mozilla/pdf.js/blob/master/src/image.js#L131

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specialized algorithm is addressed only this specific problem. The algorithm in the image.js does not take in account pixels "in between" steps of copy operation, this does. Which gives you this smoothing effect. Also, I don't this we have to use this (in canvas.js) algorithm in some other place.

var scaledWidth = Math.ceil(width / widthScale);
var scaledHeight = Math.ceil(height / heightScale);

var itemsSum = new Uint32Array(scaledWidth * scaledHeight * 4);
var itemsCount = new Uint32Array(scaledWidth * scaledHeight);
for (var i = 0, position = 0; i < height; i++) {
var lineOffset = (0 | (i / heightScale)) * scaledWidth;
for (var j = 0; j < width; j++) {
var countOffset = lineOffset + (0 | (j / widthScale));
var sumOffset = countOffset << 2;
itemsSum[sumOffset] += pixels[position];
itemsSum[sumOffset + 1] += pixels[position + 1];
itemsSum[sumOffset + 2] += pixels[position + 2];
itemsSum[sumOffset + 3] += pixels[position + 3];
itemsCount[countOffset]++;
position += 4;
}
}
var tmpCanvas = createScratchCanvas(scaledWidth, scaledHeight);
var tmpCtx = tmpCanvas.getContext('2d');
var imgData = tmpCtx.getImageData(0, 0, scaledWidth, scaledHeight);
pixels = imgData.data;
for (var i = 0, j = 0, ii = scaledWidth * scaledHeight; i < ii; i++) {
var count = itemsCount[i];
pixels[j] = itemsSum[j] / count;
pixels[j + 1] = itemsSum[j + 1] / count;
pixels[j + 2] = itemsSum[j + 2] / count;
pixels[j + 3] = itemsSum[j + 3] / count;
j += 4;
}
tmpCtx.putImageData(imgData, 0, 0);
return tmpCanvas;
}

this.save();

Expand All @@ -1116,8 +1150,18 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {

applyStencilMask(pixels, inverseDecode);

tmpCtx.putImageData(imgData, 0, 0);
ctx.drawImage(tmpCanvas, 0, -h);
var currentTransform = ctx.mozCurrentTransformInverse;
var widthScale = Math.max(Math.abs(currentTransform[0]), 1);
var heightScale = Math.max(Math.abs(currentTransform[3]), 1);
if (widthScale >= 2 || heightScale >= 2) {
// canvas does not resize well large images to small -- using simple
// algorithm to perform pre-scaling
tmpCanvas = rescaleImage(imgData.data, widthScale, heightScale);
ctx.scale(widthScale, heightScale);
ctx.drawImage(tmpCanvas, 0, -h / heightScale);
} else
tmpCtx.putImageData(imgData, 0, 0);
ctx.drawImage(tmpCanvas, 0, -h);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing brackets?

this.restore();
},

Expand Down