2
#include "NikonDecompressor.h"
4
RawSpeed - RAW file decoder.
6
Copyright (C) 2009 Klaus Post
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.
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.
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
22
http://www.klauspost.com
27
NikonDecompressor::NikonDecompressor(FileMap* file, RawImage img) :
28
LJpegDecompressor(file, img) {
29
for (uint32 i = 0; i < 0xffff ; i++) {
35
NikonDecompressor::~NikonDecompressor(void) {
41
void NikonDecompressor::initTable(uint32 huffSelect) {
42
HuffmanTable *dctbl1 = &huff[0];
44
for (uint32 i = 0; i < 16 ;i++) {
45
dctbl1->bits[i+1] = nikon_tree[huffSelect][i];
46
acc += dctbl1->bits[i+1];
50
for (uint32 i = 0 ; i < acc; i++) {
51
dctbl1->huffval[i] = nikon_tree[huffSelect][i+16];
53
createHuffmanTable(dctbl1);
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;
65
_RPT2(0, "Nef version v0:%u, v1:%u\n", v0, v1);
67
if (v0 == 73 || v1 == 88)
68
metadata->skipBytes(2110);
70
if (v0 == 70) huffSelect = 2;
71
if (bitsPS == 14) huffSelect += 3;
73
pUp1[0] = metadata->getShort();
74
pUp1[1] = metadata->getShort();
75
pUp2[0] = metadata->getShort();
76
pUp2[1] = metadata->getShort();
78
int _max = 1 << bitsPS & 0x7fff;
80
uint32 csize = metadata->getShort();
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();
97
while (curve[_max-2] == curve[_max-1]) _max--;
98
initTable(huffSelect);
100
mRaw->whitePoint = curve[_max-1];
101
mRaw->blackLevel = curve[0];
104
bits = new BitPumpMSB(mFile->getData(offset), size);
105
uchar8 *draw = mRaw->getData();
107
uint32 pitch = mRaw->pitch;
113
for (y = 0; y < h; y++) {
114
if (split && y == split) {
115
initTable(huffSelect + 1);
117
dest = (uint32*) & draw[y*pitch]; // Adjust destination
118
pUp1[y&1] += HuffDecodeNikon();
119
pUp2[y&1] += HuffDecodeNikon();
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++) {
125
pLeft1 += HuffDecodeNikon();
126
pLeft2 += HuffDecodeNikon();
127
dest[x] = curve[MIN(_max-1, MAX(0,pLeft1))] | (curve[MIN(_max-1, MAX(0,pLeft2))] << 16);
133
*--------------------------------------------------------------
137
* Taken from Figure F.16: extract next coded symbol from
138
* input stream. This should becode a macro.
144
* Bitstream is parsed.
146
*--------------------------------------------------------------
148
int NikonDecompressor::HuffDecodeNikon() {
153
HuffmanTable *dctbl1 = &huff[0];
156
code = bits->peekBitsNoFill(14);
157
val = dctbl1->bigTable[code];
158
if ((val&0xff) != 0xff) {
159
bits->skipBitsNoFill(val&0xff);
164
code = bits->peekByteNoFill();
165
val = dctbl1->numbits[code];
168
bits->skipBitsNoFill(l);
173
while (code > dctbl1->maxcode[l]) {
174
temp = bits->getBitNoFill();
175
code = (code << 1) | temp;
180
ThrowIOE("Corrupt JPEG data: bad Huffman code:%u\n", l);
182
rv = dctbl1->huffval[dctbl1->valptr[l] +
183
((int)(code - dctbl1->mincode[l]))];
191
* Section F.2.2.1: decode the difference and
192
* Figure F.12: extend sign bit
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;
202
} // namespace RawSpeed