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

« back to all changes in this revision

Viewing changes to extra/libkdcraw/libraw/RawSpeed/PentaxDecompressor.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 "PentaxDecompressor.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
PentaxDecompressor::PentaxDecompressor(FileMap* file, RawImage img) :
 
28
    LJpegDecompressor(file, img) {
 
29
  pentaxBits = 0;
 
30
}
 
31
 
 
32
PentaxDecompressor::~PentaxDecompressor(void) {
 
33
  if (pentaxBits)
 
34
    delete(pentaxBits);
 
35
  pentaxBits = 0;
 
36
}
 
37
 
 
38
 
 
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
 
43
                                       };
 
44
  //                                     0 1 2 3 4 5 6 7 8 9  0  1  2 = 13 entries
 
45
  HuffmanTable *dctbl1 = &huff[0];
 
46
 
 
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;
 
53
      data +=14;
 
54
      uint32 v0[16];
 
55
      uint32 v1[16];
 
56
      uint32 v2[16];
 
57
      for (uint32 i = 0; i < depth; i++)
 
58
         v0[i] = (uint32)(data[i*2])<<8 | (uint32)(data[i*2+1]);
 
59
      data+=depth*2;
 
60
 
 
61
      for (uint32 i = 0; i < depth; i++)
 
62
        v1[i] = data[i];
 
63
 
 
64
      /* Reset bits */
 
65
      for (uint32 i = 0; i < 17; i++)
 
66
        dctbl1->bits[i] = 0;
 
67
 
 
68
      /* Calculate codes and store bitcounts */
 
69
      for (uint32 c = 0; c < depth; c++)
 
70
      {
 
71
        v2[c] = v0[c]>>(12-v1[c]);
 
72
        dctbl1->bits[v1[c]]++;
 
73
      }
 
74
      /* Find smallest */
 
75
      for (uint32 i = 0; i < depth; i++)
 
76
      {
 
77
        uint32 sm_val = 0xfffffff;
 
78
        uint32 sm_num = 0xff;
 
79
        for (uint32 j = 0; j < depth; j++)
 
80
        {
 
81
          if(v2[j]<=sm_val)
 
82
          {
 
83
            sm_num = j;
 
84
            sm_val = v2[j];
 
85
          }
 
86
        }
 
87
        dctbl1->huffval[i] = sm_num;
 
88
        v2[sm_num]=0xffffffff;
 
89
      }
 
90
    }
 
91
  } else {
 
92
    /* Initialize with legacy data */
 
93
    uint32 acc = 0;
 
94
    for (uint32 i = 0; i < 16 ;i++) {
 
95
      dctbl1->bits[i+1] = pentax_tree[i];
 
96
      acc += dctbl1->bits[i+1];
 
97
    }
 
98
    dctbl1->bits[0] = 0;
 
99
    for (uint32 i = 0 ; i < acc; i++) {
 
100
      dctbl1->huffval[i] = pentax_tree[i+16];
 
101
    }
 
102
  }
 
103
  mUseBigtable = true;
 
104
  createHuffmanTable(dctbl1);
 
105
 
 
106
  pentaxBits = new BitPumpMSB(mFile->getData(offset), size);
 
107
  uchar8 *draw = mRaw->getData();
 
108
  ushort16 *dest;
 
109
  uint32 w = mRaw->dim.x;
 
110
  uint32 h = mRaw->dim.y;
 
111
  int pUp1[2] = {0, 0};
 
112
  int pUp2[2] = {0, 0};
 
113
  int pLeft1 = 0;
 
114
  int pLeft2 = 0;
 
115
 
 
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();
 
126
      dest[x] =  pLeft1;
 
127
      dest[x+1] =  pLeft2;
 
128
      _ASSERTE(pLeft1 >= 0 && pLeft1 <= (65536));
 
129
      _ASSERTE(pLeft2 >= 0 && pLeft2 <= (65536));
 
130
    }
 
131
  }
 
132
}
 
133
 
 
134
/*
 
135
*--------------------------------------------------------------
 
136
*
 
137
* HuffDecode --
 
138
*
 
139
* Taken from Figure F.16: extract next coded symbol from
 
140
* input stream.  This should becode a macro.
 
141
*
 
142
* Results:
 
143
* Next coded symbol
 
144
*
 
145
* Side effects:
 
146
* Bitstream is parsed.
 
147
*
 
148
*--------------------------------------------------------------
 
149
*/
 
150
int PentaxDecompressor::HuffDecodePentax() {
 
151
  int rv;
 
152
  int l, temp;
 
153
  int code, val;
 
154
 
 
155
  HuffmanTable *dctbl1 = &huff[0];
 
156
  /*
 
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
 
159
  * 3-4% of the time.
 
160
  */
 
161
  pentaxBits->fill();
 
162
  code = pentaxBits->peekBitsNoFill(14);
 
163
  val = dctbl1->bigTable[code];
 
164
  if ((val&0xff) !=  0xff) {
 
165
    pentaxBits->skipBitsNoFill(val&0xff);
 
166
    return val >> 8;
 
167
  }
 
168
 
 
169
  rv = 0;
 
170
  code = pentaxBits->peekByteNoFill();
 
171
  val = dctbl1->numbits[code];
 
172
  l = val & 15;
 
173
  if (l) {
 
174
    pentaxBits->skipBitsNoFill(l);
 
175
    rv = val >> 4;
 
176
  }  else {
 
177
    pentaxBits->skipBits(8);
 
178
    l = 8;
 
179
    while (code > dctbl1->maxcode[l]) {
 
180
      temp = pentaxBits->getBitNoFill();
 
181
      code = (code << 1) | temp;
 
182
      l++;
 
183
    }
 
184
 
 
185
    /*
 
186
    * With garbage input we may reach the sentinel value l = 17.
 
187
    */
 
188
 
 
189
    if (l > 12) {
 
190
      ThrowRDE("Corrupt JPEG data: bad Huffman code:%u\n", l);
 
191
    } else {
 
192
      rv = dctbl1->huffval[dctbl1->valptr[l] +
 
193
                           ((int)(code - dctbl1->mincode[l]))];
 
194
    }
 
195
  }
 
196
 
 
197
  if (rv == 16)
 
198
    return -32768;
 
199
 
 
200
  /*
 
201
  * Section F.2.2.1: decode the difference and
 
202
  * Figure F.12: extend sign bit
 
203
  */
 
204
 
 
205
  if (rv) {
 
206
    int x = pentaxBits->getBits(rv);
 
207
    if ((x & (1 << (rv - 1))) == 0)
 
208
      x -= (1 << rv) - 1;
 
209
    return x;
 
210
  }
 
211
  return 0;
 
212
}
 
213
 
 
214
} // namespace RawSpeed