Merge pull request #7591 from Snuffleupagus/jpeg-error

Change `src/core/jpg.js` to use the `error` utility function instead of `throw`ing
This commit is contained in:
Tim van der Meij 2016-09-04 20:11:09 +02:00 committed by GitHub
commit 7db1983d64
2 changed files with 52 additions and 55 deletions

View File

@ -1,4 +1,3 @@
/* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the 'License');
@ -18,26 +17,28 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs/core/jpg', ['exports'], factory);
define('pdfjs/core/jpg', ['exports', 'pdfjs/shared/util'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
factory(exports, require('../shared/util.js'));
} else {
factory((root.pdfjsCoreJpg = {}));
factory((root.pdfjsCoreJpg = {}), root.pdfjsSharedUtil);
}
}(this, function (exports) {
}(this, function (exports, sharedUtil) {
/*
This code was forked from https://github.com/notmasteryet/jpgjs. The original
version was created by github user notmasteryet
var error = sharedUtil.error;
- The JPEG specification can be found in the ITU CCITT Recommendation T.81
(www.w3.org/Graphics/JPEG/itu-t81.pdf)
- The JFIF specification can be found in the JPEG File Interchange Format
(www.w3.org/Graphics/JPEG/jfif3.pdf)
- The Adobe Application-Specific JPEG markers in the Supporting the DCT Filters
in PostScript Level 2, Technical Note #5116
(partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
*/
/**
* This code was forked from https://github.com/notmasteryet/jpgjs.
* The original version was created by GitHub user notmasteryet.
*
* - The JPEG specification can be found in the ITU CCITT Recommendation T.81
* (www.w3.org/Graphics/JPEG/itu-t81.pdf)
* - The JFIF specification can be found in the JPEG File Interchange Format
* (www.w3.org/Graphics/JPEG/jfif3.pdf)
* - The Adobe Application-Specific JPEG markers in the
* Supporting the DCT Filters in PostScript Level 2, Technical Note #5116
* (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
*/
var JpegImage = (function jpegImage() {
var dctZigZag = new Uint8Array([
@ -123,8 +124,8 @@ var JpegImage = (function jpegImage() {
if (bitsData === 0xFF) {
var nextByte = data[offset++];
if (nextByte) {
throw 'unexpected marker: ' +
((bitsData << 8) | nextByte).toString(16);
error('JPEG error: unexpected marker ' +
((bitsData << 8) | nextByte).toString(16));
}
// unstuff 0
}
@ -140,7 +141,7 @@ var JpegImage = (function jpegImage() {
return node;
}
if (typeof node !== 'object') {
throw 'invalid huffman sequence';
error('JPEG error: invalid huffman sequence');
}
}
}
@ -247,7 +248,7 @@ var JpegImage = (function jpegImage() {
}
} else {
if (s !== 1) {
throw 'invalid ACn encoding';
error('JPEG error: invalid ACn encoding');
}
successiveACNextValue = receiveAndExtend(s);
successiveACState = r ? 2 : 3;
@ -363,7 +364,7 @@ var JpegImage = (function jpegImage() {
bitsCount = 0;
marker = (data[offset] << 8) | data[offset + 1];
if (marker <= 0xFF00) {
throw 'marker was not found';
error('JPEG error: marker was not found');
}
if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
@ -388,7 +389,7 @@ var JpegImage = (function jpegImage() {
var t;
if (!qt) {
throw 'missing required Quantization Table.';
error('JPEG error: missing required Quantization Table.');
}
// inverse DCT on rows
@ -630,7 +631,7 @@ var JpegImage = (function jpegImage() {
var huffmanTablesAC = [], huffmanTablesDC = [];
var fileMarker = readUint16();
if (fileMarker !== 0xFFD8) { // SOI (Start of Image)
throw 'SOI not found';
error('JPEG error: SOI not found');
}
fileMarker = readUint16();
@ -705,7 +706,7 @@ var JpegImage = (function jpegImage() {
tableData[z] = readUint16();
}
} else {
throw 'DQT: invalid table spec';
error('JPEG error: DQT - invalid table spec');
}
quantizationTables[quantizationTableSpec & 15] = tableData;
}
@ -715,7 +716,7 @@ var JpegImage = (function jpegImage() {
case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
case 0xFFC2: // SOF2 (Start of Frame, Progressive DCT)
if (frame) {
throw 'Only single frame JPEGs supported';
error('JPEG error: Only single frame JPEGs supported');
}
readUint16(); // skip data length
frame = {};
@ -815,7 +816,7 @@ var JpegImage = (function jpegImage() {
offset -= 3;
break;
}
throw 'unknown JPEG marker ' + fileMarker.toString(16);
error('JPEG error: unknown marker ' + fileMarker.toString(16));
}
fileMarker = readUint16();
}
@ -1028,7 +1029,7 @@ var JpegImage = (function jpegImage() {
getData: function getData(width, height, forceRGBoutput) {
if (this.numComponents > 4) {
throw 'Unsupported color mode';
error('JPEG error: Unsupported color mode');
}
// type of data: Uint8Array(width * height * numComponents)
var data = this._getLinearizedBlockData(width, height);

View File

@ -922,38 +922,34 @@ var JpegStream = (function JpegStreamClosure() {
if (this.bufferLength) {
return;
}
try {
var jpegImage = new JpegImage();
var jpegImage = new JpegImage();
// checking if values needs to be transformed before conversion
if (this.forceRGB && this.dict && isArray(this.dict.get('Decode'))) {
var decodeArr = this.dict.getArray('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;
// Checking if values need to be transformed before conversion.
if (this.forceRGB && this.dict && isArray(this.dict.get('Decode'))) {
var decodeArr = this.dict.getArray('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;
}
}
jpegImage.parse(this.bytes);
var data = jpegImage.getData(this.drawWidth, this.drawHeight,
this.forceRGB);
this.buffer = data;
this.bufferLength = data.length;
this.eof = true;
} catch (e) {
error('JPEG error: ' + e);
if (transformNeeded) {
jpegImage.decodeTransform = transform;
}
}
jpegImage.parse(this.bytes);
var data = jpegImage.getData(this.drawWidth, this.drawHeight,
this.forceRGB);
this.buffer = data;
this.bufferLength = data.length;
this.eof = true;
};
JpegStream.prototype.getBytes = function JpegStream_getBytes(length) {