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