VladZpp commited on
Commit
850a25d
·
verified ·
1 Parent(s): f5d16f8

EXIF rules of Pillow 13

Browse files
Files changed (1) hide show
  1. image-header.js +12 -6
image-header.js CHANGED
@@ -32,13 +32,15 @@ export function sniffFormat(b) {
32
 
33
  // --- EXIF and XMP ----------------------------------------------------------------------
34
 
35
- // Orientation from a TIFF-structured EXIF block, read like Pillow's Exif.load and
36
  // ImageFileDirectory_v2.load:
37
  // - the header must be one of Pillow's TIFF prefixes, or Pillow raises (ExifError here);
38
- // - entries of unknown type are skipped; an entry whose data lies outside the block stops
39
- // the whole directory, keeping what was read before it (Pillow's truncated-read path);
40
- // - the last entry for a tag wins, whatever its type; numeric types give their first
41
- // value, byte and string types never match an orientation.
 
 
42
  // Returns the value (NaN when not numeric) or null when there is none.
43
  export class ExifError extends Error {}
44
  const UNIT = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8, 13: 4, 16: 8 };
@@ -77,17 +79,21 @@ export function exifOrientation(exif) {
77
  const ifd = u32(b, 4, le);
78
  if (ifd + 2 > b.length) return null;
79
  let found = null;
 
80
  const count = u16(b, ifd, le);
81
  for (let i = 0; i < count; i++) {
82
  const e = ifd + 2 + i * 12;
83
  if (e + 12 > b.length) break;
84
  const tag = u16(b, e, le), type = u16(b, e + 2, le), n = u32(b, e + 4, le);
 
85
  if (!UNIT[type]) continue;
86
  const size = UNIT[type] * n;
87
  let at = e + 8;
88
  if (size > 4) {
89
  at = u32(b, e + 8, le);
90
- if (at + size > b.length) break;
 
 
91
  }
92
  if (size === 0) continue;
93
  if (tag === 0x0112) found = firstValue(b, at, type, le);
 
32
 
33
  // --- EXIF and XMP ----------------------------------------------------------------------
34
 
35
+ // Orientation from a TIFF-structured EXIF block, read like Pillow 13's Exif.load and
36
  // ImageFileDirectory_v2.load:
37
  // - the header must be one of Pillow's TIFF prefixes, or Pillow raises (ExifError here);
38
+ // - entries of unknown type, or with no data, are skipped;
39
+ // - out-of-line data counts towards a total for the whole directory; an entry that takes
40
+ // the total past the block's size is skipped, and one whose data runs past the end of the
41
+ // block stops the directory, keeping what was read before it;
42
+ // - the first stored entry for a tag wins; numeric types give their first value, byte and
43
+ // string types never match an orientation.
44
  // Returns the value (NaN when not numeric) or null when there is none.
45
  export class ExifError extends Error {}
46
  const UNIT = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8, 13: 4, 16: 8 };
 
79
  const ifd = u32(b, 4, le);
80
  if (ifd + 2 > b.length) return null;
81
  let found = null;
82
+ let outOfLine = 0;
83
  const count = u16(b, ifd, le);
84
  for (let i = 0; i < count; i++) {
85
  const e = ifd + 2 + i * 12;
86
  if (e + 12 > b.length) break;
87
  const tag = u16(b, e, le), type = u16(b, e + 2, le), n = u32(b, e + 4, le);
88
+ if (tag === 0x0112 && found !== null) continue; // the first stored entry wins
89
  if (!UNIT[type]) continue;
90
  const size = UNIT[type] * n;
91
  let at = e + 8;
92
  if (size > 4) {
93
  at = u32(b, e + 8, le);
94
+ outOfLine += size;
95
+ if (outOfLine > b.length) continue; // larger than the block: skipped
96
+ if (at + size > b.length) break; // a truncated read stops the directory
97
  }
98
  if (size === 0) continue;
99
  if (tag === 0x0112) found = firstValue(b, at, type, le);