2
#include "PentaxDecompressor.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
PentaxDecompressor::PentaxDecompressor(FileMap* file, RawImage img) :
28
LJpegDecompressor(file, img) {
32
PentaxDecompressor::~PentaxDecompressor(void) {
39
void PentaxDecompressor::decodePentax(TiffIFD *root, uint32 offset, uint32 size) {
40
// Prepare huffmann table 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 = 16 entries
41
static const uchar8 pentax_tree[] = { 0, 2, 3, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0,
42
3, 4, 2, 5, 1, 6, 0, 7, 8, 9, 10, 11, 12
44
// 0 1 2 3 4 5 6 7 8 9 0 1 2 = 13 entries
45
HuffmanTable *dctbl1 = &huff[0];
47
/* Attempt to read huffman table, if found in makernote */
48
if (root->hasEntryRecursive((TiffTag)0x220)) {
49
TiffEntry *t = root->getEntryRecursive((TiffTag)0x220);
50
if (t->type == TIFF_UNDEFINED) {
51
const uchar8* data = t->getData();
52
uint32 depth = (data[1]+12)&0xf;
57
for (uint32 i = 0; i < depth; i++)
58
v0[i] = (uint32)(data[i*2])<<8 | (uint32)(data[i*2+1]);
61
for (uint32 i = 0; i < depth; i++)
65
for (uint32 i = 0; i < 17; i++)
68
/* Calculate codes and store bitcounts */
69
for (uint32 c = 0; c < depth; c++)
71
v2[c] = v0[c]>>(12-v1[c]);
72
dctbl1->bits[v1[c]]++;
75
for (uint32 i = 0; i < depth; i++)
77
uint32 sm_val = 0xfffffff;
79
for (uint32 j = 0; j < depth; j++)
87
dctbl1->huffval[i] = sm_num;
88
v2[sm_num]=0xffffffff;
92
/* Initialize with legacy data */
94
for (uint32 i = 0; i < 16 ;i++) {
95
dctbl1->bits[i+1] = pentax_tree[i];
96
acc += dctbl1->bits[i+1];
99
for (uint32 i = 0 ; i < acc; i++) {
100
dctbl1->huffval[i] = pentax_tree[i+16];
104
createHuffmanTable(dctbl1);
106
pentaxBits = new BitPumpMSB(mFile->getData(offset), size);
107
uchar8 *draw = mRaw->getData();
109
uint32 w = mRaw->dim.x;
110
uint32 h = mRaw->dim.y;
111
int pUp1[2] = {0, 0};
112
int pUp2[2] = {0, 0};
116
for (uint32 y = 0;y < h;y++) {
117
pentaxBits->checkPos();
118
dest = (ushort16*) & draw[y*mRaw->pitch]; // Adjust destination
119
pUp1[y&1] += HuffDecodePentax();
120
pUp2[y&1] += HuffDecodePentax();
121
dest[0] = pLeft1 = pUp1[y&1];
122
dest[1] = pLeft2 = pUp2[y&1];
123
for (uint32 x = 2; x < w ; x += 2) {
124
pLeft1 += HuffDecodePentax();
125
pLeft2 += HuffDecodePentax();
128
_ASSERTE(pLeft1 >= 0 && pLeft1 <= (65536));
129
_ASSERTE(pLeft2 >= 0 && pLeft2 <= (65536));
135
*--------------------------------------------------------------
139
* Taken from Figure F.16: extract next coded symbol from
140
* input stream. This should becode a macro.
146
* Bitstream is parsed.
148
*--------------------------------------------------------------
150
int PentaxDecompressor::HuffDecodePentax() {
155
HuffmanTable *dctbl1 = &huff[0];
157
* If the huffman code is less than 8 bits, we can use the fast
158
* table lookup to get its value. It's more than 8 bits about
162
code = pentaxBits->peekBitsNoFill(14);
163
val = dctbl1->bigTable[code];
164
if ((val&0xff) != 0xff) {
165
pentaxBits->skipBitsNoFill(val&0xff);
170
code = pentaxBits->peekByteNoFill();
171
val = dctbl1->numbits[code];
174
pentaxBits->skipBitsNoFill(l);
177
pentaxBits->skipBits(8);
179
while (code > dctbl1->maxcode[l]) {
180
temp = pentaxBits->getBitNoFill();
181
code = (code << 1) | temp;
186
* With garbage input we may reach the sentinel value l = 17.
190
ThrowIOE("Corrupt JPEG data: bad Huffman code:%u\n", l);
192
rv = dctbl1->huffval[dctbl1->valptr[l] +
193
((int)(code - dctbl1->mincode[l]))];
201
* Section F.2.2.1: decode the difference and
202
* Figure F.12: extend sign bit
206
int x = pentaxBits->getBits(rv);
207
if ((x & (1 << (rv - 1))) == 0)
214
} // namespace RawSpeed