Skip to content

Commit

Permalink
Use the built-in jpg.js decoder for JPEG images with non-default EX…
Browse files Browse the repository at this point in the history
…IF orientation (bug 1942064)

The new EXIF parsing, during `JpegImage.canUseImageDecoder`, takes *well below* `1 ms` in my testing which should be fast enough to not be an issue.

The EXIF parsing is done using https://github.com/exif-js/exif-js/, which is released under the MIT License. Note that while it's available on NPM we're however not using that since:
 - The package is not fully up-to-date, given the latest changes in the Git repository.
 - We only need a *subset* of the functionality, and this way we're able to simplify and shorten the code.
 - Given the age of that project the code isn't directly compatible with JavaScript modules.
  • Loading branch information
Snuffleupagus committed Jan 19, 2025
1 parent 50b7922 commit 9bd4b4c
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 0 deletions.
21 changes: 21 additions & 0 deletions external/exif-js/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2008 Jacob Seidelin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
235 changes: 235 additions & 0 deletions external/exif-js/exif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/* The MIT License (MIT)
*
* Copyright (c) 2008 Jacob Seidelin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* This implementation is based on
* https://github.com/exif-js/exif-js/blob/51a8f7d2f3aa71cb03463c84088067c9a4ebe8cb/exif.js
*
* with the following modifications:
* - Removal of, for the PDF.js use-case, unneeded and unused methods.
* - Skip `ExifIFDPointer` tags, `GPSInfoIFDPointer` tags and
* thumbnail image extraction.
* - Removal of debug logging.
* - Make it pass PDF.js linting, and general modernization of the code.
*/

const TiffTags = {
0x0100: "ImageWidth",
0x0101: "ImageHeight",
0x8769: "ExifIFDPointer",
0x8825: "GPSInfoIFDPointer",
0xa005: "InteroperabilityIFDPointer",
0x0102: "BitsPerSample",
0x0103: "Compression",
0x0106: "PhotometricInterpretation",
0x0112: "Orientation",
0x0115: "SamplesPerPixel",
0x011c: "PlanarConfiguration",
0x0212: "YCbCrSubSampling",
0x0213: "YCbCrPositioning",
0x011a: "XResolution",
0x011b: "YResolution",
0x0128: "ResolutionUnit",
0x0111: "StripOffsets",
0x0116: "RowsPerStrip",
0x0117: "StripByteCounts",
0x0201: "JPEGInterchangeFormat",
0x0202: "JPEGInterchangeFormatLength",
0x012d: "TransferFunction",
0x013e: "WhitePoint",
0x013f: "PrimaryChromaticities",
0x0211: "YCbCrCoefficients",
0x0214: "ReferenceBlackWhite",
0x0132: "DateTime",
0x010e: "ImageDescription",
0x010f: "Make",
0x0110: "Model",
0x0131: "Software",
0x013b: "Artist",
0x8298: "Copyright",
};

function readTags(file, tiffStart, dirStart, strings, bigEnd) {
const entries = file.getUint16(dirStart, !bigEnd),
tags = Object.create(null);

for (let i = 0; i < entries; i++) {
const entryOffset = dirStart + i * 12 + 2;
const tag = strings[file.getUint16(entryOffset, !bigEnd)];
tags[tag] = readTagValue(file, entryOffset, tiffStart, dirStart, bigEnd);
}
return tags;
}

function readTagValue(file, entryOffset, tiffStart, dirStart, bigEnd) {
const type = file.getUint16(entryOffset + 2, !bigEnd),
numValues = file.getUint32(entryOffset + 4, !bigEnd),
valueOffset = file.getUint32(entryOffset + 8, !bigEnd) + tiffStart;
let offset, vals, val, n, numerator, denominator;

switch (type) {
case 1: // byte, 8-bit unsigned int
case 7: // undefined, 8-bit byte, value depending on field
if (numValues === 1) {
return file.getUint8(entryOffset + 8, !bigEnd);
}
offset = numValues > 4 ? valueOffset : entryOffset + 8;
vals = [];
for (n = 0; n < numValues; n++) {
vals[n] = file.getUint8(offset + n);
}
return vals;

case 2: // ascii, 8-bit byte
offset = numValues > 4 ? valueOffset : entryOffset + 8;
return getStringFromDB(file, offset, numValues - 1);

case 3: // short, 16 bit int
if (numValues === 1) {
return file.getUint16(entryOffset + 8, !bigEnd);
}
offset = numValues > 2 ? valueOffset : entryOffset + 8;
vals = [];
for (n = 0; n < numValues; n++) {
vals[n] = file.getUint16(offset + 2 * n, !bigEnd);
}
return vals;

case 4: // long, 32 bit int
if (numValues === 1) {
return file.getUint32(entryOffset + 8, !bigEnd);
}
vals = [];
for (n = 0; n < numValues; n++) {
vals[n] = file.getUint32(valueOffset + 4 * n, !bigEnd);
}
return vals;

case 5: // rational = two long values, first is numerator, second is denominator
if (numValues === 1) {
numerator = file.getUint32(valueOffset, !bigEnd);
denominator = file.getUint32(valueOffset + 4, !bigEnd);
val = numerator / denominator;
return val;
}
vals = [];
for (n = 0; n < numValues; n++) {
numerator = file.getUint32(valueOffset + 8 * n, !bigEnd);
denominator = file.getUint32(valueOffset + 4 + 8 * n, !bigEnd);
vals[n] = numerator / denominator;
}
return vals;

case 9: // slong, 32 bit signed int
if (numValues === 1) {
return file.getInt32(entryOffset + 8, !bigEnd);
}
vals = [];
for (n = 0; n < numValues; n++) {
vals[n] = file.getInt32(valueOffset + 4 * n, !bigEnd);
}
return vals;

case 10: // signed rational, two slongs, first is numerator, second is denominator
if (numValues === 1) {
return (
file.getInt32(valueOffset, !bigEnd) /
file.getInt32(valueOffset + 4, !bigEnd)
);
}
vals = [];
for (n = 0; n < numValues; n++) {
vals[n] =
file.getInt32(valueOffset + 8 * n, !bigEnd) /
file.getInt32(valueOffset + 4 + 8 * n, !bigEnd);
}
return vals;
}
throw new Error(`readTagValue - unsupported type: "${type}".`);
}

function getStringFromDB(buffer, start, length) {
let outstr = "";
for (let n = start; n < start + length; n++) {
outstr += String.fromCharCode(buffer.getUint8(n));
}
return outstr;
}

/**
* @param [DataView] file
* @param [number] start
* @return {Object | null}
*/
function readEXIFData(file, start) {
if (getStringFromDB(file, start, 4) !== "Exif") {
// console.log("Not valid EXIF data! " + getStringFromDB(file, start, 4));
return null;
}

const tiffOffset = start + 6;
let bigEnd;

// test for TIFF validity and endianness
if (file.getUint16(tiffOffset) === 0x4949) {
bigEnd = false;
} else if (file.getUint16(tiffOffset) === 0x4d4d) {
bigEnd = true;
} else {
// console.log("Not valid TIFF data! (no 0x4949 or 0x4D4D)");
return null;
}

if (file.getUint16(tiffOffset + 2, !bigEnd) !== 0x002a) {
// console.log("Not valid TIFF data! (no 0x002A)");
return null;
}

const firstIFDOffset = file.getUint32(tiffOffset + 4, !bigEnd);

if (firstIFDOffset < 0x00000008) {
// console.log(
// "Not valid TIFF data! (First offset less than 8)",
// file.getUint32(tiffOffset + 4, !bigEnd)
// );
return null;
}

const tags = readTags(
file,
tiffOffset,
tiffOffset + firstIFDOffset,
TiffTags,
bigEnd
);

// Skip `ExifIFDPointer` tags.
//
// Skip `GPSInfoIFDPointer` tags.
//
// Skip thumbnail image extraction.

return tags;
}

export { readEXIFData };
1 change: 1 addition & 0 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ function buildLib(defines, dir) {
encoding: false,
}),
gulp.src("test/unit/*.js", { base: ".", encoding: false }),
gulp.src("external/exif-js/*.js", { base: "exif-js/", encoding: false }),
gulp.src("external/openjpeg/*.js", { base: "openjpeg/", encoding: false }),
]);

Expand Down
37 changes: 37 additions & 0 deletions src/core/jpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { assert, BaseException, warn } from "../shared/util.js";
import { grayToRGBA } from "../shared/image_utils.js";
import { readEXIFData } from "../../external/exif-js/exif.js";
import { readUint16 } from "./core_utils.js";

class JpegError extends BaseException {
Expand Down Expand Up @@ -804,6 +805,16 @@ class JpegImage {
this._colorTransform = colorTransform;
}

static #getEXIF(data) {
try {
const dv = new DataView(data.buffer, data.byteOffset, data.byteLength);
return readEXIFData(dv, 0);
} catch (ex) {
warn(`#getEXIF - EXIF parsing failed: "${ex}".`);
}
return null;
}

static canUseImageDecoder(data, colorTransform = -1) {
let offset = 0;
let numComponents = null;
Expand All @@ -817,6 +828,32 @@ class JpegImage {

markerLoop: while (fileMarker !== /* EOI (End of Image) = */ 0xffd9) {
switch (fileMarker) {
case 0xffe1: // APP1 - Exif
const { appData, newOffset } = readDataBlock(data, offset);
offset = newOffset;

// 'Exif\x00\x00'
if (
appData[0] === 0x45 &&
appData[1] === 0x78 &&
appData[2] === 0x69 &&
appData[3] === 0x66 &&
appData[4] === 0 &&
appData[5] === 0
) {
const exif = this.#getEXIF(appData);

if (exif) {
// Skip images with non-default orientation
// (fixes bug1942064.pdf).
if (exif.Orientation !== undefined && exif.Orientation !== 1) {
return false;
}
}
}
fileMarker = readUint16(data, offset);
offset += 2;
continue;
case 0xffc0: // SOF0 (Start of Frame, Baseline DCT)
case 0xffc1: // SOF1 (Start of Frame, Extended DCT)
case 0xffc2: // SOF2 (Start of Frame, Progressive DCT)
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
!annotation-line-without-appearance.pdf
!bug1669099.pdf
!annotation-square-circle.pdf
!bug1942064.pdf
!annotation-square-circle-without-appearance.pdf
!annotation-stamp.pdf
!issue14048.pdf
Expand Down
Binary file added test/pdfs/bug1942064.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8997,6 +8997,13 @@
"link": true,
"type": "other"
},
{
"id": "bug1942064",
"file": "pdfs/bug1942064.pdf",
"md5": "d50b5ebb8cab1211609d16faa54ec47d",
"rounds": 1,
"type": "eq"
},
{
"id": "issue16221-text",
"file": "pdfs/issue16221.pdf",
Expand Down

0 comments on commit 9bd4b4c

Please sign in to comment.