-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the built-in
jpg.js
decoder for JPEG images with non-default EX…
…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
1 parent
50b7922
commit 9bd4b4c
Showing
7 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters