home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / SBHC.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  310 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* sbhc.c */
  20. /* Bounded Huffman code filters */
  21. #include "stdio_.h"
  22. #include "gdebug.h"
  23. #include "strimpl.h"
  24. #include "sbhc.h"
  25.  
  26. /*#define TEST*/
  27.  
  28. /* ------ Huffman coding support ------ */
  29.  
  30. /* Generate the encoding table from the definition. */
  31. /* The size of the encode array is def->num_values. */
  32. void
  33. bhc_make_encoding(hce_code *encode, const hc_definition *def)
  34. {    uint next = 0;
  35.     const ushort *pvalue = def->values;
  36.     uint i, k;
  37.     for ( i = 1; i <= def->num_counts; i++ )
  38.       {    for ( k = 0; k < def->counts[i]; k++, pvalue++, next++ )
  39.           {    hce_code *pce = encode + *pvalue;
  40.             pce->code = next;
  41.             pce->code_length = i;
  42.           }
  43.         next <<= 1;
  44.       }
  45. }
  46.  
  47. /* We decode in two steps, first indexing into a table with */
  48. /* a fixed number of bits from the source, and then indexing into */
  49. /* an auxiliary table if necessary.  (See shc.h for details.) */
  50.  
  51. /* Calculate the size of the decoding table. */
  52. uint
  53. bhc_sizeof_decoding(const hc_definition *def, int initial_bits)
  54. {    uint size = 1 << initial_bits;
  55.     uint carry = 0, mask = ~1;
  56.     uint i;
  57.     for ( i = initial_bits + 1; i <= def->num_counts;
  58.           i++, carry <<= 1, mask <<= 1
  59.         )
  60.       {    carry += def->counts[i];
  61.         size += carry & mask;
  62.         carry &= ~mask;
  63.       }
  64.     return size;
  65. }
  66.  
  67. /* Generate the decoding tables. */
  68. void
  69. bhc_make_decoding(hcd_code *decode, const hc_definition *def,
  70.   int initial_bits)
  71. {    /* Make entries for single-dispatch codes. */
  72.     {    hcd_code *pcd = decode;
  73.         const ushort *pvalue = def->values;
  74.         uint i, k, d;
  75.         for ( i = 0; i <= initial_bits; i++ )
  76.           {    for ( k = 0; k < def->counts[i]; k++, pvalue++ )
  77.               {    for ( d = 1 << (initial_bits - i); d > 0;
  78.                       d--, pcd++
  79.                     )
  80.                   pcd->value = *pvalue,
  81.                   pcd->code_length = i;
  82.               }
  83.           }
  84.     }
  85.     /* Make entries for two-dispatch codes. */
  86.     /* By working backward, we can do this more easily */
  87.     /* in a single pass. */
  88.     {    uint dsize = bhc_sizeof_decoding(def, initial_bits);
  89.         hcd_code *pcd = decode + (1 << initial_bits);
  90.         hcd_code *pcd2 = decode + dsize;
  91.         const ushort *pvalue = def->values + def->num_values;
  92.         uint entries_left = 0, slots_left = 0, mult_shift = 0;
  93.         uint i = def->num_counts + 1, j;
  94.         for ( ; ; )
  95.           {    if ( slots_left == 0 )
  96.               {    if ( entries_left != 0 )
  97.                   {    slots_left = 1 << (i - initial_bits);
  98.                     mult_shift = 0;
  99.                     continue;
  100.                   }
  101.                 if ( --i <= initial_bits )
  102.                     break;
  103.                 entries_left = def->counts[i];
  104.                 continue;
  105.               }
  106.             if ( entries_left == 0 )
  107.               {    entries_left = def->counts[--i];
  108.                 mult_shift++;
  109.                 continue;
  110.               }
  111.             --entries_left, --pvalue;
  112.             for ( j = 1 << mult_shift; j > 0; j-- )
  113.               {    --pcd2;
  114.                 pcd2->value = *pvalue;
  115.                 pcd2->code_length = i - initial_bits;
  116.               }
  117.             if ( (slots_left -= 1 << mult_shift) == 0 )
  118.               {    --pcd;
  119.                 pcd->value = pcd2 - decode;
  120.                 pcd->code_length = i + mult_shift;
  121.               }
  122.           }
  123.     }
  124. }
  125.  
  126. /* Test program */
  127. #ifdef TEST
  128. int
  129. main(int argc, const char *argv[])
  130. {    static uint counts[5] = { 0, 0, 1, 5, 2 };
  131.     static uint values[8] = { 0, 1, 4, 5, 6, 7, 2, 3 };
  132.     static const hc_definition def = {
  133.       counts, countof(counts) - 1,
  134.       values, countof(values)
  135.     };
  136.     hce_code encode[8];
  137.     hcd_code decode[99];
  138.     uint i;
  139.     uint dsize;
  140.     bhc_make_encoding(encode, &def);
  141.     for ( i = 0; i < countof(values); i++ )
  142.       printf("encode[%d] size=%d code=%d\n",
  143.          i, (int)encode[i].code_length, (int)encode[i].code);
  144.     dsize = bhc_sizeof_decoding(&def, 2);
  145.     printf("Decoding size = %d\n", dsize);
  146.     bhc_make_decoding(decode, &def, 2);
  147.     for ( i = 0; i < dsize; i++ )
  148.       printf("decode[%d] size=%d code=%d\n",
  149.          i, (int)decode[i].code_length, (int)decode[i].value);
  150.     return 0;
  151. }
  152. #endif
  153.  
  154. /* ------ BoundedHuffmanEncode ------ */
  155.  
  156. private_st_BHCE_state();
  157.  
  158. #define ss ((stream_BHCE_state *)st)
  159.  
  160. /* Initialize BoundedHuffmanEncode filter. */
  161. private int
  162. s_BHCE_init(register stream_state *st)
  163. {    uint count = ss->encode.count = ss->definition.num_values;
  164.     hce_code *encode = ss->encode.codes =
  165.       (hce_code *)gs_alloc_byte_array(st->memory, count,
  166.                       sizeof(hce_code), "BHCE encode");
  167.     if ( encode == 0 )
  168.       return ERRC;            /****** WRONG ******/
  169.     bhc_make_encoding(encode, &ss->definition);
  170.     s_hce_init_inline(ss);
  171.     return 0;
  172. }
  173.  
  174. /* Release the filter. */
  175. private void
  176. s_BHCE_release(stream_state *st)
  177. {    gs_free_object(st->memory, ss->encode.codes, "BHCE encode");
  178. }
  179.  
  180. /* Process a buffer. */
  181. private int
  182. s_BHCE_process(stream_state *st, stream_cursor_read *pr,
  183.   stream_cursor_write *pw, bool last)
  184. {    const byte *p = pr->ptr;
  185.     const byte *rlimit = pr->limit;
  186.     byte *q = pw->ptr;
  187.     byte *wlimit = pw->limit - (hc_bits_size >> 3);
  188.     const hce_code *encode = ss->encode.codes;
  189.     uint num_values = ss->definition.num_values;
  190.     int status = 0;
  191.     while ( p < rlimit && q < wlimit )
  192.       {    uint value = *++p;
  193.         const hce_code *cp = &encode[value];
  194.         if ( value >= num_values )
  195.           {    status = ERRC;
  196.             break;
  197.           }
  198.         hc_put_code((stream_hc_state *)ss, q, cp);
  199.       }
  200.     if ( last && status == 0 )
  201.       {    if ( q >= wlimit )
  202.           status = 1;
  203.         else
  204.           q = hc_put_last_bits((stream_hc_state *)ss, q);
  205.       }
  206.     pr->ptr = p;
  207.     pw->ptr = q;
  208.     return (p == rlimit ? 0 : 1);
  209. }
  210.  
  211. #undef ss
  212.  
  213. /* Stream template */
  214. const stream_template s_BHCE_template =
  215. {    &st_BHCE_state, s_BHCE_init, s_BHCE_process,
  216.     1, hc_bits_size >> 3, s_BHCE_release
  217. };
  218.  
  219. /* ------ BoundedHuffmanDecode ------ */
  220.  
  221. private_st_BHCD_state();
  222.  
  223. #define ss ((stream_BHCD_state *)st)
  224.  
  225. #define hcd_initial_bits 7        /* arbitrary, >= 1 and <= 8 */
  226.  
  227. /* Initialize BoundedHuffmanDecode filter. */
  228. private int
  229. s_BHCD_init(register stream_state *st)
  230. {    uint initial_bits = ss->decode.initial_bits =
  231.       min(hcd_initial_bits, ss->definition.num_counts);
  232.     uint dsize = bhc_sizeof_decoding(&ss->definition, initial_bits);
  233.     hcd_code *decode = ss->decode.codes =
  234.       (hcd_code *)gs_alloc_byte_array(st->memory, dsize,
  235.                       sizeof(hcd_code), "BHCD decode");
  236.     if ( decode == 0 )
  237.       return ERRC;            /****** WRONG ******/
  238.     bhc_make_decoding(decode, &ss->definition, initial_bits);
  239.     ss->decode.count = ss->definition.num_values;
  240.     s_hcd_init_inline(ss);
  241.     return 0;
  242. }
  243.  
  244. /* Release the filter. */
  245. private void
  246. s_BHCD_release(stream_state *st)
  247. {    gs_free_object(st->memory, ss->decode.codes, "BHCD decode");
  248. }
  249.  
  250. /* Process a buffer. */
  251. private int
  252. s_BHCD_process(stream_state *st, stream_cursor_read *pr,
  253.   stream_cursor_write *pw, bool last)
  254. {    hcd_declare_state;
  255.     byte *q = pw->ptr;
  256.     byte *wlimit = pw->limit;
  257.     const hcd_code *decode = ss->decode.codes;
  258.     uint initial_bits = ss->decode.initial_bits;
  259.     int status = 0;
  260.     hcd_load_state();
  261.     for ( ; ; )
  262.       {    const hcd_code *cp;
  263.         int clen;
  264.         hcd_ensure_bits(initial_bits, x1);
  265.         cp = &decode[hcd_peek_var_bits(initial_bits)];
  266. w1:        if ( q >= wlimit )
  267.           {    status = 1;
  268.             break;
  269.           }
  270.         if ( (clen = cp->code_length) > initial_bits )
  271.           {    if ( !hcd_bits_available(clen) )
  272.               {    /* We don't have enough bits for */
  273.                 /* all possible codes that begin this way, */
  274.                 /* but we might have enough for */
  275.                 /* the next code. */
  276.                 /****** NOT IMPLEMENTED YET ******/
  277.                 break;
  278.               }
  279.             clen -= initial_bits;
  280.             hcd_skip_bits(initial_bits);
  281.             hcd_ensure_bits(clen, out);    /* can't exit */
  282.             cp = &decode[cp->value + hcd_peek_var_bits(clen)];
  283.             hcd_skip_bits(cp->code_length);
  284.           }
  285.         else
  286.           {    hcd_skip_bits(clen);
  287.           }
  288.         *++q = cp->value;
  289.         continue;
  290.             /* We don't have enough bits for all possible */
  291.             /* codes, but we might have enough for */
  292.             /* the next code. */
  293. x1:        cp = &decode[(bits & ((1 << bits_left) - 1)) <<
  294.                  (initial_bits - bits_left)];
  295.         if ( (clen = cp->code_length) <= bits_left )
  296.           goto w1;
  297.         break;
  298.       }
  299. out:    hcd_store_state();
  300.     pw->ptr = q;
  301.     return status;
  302. }
  303.  
  304. #undef ss
  305.  
  306. /* Stream template */
  307. const stream_template s_BHCD_template =
  308. {    &st_BHCD_state, s_BHCD_init, s_BHCD_process, 1, 1, s_BHCD_release
  309. };
  310.