2
#include "TiffParser.h"
5
RawSpeed - RAW file decoder.
7
Copyright (C) 2009 Klaus Post
9
This library is free software; you can redistribute it and/or
10
modify it under the terms of the GNU Lesser General Public
11
License as published by the Free Software Foundation; either
12
version 2 of the License, or (at your option) any later version.
14
This library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public
20
License along with this library; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
http://www.klauspost.com
29
TiffParser::TiffParser(FileMap* inputData): mInput(inputData), mRootIFD(0) {
30
host_endian = getHostEndianness();
34
TiffParser::~TiffParser(void) {
47
#define CHECKSIZE(A) if (A >= mInput->getSize()) throw TiffParserException("Error reading TIFF structure (size out of bounds). File Corrupt")
48
#define CHECKPTR(A) if ((int)A >= ((int)(mInput->data) + size))) throw TiffParserException("Error reading TIFF structure (size out of bounds). File Corrupt")
50
void TiffParser::parseData() {
51
const unsigned char* data = mInput->getData(0);
52
if (mInput->getSize() < 16)
53
throw TiffParserException("Not a TIFF file (size too small)");
54
if (data[0] != 0x49 || data[1] != 0x49) {
56
if (data[0] != 0x4D || data[1] != 0x4D)
57
throw TiffParserException("Not a TIFF file (ID)");
60
throw TiffParserException("Not a TIFF file (magic 42)");
63
if (data[2] != 42 && data[2] != 0x52 && data[2] != 0x55) // ORF has 0x52, RW2 0x55 - Brillant!
64
throw TiffParserException("Not a TIFF file (magic 42)");
67
if (tiff_endian == host_endian)
68
mRootIFD = new TiffIFD();
70
mRootIFD = new TiffIFDBE();
73
data = mInput->getData(4);
74
if (tiff_endian == host_endian) {
75
nextIFD = *(int*)data;
77
nextIFD = (unsigned int)data[0] << 24 | (unsigned int)data[1] << 16 | (unsigned int)data[2] << 8 | (unsigned int)data[3];
82
if (tiff_endian == host_endian)
83
mRootIFD->mSubIFD.push_back(new TiffIFD(mInput, nextIFD));
85
mRootIFD->mSubIFD.push_back(new TiffIFDBE(mInput, nextIFD));
87
nextIFD = mRootIFD->mSubIFD.back()->getNextIFD();
91
RawDecoder* TiffParser::getDecoder() {
92
vector<TiffIFD*> potentials;
93
potentials = mRootIFD->getIFDsWithTag(DNGVERSION);
95
if (!potentials.empty()) { // We have a dng image entry
96
TiffIFD *t = potentials[0];
97
const unsigned char* c = t->getEntry(DNGVERSION)->getData();
99
throw TiffParserException("DNG version too new.");
101
throw TiffParserException("DNG version not supported.");
102
return new DngDecoder(mRootIFD, mInput);
105
potentials = mRootIFD->getIFDsWithTag(MAKE);
107
if (!potentials.empty()) { // We have make entry
108
for (vector<TiffIFD*>::iterator i = potentials.begin(); i != potentials.end(); ++i) {
109
string make = (*i)->getEntry(MAKE)->getString();
111
if (!make.compare("Canon")) {
112
return new Cr2Decoder(mRootIFD, mInput);
114
if (!make.compare("NIKON CORPORATION")) {
115
return new NefDecoder(mRootIFD, mInput);
117
if (!make.compare("NIKON")) {
118
return new NefDecoder(mRootIFD, mInput);
120
if (!make.compare("OLYMPUS IMAGING CORP.")) {
121
return new OrfDecoder(mRootIFD, mInput);
123
if (!make.compare("SONY")) {
124
return new ArwDecoder(mRootIFD, mInput);
126
if (!make.compare("PENTAX Corporation ")) {
127
return new PefDecoder(mRootIFD, mInput);
129
if (!make.compare("PENTAX")) {
130
return new PefDecoder(mRootIFD, mInput);
132
if (!make.compare("Panasonic")) {
133
return new Rw2Decoder(mRootIFD, mInput);
135
if (!make.compare("SAMSUNG")) {
136
return new SrwDecoder(mRootIFD, mInput);
140
throw TiffParserException("No decoder found. Sorry.");
144
} // namespace RawSpeed