~ubuntu-branches/ubuntu/trusty/digikam/trusty

« back to all changes in this revision

Viewing changes to extra/libkdcraw/libraw/RawSpeed/ArwDecoder.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-11-26 18:24:20 UTC
  • mfrom: (1.9.1) (3.1.23 experimental)
  • Revision ID: package-import@ubuntu.com-20121126182420-qoy6z0nx4ai0wzcl
Tags: 4:3.0.0~beta3-0ubuntu1
* New upstream release
  - Add build-deps :  libhupnp-dev, libqtgstreamer-dev, libmagickcore-dev
* Merge from debian, remaining changes:
  - Make sure libqt4-opengl-dev, libgl1-mesa-dev and libglu1-mesa-dev only
    install on i386,amd64 and powerpc
  - Depend on libtiff-dev instead of libtiff4-dev
  - Drop digikam breaks/replaces kipi-plugins-common since we're past the
    LTS release now
  - digikam to recommend mplayerthumbs | ffmpegthumbs. We currently only
    have latter in the archives, even though former is also supposed to
    be part of kdemultimedia. (LP: #890059)
  - kipi-plugins to recommend www-browser rather than konqueror directly
    since 2.8 no direct usage of konqueror is present in the flickr
    plugin anymore (LP: #1011211)
  - Keep kubuntu_mysqld_executable_name.diff
  - Don't install libkipi translations
  - Keep deps on libcv-dev, libcvaux-dev
  - Keep split packaging of libraries
  - Replace icons from KDE 3 time in debian/xpm.d/*.xpm with the new
    versions (LP: #658047)
* Update debian/not-installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "StdAfx.h"
 
2
#include "ArwDecoder.h"
 
3
/*
 
4
    RawSpeed - RAW file decoder.
 
5
 
 
6
    Copyright (C) 2009 Klaus Post
 
7
 
 
8
    This library is free software; you can redistribute it and/or
 
9
    modify it under the terms of the GNU Lesser General Public
 
10
    License as published by the Free Software Foundation; either
 
11
    version 2 of the License, or (at your option) any later version.
 
12
 
 
13
    This library is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
    Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public
 
19
    License along with this library; if not, write to the Free Software
 
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
21
 
 
22
    http://www.klauspost.com
 
23
*/
 
24
 
 
25
namespace RawSpeed {
 
26
 
 
27
ArwDecoder::ArwDecoder(TiffIFD *rootIFD, FileMap* file) :
 
28
    RawDecoder(file), mRootIFD(rootIFD) {
 
29
 
 
30
}
 
31
 
 
32
ArwDecoder::~ArwDecoder(void) {
 
33
  if (mRootIFD)
 
34
    delete mRootIFD;
 
35
  mRootIFD = NULL;
 
36
}
 
37
 
 
38
RawImage ArwDecoder::decodeRawInternal() {
 
39
  vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(STRIPOFFSETS);
 
40
 
 
41
  if (data.empty())
 
42
    ThrowRDE("ARW Decoder: No image data found");
 
43
 
 
44
  TiffIFD* raw = data[0];
 
45
  int compression = raw->getEntry(COMPRESSION)->getInt();
 
46
  if (32767 != compression)
 
47
    ThrowRDE("ARW Decoder: Unsupported compression");
 
48
 
 
49
  TiffEntry *offsets = raw->getEntry(STRIPOFFSETS);
 
50
  TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS);
 
51
 
 
52
  if (offsets->count != 1) {
 
53
    ThrowRDE("ARW Decoder: Multiple Strips found: %u", offsets->count);
 
54
  }
 
55
  if (counts->count != offsets->count) {
 
56
    ThrowRDE("ARW Decoder: Byte count number does not match strip size: count:%u, strips:%u ", counts->count, offsets->count);
 
57
  }
 
58
  uint32 width = raw->getEntry(IMAGEWIDTH)->getInt();
 
59
  uint32 height = raw->getEntry(IMAGELENGTH)->getInt();
 
60
  uint32 bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getInt();
 
61
 
 
62
  // Sony E-550 marks compressed 8bpp ARW with 12 bit per pixel
 
63
  // this makes the compression detect it as a ARW v1.
 
64
  // This camera has however another MAKER entry, so we MAY be able
 
65
  // to detect it this way in the future.
 
66
  data = mRootIFD->getIFDsWithTag(MAKE);
 
67
  string make = data[0]->getEntry(MAKE)->getString();
 
68
  if (!make.compare("SONY"))
 
69
    bitPerPixel = 8;
 
70
 
 
71
  bool arw1 = counts->getInt() * 8 != width * height * bitPerPixel;
 
72
  if (arw1)
 
73
    height += 8;
 
74
 
 
75
  mRaw->dim = iPoint2D(width, height);
 
76
  mRaw->createData();
 
77
 
 
78
  const ushort16* c = raw->getEntry(SONY_CURVE)->getShortArray();
 
79
  uint32 sony_curve[] = { 0, 0, 0, 0, 0, 4095 };
 
80
 
 
81
  for (uint32 i = 0; i < 4; i++)
 
82
    sony_curve[i+1] = (c[i] >> 2) & 0xfff;
 
83
 
 
84
  for (uint32 i = 0; i < 0x4001; i++)
 
85
    curve[i] = i;
 
86
 
 
87
  for (uint32 i = 0; i < 5; i++)
 
88
    for (uint32 j = sony_curve[i] + 1; j <= sony_curve[i+1]; j++)
 
89
      curve[j] = curve[j-1] + (1 << i);
 
90
 
 
91
  uint32 c2 = counts->getInt();
 
92
  uint32 off = offsets->getInt();
 
93
 
 
94
  if (!mFile->isValid(off))
 
95
    ThrowRDE("Sony ARW decoder: Data offset after EOF, file probably truncated");
 
96
 
 
97
  if (!mFile->isValid(off + c2))
 
98
    c2 = mFile->getSize() - off;
 
99
 
 
100
 
 
101
  ByteStream input(mFile->getData(off), c2);
 
102
 
 
103
  try {
 
104
    if (arw1)
 
105
      DecodeARW(input, width, height);
 
106
    else
 
107
      DecodeARW2(input, width, height, bitPerPixel);
 
108
  } catch (IOException &e) {
 
109
    mRaw->setError(e.what());
 
110
    // Let's ignore it, it may have delivered somewhat useful data.
 
111
  }
 
112
 
 
113
  return mRaw;
 
114
}
 
115
 
 
116
void ArwDecoder::DecodeARW(ByteStream &input, uint32 w, uint32 h) {
 
117
  BitPumpMSB bits(&input);
 
118
  uchar8* data = mRaw->getData();
 
119
  ushort16* dest = (ushort16*) & data[0];
 
120
  uint32 pitch = mRaw->pitch / sizeof(ushort16);
 
121
  int sum = 0;
 
122
  for (uint32 x = w; x--;)
 
123
    for (uint32 y = 0; y < h + 1; y += 2) {
 
124
      bits.checkPos();
 
125
      bits.fill();
 
126
      if (y == h) y = 1;
 
127
      uint32 len = 4 - bits.getBitsNoFill(2);
 
128
      if (len == 3 && bits.getBitNoFill()) len = 0;
 
129
      if (len == 4)
 
130
        while (len < 17 && !bits.getBitNoFill()) len++;
 
131
      int diff = bits.getBits(len);
 
132
      if ((diff & (1 << (len - 1))) == 0)
 
133
        diff -= (1 << len) - 1;
 
134
      sum += diff;
 
135
      _ASSERTE(!(sum >> 12));
 
136
      if (y < h) dest[x+y*pitch] = sum;
 
137
    }
 
138
}
 
139
 
 
140
void ArwDecoder::DecodeARW2(ByteStream &input, uint32 w, uint32 h, uint32 bpp) {
 
141
 
 
142
  if (bpp == 8) {
 
143
    in = &input;
 
144
    this->startThreads();
 
145
    return;
 
146
  } // End bpp = 8
 
147
 
 
148
  if (bpp == 12) {
 
149
    uchar8* data = mRaw->getData();
 
150
    uint32 pitch = mRaw->pitch;
 
151
    const uchar8 *in = input.getData();
 
152
 
 
153
    if (input.getRemainSize() < (w * 3 / 2))
 
154
      ThrowRDE("Sony Decoder: Image data section too small, file probably truncated");
 
155
 
 
156
    if (input.getRemainSize() < (w*h*3 / 2))
 
157
      h = input.getRemainSize() / (w * 3 / 2) - 1;
 
158
 
 
159
    for (uint32 y = 0; y < h; y++) {
 
160
      ushort16* dest = (ushort16*) & data[y*pitch];
 
161
      for (uint32 x = 0 ; x < w; x += 2) {
 
162
        uint32 g1 = *in++;
 
163
        uint32 g2 = *in++;
 
164
        // Shift up to match compressed precision
 
165
        dest[x] = (g1 | ((g2 & 0xf) << 8)) << 2;
 
166
        uint32 g3 = *in++;
 
167
        dest[x+1] = ((g2 >> 4) | (g3 << 4)) << 2;
 
168
      }
 
169
    }
 
170
    return;
 
171
  }
 
172
  ThrowRDE("Unsupported bit depth");
 
173
}
 
174
 
 
175
void ArwDecoder::checkSupportInternal(CameraMetaData *meta) {
 
176
  vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(MODEL);
 
177
  if (data.empty())
 
178
    ThrowRDE("ARW Support check: Model name found");
 
179
  string make = data[0]->getEntry(MAKE)->getString();
 
180
  string model = data[0]->getEntry(MODEL)->getString();
 
181
  this->checkCameraSupported(meta, make, model, "");
 
182
}
 
183
 
 
184
void ArwDecoder::decodeMetaDataInternal(CameraMetaData *meta) {
 
185
  //Default
 
186
  int iso = 0;
 
187
 
 
188
  mRaw->cfa.setCFA(CFA_RED, CFA_GREEN, CFA_GREEN2, CFA_BLUE);
 
189
  vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(MODEL);
 
190
 
 
191
  if (data.empty())
 
192
    ThrowRDE("ARW Meta Decoder: Model name found");
 
193
  if (!data[0]->hasEntry(MAKE))
 
194
    ThrowRDE("ARW Decoder: Make name not found");
 
195
 
 
196
  string make = data[0]->getEntry(MAKE)->getString();
 
197
  string model = data[0]->getEntry(MODEL)->getString();
 
198
 
 
199
  if (mRootIFD->hasEntryRecursive(ISOSPEEDRATINGS))
 
200
    iso = mRootIFD->getEntryRecursive(ISOSPEEDRATINGS)->getInt();
 
201
 
 
202
  setMetaData(meta, make, model, "", iso);
 
203
}
 
204
 
 
205
/* Since ARW2 compressed images have predictable offsets, we decode them threaded */
 
206
 
 
207
void ArwDecoder::decodeThreaded(RawDecoderThread * t) {
 
208
  uchar8* data = mRaw->getData();
 
209
  uint32 pitch = mRaw->pitch;
 
210
  uint32 w = mRaw->dim.x;
 
211
 
 
212
  BitPumpPlain bits(in);
 
213
  for (uint32 y = t->start_y; y < t->end_y; y++) {
 
214
    ushort16* dest = (ushort16*) & data[y*pitch];
 
215
    // Realign
 
216
    bits.setAbsoluteOffset((w*8*y) >> 3);
 
217
 
 
218
    // Process 32 pixels (16x2) per loop.
 
219
    for (uint32 x = 0; x < w - 30;) {
 
220
      bits.checkPos();
 
221
      int _max = bits.getBits(11);
 
222
      int _min = bits.getBits(11);
 
223
      int _imax = bits.getBits(4);
 
224
      int _imin = bits.getBits(4);
 
225
      int sh;
 
226
      for (sh = 0; sh < 4 && 0x80 << sh <= _max - _min; sh++);
 
227
      for (int i = 0; i < 16; i++) {
 
228
        int p;
 
229
        if (i == _imax) p = _max;
 
230
        else if (i == _imin) p = _min;
 
231
        else {
 
232
          p = (bits.getBits(7) << sh) + _min;
 
233
          if (p > 0x7ff)
 
234
            p = 0x7ff;
 
235
        }
 
236
        dest[x+i*2] = curve[p << 1];
 
237
      }
 
238
      x += x & 1 ? 31 : 1;  // Skip to next 32 pixels
 
239
    }
 
240
  }
 
241
}
 
242
 
 
243
} // namespace RawSpeed