2013-08-25 11:19:42 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-04-06 02:22:00 +03:00
|
|
|
#ifndef mozilla_image_decoders_EXIF_h
|
|
|
|
#define mozilla_image_decoders_EXIF_h
|
2013-08-25 11:19:42 +04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
#include "Orientation.h"
|
2021-05-05 12:41:25 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2021-05-05 12:41:23 +03:00
|
|
|
#include "mozilla/image/Resolution.h"
|
2021-05-05 12:41:25 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2013-08-25 11:19:42 +04:00
|
|
|
|
2021-05-05 12:41:23 +03:00
|
|
|
namespace mozilla::image {
|
2013-08-25 11:19:42 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class ByteOrder : uint8_t { Unknown, LittleEndian, BigEndian };
|
2013-08-25 11:19:42 +04:00
|
|
|
|
|
|
|
struct EXIFData {
|
2021-05-05 12:41:23 +03:00
|
|
|
const Orientation orientation = Orientation();
|
|
|
|
const Resolution resolution = Resolution();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ParsedEXIFData;
|
2013-08-25 11:19:42 +04:00
|
|
|
|
2021-05-05 12:41:23 +03:00
|
|
|
enum class ResolutionUnit : uint8_t {
|
|
|
|
Dpi,
|
|
|
|
Dpcm,
|
2013-08-25 11:19:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class EXIFParser {
|
|
|
|
public:
|
2021-05-05 12:41:25 +03:00
|
|
|
static EXIFData Parse(const uint8_t* aData, const uint32_t aLength,
|
|
|
|
const gfx::IntSize& aRealImageSize) {
|
2013-08-25 11:19:42 +04:00
|
|
|
EXIFParser parser;
|
2021-05-05 12:41:25 +03:00
|
|
|
return parser.ParseEXIF(aData, aLength, aRealImageSize);
|
2013-08-25 11:19:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EXIFParser()
|
|
|
|
: mStart(nullptr),
|
|
|
|
mCurrent(nullptr),
|
|
|
|
mLength(0),
|
|
|
|
mRemainingLength(0),
|
|
|
|
mByteOrder(ByteOrder::Unknown) {}
|
|
|
|
|
2021-05-05 12:41:25 +03:00
|
|
|
EXIFData ParseEXIF(const uint8_t* aData, const uint32_t aLength,
|
|
|
|
const gfx::IntSize& aRealImageSize);
|
2013-08-25 11:19:42 +04:00
|
|
|
bool ParseEXIFHeader();
|
|
|
|
bool ParseTIFFHeader(uint32_t& aIFD0OffsetOut);
|
2021-05-05 12:41:23 +03:00
|
|
|
|
2021-05-05 12:41:25 +03:00
|
|
|
void ParseIFD(ParsedEXIFData&, uint32_t aDepth = 0);
|
2021-05-05 12:41:23 +03:00
|
|
|
bool ParseOrientation(uint16_t aType, uint32_t aCount, Orientation&);
|
2021-05-05 12:41:25 +03:00
|
|
|
bool ParseResolution(uint16_t aType, uint32_t aCount, Maybe<float>&);
|
|
|
|
bool ParseResolutionUnit(uint16_t aType, uint32_t aCount,
|
|
|
|
Maybe<ResolutionUnit>&);
|
|
|
|
bool ParseDimension(uint16_t aType, uint32_t aCount, Maybe<uint32_t>&);
|
2013-08-25 11:19:42 +04:00
|
|
|
|
|
|
|
bool Initialize(const uint8_t* aData, const uint32_t aLength);
|
|
|
|
void Advance(const uint32_t aDistance);
|
|
|
|
void JumpTo(const uint32_t aOffset);
|
|
|
|
|
2021-05-05 12:41:23 +03:00
|
|
|
uint32_t CurrentOffset() const { return mCurrent - mStart; }
|
|
|
|
|
|
|
|
class ScopedJump {
|
|
|
|
EXIFParser& mParser;
|
|
|
|
uint32_t mOldOffset;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedJump(EXIFParser& aParser, uint32_t aOffset)
|
|
|
|
: mParser(aParser), mOldOffset(aParser.CurrentOffset()) {
|
|
|
|
mParser.JumpTo(aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedJump() { mParser.JumpTo(mOldOffset); }
|
|
|
|
};
|
|
|
|
|
2013-08-25 11:19:42 +04:00
|
|
|
bool MatchString(const char* aString, const uint32_t aLength);
|
|
|
|
bool MatchUInt16(const uint16_t aValue);
|
|
|
|
bool ReadUInt16(uint16_t& aOut);
|
|
|
|
bool ReadUInt32(uint32_t& aOut);
|
2021-05-05 12:41:23 +03:00
|
|
|
bool ReadRational(float& aOut);
|
2013-08-25 11:19:42 +04:00
|
|
|
|
|
|
|
const uint8_t* mStart;
|
|
|
|
const uint8_t* mCurrent;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint32_t mRemainingLength;
|
|
|
|
ByteOrder mByteOrder;
|
|
|
|
};
|
|
|
|
|
2021-05-05 12:41:23 +03:00
|
|
|
} // namespace mozilla::image
|
2013-08-25 11:19:42 +04:00
|
|
|
|
2015-04-06 02:22:00 +03:00
|
|
|
#endif // mozilla_image_decoders_EXIF_h
|