~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/external/rawspeed/RawSpeed/TiffParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-08-02 21:32:31 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110802213231-r9v63trgyk1e822j
Tags: 0.9.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "StdAfx.h"
 
2
#include "TiffParser.h"
 
3
 
 
4
/*
 
5
    RawSpeed - RAW file decoder.
 
6
 
 
7
    Copyright (C) 2009 Klaus Post
 
8
 
 
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.
 
13
 
 
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.
 
18
 
 
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
 
22
 
 
23
    http://www.klauspost.com
 
24
*/
 
25
 
 
26
namespace RawSpeed {
 
27
 
 
28
 
 
29
TiffParser::TiffParser(FileMap* inputData): mInput(inputData), mRootIFD(0) {
 
30
  host_endian = getHostEndianness();
 
31
}
 
32
 
 
33
 
 
34
TiffParser::~TiffParser(void) {
 
35
  if (mRootIFD)
 
36
    delete mRootIFD;
 
37
  mRootIFD = NULL;
 
38
}
 
39
 
 
40
#ifdef CHECKSIZE
 
41
#undef CHECKSIZE
 
42
#endif
 
43
#ifdef CHECKPTR
 
44
#undef CHECKPTR
 
45
#endif
 
46
 
 
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")
 
49
 
 
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) {
 
55
    tiff_endian = big;
 
56
    if (data[0] != 0x4D || data[1] != 0x4D)
 
57
      throw TiffParserException("Not a TIFF file (ID)");
 
58
 
 
59
    if (data[3] != 42)
 
60
      throw TiffParserException("Not a TIFF file (magic 42)");
 
61
  } else {
 
62
    tiff_endian = little;
 
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)");
 
65
  }
 
66
 
 
67
  if (tiff_endian == host_endian)
 
68
    mRootIFD = new TiffIFD();
 
69
  else
 
70
    mRootIFD = new TiffIFDBE();
 
71
 
 
72
  uint32 nextIFD;
 
73
  data = mInput->getData(4);
 
74
  if (tiff_endian == host_endian) {
 
75
    nextIFD = *(int*)data;
 
76
  } else {
 
77
    nextIFD = (unsigned int)data[0] << 24 | (unsigned int)data[1] << 16 | (unsigned int)data[2] << 8 | (unsigned int)data[3];
 
78
  }
 
79
  while (nextIFD) {
 
80
    CHECKSIZE(nextIFD);
 
81
 
 
82
    if (tiff_endian == host_endian)
 
83
      mRootIFD->mSubIFD.push_back(new TiffIFD(mInput, nextIFD));
 
84
    else
 
85
      mRootIFD->mSubIFD.push_back(new TiffIFDBE(mInput, nextIFD));
 
86
 
 
87
    nextIFD = mRootIFD->mSubIFD.back()->getNextIFD();
 
88
  }
 
89
}
 
90
 
 
91
RawDecoder* TiffParser::getDecoder() {
 
92
  vector<TiffIFD*> potentials;
 
93
  potentials = mRootIFD->getIFDsWithTag(DNGVERSION);
 
94
 
 
95
  if (!potentials.empty()) {  // We have a dng image entry
 
96
    TiffIFD *t = potentials[0];
 
97
    const unsigned char* c = t->getEntry(DNGVERSION)->getData();
 
98
    if (c[0] > 1)
 
99
      throw TiffParserException("DNG version too new.");
 
100
    if (c[1] > 2)
 
101
      throw TiffParserException("DNG version not supported.");
 
102
    return new DngDecoder(mRootIFD, mInput);
 
103
  }
 
104
 
 
105
  potentials = mRootIFD->getIFDsWithTag(MAKE);
 
106
 
 
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();
 
110
      TrimSpaces(make);
 
111
      if (!make.compare("Canon")) {
 
112
        return new Cr2Decoder(mRootIFD, mInput);
 
113
      }
 
114
      if (!make.compare("NIKON CORPORATION")) {
 
115
        return new NefDecoder(mRootIFD, mInput);
 
116
      }
 
117
      if (!make.compare("NIKON")) {
 
118
        return new NefDecoder(mRootIFD, mInput);
 
119
      }
 
120
      if (!make.compare("OLYMPUS IMAGING CORP.")) {
 
121
        return new OrfDecoder(mRootIFD, mInput);
 
122
      }
 
123
      if (!make.compare("SONY")) {
 
124
        return new ArwDecoder(mRootIFD, mInput);
 
125
      }
 
126
      if (!make.compare("PENTAX Corporation ")) {
 
127
        return new PefDecoder(mRootIFD, mInput);
 
128
      }
 
129
      if (!make.compare("PENTAX")) {
 
130
        return new PefDecoder(mRootIFD, mInput);
 
131
      }
 
132
      if (!make.compare("Panasonic")) {
 
133
        return new Rw2Decoder(mRootIFD, mInput);
 
134
      }
 
135
      if (!make.compare("SAMSUNG")) {
 
136
        return new SrwDecoder(mRootIFD, mInput);
 
137
      }
 
138
    }
 
139
  }
 
140
  throw TiffParserException("No decoder found. Sorry.");
 
141
  return NULL;
 
142
}
 
143
 
 
144
} // namespace RawSpeed