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

  1. /* Copyright (C) 1992, 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. /* scfe.c */
  20. /* CCITTFax encoding filter */
  21. #include "stdio_.h"    /* includes std.h */
  22. #include "memory_.h"
  23. #include "gdebug.h"
  24. #include "strimpl.h"
  25. #include "scf.h"
  26. #include "scfx.h"
  27.  
  28. /* ------ Macros and support routines ------ */
  29.  
  30. /* Put a run onto the output stream. */
  31. /* Free variables: q, wlimit, status. */
  32.  
  33. #define cf_ensure_put_runs(n, color, out)\
  34.     if ( wlimit - q < (n) * cfe_max_code_bytes )    /* worst case */\
  35.     {    ss->run_color = color;\
  36.         status = 1;\
  37.         goto out;\
  38.     }
  39. #define cf_put_run(ss, lenv, tt, mut)\
  40. {    const cfe_run *rp;\
  41.     if ( lenv >= 64 )\
  42.     {    while ( lenv >= 2560 + 64 )\
  43.           {    rp = &mut[40];\
  44.             hc_put_code(ss, q, rp);\
  45.             lenv -= 2560;\
  46.           }\
  47.         rp = &mut[lenv >> 6];\
  48.         hc_put_code(ss, q, rp);\
  49.         lenv &= 63;\
  50.     }\
  51.     rp = &tt[lenv];\
  52.     hc_put_code(ss, q, rp);\
  53. }
  54.  
  55. #define cf_put_white_run(ss, lenv)\
  56.   cf_put_run(ss, lenv, cf_white_termination, cf_white_make_up)
  57.  
  58. #define cf_put_black_run(ss, lenv)\
  59.   cf_put_run(ss, lenv, cf_black_termination, cf_black_make_up)
  60.  
  61. /* ------ Stream procedures ------ */
  62.  
  63. private_st_CFE_state();
  64.       
  65. #define ss ((stream_CFE_state *)st)
  66.  
  67. /*
  68.  * For the 2-D encoding modes, we leave the previous complete scan line
  69.  * at the beginning of the buffer, and start the new data after it.
  70.  */
  71.  
  72. /* Initialize CCITTFaxEncode filter */
  73. private int
  74. s_CFE_init(register stream_state *st)
  75. {    int columns = ss->Columns;
  76.     int raster = ss->raster = (columns + 7) >> 3;
  77.     s_hce_init_inline(ss);
  78.     ss->count = raster << 3;    /* starting a scan line */
  79.     ss->lbuf = ss->lprev = 0;
  80.     if ( columns > cfe_max_width )
  81.       return ERRC;            /****** WRONG ******/
  82.     if ( ss->K != 0 )
  83.     {    ss->lbuf = gs_alloc_bytes(st->memory, raster + 1, "CFE lbuf");
  84.         ss->lprev = gs_alloc_bytes(st->memory, raster + 1, "CFE lprev");
  85.         if ( ss->lbuf == 0 || ss->lprev == 0 )
  86.             return ERRC;        /****** WRONG ******/
  87.         /* Clear the initial reference line for 2-D encoding. */
  88.         /* Make sure it is terminated properly. */
  89.         memset(ss->lprev, (ss->BlackIs1 ? 0 : 0xff), raster);
  90.         if ( columns & 7 )
  91.           ss->lprev[raster - 1] ^= 0x80 >> (columns & 7);
  92.         else
  93.           ss->lprev[raster] = ~ss->lprev[0];
  94.         ss->copy_count = raster;
  95.     }
  96.     else
  97.         ss->copy_count = 0;
  98.     ss->k_left = (ss->K > 0 ? 1 : ss->K);
  99.     return 0;
  100. }
  101.  
  102. /* Release the filter. */
  103. private void
  104. s_CFE_release(stream_state *st)
  105. {    gs_free_object(st->memory, ss->lprev, "CFE lprev(close)");
  106.     gs_free_object(st->memory, ss->lbuf, "CFE lbuf(close)");
  107. }
  108.  
  109. /* Flush the buffer */
  110. private int cf_encode_1d(P4(stream_CFE_state *, stream_cursor_read *,
  111.   stream_cursor_write *, uint));
  112. private int cf_encode_2d(P5(stream_CFE_state *, const byte *,
  113.   stream_cursor_write *, uint, const byte *));
  114. private int
  115. s_CFE_process(stream_state *st, stream_cursor_read *pr,
  116.   stream_cursor_write *pw, bool last)
  117. {    const byte *rlimit = pr->limit;
  118.     byte *wlimit = pw->limit;
  119.     stream_cursor_read lr;
  120.     stream_cursor_read *plr = pr;
  121.     int raster = ss->raster;
  122.     int initial_count = raster << 3;
  123.     int last_count = -ss->Columns & 7;
  124.     byte last_mask = 1 << (-ss->Columns & 7);
  125.     int status = 0;
  126.     while ( pr->ptr < rlimit )
  127.     {    int count = ss->count;
  128.         int rcount = rlimit - pr->ptr;
  129.         int row_left = (count + 7) >> 3;
  130.         int end_count;
  131.         byte *end;
  132.         byte save_end, end_byte;
  133.         byte end_mask;
  134.         if_debug5('w', "[w]CFE: count = %d, pr = 0x%lx, 0x%lx, pw=0x%lx, 0x%lx\n",
  135.               count, (ulong)pr->ptr, (ulong)rlimit,
  136.               (ulong)pw->ptr, (ulong)wlimit);
  137.         /* Check whether we are still accumulating a scan line */
  138.         /* for 2-D encoding. */
  139.         if ( ss->copy_count != 0 )
  140.         {    int ccount = min(rcount, ss->copy_count);
  141.             memcpy(ss->lbuf + raster - ss->copy_count,
  142.                    pr->ptr + 1, ccount);
  143.             pr->ptr += ccount;
  144.             if ( (ss->copy_count -= ccount) != 0 )
  145.                 goto out;
  146.         }
  147.         if ( ss->K != 0 )
  148.         {    /* Use lbuf as the source of data. */
  149.             lr.limit = ss->lbuf + raster - 1;
  150.             lr.ptr = lr.limit - row_left;
  151.             plr = &lr;
  152.             rcount = row_left;
  153.         }
  154.         if ( count == initial_count )
  155.         {    /* Start a new scan line. */
  156.             byte *q = pw->ptr;
  157.             if ( wlimit - q < 15 )    /* byte align, eol, run_horizontal + 2 runs */
  158.             {    status = 1;
  159.                 break;
  160.             }
  161.             if ( ss->EncodedByteAlign )
  162.                 ss->bits_left &= ~7;
  163.             if ( ss->EndOfLine )
  164.             {    const cfe_run *rp =
  165.                     (ss->k_left <= 0 ? &cf_run_eol :
  166.                      ss->k_left > 1 ? &cf2_run_eol_2d :
  167.                      &cf2_run_eol_1d);
  168.                 hc_put_code(ss, q, rp);
  169.                 pw->ptr = q;
  170.             }
  171.             ss->run_count = initial_count;
  172.             ss->run_color = 0;
  173.         }
  174.         /* Ensure that the scan line ends with a polarity change. */
  175.         /* This may involve saving and restoring one byte beyond */
  176.         /* the scan line. */
  177.         if ( rcount < row_left )    /* partial scan line */
  178.             end = (byte *)plr->limit,
  179.             end_mask = 1,
  180.             end_count = (row_left - rcount) << 3;
  181.         else
  182.             end = (byte *)plr->ptr + row_left,
  183.             end_mask = last_mask,
  184.             end_count = last_count;
  185.         if ( end_mask == 1 )        /* set following byte */
  186.             save_end = *++end,
  187.             end_byte = *end = (end[-1] & 1) - 1;
  188.         else if ( (save_end = *end) & end_mask ) /* clear lower bits */
  189.             end_byte = *end &= -end_mask;
  190.         else                /* set lower bits */
  191.             end_byte = *end |= end_mask - 1;
  192.         if ( ss->K > 0 )
  193.         {    /* Group 3, mixed encoding */
  194.             if ( --(ss->k_left) )    /* Use 2-D encoding */
  195.             {    status = cf_encode_2d(ss, ss->lbuf, pw, end_count, ss->lprev);
  196.             }
  197.             else    /* Use 1-D encoding */
  198.             {    status = cf_encode_1d(ss, plr, pw, end_count);
  199.                 ss->k_left = ss->K;
  200.             }
  201.         }
  202.         else    /* Uniform encoding */
  203.         {    status = (ss->K == 0 ?
  204.                 cf_encode_1d(ss, pr, pw, end_count) :
  205.                 cf_encode_2d(ss, ss->lbuf, pw, end_count, ss->lprev));
  206.         }
  207.         *end = save_end;
  208.         if ( status )
  209.             break;
  210.         if ( ss->count == last_count )
  211.         {    /* Finished a scan line, start a new one. */
  212.             ss->count = initial_count;
  213.             if ( ss->K != 0 )
  214.             {    byte *temp = ss->lbuf;
  215.                 ss->copy_count = raster;
  216.                 ss->lbuf = ss->lprev;
  217.                 ss->lprev = temp;
  218.                 *end = end_byte;    /* re-terminate */
  219.             }
  220.         }
  221.     }
  222.     /* Check for end of data. */
  223.     if ( last && status == 0 )
  224.     {    const cfe_run *rp =
  225.             (ss->K > 0 ? &cf2_run_eol_1d : &cf_run_eol);
  226.         int i = (!ss->EndOfBlock ? 0 : ss->K < 0 ? 2 : 6);
  227.         uint bits_to_write = hc_bits_size - ss->bits_left +
  228.             i * rp->code_length;
  229.         byte *q = pw->ptr;
  230.         if ( wlimit - q < (bits_to_write + 7) >> 3 )
  231.         {    status = 1;
  232.             goto out;
  233.         }
  234.         if ( ss->EncodedByteAlign )
  235.             ss->bits_left &= ~7;
  236.         while ( --i >= 0 )
  237.             hc_put_code(ss, q, rp);
  238.         /* Force out the last byte or bytes. */
  239.         pw->ptr = q = hc_put_last_bits((stream_hc_state *)ss, q);
  240.     }
  241. out:    if_debug8('w', "[w]CFE exit %d: count = %d, run_count = %d, run_color = %d,\n     pr = 0x%lx, 0x%lx; pw = 0x%lx, 0x%lx\n",
  242.           status, ss->count, ss->run_count, ss->run_color,
  243.           (ulong)pr->ptr, (ulong)rlimit,
  244.           (ulong)pw->ptr, (ulong)wlimit);
  245. #ifdef DEBUG
  246.     if ( pr->ptr > rlimit || pw->ptr > wlimit )
  247.     {    lprintf("Pointer overrun!\n");
  248.         status = ERRC;
  249.     }
  250. #endif
  251.     return status;
  252. }
  253.  
  254. #undef ss
  255.  
  256. /* Encode a 1-D scan line. */
  257. private int
  258. cf_encode_1d(stream_CFE_state *ss, stream_cursor_read *pr,
  259.   stream_cursor_write *pw, uint end_count)
  260. {    register const byte *p = pr->ptr + 1;
  261.     byte *q = pw->ptr;
  262.     byte *wlimit = pw->limit;
  263.     byte invert = (ss->BlackIs1 ? 0 : 0xff);
  264.     register uint count = ss->count;
  265.     /* Invariant: data = p[-1] ^ invert. */
  266.     register uint data = *p++ ^ invert;
  267.     int run = ss->run_count;
  268.     int status = 0;
  269.     if ( ss->run_color )
  270.     {    cf_ensure_put_runs(1, 1, out);
  271.         goto sb;
  272.     }
  273. sw:    /* Parse a white run. */
  274.     cf_ensure_put_runs(2, 0, out);
  275.     skip_white_pixels(data, p, count, invert);
  276.     if ( count == end_count && count >= 8 )
  277.     {    /* We only have a partial scan line. */
  278.         ss->run_color = 0;
  279.         goto out;
  280.     }
  281.     run -= count;
  282.     cf_put_white_run(ss, run);
  283.     run = count;
  284.     if ( count == end_count )
  285.     {    ss->run_color = 1;
  286.         goto out;
  287.     }
  288. sb:    /* Parse a black run. */
  289.     skip_black_pixels(data, p, count, invert);
  290.     if ( count == end_count && count >= 8 )
  291.     {    /* We only have a partial scan line. */
  292.         ss->run_color = 1;
  293.         goto out;
  294.     }
  295.     run -= count;
  296.     cf_put_black_run(ss, run);
  297.     run = count;
  298.     if ( count > end_count )
  299.         goto sw;
  300.     ss->run_color = 0;
  301. out:    pr->ptr = p - 2;
  302.     pw->ptr = q;
  303.     ss->count = count;
  304.     ss->run_count = run;
  305.     return status;
  306. }
  307.  
  308. /* Encode a 2-D scan line. */
  309. /* We know we have a full scan line of input, */
  310. /* but we may run out of space to store the output. */
  311. private int
  312. cf_encode_2d(stream_CFE_state *ss, const byte *lbuf,
  313.   stream_cursor_write *pw, uint end_count, const byte *lprev)
  314. {    byte invert_white = (ss->BlackIs1 ? 0 : 0xff);
  315.     byte invert = (ss->run_color ? ~invert_white : invert_white);
  316.     register uint count = ss->count;
  317.     const byte *p = lbuf + ss->raster - ((count + 7) >> 3);
  318.     byte *q = pw->ptr;
  319.     byte *wlimit = pw->limit;
  320.     register uint data = *p++ ^ invert;
  321.     int status = 0;
  322.     while ( count != end_count )
  323.     {    /* If invert == invert_white, white and black have their */
  324.         /* correct meanings; if invert == ~invert_white, */
  325.         /* black and white are interchanged. */
  326.         uint a0 = count;
  327.         uint a1, b1;
  328.         int diff;
  329.         uint prev_count = count;
  330.         static const byte count_bit[8] =
  331.             { 0x80, 1, 2, 4, 8, 0x10, 0x20, 0x40 };
  332.         const byte *prev_p = p - lbuf + lprev;
  333.         byte prev_data = prev_p[-1] ^ invert;
  334.         /* Make sure we have room for a run_horizontal plus */
  335.         /* two data runs. */
  336.         cf_ensure_put_runs(3, invert != invert_white, out);
  337.         /* Find the a1 and b1 transitions. */
  338.         skip_white_pixels(data, p, count, invert);
  339.         a1 = count;
  340.         if ( (prev_data & count_bit[prev_count & 7]) )
  341.         {    /* Look for changing white first. */
  342.             skip_black_pixels(prev_data, prev_p, prev_count, invert);
  343.         }
  344.         if ( prev_count != end_count )
  345.         {    skip_white_pixels(prev_data, prev_p, prev_count, invert);
  346.         }
  347.         b1 = prev_count;
  348.         /* In all the comparisons below, remember that count */
  349.         /* runs downward, not upward, so the comparisons are */
  350.         /* reversed. */
  351.         if ( b1 >= a1 + 2 )
  352.         {    /* Could be a pass mode.  Find b2. */
  353.             if ( prev_count != end_count )
  354.             {    skip_black_pixels(prev_data, prev_p,
  355.                          prev_count, invert);
  356.             }
  357.             if ( prev_count > a1 )
  358.             {    /* Use pass mode. */
  359.                 hc_put_code(ss, q, &cf2_run_pass);
  360.                 count = prev_count;
  361.                 p = prev_p - lprev + lbuf;
  362.                 data = p[-1] ^ invert;
  363.                 continue;
  364.             }
  365.         }
  366.         /* Check for vertical coding. */
  367.         diff = a1 - b1;        /* i.e., logical b1 - a1 */
  368.         if ( diff <= 3 && diff >= -3 )
  369.         {    /* Use vertical coding. */
  370.             hc_put_code(ss, q, &cf2_run_vertical[diff + 3]);
  371.             invert = ~invert;    /* a1 polarity changes */
  372.             data ^= 0xff;
  373.             continue;
  374.         }
  375.         /* No luck, use horizontal coding. */
  376.         hc_put_code(ss, q, &cf2_run_horizontal);
  377.         if ( count != end_count )
  378.         {    skip_black_pixels(data, p, count, invert);    /* find a2 */
  379.         }
  380.         a0 -= a1;
  381.         a1 -= count;
  382.         if ( invert == invert_white )
  383.         {    cf_put_white_run(ss, a0);
  384.             cf_put_black_run(ss, a1);
  385.         }
  386.         else
  387.         {    cf_put_black_run(ss, a0);
  388.             cf_put_white_run(ss, a1);
  389.         }
  390.     }
  391. out:    pw->ptr = q;
  392.     ss->count = count;
  393.     return status;
  394. }
  395.  
  396. /* Stream template */
  397. const stream_template s_CFE_template =
  398. {    &st_CFE_state, s_CFE_init, s_CFE_process,
  399.     2, 15, /* 31 left-over bits + 7 bits of padding + 6 13-bit EOLs */
  400.     s_CFE_release
  401. };
  402.