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

Adds color components decoding to the JPEG #4886

Merged
merged 2 commits into from
Jun 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/core/jpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ var JpegImage = (function jpegImage() {
var scaleX = this.width / width, scaleY = this.height / height;

var component, componentScaleX, componentScaleY, blocksPerScanline;
var x, y, i, j;
var x, y, i, j, k;
var index;
var offset = 0;
var output;
Expand Down Expand Up @@ -843,6 +843,17 @@ var JpegImage = (function jpegImage() {
}
}
}

// decodeTransform will contains pairs of multiplier (-256..256) and
// additive
var transform = this.decodeTransform;
if (transform) {
for (i = 0; i < dataLength;) {
for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
data[i] = ((data[i] * transform[k]) >> 8) + transform[k + 1];
}
}
}
return data;
},

Expand All @@ -852,8 +863,6 @@ var JpegImage = (function jpegImage() {
return true;
} else if (this.numComponents == 3) {
return true;
} else if (typeof this.colorTransform !== 'undefined') {
return !!this.colorTransform;
} else {
return false;
}
Expand Down
22 changes: 20 additions & 2 deletions src/core/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,27 @@ var JpegStream = (function JpegStreamClosure() {
}
try {
var jpegImage = new JpegImage();
if (this.colorTransform != -1) {
jpegImage.colorTransform = this.colorTransform;

// checking if values needs to be transformed before conversion
if (this.dict && isArray(this.dict.get('Decode'))) {
var decodeArr = this.dict.get('Decode');
var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
var decodeArrLength = decodeArr.length;
var transform = new Int32Array(decodeArrLength);
var transformNeeded = false;
var maxValue = (1 << bitsPerComponent) - 1;
for (var i = 0; i < decodeArrLength; i += 2) {
transform[i] = ((decodeArr[i + 1] - decodeArr[i]) * 256) | 0;
transform[i + 1] = (decodeArr[i] * maxValue) | 0;
if (transform[i] !== 256 || transform[i + 1] !== 0) {
transformNeeded = true;
}
}
if (transformNeeded) {
jpegImage.decodeTransform = transform;
}
}

jpegImage.parse(this.bytes);
var data = jpegImage.getData(this.drawWidth, this.drawHeight,
/* forceRGBoutput = */true);
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/issue4883.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://web.archive.org/web/20140603135304/http://www.tipp2014.nl/TIPP2014_3rd_bulletin.pdf
10 changes: 10 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,16 @@
"lastPage": 2,
"type": "eq"
},
{ "id": "issue4883",
"file": "pdfs/issue4883.pdf",
"md5": "2fac0d9a189ca5fcef8626153d050be8",
"rounds": 1,
"link": true,
"firstPage": 1,
"lastPage": 1,
"type": "eq",
"about": "CMYK JPEG with Decode"
},
{ "id": "bug903856",
"file": "pdfs/bug903856.pdf",
"md5": "286eaa9d06a5809f4f08f2093cef8f3f",
Expand Down