1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/image/decoders/EXIF.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,309 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "EXIF.h" 1.10 + 1.11 +#include "mozilla/Endian.h" 1.12 + 1.13 +namespace mozilla { 1.14 +namespace image { 1.15 + 1.16 +// Section references in this file refer to the EXIF v2.3 standard, also known 1.17 +// as CIPA DC-008-Translation-2010. 1.18 + 1.19 +// See Section 4.6.4, Table 4. 1.20 +// Typesafe enums are intentionally not used here since we're comparing to raw 1.21 +// integers produced by parsing. 1.22 +enum EXIFTag 1.23 +{ 1.24 + OrientationTag = 0x112, 1.25 +}; 1.26 + 1.27 +// See Section 4.6.2. 1.28 +enum EXIFType 1.29 +{ 1.30 + ByteType = 1, 1.31 + ASCIIType = 2, 1.32 + ShortType = 3, 1.33 + LongType = 4, 1.34 + RationalType = 5, 1.35 + UndefinedType = 7, 1.36 + SignedLongType = 9, 1.37 + SignedRational = 10, 1.38 +}; 1.39 + 1.40 +static const char* EXIFHeader = "Exif\0\0"; 1.41 +static const uint32_t EXIFHeaderLength = 6; 1.42 + 1.43 +///////////////////////////////////////////////////////////// 1.44 +// Parse EXIF data, typically found in a JPEG's APP1 segment. 1.45 +///////////////////////////////////////////////////////////// 1.46 +EXIFData 1.47 +EXIFParser::ParseEXIF(const uint8_t* aData, const uint32_t aLength) 1.48 +{ 1.49 + if (!Initialize(aData, aLength)) 1.50 + return EXIFData(); 1.51 + 1.52 + if (!ParseEXIFHeader()) 1.53 + return EXIFData(); 1.54 + 1.55 + uint32_t offsetIFD; 1.56 + if (!ParseTIFFHeader(offsetIFD)) 1.57 + return EXIFData(); 1.58 + 1.59 + JumpTo(offsetIFD); 1.60 + 1.61 + Orientation orientation; 1.62 + if (!ParseIFD0(orientation)) 1.63 + return EXIFData(); 1.64 + 1.65 + // We only care about orientation at this point, so we don't bother with the 1.66 + // other IFDs. If we got this far we're done. 1.67 + return EXIFData(orientation); 1.68 +} 1.69 + 1.70 +///////////////////////////////////////////////////////// 1.71 +// Parse the EXIF header. (Section 4.7.2, Figure 30) 1.72 +///////////////////////////////////////////////////////// 1.73 +bool 1.74 +EXIFParser::ParseEXIFHeader() 1.75 +{ 1.76 + return MatchString(EXIFHeader, EXIFHeaderLength); 1.77 +} 1.78 + 1.79 +///////////////////////////////////////////////////////// 1.80 +// Parse the TIFF header. (Section 4.5.2, Table 1) 1.81 +///////////////////////////////////////////////////////// 1.82 +bool 1.83 +EXIFParser::ParseTIFFHeader(uint32_t& aIFD0OffsetOut) 1.84 +{ 1.85 + // Determine byte order. 1.86 + if (MatchString("MM\0*", 4)) 1.87 + mByteOrder = ByteOrder::BigEndian; 1.88 + else if (MatchString("II*\0", 4)) 1.89 + mByteOrder = ByteOrder::LittleEndian; 1.90 + else 1.91 + return false; 1.92 + 1.93 + // Determine offset of the 0th IFD. (It shouldn't be greater than 64k, which 1.94 + // is the maximum size of the entry APP1 segment.) 1.95 + uint32_t ifd0Offset; 1.96 + if (!ReadUInt32(ifd0Offset) || ifd0Offset > 64 * 1024) 1.97 + return false; 1.98 + 1.99 + // The IFD offset is relative to the beginning of the TIFF header, which 1.100 + // begins after the EXIF header, so we need to increase the offset 1.101 + // appropriately. 1.102 + aIFD0OffsetOut = ifd0Offset + EXIFHeaderLength; 1.103 + return true; 1.104 +} 1.105 + 1.106 +///////////////////////////////////////////////////////// 1.107 +// Parse the entries in IFD0. (Section 4.6.2) 1.108 +///////////////////////////////////////////////////////// 1.109 +bool 1.110 +EXIFParser::ParseIFD0(Orientation& aOrientationOut) 1.111 +{ 1.112 + uint16_t entryCount; 1.113 + if (!ReadUInt16(entryCount)) 1.114 + return false; 1.115 + 1.116 + for (uint16_t entry = 0 ; entry < entryCount ; ++entry) { 1.117 + // Read the fields of the entry. 1.118 + uint16_t tag; 1.119 + if (!ReadUInt16(tag)) 1.120 + return false; 1.121 + 1.122 + // Right now, we only care about orientation, so we immediately skip to the 1.123 + // next entry if we find anything else. 1.124 + if (tag != OrientationTag) { 1.125 + Advance(10); 1.126 + continue; 1.127 + } 1.128 + 1.129 + uint16_t type; 1.130 + if (!ReadUInt16(type)) 1.131 + return false; 1.132 + 1.133 + uint32_t count; 1.134 + if (!ReadUInt32(count)) 1.135 + return false; 1.136 + 1.137 + // We should have an orientation value here; go ahead and parse it. 1.138 + Orientation orientation; 1.139 + if (!ParseOrientation(type, count, aOrientationOut)) 1.140 + return false; 1.141 + 1.142 + // Since the orientation is all we care about, we're done. 1.143 + return true; 1.144 + } 1.145 + 1.146 + // We didn't find an orientation field in the IFD. That's OK; we assume the 1.147 + // default orientation in that case. 1.148 + aOrientationOut = Orientation(); 1.149 + return true; 1.150 +} 1.151 + 1.152 +bool 1.153 +EXIFParser::ParseOrientation(uint16_t aType, uint32_t aCount, Orientation& aOut) 1.154 +{ 1.155 + // Sanity check the type and count. 1.156 + if (aType != ShortType || aCount != 1) 1.157 + return false; 1.158 + 1.159 + uint16_t value; 1.160 + if (!ReadUInt16(value)) 1.161 + return false; 1.162 + 1.163 + switch (value) { 1.164 + case 1: aOut = Orientation(Angle::D0, Flip::Unflipped); break; 1.165 + case 2: aOut = Orientation(Angle::D0, Flip::Horizontal); break; 1.166 + case 3: aOut = Orientation(Angle::D180, Flip::Unflipped); break; 1.167 + case 4: aOut = Orientation(Angle::D180, Flip::Horizontal); break; 1.168 + case 5: aOut = Orientation(Angle::D90, Flip::Horizontal); break; 1.169 + case 6: aOut = Orientation(Angle::D90, Flip::Unflipped); break; 1.170 + case 7: aOut = Orientation(Angle::D270, Flip::Horizontal); break; 1.171 + case 8: aOut = Orientation(Angle::D270, Flip::Unflipped); break; 1.172 + default: return false; 1.173 + } 1.174 + 1.175 + // This is a 32-bit field, but the orientation value only occupies the first 1.176 + // 16 bits. We need to advance another 16 bits to consume the entire field. 1.177 + Advance(2); 1.178 + return true; 1.179 +} 1.180 + 1.181 +bool 1.182 +EXIFParser::Initialize(const uint8_t* aData, const uint32_t aLength) 1.183 +{ 1.184 + if (aData == nullptr) 1.185 + return false; 1.186 + 1.187 + // An APP1 segment larger than 64k violates the JPEG standard. 1.188 + if (aLength > 64 * 1024) 1.189 + return false; 1.190 + 1.191 + mStart = mCurrent = aData; 1.192 + mLength = mRemainingLength = aLength; 1.193 + mByteOrder = ByteOrder::Unknown; 1.194 + return true; 1.195 +} 1.196 + 1.197 +void 1.198 +EXIFParser::Advance(const uint32_t aDistance) 1.199 +{ 1.200 + if (mRemainingLength >= aDistance) { 1.201 + mCurrent += aDistance; 1.202 + mRemainingLength -= aDistance; 1.203 + } else { 1.204 + mCurrent = mStart; 1.205 + mRemainingLength = 0; 1.206 + } 1.207 +} 1.208 + 1.209 +void 1.210 +EXIFParser::JumpTo(const uint32_t aOffset) 1.211 +{ 1.212 + if (mLength >= aOffset) { 1.213 + mCurrent = mStart + aOffset; 1.214 + mRemainingLength = mLength - aOffset; 1.215 + } else { 1.216 + mCurrent = mStart; 1.217 + mRemainingLength = 0; 1.218 + } 1.219 +} 1.220 + 1.221 +bool 1.222 +EXIFParser::MatchString(const char* aString, const uint32_t aLength) 1.223 +{ 1.224 + if (mRemainingLength < aLength) 1.225 + return false; 1.226 + 1.227 + for (uint32_t i = 0 ; i < aLength ; ++i) { 1.228 + if (mCurrent[i] != aString[i]) 1.229 + return false; 1.230 + } 1.231 + 1.232 + Advance(aLength); 1.233 + return true; 1.234 +} 1.235 + 1.236 +bool 1.237 +EXIFParser::MatchUInt16(const uint16_t aValue) 1.238 +{ 1.239 + if (mRemainingLength < 2) 1.240 + return false; 1.241 + 1.242 + bool matched; 1.243 + switch (mByteOrder) { 1.244 + case ByteOrder::LittleEndian: 1.245 + matched = LittleEndian::readUint16(mCurrent) == aValue; 1.246 + break; 1.247 + case ByteOrder::BigEndian: 1.248 + matched = BigEndian::readUint16(mCurrent) == aValue; 1.249 + break; 1.250 + default: 1.251 + NS_NOTREACHED("Should know the byte order by now"); 1.252 + matched = false; 1.253 + } 1.254 + 1.255 + if (matched) 1.256 + Advance(2); 1.257 + 1.258 + return matched; 1.259 +} 1.260 + 1.261 +bool 1.262 +EXIFParser::ReadUInt16(uint16_t& aValue) 1.263 +{ 1.264 + if (mRemainingLength < 2) 1.265 + return false; 1.266 + 1.267 + bool matched = true; 1.268 + switch (mByteOrder) { 1.269 + case ByteOrder::LittleEndian: 1.270 + aValue = LittleEndian::readUint16(mCurrent); 1.271 + break; 1.272 + case ByteOrder::BigEndian: 1.273 + aValue = BigEndian::readUint16(mCurrent); 1.274 + break; 1.275 + default: 1.276 + NS_NOTREACHED("Should know the byte order by now"); 1.277 + matched = false; 1.278 + } 1.279 + 1.280 + if (matched) 1.281 + Advance(2); 1.282 + 1.283 + return matched; 1.284 +} 1.285 + 1.286 +bool 1.287 +EXIFParser::ReadUInt32(uint32_t& aValue) 1.288 +{ 1.289 + if (mRemainingLength < 4) 1.290 + return false; 1.291 + 1.292 + bool matched = true; 1.293 + switch (mByteOrder) { 1.294 + case ByteOrder::LittleEndian: 1.295 + aValue = LittleEndian::readUint32(mCurrent); 1.296 + break; 1.297 + case ByteOrder::BigEndian: 1.298 + aValue = BigEndian::readUint32(mCurrent); 1.299 + break; 1.300 + default: 1.301 + NS_NOTREACHED("Should know the byte order by now"); 1.302 + matched = false; 1.303 + } 1.304 + 1.305 + if (matched) 1.306 + Advance(4); 1.307 + 1.308 + return matched; 1.309 +} 1.310 + 1.311 +} // namespace image 1.312 +} // namespace mozilla