~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/3rdparty/libpgf/Decoder.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * The Progressive Graphics File; http://www.libpgf.org
3
 
 * 
4
 
 * $Date: 2006-06-04 22:05:59 +0200 (So, 04 Jun 2006) $
5
 
 * $Revision: 229 $
6
 
 * 
7
 
 * This file Copyright (C) 2006 xeraina GmbH, Switzerland
8
 
 * 
9
 
 * This program is free software; you can redistribute it and/or
10
 
 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11
 
 * as published by the Free Software Foundation; either version 2.1
12
 
 * of the License, or (at your option) any later version.
13
 
 * 
14
 
 * This program is distributed in the hope that it will be useful,
15
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 * GNU General Public License for more details.
18
 
 * 
19
 
 * You should have received a copy of the GNU General Public License
20
 
 * along with this program; if not, write to the Free Software
21
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 
 */
23
 
 
24
 
#include "Decoder.h"
25
 
#ifdef TRACE
26
 
        #include <stdio.h>
27
 
#endif
28
 
 
29
 
//////////////////////////////////////////////////////
30
 
// PGF: file structure
31
 
//
32
 
// PGFPreHeader PGFHeader PGFPostHeader LevelLengths Level_n-1 Level_n-2 ... Level_0
33
 
// PGFPostHeader ::= [ColorTable] [UserData]
34
 
// LevelLengths  ::= UINT32[nLevels]
35
 
 
36
 
//////////////////////////////////////////////////////
37
 
// Decoding scheme
38
 
// input:  binary file
39
 
// output: wavelet coefficients stored in subbands
40
 
//
41
 
//                    file      (for each buffer: packedLength (16 bit), packed bits)
42
 
//                      |
43
 
//                m_codeBuffer  (for each plane: RLcodeLength (16 bit), RLcoded sigBits + m_sign, refBits)
44
 
//                |     |     |
45
 
//           m_sign  sigBits  refBits   [BufferLen, BufferLen, BufferLen]
46
 
//                |     |     |
47
 
//                   m_value    [BufferSize]
48
 
//                      |
49
 
//                   subband
50
 
//  
51
 
 
52
 
// Constants
53
 
#define CodeBufferBitLen                (BufferSize*WordWidth)  // max number of bits in m_codeBuffer
54
 
 
55
 
/////////////////////////////////////////////////////////////////////
56
 
// Default Constructor
57
 
CDecoder::CDecoder(CPGFStream* stream /*= NULL */) 
58
 
: m_stream(stream), m_startPos(0), m_encodedHeaderLength(0), m_valuePos(0), m_bufferIsAvailable(false)
59
 
#ifdef __PGFROISUPPORT__
60
 
, m_roi(false)
61
 
#endif
62
 
{
63
 
}
64
 
 
65
 
/////////////////////////////////////////////////////////////////////
66
 
// Constructor
67
 
// Read pre-header, header, and levelLength
68
 
// throws IOException
69
 
CDecoder::CDecoder(CPGFStream* stream, PGFPreHeader& preHeader, PGFHeader& header, PGFPostHeader& postHeader, UINT32*& levelLength) THROW_
70
 
: m_stream(stream), m_startPos(0), m_encodedHeaderLength(0), m_valuePos(0), m_bufferIsAvailable(false)
71
 
#ifdef __PGFROISUPPORT__
72
 
, m_roi(false)
73
 
#endif
74
 
{
75
 
        ASSERT(m_stream);
76
 
 
77
 
        int count;
78
 
 
79
 
        // store current stream position
80
 
        m_startPos = m_stream->GetPos();
81
 
 
82
 
        // read magic and version
83
 
        count = MagicVersionSize;
84
 
        m_stream->Read(&count, &preHeader);
85
 
 
86
 
        // read header size
87
 
        if (preHeader.version & Version6) {
88
 
                // 32 bit header size since version 6
89
 
                count = 4;
90
 
        } else {
91
 
                count = 2;
92
 
        }
93
 
        m_stream->Read(&count, ((UINT8*)&preHeader) + MagicVersionSize);
94
 
 
95
 
        // make sure the values are correct read
96
 
        preHeader.hSize = __VAL(preHeader.hSize);
97
 
 
98
 
        // check magic number
99
 
        if (memcmp(preHeader.magic, Magic, 3) != 0) {
100
 
                // error condition: wrong Magic number
101
 
                ReturnWithError(FormatCannotRead);
102
 
        }
103
 
 
104
 
        // read file header
105
 
        count = (preHeader.hSize < HeaderSize) ? preHeader.hSize : HeaderSize;
106
 
        m_stream->Read(&count, &header);
107
 
        // make sure the values are correct read
108
 
        header.height = __VAL(UINT32(header.height));
109
 
        header.width = __VAL(UINT32(header.width));
110
 
 
111
 
        // be ready to read all versions including version 0
112
 
        if (preHeader.version > 0) {
113
 
#ifndef __PGFROISUPPORT__
114
 
                // check ROI usage
115
 
                if (preHeader.version & PGFROI) ReturnWithError(FormatCannotRead);
116
 
#endif
117
 
 
118
 
                int size = preHeader.hSize - HeaderSize;
119
 
 
120
 
                if (size > 0) {
121
 
                        // read post header
122
 
                        if (header.mode == ImageModeIndexedColor) {
123
 
                                ASSERT(size >= ColorTableSize);
124
 
                                // read color table
125
 
                                count = ColorTableSize;
126
 
                                m_stream->Read(&count, postHeader.clut);
127
 
                                ASSERT(count == ColorTableSize);
128
 
                                size -= count;
129
 
                        }
130
 
 
131
 
                        if (size > 0) {
132
 
                                // create user data memory block
133
 
                                postHeader.userDataLen = size;
134
 
                                postHeader.userData = new UINT8[postHeader.userDataLen];
135
 
 
136
 
                                // read user data
137
 
                                count = postHeader.userDataLen;
138
 
                                m_stream->Read(&count, postHeader.userData);
139
 
                                ASSERT(count == size);
140
 
                        }
141
 
                }
142
 
 
143
 
                // create levelLength
144
 
                levelLength = new UINT32[header.nLevels];
145
 
                if (!levelLength) ReturnWithError(InsufficientMemory);
146
 
 
147
 
                // read levelLength
148
 
                count = header.nLevels*WordBytes;
149
 
                m_stream->Read(&count, levelLength);
150
 
 
151
 
#ifdef PGF_USE_BIG_ENDIAN 
152
 
                // make sure the values are correct read
153
 
                count /= WordBytes;
154
 
                for (int i=0; i < count; i++) {
155
 
                        levelLength[i] = __VAL(levelLength[i]);
156
 
                }
157
 
#endif
158
 
        }
159
 
 
160
 
        // store current stream position
161
 
        m_encodedHeaderLength = UINT32(m_stream->GetPos() - m_startPos);
162
 
}
163
 
 
164
 
/////////////////////////////////////////////////////////////////////
165
 
// Destructor
166
 
CDecoder::~CDecoder() {
167
 
}
168
 
 
169
 
//////////////////////////////////////////////////////////////////////
170
 
/// Copies data from the open stream to a target buffer.
171
 
/// It might throw an IOException.
172
 
/// @param target The target buffer
173
 
/// @param len The number of bytes to read
174
 
/// @return The number of bytes copied to the target buffer
175
 
UINT32 CDecoder::ReadEncodedData(UINT8* target, UINT32 len) const THROW_ {
176
 
        ASSERT(m_stream);
177
 
 
178
 
        int count = len;
179
 
        m_stream->Read(&count, target);
180
 
 
181
 
        return count;
182
 
}
183
 
 
184
 
/////////////////////////////////////////////////////////////////////
185
 
/// Unpartitions a rectangular region of a given subband.
186
 
/// Partitioning scheme: The plane is partitioned in squares of side length LinBlockSize.
187
 
/// Write wavelet coefficients into buffer.
188
 
/// It might throw an IOException.
189
 
/// @param band A subband
190
 
/// @param quantParam Dequantization value
191
 
/// @param width The width of the rectangle
192
 
/// @param height The height of the rectangle
193
 
/// @param startPos The buffer position of the top left corner of the rectangular region
194
 
/// @param pitch The number of bytes in row of the subband
195
 
void CDecoder::Partition(CSubband* band, int quantParam, int width, int height, int startPos, int pitch) THROW_ {
196
 
        ASSERT(band);
197
 
 
198
 
        const div_t ww = div(width, LinBlockSize);
199
 
        const div_t hh = div(height, LinBlockSize);
200
 
        const int ws = pitch - LinBlockSize;
201
 
        const int wr = pitch - ww.rem;
202
 
        int x, y, i, j;
203
 
        int pos, base = startPos, base2;
204
 
 
205
 
        // main height
206
 
        for (i=0; i < hh.quot; i++) {
207
 
                // main width
208
 
                base2 = base;
209
 
                for (j=0; j < ww.quot; j++) {
210
 
                        pos = base2;
211
 
                        for (y=0; y < LinBlockSize; y++) {
212
 
                                for (x=0; x < LinBlockSize; x++) {
213
 
                                        DequantizeValue(band, pos, quantParam);
214
 
                                        pos++;
215
 
                                }
216
 
                                pos += ws;
217
 
                        }
218
 
                        base2 += LinBlockSize;
219
 
                }
220
 
                // rest of width
221
 
                pos = base2;
222
 
                for (y=0; y < LinBlockSize; y++) {
223
 
                        for (x=0; x < ww.rem; x++) {
224
 
                                DequantizeValue(band, pos, quantParam);
225
 
                                pos++;
226
 
                        }
227
 
                        pos += wr;
228
 
                        base += pitch;
229
 
                }
230
 
        }
231
 
        // main width 
232
 
        base2 = base;
233
 
        for (j=0; j < ww.quot; j++) {
234
 
                // rest of height
235
 
                pos = base2;
236
 
                for (y=0; y < hh.rem; y++) {
237
 
                        for (x=0; x < LinBlockSize; x++) {
238
 
                                DequantizeValue(band, pos, quantParam);
239
 
                                pos++;
240
 
                        }
241
 
                        pos += ws;
242
 
                }
243
 
                base2 += LinBlockSize;
244
 
        }
245
 
        // rest of height
246
 
        pos = base2;
247
 
        for (y=0; y < hh.rem; y++) {
248
 
                // rest of width
249
 
                for (x=0; x < ww.rem; x++) {
250
 
                        DequantizeValue(band, pos, quantParam);
251
 
                        pos++;
252
 
                }
253
 
                pos += wr;
254
 
        }
255
 
}
256
 
 
257
 
////////////////////////////////////////////////////////////////////
258
 
// Decode and dequantize HL, and LH band of one level
259
 
// LH and HH are interleaved in the codestream and must be split
260
 
// Deccoding and dequantization of HL and LH Band (interleaved) using partitioning scheme
261
 
// partitions the plane in squares of side length InterBlockSize
262
 
// throws IOException
263
 
void CDecoder::DecodeInterleaved(CWaveletTransform* wtChannel, int level, int quantParam) THROW_ {
264
 
        CSubband* hlBand = wtChannel->GetSubband(level, HL);
265
 
        CSubband* lhBand = wtChannel->GetSubband(level, LH);
266
 
        const div_t lhH = div(lhBand->GetHeight(), InterBlockSize);
267
 
        const div_t hlW = div(hlBand->GetWidth(), InterBlockSize);
268
 
        const int hlws = hlBand->GetWidth() - InterBlockSize;
269
 
        const int hlwr = hlBand->GetWidth() - hlW.rem;
270
 
        const int lhws = lhBand->GetWidth() - InterBlockSize;
271
 
        const int lhwr = lhBand->GetWidth() - hlW.rem;
272
 
        int x, y, i, j;
273
 
        int hlPos, lhPos;
274
 
        int hlBase = 0, lhBase = 0, hlBase2, lhBase2;
275
 
 
276
 
        ASSERT(lhBand->GetWidth() >= hlBand->GetWidth());
277
 
        ASSERT(hlBand->GetHeight() >= lhBand->GetHeight());
278
 
 
279
 
        hlBand->AllocMemory();
280
 
        lhBand->AllocMemory();
281
 
 
282
 
        // correct quantParam with normalization factor
283
 
        quantParam -= level;
284
 
        if (quantParam < 0) quantParam = 0;
285
 
 
286
 
        // main height
287
 
        for (i=0; i < lhH.quot; i++) {
288
 
                // main width
289
 
                hlBase2 = hlBase;
290
 
                lhBase2 = lhBase;
291
 
                for (j=0; j < hlW.quot; j++) {
292
 
                        hlPos = hlBase2;
293
 
                        lhPos = lhBase2;
294
 
                        for (y=0; y < InterBlockSize; y++) {
295
 
                                for (x=0; x < InterBlockSize; x++) {
296
 
                                        DequantizeValue(hlBand, hlPos, quantParam);
297
 
                                        DequantizeValue(lhBand, lhPos, quantParam);
298
 
                                        hlPos++;
299
 
                                        lhPos++;
300
 
                                }
301
 
                                hlPos += hlws;
302
 
                                lhPos += lhws;
303
 
                        }
304
 
                        hlBase2 += InterBlockSize;
305
 
                        lhBase2 += InterBlockSize;
306
 
                }
307
 
                // rest of width
308
 
                hlPos = hlBase2;
309
 
                lhPos = lhBase2;
310
 
                for (y=0; y < InterBlockSize; y++) {
311
 
                        for (x=0; x < hlW.rem; x++) {
312
 
                                DequantizeValue(hlBand, hlPos, quantParam);
313
 
                                DequantizeValue(lhBand, lhPos, quantParam);
314
 
                                hlPos++;
315
 
                                lhPos++;
316
 
                        }
317
 
                        // width difference between HL and LH
318
 
                        if (lhBand->GetWidth() > hlBand->GetWidth()) {
319
 
                                DequantizeValue(lhBand, lhPos, quantParam);
320
 
                        }
321
 
                        hlPos += hlwr;
322
 
                        lhPos += lhwr;
323
 
                        hlBase += hlBand->GetWidth();
324
 
                        lhBase += lhBand->GetWidth();
325
 
                }
326
 
        }
327
 
        // main width 
328
 
        hlBase2 = hlBase;
329
 
        lhBase2 = lhBase;
330
 
        for (j=0; j < hlW.quot; j++) {
331
 
                // rest of height
332
 
                hlPos = hlBase2;
333
 
                lhPos = lhBase2;
334
 
                for (y=0; y < lhH.rem; y++) {
335
 
                        for (x=0; x < InterBlockSize; x++) {
336
 
                                DequantizeValue(hlBand, hlPos, quantParam);
337
 
                                DequantizeValue(lhBand, lhPos, quantParam);
338
 
                                hlPos++;
339
 
                                lhPos++;
340
 
                        }
341
 
                        hlPos += hlws;
342
 
                        lhPos += lhws;
343
 
                }
344
 
                hlBase2 += InterBlockSize;
345
 
                lhBase2 += InterBlockSize;
346
 
        }
347
 
        // rest of height
348
 
        hlPos = hlBase2;
349
 
        lhPos = lhBase2;
350
 
        for (y=0; y < lhH.rem; y++) {
351
 
                // rest of width
352
 
                for (x=0; x < hlW.rem; x++) {
353
 
                        DequantizeValue(hlBand, hlPos, quantParam);
354
 
                        DequantizeValue(lhBand, lhPos, quantParam);
355
 
                        hlPos++;
356
 
                        lhPos++;
357
 
                }
358
 
                // width difference between HL and LH
359
 
                if (lhBand->GetWidth() > hlBand->GetWidth()) {
360
 
                        DequantizeValue(lhBand, lhPos, quantParam);
361
 
                }
362
 
                hlPos += hlwr;
363
 
                lhPos += lhwr;
364
 
                hlBase += hlBand->GetWidth();
365
 
        }
366
 
        // height difference between HL and LH
367
 
        if (hlBand->GetHeight() > lhBand->GetHeight()) {
368
 
                // total width
369
 
                hlPos = hlBase;
370
 
                for (j=0; j < hlBand->GetWidth(); j++) {
371
 
                        DequantizeValue(hlBand, hlPos, quantParam);
372
 
                        hlPos++;
373
 
                }
374
 
        }
375
 
}
376
 
 
377
 
////////////////////////////////////////////////////////////////////
378
 
/// Skip a given number of bytes in the open stream.
379
 
/// It might throw an IOException.
380
 
void CDecoder::Skip(UINT64 offset) THROW_ {
381
 
        m_stream->SetPos(FSFromCurrent, offset);
382
 
}
383
 
 
384
 
//////////////////////////////////////////////////////////////////////
385
 
/// Dequantization of a single value at given position in subband.
386
 
/// If encoded data is available, then stores dequantized band value into 
387
 
/// buffer m_value at position m_valuePos.
388
 
/// Otherwise reads encoded data buffer and decodes it.
389
 
/// @param band A subband
390
 
/// @param bandPos A valid position in subband band
391
 
/// @param quantParam The quantization parameter
392
 
void CDecoder::DequantizeValue(CSubband* band, UINT32 bandPos, int quantParam) {
393
 
        if (!m_bufferIsAvailable) {
394
 
                DecodeBuffer();
395
 
        }
396
 
        band->SetData(bandPos, m_value[m_valuePos] << quantParam);
397
 
        m_valuePos++;
398
 
        if (m_valuePos == BufferSize) {
399
 
                m_bufferIsAvailable = false;
400
 
        }
401
 
}
402
 
 
403
 
///////////////////////////////////////////////////////
404
 
// Read next block from stream and decode into buffer
405
 
// Decoding scheme: <wordLen>(16 bits) [ ROI ] data
406
 
//              ROI       ::= <bufferSize>(15 bits) <eofTile>(1 bit)
407
 
// throws IOException
408
 
void CDecoder::DecodeBuffer() THROW_ {
409
 
        UINT16 wordLen;
410
 
        ROIBlockHeader h(BufferSize);
411
 
        int count;
412
 
 
413
 
#ifdef TRACE
414
 
        UINT32 filePos = (UINT32)m_stream->GetPos();
415
 
        printf("%d\n", filePos);
416
 
#endif
417
 
 
418
 
        // read wordLen
419
 
        count = sizeof(UINT16);
420
 
        m_stream->Read(&count, &wordLen); ASSERT(count == sizeof(UINT16));
421
 
        wordLen = __VAL(wordLen);
422
 
        ASSERT(wordLen <= BufferSize);
423
 
 
424
 
#ifdef __PGFROISUPPORT__
425
 
        // read ROIBlockHeader
426
 
        if (m_roi) {
427
 
                m_stream->Read(&count, &h.val); ASSERT(count == sizeof(UINT16));
428
 
                
429
 
                // convert ROIBlockHeader
430
 
                h.val = __VAL(h.val);
431
 
        }
432
 
#endif
433
 
 
434
 
        // read data
435
 
        count = wordLen*WordBytes;
436
 
        m_stream->Read(&count, m_codeBuffer);
437
 
 
438
 
#ifdef PGF_USE_BIG_ENDIAN 
439
 
        // convert data
440
 
        count /= WordBytes;
441
 
        for (int i=0; i < count; i++) {
442
 
                m_codeBuffer[i] = __VAL(m_codeBuffer[i]);
443
 
        }
444
 
#endif
445
 
 
446
 
#ifdef __PGFROISUPPORT__
447
 
        ASSERT(m_roi && h.bufferSize <= BufferSize || h.bufferSize == BufferSize);
448
 
#else
449
 
        ASSERT(h.bufferSize == BufferSize);
450
 
#endif
451
 
        BitplaneDecode(h.bufferSize);
452
 
 
453
 
        // data is available
454
 
        m_bufferIsAvailable = true;
455
 
        m_valuePos = 0;
456
 
}
457
 
 
458
 
///////////////////////////////////////////////////////
459
 
// Read next block from stream but don't decode into buffer
460
 
// Encoding scheme: <wordLen>(16 bits) [ ROI ] data
461
 
//              ROI       ::= <bufferSize>(15 bits) <eofTile>(1 bit)
462
 
// throws IOException
463
 
void CDecoder::SkipBuffer() THROW_ {
464
 
        UINT16 wordLen;
465
 
        int count;
466
 
 
467
 
        // read wordLen
468
 
        count = sizeof(UINT16);
469
 
        m_stream->Read(&count, &wordLen); ASSERT(count == sizeof(UINT16));
470
 
        wordLen = __VAL(wordLen);
471
 
        ASSERT(wordLen <= BufferSize);
472
 
 
473
 
#ifdef __PGFROISUPPORT__
474
 
        if (m_roi) {
475
 
                // skip ROIBlockHeader
476
 
                m_stream->SetPos(FSFromCurrent, count);
477
 
        }
478
 
#endif
479
 
 
480
 
        // skip data
481
 
        m_stream->SetPos(FSFromCurrent, wordLen*WordBytes);
482
 
}
483
 
 
484
 
//////////////////////////////////////////////////////////////////////
485
 
// Decode block into buffer of given size using bit plane coding.
486
 
// A buffer contains bufferLen UINT32 values, thus, bufferSize bits per bit plane.
487
 
// Following coding scheme is used: 
488
 
//              Buffer          ::= <nPlanes>(5 bits) foreach(plane i): Plane[i]  
489
 
//              Plane[i]        ::= [ Sig1 | Sig2 ] [DWORD alignment] refBits
490
 
//              Sig1            ::= 1 <codeLen>(15 bits) codedSigAndSignBits 
491
 
//              Sig2            ::= 0 <sigLen>(15 bits) [Sign1 | Sign2 ] sigBits 
492
 
//              Sign1           ::= 1 <codeLen>(15 bits) [DWORD alignment] codedSignBits
493
 
//              Sign2           ::= 0 <signLen>(15 bits) [DWORD alignment] signBits
494
 
void CDecoder::BitplaneDecode(UINT32 bufferSize) {
495
 
        ASSERT(bufferSize <= BufferSize);
496
 
        const UINT32 bufferLen = AlignWordPos(bufferSize)/WordWidth;
497
 
 
498
 
        UINT32 nPlanes, k;
499
 
        UINT32 codePos = 0, codeLen, sigLen, sigPos, signLen, wordPos;
500
 
        int    plane;
501
 
        UINT32 sigBits[BufferLen];
502
 
        UINT32 signBits[BufferLen];
503
 
        UINT32 planeMask;
504
 
 
505
 
        // clear significance vector
506
 
        for (k=0; k < bufferLen; k++) {
507
 
                m_sigFlagVector[k] = 0;
508
 
        }
509
 
 
510
 
        // clear buffer
511
 
        for (k=0; k < bufferSize; k++) {
512
 
                m_value[k] = 0;
513
 
        }
514
 
 
515
 
        // read number of bit planes
516
 
        nPlanes = GetValueBlock(m_codeBuffer, 0, MaxBitPlanesLog); 
517
 
        codePos += MaxBitPlanesLog;
518
 
 
519
 
        // loop through all bit planes
520
 
        if (nPlanes == 0) nPlanes = MaxBitPlanes + 1;
521
 
        ASSERT(0 < nPlanes && nPlanes <= MaxBitPlanes + 1);
522
 
        planeMask = 1 << (nPlanes - 1);
523
 
 
524
 
        for (plane = nPlanes - 1; plane >= 0; plane--) {
525
 
                // read RL code
526
 
                if (GetBit(m_codeBuffer, codePos)) {
527
 
                        // RL coding of sigBits is used
528
 
                        codePos++;
529
 
 
530
 
                        // read codeLen
531
 
                        codeLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen);
532
 
 
533
 
                        // run-length decode significant bits and signs from m_codeBuffer and store them to sigBits
534
 
                        ASSERT(codeLen < (1 << RLblockSizeLen)); 
535
 
                        m_codePos = codePos + RLblockSizeLen;
536
 
                        sigLen = RLDSigsAndSigns(bufferSize, codeLen, sigBits, signBits); ASSERT(sigLen <= bufferSize);
537
 
                        //printf("%d\n", sigLen);
538
 
 
539
 
                        // adjust code buffer position
540
 
                        codePos += RLblockSizeLen + codeLen;
541
 
                        codePos = AlignWordPos(codePos); ASSERT(codePos < CodeBufferBitLen);
542
 
 
543
 
                        // read refinement bits from m_codeBuffer and compose bit plane
544
 
                        sigLen = ComposeBitplane(bufferSize, planeMask, sigBits, &m_codeBuffer[codePos >> WordWidthLog], signBits);
545
 
 
546
 
                        // adjust code buffer position
547
 
                        codePos += bufferSize - sigLen;
548
 
                        codePos = AlignWordPos(codePos); ASSERT(codePos < CodeBufferBitLen);
549
 
                } else {
550
 
                        // no RL coding is used
551
 
                        codePos++;
552
 
 
553
 
                        // read sigLen
554
 
                        sigLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(sigLen <= BufferSize);
555
 
                        codePos += RLblockSizeLen; ASSERT(codePos < CodeBufferBitLen);
556
 
 
557
 
                        // read RL code for signBits
558
 
                        if (GetBit(m_codeBuffer, codePos)) {
559
 
                                // RL coding is used
560
 
                                codePos++;
561
 
 
562
 
                                // read codeLen
563
 
                                codeLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen);
564
 
                                codePos += RLblockSizeLen;
565
 
 
566
 
                                // RL decode signBits
567
 
                                m_codePos = codePos;
568
 
                                signLen = RLDSigns(bufferSize, codeLen, signBits);
569
 
 
570
 
                                // adjust code buffer position
571
 
                                codePos += codeLen;
572
 
                                codePos = AlignWordPos(codePos); ASSERT(codePos < CodeBufferBitLen);
573
 
                        } else {
574
 
                                // RL coding of signBits was not efficient and therefore not used
575
 
                                codePos++;
576
 
 
577
 
                                // read signLen
578
 
                                signLen = GetValueBlock(m_codeBuffer, codePos, RLblockSizeLen); ASSERT(signLen <= bufferSize);
579
 
                                
580
 
                                // adjust code buffer position
581
 
                                codePos += RLblockSizeLen; 
582
 
                                codePos = AlignWordPos(codePos); ASSERT(codePos < CodeBufferBitLen);
583
 
 
584
 
                                // read signBits
585
 
                                wordPos = codePos >> WordWidthLog; ASSERT(0 <= wordPos && wordPos < BufferSize);
586
 
                                signLen = AlignWordPos(signLen) >> WordWidthLog;
587
 
                                for (k=0; k < signLen; k++) {
588
 
                                        signBits[k] = m_codeBuffer[wordPos];
589
 
                                        wordPos++;
590
 
                                }
591
 
 
592
 
                                // adjust code buffer position
593
 
                                codePos = wordPos << WordWidthLog; ASSERT(codePos < CodeBufferBitLen);
594
 
                        }
595
 
 
596
 
                        sigPos = codePos; // position of sigBits
597
 
                        codePos += sigLen;
598
 
 
599
 
                        codePos = AlignWordPos(codePos);
600
 
                        ASSERT(codePos < CodeBufferBitLen);
601
 
 
602
 
                        // read significant and refinement bitset from m_codeBuffer
603
 
                        sigLen = ComposeBitplane(bufferSize, planeMask, &m_codeBuffer[sigPos >> WordWidthLog], &m_codeBuffer[codePos >> WordWidthLog], signBits);
604
 
                        
605
 
                        // adjust code buffer position
606
 
                        codePos += bufferSize - sigLen;
607
 
                        codePos = AlignWordPos(codePos);
608
 
                }
609
 
                planeMask >>= 1;
610
 
        }
611
 
}
612
 
 
613
 
////////////////////////////////////////////////////////////////////
614
 
// Reconstruct bitplane from significant bitset and refinement bitset
615
 
// returns length [bits] of sigBits
616
 
// input:  sigBits, refBits
617
 
// output: m_value
618
 
UINT32 CDecoder::ComposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32* sigBits, UINT32* refBits, UINT32* signBits) {
619
 
        ASSERT(sigBits);
620
 
        ASSERT(refBits);
621
 
        ASSERT(signBits);
622
 
 
623
 
        UINT32 valPos = 0, signPos = 0, refPos = 0;
624
 
        UINT32 sigPos = 0, sigEnd, sigRunLen;
625
 
        UINT32 zerocnt;
626
 
 
627
 
        while (valPos < bufferSize) {
628
 
                // search next 1 in m_sigFlagVector
629
 
                sigRunLen = SeekBitRange(m_sigFlagVector, valPos, bufferSize - valPos);
630
 
 
631
 
                // search 1's in sigBits[valuePos..valuePos+sigRunLen)
632
 
                // these 1's are significant bits
633
 
                sigEnd = sigPos + sigRunLen;
634
 
                while (sigPos < sigEnd) {
635
 
                        // search 0's
636
 
                        zerocnt = SeekBitRange(sigBits, sigPos, sigEnd - sigPos);
637
 
                        sigPos += zerocnt;
638
 
                        valPos += zerocnt;
639
 
                        if (sigPos < sigEnd) {
640
 
                                // write bit to m_value
641
 
                                SetBitAtPos(valPos, planeMask);
642
 
 
643
 
                                // copy sign bit
644
 
                                SetSign(valPos, GetBit(signBits, signPos)); 
645
 
                                signPos++;
646
 
 
647
 
                                // update significance flag vector
648
 
                                SetBit(m_sigFlagVector, valPos);
649
 
                                sigPos++; 
650
 
                                valPos++;
651
 
                        }
652
 
                }
653
 
                // refinement bit
654
 
                if (valPos < bufferSize) {
655
 
                        // write one refinement bit
656
 
                        if (GetBit(refBits, refPos)) {
657
 
                                SetBitAtPos(valPos, planeMask);
658
 
                        }
659
 
                        refPos++;
660
 
                        valPos++;
661
 
                }
662
 
        }
663
 
        ASSERT(sigPos <= bufferSize);
664
 
        ASSERT(refPos <= bufferSize);
665
 
        ASSERT(signPos <= bufferSize);
666
 
        ASSERT(valPos == bufferSize);
667
 
 
668
 
        return sigPos;
669
 
}
670
 
 
671
 
//////////////////////////////////////////////////////////////////////
672
 
// Adaptive run-Length decoder for significant bits with a lot of zeros and sign bits.
673
 
// Returns length of sigBits.
674
 
// - Decode run of 2^k zeros by a single 0.
675
 
// - Decode run of count 0's followed by a 1 with codeword: 1<count>x
676
 
// - x is 0: if a positive sign has been stored, otherwise 1
677
 
// - Read each bit from m_codeBuffer[m_codePos] and increment m_codePos.
678
 
UINT32 CDecoder::RLDSigsAndSigns(UINT32 bufferSize, UINT32 codeLen, UINT32* sigBits, UINT32* signBits) {
679
 
        ASSERT(sigBits);
680
 
        ASSERT(signBits);
681
 
        ASSERT(m_codePos < CodeBufferBitLen);
682
 
        const UINT32 inEndPos = m_codePos + codeLen; ASSERT(inEndPos < CodeBufferBitLen);
683
 
 
684
 
        UINT32 k = 3;
685
 
        UINT32 runlen = 1 << k; // = 2^k
686
 
        UINT32 count = 0;
687
 
        UINT32 sigPos = 0, signPos = 0;
688
 
 
689
 
        ASSERT(inEndPos < CodeBufferBitLen);
690
 
        while (m_codePos < inEndPos) {
691
 
                if (GetBit(m_codeBuffer, m_codePos)) {
692
 
                        m_codePos++;
693
 
                        
694
 
                        // extract counter and generate zero run of length count
695
 
                        if (k > 0) {
696
 
                                // extract counter
697
 
                                count = GetValueBlock(m_codeBuffer, m_codePos, k); 
698
 
                                m_codePos += k;
699
 
                                if (count > 0) {
700
 
                                        ClearBitBlock(sigBits, sigPos, count); 
701
 
                                        sigPos += count;
702
 
                                }
703
 
                        }
704
 
                        // write 1 bit
705
 
                        if (sigPos < bufferSize) {
706
 
                                SetBit(sigBits, sigPos); sigPos++;
707
 
                        }
708
 
 
709
 
                        // get sign bit
710
 
                        if (GetBit(m_codeBuffer, m_codePos)) {
711
 
                                SetBit(signBits, signPos);
712
 
                        } else {
713
 
                                ClearBit(signBits, signPos);
714
 
                        }
715
 
                        signPos++; m_codePos++;
716
 
 
717
 
                        // adapt k (half run-length interval)
718
 
                        if (k > 0) {
719
 
                                k--;
720
 
                                runlen >>= 1;
721
 
                        }
722
 
                } else {
723
 
                        m_codePos++;
724
 
                        // generate zero run of length 2^k
725
 
                        //ASSERT(sigPos + runlen <= BufferSize);
726
 
                        ClearBitBlock(sigBits, sigPos, runlen); 
727
 
                        sigPos += runlen;
728
 
 
729
 
                        // adapt k (double run-length interval)
730
 
                        if (k < WordWidth) {
731
 
                                k++;
732
 
                                runlen <<= 1;
733
 
                        }
734
 
                }
735
 
        }
736
 
        return sigPos;
737
 
}
738
 
 
739
 
/////////////////////////////////////////////////////
740
 
// Adaptive run-length decoder.
741
 
// Returns signLen in bits.
742
 
// decodes codeLen bits from m_codeBuffer at m_codePos
743
 
// decode run of 2^k 1's by a single 1
744
 
// decode run of count 1's followed by a 0 with codeword: 0<count>
745
 
// output is stored in signBits
746
 
UINT32 CDecoder::RLDSigns(UINT32 bufferSize, UINT32 codeLen, UINT32* signBits) {
747
 
        ASSERT(signBits);
748
 
        ASSERT(0 <= m_codePos && m_codePos < CodeBufferBitLen);
749
 
        const UINT32 inEndPos = m_codePos + codeLen; ASSERT(inEndPos < CodeBufferBitLen);
750
 
 
751
 
        UINT32 k = 0;
752
 
        UINT32 runlen = 1 << k; // = 2^k
753
 
        UINT32 count = 0, signPos = 0;
754
 
 
755
 
        while (m_codePos < inEndPos) {
756
 
                if (GetBit(m_codeBuffer, m_codePos)) {
757
 
                        m_codePos++;
758
 
                        // generate 1's run of length 2^k
759
 
                        SetBitBlock(signBits, signPos, runlen); 
760
 
                        signPos += runlen;
761
 
                        
762
 
                        // adapt k (double run-length interval)
763
 
                        if (k < WordWidth) {
764
 
                                k++; 
765
 
                                runlen <<= 1;
766
 
                        }
767
 
                } else {
768
 
                        m_codePos++;
769
 
                        // extract counter and generate zero run of length count
770
 
                        if (k > 0) {
771
 
                                // extract counter
772
 
                                count = GetValueBlock(m_codeBuffer, m_codePos, k); 
773
 
                                m_codePos += k;
774
 
                                if (count > 0) {
775
 
                                        SetBitBlock(signBits, signPos, count); 
776
 
                                        signPos += count;
777
 
                                }
778
 
                        }
779
 
                        if (signPos < bufferSize) {
780
 
                                ClearBit(signBits, signPos); 
781
 
                                signPos++;
782
 
                        }
783
 
                        
784
 
                        // adapt k (half run-length interval)
785
 
                        if (k > 0) {
786
 
                                k--; 
787
 
                                runlen >>= 1;
788
 
                        }
789
 
                }
790
 
        }
791
 
        return signPos;
792
 
}
793
 
 
794
 
////////////////////////////////////////////////////////////////////
795
 
#ifdef TRACE
796
 
void CDecoder::DumpBuffer() {
797
 
        printf("\nDump\n");
798
 
        for (int i=0; i < BufferSize; i++) {
799
 
                printf("%d", m_value[i]);
800
 
        }
801
 
}
802
 
#endif //TRACE