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

« back to all changes in this revision

Viewing changes to src/external/rawspeed/RawSpeed/NikonDecompressor.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 "NikonDecompressor.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
NikonDecompressor::NikonDecompressor(FileMap* file, RawImage img) :
 
28
    LJpegDecompressor(file, img) {
 
29
  for (uint32 i = 0; i < 0xffff ; i++) {
 
30
    curve[i]  = i;
 
31
  }
 
32
  bits = 0;
 
33
}
 
34
 
 
35
NikonDecompressor::~NikonDecompressor(void) {
 
36
  if (bits)
 
37
    delete(bits);
 
38
  bits = 0;
 
39
 
 
40
}
 
41
void NikonDecompressor::initTable(uint32 huffSelect) {
 
42
  HuffmanTable *dctbl1 = &huff[0];
 
43
  uint32 acc = 0;
 
44
  for (uint32 i = 0; i < 16 ;i++) {
 
45
    dctbl1->bits[i+1] = nikon_tree[huffSelect][i];
 
46
    acc += dctbl1->bits[i+1];
 
47
  }
 
48
  dctbl1->bits[0] = 0;
 
49
 
 
50
  for (uint32 i = 0 ; i < acc; i++) {
 
51
    dctbl1->huffval[i] = nikon_tree[huffSelect][i+16];
 
52
  }
 
53
  createHuffmanTable(dctbl1);
 
54
}
 
55
 
 
56
void NikonDecompressor::DecompressNikon(ByteStream *metadata, uint32 w, uint32 h, uint32 bitsPS, uint32 offset, uint32 size) {
 
57
  uint32 v0 = metadata->getByte();
 
58
  uint32 v1 = metadata->getByte();
 
59
  uint32 huffSelect = 0;
 
60
  uint32 split = 0;
 
61
  int pUp1[2];
 
62
  int pUp2[2];
 
63
  mUseBigtable = true;
 
64
 
 
65
  _RPT2(0, "Nef version v0:%u, v1:%u\n", v0, v1);
 
66
 
 
67
  if (v0 == 73 || v1 == 88)
 
68
    metadata->skipBytes(2110);
 
69
 
 
70
  if (v0 == 70) huffSelect = 2;
 
71
  if (bitsPS == 14) huffSelect += 3;
 
72
 
 
73
  pUp1[0] = metadata->getShort();
 
74
  pUp1[1] = metadata->getShort();
 
75
  pUp2[0] = metadata->getShort();
 
76
  pUp2[1] = metadata->getShort();
 
77
 
 
78
  int _max = 1 << bitsPS & 0x7fff;
 
79
  uint32 step = 0;
 
80
  uint32 csize = metadata->getShort();
 
81
  if (csize  > 1)
 
82
    step = _max / (csize - 1);
 
83
  if (v0 == 68 && v1 == 32 && step > 0) {
 
84
    for (uint32 i = 0; i < csize; i++)
 
85
      curve[i*step] = metadata->getShort();
 
86
    for (int i = 0; i < _max; i++)
 
87
      curve[i] = (curve[i-i%step] * (step - i % step) +
 
88
                  curve[i-i%step+step] * (i % step)) / step;
 
89
    metadata->setAbsoluteOffset(562);
 
90
    split = metadata->getShort();
 
91
  } else if (v0 != 70 && csize <= 0x4001) {
 
92
    for (uint32 i = 0; i < csize; i++) {
 
93
      curve[i] = metadata->getShort();
 
94
    }
 
95
    _max = csize;
 
96
  }
 
97
  while (curve[_max-2] == curve[_max-1]) _max--;
 
98
  initTable(huffSelect);
 
99
 
 
100
  mRaw->whitePoint = curve[_max-1];
 
101
  mRaw->blackLevel = curve[0];
 
102
 
 
103
  uint32 x, y;
 
104
  bits = new BitPumpMSB(mFile->getData(offset), size);
 
105
  uchar8 *draw = mRaw->getData();
 
106
  uint32 *dest;
 
107
  uint32 pitch = mRaw->pitch;
 
108
 
 
109
  int pLeft1 = 0;
 
110
  int pLeft2 = 0;
 
111
  uint32 cw = w / 2;
 
112
 
 
113
  for (y = 0; y < h; y++) {
 
114
    if (split && y == split) {
 
115
      initTable(huffSelect + 1);
 
116
    }
 
117
    dest = (uint32*) & draw[y*pitch];  // Adjust destination
 
118
    pUp1[y&1] += HuffDecodeNikon();
 
119
    pUp2[y&1] += HuffDecodeNikon();
 
120
    pLeft1 = pUp1[y&1];
 
121
    pLeft2 = pUp2[y&1];
 
122
    dest[0] = curve[MIN(_max-1, MAX(0,pLeft1))] | (curve[MIN(_max-1, MAX(0,pLeft2))] << 16);
 
123
    for (x = 1; x < cw; x++) {
 
124
      bits->checkPos();
 
125
      pLeft1 += HuffDecodeNikon();
 
126
      pLeft2 += HuffDecodeNikon();
 
127
      dest[x] =  curve[MIN(_max-1, MAX(0,pLeft1))] | (curve[MIN(_max-1, MAX(0,pLeft2))] << 16);
 
128
    }
 
129
  }
 
130
}
 
131
 
 
132
/*
 
133
*--------------------------------------------------------------
 
134
*
 
135
* HuffDecode --
 
136
*
 
137
* Taken from Figure F.16: extract next coded symbol from
 
138
* input stream.  This should becode a macro.
 
139
*
 
140
* Results:
 
141
* Next coded symbol
 
142
*
 
143
* Side effects:
 
144
* Bitstream is parsed.
 
145
*
 
146
*--------------------------------------------------------------
 
147
*/
 
148
int NikonDecompressor::HuffDecodeNikon() {
 
149
  int rv;
 
150
  int l, temp;
 
151
  int code, val ;
 
152
 
 
153
  HuffmanTable *dctbl1 = &huff[0];
 
154
 
 
155
  bits->fill();
 
156
  code = bits->peekBitsNoFill(14);
 
157
  val = dctbl1->bigTable[code];
 
158
  if ((val&0xff) !=  0xff) {
 
159
    bits->skipBitsNoFill(val&0xff);
 
160
    return val >> 8;
 
161
  }
 
162
 
 
163
  rv = 0;
 
164
  code = bits->peekByteNoFill();
 
165
  val = dctbl1->numbits[code];
 
166
  l = val & 15;
 
167
  if (l) {
 
168
    bits->skipBitsNoFill(l);
 
169
    rv = val >> 4;
 
170
  }  else {
 
171
    bits->skipBits(8);
 
172
    l = 8;
 
173
    while (code > dctbl1->maxcode[l]) {
 
174
      temp = bits->getBitNoFill();
 
175
      code = (code << 1) | temp;
 
176
      l++;
 
177
    }
 
178
 
 
179
    if (l > 16) {
 
180
      ThrowIOE("Corrupt JPEG data: bad Huffman code:%u\n", l);
 
181
    } else {
 
182
      rv = dctbl1->huffval[dctbl1->valptr[l] +
 
183
                           ((int)(code - dctbl1->mincode[l]))];
 
184
    }
 
185
  }
 
186
 
 
187
  if (rv == 16)
 
188
    return -32768;
 
189
 
 
190
  /*
 
191
  * Section F.2.2.1: decode the difference and
 
192
  * Figure F.12: extend sign bit
 
193
  */
 
194
  uint32 len = rv & 15;
 
195
  uint32 shl = rv >> 4;
 
196
  int diff = ((bits->getBits(len - shl) << 1) + 1) << shl >> 1;
 
197
  if ((diff & (1 << (len - 1))) == 0)
 
198
    diff -= (1 << len) - !shl;
 
199
  return diff;
 
200
}
 
201
 
 
202
} // namespace RawSpeed