exif.cc (1467B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/extras/exif.h" 7 8 #include "lib/jxl/base/byte_order.h" 9 10 namespace jxl { 11 12 constexpr uint16_t kExifOrientationTag = 274; 13 14 void ResetExifOrientation(std::vector<uint8_t>& exif) { 15 if (exif.size() < 12) return; // not enough bytes for a valid exif blob 16 bool bigendian; 17 uint8_t* t = exif.data(); 18 if (LoadLE32(t) == 0x2A004D4D) { 19 bigendian = true; 20 } else if (LoadLE32(t) == 0x002A4949) { 21 bigendian = false; 22 } else { 23 return; // not a valid tiff header 24 } 25 t += 4; 26 uint64_t offset = (bigendian ? LoadBE32(t) : LoadLE32(t)); 27 if (exif.size() < 12 + offset + 2 || offset < 8) return; 28 t += offset - 4; 29 uint16_t nb_tags = (bigendian ? LoadBE16(t) : LoadLE16(t)); 30 t += 2; 31 while (nb_tags > 0) { 32 if (t + 12 >= exif.data() + exif.size()) return; 33 uint16_t tag = (bigendian ? LoadBE16(t) : LoadLE16(t)); 34 t += 2; 35 if (tag == kExifOrientationTag) { 36 uint16_t type = (bigendian ? LoadBE16(t) : LoadLE16(t)); 37 t += 2; 38 uint32_t count = (bigendian ? LoadBE32(t) : LoadLE32(t)); 39 t += 4; 40 if (type == 3 && count == 1) { 41 if (bigendian) { 42 StoreBE16(1, t); 43 } else { 44 StoreLE16(1, t); 45 } 46 } 47 return; 48 } else { 49 t += 10; 50 nb_tags--; 51 } 52 } 53 } 54 55 } // namespace jxl