summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/extras/exif.cc
blob: 7d926558c3dd80ddb02a0aac1be86f24bbb9ebe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/extras/exif.h"

#include "lib/jxl/base/byte_order.h"

namespace jxl {

constexpr uint16_t kExifOrientationTag = 274;

void ResetExifOrientation(std::vector<uint8_t>& exif) {
  if (exif.size() < 12) return;  // not enough bytes for a valid exif blob
  bool bigendian;
  uint8_t* t = exif.data();
  if (LoadLE32(t) == 0x2A004D4D) {
    bigendian = true;
  } else if (LoadLE32(t) == 0x002A4949) {
    bigendian = false;
  } else {
    return;  // not a valid tiff header
  }
  t += 4;
  uint32_t offset = (bigendian ? LoadBE32(t) : LoadLE32(t));
  if (exif.size() < 12 + offset + 2 || offset < 8) return;
  t += offset - 4;
  uint16_t nb_tags = (bigendian ? LoadBE16(t) : LoadLE16(t));
  t += 2;
  while (nb_tags > 0) {
    if (t + 12 >= exif.data() + exif.size()) return;
    uint16_t tag = (bigendian ? LoadBE16(t) : LoadLE16(t));
    t += 2;
    if (tag == kExifOrientationTag) {
      uint16_t type = (bigendian ? LoadBE16(t) : LoadLE16(t));
      t += 2;
      uint32_t count = (bigendian ? LoadBE32(t) : LoadLE32(t));
      t += 4;
      if (type == 3 && count == 1) {
        if (bigendian) {
          StoreBE16(1, t);
        } else {
          StoreLE16(1, t);
        }
      }
      return;
    } else {
      t += 10;
      nb_tags--;
    }
  }
}

}  // namespace jxl