~ubuntu-branches/ubuntu/saucy/openexr/saucy

« back to all changes in this revision

Viewing changes to IlmImf/ImfHuf.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adeodato Simó
  • Date: 2008-03-24 23:00:21 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080324230021-gnofz9mnvcj1xlv3
Tags: 1.6.1-3
Disable (hopefully temporarily) the test suite on arm and ia64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
#include <ImfHuf.h>
49
49
#include <ImfInt64.h>
50
50
#include <ImfAutoArray.h>
51
 
#include <Iex.h>
 
51
#include "Iex.h"
52
52
#include <string.h>
53
53
#include <assert.h>
54
54
#include <algorithm>
79
79
 
80
80
 
81
81
void
 
82
invalidNBits ()
 
83
{
 
84
    throw InputExc ("Error in header for Huffman-encoded data "
 
85
                    "(invalid number of bits).");
 
86
}
 
87
 
 
88
 
 
89
void
82
90
tooMuchData ()
83
91
{
84
92
    throw InputExc ("Error in Huffman-encoded data "
97
105
void
98
106
invalidCode ()
99
107
{
100
 
    throw InputExc ("Error in Huffman-encoded data (invalid code).");
 
108
    throw InputExc ("Error in Huffman-encoded data "
 
109
                    "(invalid code).");
101
110
}
102
111
 
103
112
 
104
113
void
105
114
invalidTableSize ()
106
115
{
107
 
    throw InputExc ("Error in Huffman-encoded data (invalid code table size).");
 
116
    throw InputExc ("Error in Huffman-encoded data "
 
117
                    "(invalid code table size).");
 
118
}
 
119
 
 
120
 
 
121
void
 
122
unexpectedEndOfTable ()
 
123
{
 
124
    throw InputExc ("Error in Huffman-encoded data "
 
125
                    "(unexpected end of code table data).");
 
126
}
 
127
 
 
128
 
 
129
void
 
130
tableTooLong ()
 
131
{
 
132
    throw InputExc ("Error in Huffman-encoded data "
 
133
                    "(code table is longer than expected).");
 
134
}
 
135
 
 
136
 
 
137
void
 
138
invalidTableEntry ()
 
139
{
 
140
    throw InputExc ("Error in Huffman-encoded data "
 
141
                    "(invalid code table entry).");
108
142
}
109
143
 
110
144
 
172
206
void
173
207
hufCanonicalCodeTable (Int64 hcode[HUF_ENCSIZE])
174
208
{
175
 
    Int64 n[58];
176
 
 
177
 
    for (int i = 0; i < 58; ++i)
 
209
    Int64 n[59];
 
210
 
 
211
    //
 
212
    // For each i from 0 through 58, count the
 
213
    // number of different codes of length i, and
 
214
    // store the count in n[i].
 
215
    //
 
216
 
 
217
    for (int i = 0; i <= 58; ++i)
178
218
        n[i] = 0;
179
219
 
180
220
    for (int i = 0; i < HUF_ENCSIZE; ++i)
181
221
        n[hcode[i]] += 1;
182
222
 
 
223
    //
 
224
    // For each i from 58 through 1, compute the
 
225
    // numerically lowest code with length i, and
 
226
    // store that code in n[i].
 
227
    //
 
228
 
183
229
    Int64 c = 0;
184
230
 
185
 
    for (int i = 57; i > 0; --i)
 
231
    for (int i = 58; i > 0; --i)
186
232
    {
187
233
        Int64 nc = ((c + n[i]) >> 1);
188
234
        n[i] = c;
189
235
        c = nc;
190
236
    }
191
237
 
 
238
    //
 
239
    // hcode[i] contains the length, l, of the
 
240
    // code for symbol i.  Assign the next available
 
241
    // code of length l to the symbol and store both
 
242
    // l and the code in hcode[i].
 
243
    //
 
244
 
192
245
    for (int i = 0; i < HUF_ENCSIZE; ++i)
193
246
    {
194
247
        int l = hcode[i];
222
275
     int*       iM)     //  o: max frq index
223
276
{
224
277
    //
225
 
    // Find min/max indices in frq, mark leaves
226
 
    //
227
 
 
228
 
    static const int LEAF  = 0x80000;
229
 
    static const int INDEX = 0x7ffff;
230
 
 
231
 
    // Temporary table dependencies,
232
 
    // and heap for quickly finding min frq value
 
278
    // This function assumes that when it is called, array frq
 
279
    // indicates the frequency of all possible symbols in the data
 
280
    // that are to be Huffman-encoded.  (frq[i] contains the number
 
281
    // of occurrences of symbol i in the data.)
 
282
    //
 
283
    // The loop below does three things:
 
284
    //
 
285
    // 1) Finds the minimum and maximum indices that point
 
286
    //    to non-zero entries in frq:
 
287
    //
 
288
    //     frq[im] != 0, and frq[i] == 0 for all i < im
 
289
    //     frq[iM] != 0, and frq[i] == 0 for all i > iM
 
290
    //
 
291
    // 2) Fills array fHeap with pointers to all non-zero
 
292
    //    entries in frq.
 
293
    //
 
294
    // 3) Initializes array hlink such that hlink[i] == i
 
295
    //    for all array entries.
 
296
    //
233
297
 
234
298
    AutoArray <int, HUF_ENCSIZE> hlink;
235
299
    AutoArray <Int64 *, HUF_ENCSIZE> fHeap;
236
300
 
237
 
    *im= 0;
 
301
    *im = 0;
238
302
 
239
303
    while (!frq[*im])
240
304
        (*im)++;
243
307
 
244
308
    for (int i = *im; i < HUF_ENCSIZE; i++)
245
309
    {
246
 
        hlink[i]= LEAF | i;
 
310
        hlink[i] = i;
247
311
 
248
312
        if (frq[i])
249
313
        {
250
314
            fHeap[nf] = &frq[i];
251
315
            nf++;
252
 
            *iM= i;
 
316
            *iM = i;
253
317
        }
254
318
    }
255
319
 
256
 
    (*iM)++;    // add entry for run-length code
257
 
    frq[*iM]= 1;
 
320
    //
 
321
    // Add a pseudo-symbol, with a frequency count of 1, to frq;
 
322
    // adjust the fHeap and hlink array accordingly.  Function
 
323
    // hufEncode() uses the pseudo-symbol for run-length encoding.
 
324
    //
 
325
 
 
326
    (*iM)++;
 
327
    frq[*iM] = 1;
258
328
    fHeap[nf] = &frq[*iM];
259
329
    nf++;
260
330
 
 
331
    //
 
332
    // Build an array, scode, such that scode[i] contains the number
 
333
    // of bits assigned to symbol i.  Conceptually this is done by
 
334
    // constructing a tree whose leaves are the symbols with non-zero
 
335
    // frequency:
 
336
    //
 
337
    //     Make a heap that contains all symbols with a non-zero frequency,
 
338
    //     with the least frequent symbol on top.
 
339
    //
 
340
    //     Repeat until only one symbol is left on the heap:
 
341
    //
 
342
    //         Take the two least frequent symbols off the top of the heap.
 
343
    //         Create a new node that has first two nodes as children, and
 
344
    //         whose frequency is the sum of the frequencies of the first
 
345
    //         two nodes.  Put the new node back into the heap.
 
346
    //
 
347
    // The last node left on the heap is the root of the tree.  For each
 
348
    // leaf node, the distance between the root and the leaf is the length
 
349
    // of the code for the corresponding symbol.
 
350
    //
 
351
    // The loop below doesn't actually build the tree; instead we compute
 
352
    // the distances of the leaves from the root on the fly.  When a new
 
353
    // node is added to the heap, then that node's descendants are linked
 
354
    // into a single linear list that starts at the new node, and the code
 
355
    // lengths of the descendants (that is, their distance from the root
 
356
    // of the tree) are incremented by one.
 
357
    //
 
358
 
261
359
    make_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
262
360
 
263
 
    //
264
 
    // Init scode & loop on all values
265
 
    //
266
 
 
267
 
    //
268
 
    // Temporary encoding table
269
 
    //
270
 
 
271
361
    AutoArray <Int64, HUF_ENCSIZE> scode;
272
 
 
273
362
    memset (scode, 0, sizeof (Int64) * HUF_ENCSIZE);
274
363
 
275
364
    while (nf > 1)
290
379
        frq[m ] += frq[mm];
291
380
        push_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
292
381
 
293
 
        hlink[mm] &= ~LEAF;     // unmark min value
294
 
 
295
 
        //
296
 
        // Add 1 bit to all lower list codes
297
 
        //
298
 
 
299
 
        for (int j = m; true; j = hlink[j] & INDEX)
 
382
        //
 
383
        // The entries in scode are linked into lists with the
 
384
        // entries in hlink serving as "next" pointers and with
 
385
        // the end of a list marked by hlink[j] == j.
 
386
        //
 
387
        // Traverse the lists that start at scode[m] and scode[mm].
 
388
        // For each element visited, increment the length of the
 
389
        // corresponding code by one bit. (If we visit scode[j]
 
390
        // during the traversal, then the code for symbol j becomes
 
391
        // one bit longer.)
 
392
        //
 
393
        // Merge the lists that start at scode[m] and scode[mm]
 
394
        // into a single list that starts at scode[m].
 
395
        //
 
396
        
 
397
        //
 
398
        // Add a bit to all codes in the first list.
 
399
        //
 
400
 
 
401
        for (int j = m; true; j = hlink[j])
300
402
        {
301
403
            scode[j]++;
302
404
 
303
405
            assert (scode[j] <= 58);
304
406
 
305
 
            if ((hlink[j] & INDEX) == j)
 
407
            if (hlink[j] == j)
306
408
            {
307
409
                //
308
 
                // Merge upper & lower lists
 
410
                // Merge the two lists.
309
411
                //
310
412
 
311
 
                hlink[j] &= ~INDEX;
312
 
                hlink[j] |= mm;
 
413
                hlink[j] = mm;
313
414
                break;
314
415
            }
315
416
        }
316
417
 
317
418
        //
318
 
        // Add 1 bit to all lower list codes
 
419
        // Add a bit to all codes in the second list
319
420
        //
320
421
 
321
 
        for (int j = mm; true; j = hlink[j] & INDEX)
 
422
        for (int j = mm; true; j = hlink[j])
322
423
        {
323
424
            scode[j]++;
324
425
 
325
426
            assert (scode[j] <= 58);
326
427
 
327
 
            if ((hlink[j] & INDEX) == j)
 
428
            if (hlink[j] == j)
328
429
                break;
329
430
        }
330
431
    }
331
432
 
 
433
    //
 
434
    // Build a canonical Huffman code table, replacing the code
 
435
    // lengths in scode with (code, code length) pairs.  Copy the
 
436
    // code table from scode into frq.
 
437
    //
 
438
 
332
439
    hufCanonicalCodeTable (scode);
333
 
 
334
 
    memcpy (frq, scode, sizeof (Int64) * HUF_ENCSIZE);  // overwrite frq
 
440
    memcpy (frq, scode, sizeof (Int64) * HUF_ENCSIZE);
335
441
}
336
442
 
337
443
 
415
521
void
416
522
hufUnpackEncTable
417
523
    (const char**       pcode,          // io: ptr to packed table (updated)
 
524
     int                ni,             // i : input size (in bytes)
418
525
     int                im,             // i : min hcode index
419
526
     int                iM,             // i : max hcode index
420
527
     Int64*             hcode)          //  o: encoding table [HUF_ENCSIZE]
427
534
 
428
535
    for (; im <= iM; im++)
429
536
    {
 
537
        if (p - *pcode > ni)
 
538
            unexpectedEndOfTable();
 
539
 
430
540
        Int64 l = hcode[im] = getBits (6, c, lc, p); // code length
431
541
 
432
542
        if (l == (Int64) LONG_ZEROCODE_RUN)
433
543
        {
 
544
            if (p - *pcode > ni)
 
545
                unexpectedEndOfTable();
 
546
 
434
547
            int zerun = getBits (8, c, lc, p) + SHORTEST_LONG_RUN;
435
548
 
 
549
            if (im + zerun > iM + 1)
 
550
                tableTooLong();
 
551
 
436
552
            while (zerun--)
437
553
                hcode[im++] = 0;
438
554
 
442
558
        {
443
559
            int zerun = l - SHORT_ZEROCODE_RUN + 2;
444
560
 
 
561
            if (im + zerun > iM + 1)
 
562
                tableTooLong();
 
563
 
445
564
            while (zerun--)
446
565
                hcode[im++] = 0;
447
566
 
486
605
        Int64 c = hufCode (hcode[im]);
487
606
        int l = hufLength (hcode[im]);
488
607
 
 
608
        if (c >> l)
 
609
        {
 
610
            //
 
611
            // Error: c is supposed to be an l-bit code,
 
612
            // but c contains a value that is greater
 
613
            // than the largest l-bit number.
 
614
            //
 
615
 
 
616
            invalidTableEntry();
 
617
        }
 
618
 
489
619
        if (l > HUF_DECBITS)
490
620
        {
491
621
            //
493
623
            //
494
624
 
495
625
            HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
 
626
 
 
627
            if (pl->len)
 
628
            {
 
629
                //
 
630
                // Error: a short code has already
 
631
                // been stored in table entry *pl.
 
632
                //
 
633
 
 
634
                invalidTableEntry();
 
635
            }
 
636
 
496
637
            pl->lit++;
497
638
 
498
639
            if (pl->p)
522
663
 
523
664
            for (Int64 i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++)
524
665
            {
 
666
                if (pl->len || pl->p)
 
667
                {
 
668
                    //
 
669
                    // Error: a short code or a long code has
 
670
                    // already been stored in table entry *pl.
 
671
                    //
 
672
 
 
673
                    invalidTableEntry();
 
674
                }
 
675
 
525
676
                pl->len = l;
526
677
                pl->lit = im;
527
678
            }
893
1044
    if (im < 0 || im >= HUF_ENCSIZE || iM < 0 || iM >= HUF_ENCSIZE)
894
1045
        invalidTableSize();
895
1046
 
896
 
    compressed += 20;
 
1047
    const char *ptr = compressed + 20;
897
1048
 
898
1049
    AutoArray <Int64, HUF_ENCSIZE> freq;
899
1050
    AutoArray <HufDec, HUF_DECSIZE> hdec;
900
1051
 
901
 
    hufUnpackEncTable (&compressed, im, iM, freq);
 
1052
    hufUnpackEncTable (&ptr, nCompressed - (ptr - compressed), im, iM, freq);
902
1053
 
903
1054
    try
904
1055
    {
 
1056
        if (nBits > 8 * (nCompressed - (ptr - compressed)))
 
1057
            invalidNBits();
 
1058
 
905
1059
        hufBuildDecTable (freq, im, iM, hdec);
906
 
        hufDecode (freq, hdec, compressed, nBits, iM, nRaw, raw);
 
1060
        hufDecode (freq, hdec, ptr, nBits, iM, nRaw, raw);
907
1061
    }
908
1062
    catch (...)
909
1063
    {