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

  1. /* Copyright (C) 1992, 1993, 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. /* sdct.c */
  20. /* DCT encoding and decoding filter wrappers */
  21. #include "memory_.h"
  22. #include "stdio_.h"
  23. #include "jpeglib.h"
  24. #include "jerror.h"
  25. #include "gdebug.h"
  26. #include "strimpl.h"
  27. #include "sdct.h"
  28. #include "sjpeg.h"
  29.  
  30. private_st_DCT_state();
  31. /* GC procedures */
  32. private ENUM_PTRS_BEGIN(dct_enum_ptrs) return 0;
  33.     ENUM_CONST_STRING_PTR(0, stream_DCT_state, Markers);
  34. ENUM_PTRS_END
  35. private RELOC_PTRS_BEGIN(dct_reloc_ptrs) {
  36.     RELOC_CONST_STRING_PTR(stream_DCT_state, Markers);
  37. } RELOC_PTRS_END
  38.  
  39. #define ss ((stream_DCT_state *)st)
  40.  
  41. /* ------ DCTEncode ------ */
  42.  
  43. /* JPEG destination manager procedures */
  44. private void
  45. dcte_init_destination(j_compress_ptr cinfo)
  46. {
  47. }
  48. private boolean
  49. dcte_empty_output_buffer(j_compress_ptr cinfo)
  50. {    return FALSE;
  51. }
  52. private void
  53. dcte_term_destination(j_compress_ptr cinfo)
  54. {
  55. }
  56.  
  57. /* Initialize DCTEncode filter */
  58. private int
  59. s_DCTE_init(stream_state *st)
  60. {    struct jpeg_destination_mgr *dest = &ss->data.compress->destination;
  61.     dest->init_destination = dcte_init_destination;
  62.     dest->empty_output_buffer = dcte_empty_output_buffer;
  63.     dest->term_destination = dcte_term_destination;
  64.     ss->data.compress->cinfo.dest = dest;
  65.     ss->phase = 0;
  66.     return 0;
  67. }
  68.  
  69. /* Process a buffer */
  70. private int
  71. s_DCTE_process(stream_state *st, stream_cursor_read *pr,
  72.   stream_cursor_write *pw, bool last)
  73. {    jpeg_compress_data *jcdp = ss->data.compress;
  74.     struct jpeg_destination_mgr *dest = jcdp->cinfo.dest;
  75.     dest->next_output_byte = pw->ptr + 1;
  76.     dest->free_in_buffer = pw->limit - pw->ptr;
  77.     switch ( ss->phase )
  78.     {
  79.     case 0:                /* not initialized yet */
  80.         if ( gs_jpeg_start_compress(ss, TRUE) < 0 )
  81.             return ERRC;
  82.         pw->ptr = dest->next_output_byte - 1;
  83.         ss->phase = 1;
  84.         /* falls through */
  85.     case 1:                /* initialized, Markers not written */
  86.         if ( pw->limit - pw->ptr < ss->Markers.size )
  87.             return 1;
  88.         memcpy(pw->ptr + 1, ss->Markers.data, ss->Markers.size);
  89.         pw->ptr += ss->Markers.size;
  90.         ss->phase = 2;
  91.         /* falls through */
  92.     case 2:                /* still need to write Adobe marker */
  93.         if ( ! ss->NoMarker )
  94.         {    static const byte Adobe[] = {
  95.               0xFF, JPEG_APP0+14, 0, 14, /* parameter length */
  96.               'A', 'd', 'o', 'b', 'e',
  97.               0, 100, /* Version */
  98.               0, 0,    /* Flags0 */
  99.               0, 0,    /* Flags1 */
  100.               0    /* ColorTransform */
  101.             };
  102. #define ADOBE_MARKER_LEN sizeof(Adobe)
  103.             if ( pw->limit - pw->ptr < ADOBE_MARKER_LEN )
  104.                 return 1;
  105.             memcpy(pw->ptr + 1, Adobe, ADOBE_MARKER_LEN);
  106.             pw->ptr += ADOBE_MARKER_LEN;
  107.             *pw->ptr = ss->ColorTransform;
  108. #undef ADOBE_MARKER_LEN
  109.         }
  110.         dest->next_output_byte = pw->ptr + 1;
  111.         dest->free_in_buffer = pw->limit - pw->ptr;
  112.         ss->phase = 3;
  113.         /* falls through */
  114.     case 3:                /* markers written, processing data */
  115.         while ( jcdp->cinfo.image_height > jcdp->cinfo.next_scanline )
  116.         {    int written;
  117.             /*const*/ byte *samples = (byte *)(pr->ptr + 1);
  118.             if ( (uint)(pr->limit - pr->ptr) < ss->scan_line_size )
  119.                 return 0;        /* need more data */
  120.             written = gs_jpeg_write_scanlines(ss, &samples, 1);
  121.             if ( written < 0 )
  122.                 return ERRC;
  123.             pw->ptr = dest->next_output_byte - 1;
  124.             if ( !written )
  125.                 return 1;        /* output full */
  126.             pr->ptr += ss->scan_line_size;
  127.         }
  128.         ss->phase = 4;
  129.         /* falls through */
  130.     case 4:                /* all data processed, finishing */
  131.         /* jpeg_finish_compress can't suspend, so make sure
  132.          * it has plenty of room to write the last few bytes.
  133.          */
  134.         if ( pw->limit - pw->ptr < 100 )
  135.             return 1;
  136.         if ( gs_jpeg_finish_compress(ss) < 0 )
  137.             return ERRC;
  138.         pw->ptr = dest->next_output_byte - 1;
  139.         ss->phase = 5;
  140.         /* falls through */
  141.     case 5:                /* we are DONE */
  142.         return EOFC;
  143.     }
  144.     /* Default case can't happen.... */
  145.     return ERRC;
  146. }
  147.  
  148. /* Release the stream */
  149. private void
  150. s_DCTE_release(stream_state *st)
  151. {    gs_jpeg_destroy(ss);
  152.     gs_free(ss->data.compress, 1, sizeof(jpeg_compress_data),
  153.         "s_DCTE_release");
  154.     /* Switch the template pointer back in case we still need it. */
  155.     st->template = &s_DCTE_template;
  156. }
  157.  
  158. /* Stream template */
  159. const stream_template s_DCTE_template =
  160. {    &st_DCT_state, s_DCTE_init, s_DCTE_process, 1000, 4000, s_DCTE_release
  161. };
  162.  
  163. /* ------ DCTDecode ------ */
  164.  
  165. /* JPEG source manager procedures */
  166. private void
  167. dctd_init_source(j_decompress_ptr dinfo)
  168. {
  169. }
  170. static const JOCTET fake_eoi[2] = { 0xFF , JPEG_EOI };
  171. private boolean
  172. dctd_fill_input_buffer(j_decompress_ptr dinfo)
  173. {    jpeg_decompress_data *jddp =
  174.       (jpeg_decompress_data *)((char *)dinfo -
  175.                    offset_of(jpeg_decompress_data, dinfo));
  176.     if (! jddp->input_eod)
  177.         return FALSE;    /* normal case: suspend processing */
  178.     /* Reached end of source data without finding EOI */
  179.     WARNMS(dinfo, JWRN_JPEG_EOF);
  180.     /* Insert a fake EOI marker */
  181.     dinfo->src->next_input_byte = fake_eoi;
  182.     dinfo->src->bytes_in_buffer = 2;
  183.     jddp->faked_eoi = true;    /* so process routine doesn't use next_input_byte */
  184.     return TRUE;
  185. }
  186. private void
  187. dctd_skip_input_data(j_decompress_ptr dinfo, long num_bytes)
  188. {    struct jpeg_source_mgr *src = dinfo->src;
  189.     jpeg_decompress_data *jddp =
  190.       (jpeg_decompress_data *)((char *)dinfo -
  191.                    offset_of(jpeg_decompress_data, dinfo));
  192.     if ( num_bytes > 0 )
  193.     {    if ( num_bytes > src->bytes_in_buffer )
  194.         {    jddp->skip += num_bytes - src->bytes_in_buffer;
  195.             src->next_input_byte += src->bytes_in_buffer;
  196.             src->bytes_in_buffer = 0;
  197.             return;
  198.         }
  199.         src->next_input_byte += num_bytes;
  200.         src->bytes_in_buffer -= num_bytes;
  201.     }
  202. }
  203. private void
  204. dctd_term_source(j_decompress_ptr dinfo)
  205. {
  206. }
  207.  
  208. /* Initialize DCTDecode filter */
  209. private int
  210. s_DCTD_init(stream_state *st)
  211. {    struct jpeg_source_mgr *src = &ss->data.decompress->source;
  212.     src->init_source = dctd_init_source;
  213.     src->fill_input_buffer = dctd_fill_input_buffer;
  214.     src->skip_input_data = dctd_skip_input_data;
  215.     src->term_source = dctd_term_source;
  216.     src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
  217.     ss->data.decompress->dinfo.src = src;
  218.     ss->data.decompress->skip = 0;
  219.     ss->data.decompress->input_eod = false;
  220.     ss->data.decompress->faked_eoi = false;
  221.     ss->phase = 0;
  222.     return 0;
  223. }
  224.  
  225. /* Process a buffer */
  226. private int
  227. s_DCTD_process(stream_state *st, stream_cursor_read *pr,
  228.   stream_cursor_write *pw, bool last)
  229. {    jpeg_decompress_data *jddp = ss->data.decompress;
  230.     struct jpeg_source_mgr *src = jddp->dinfo.src;
  231.     int code;
  232.     if ( jddp->skip != 0 )
  233.     {    long avail = pr->limit - pr->ptr;
  234.         if ( avail < jddp->skip )
  235.         {    jddp->skip -= avail;
  236.             pr->ptr = pr->limit;
  237.             if (! last)
  238.                 return 0;    /* need more data */
  239.             jddp->skip = 0;        /* don't skip past input EOD */
  240.         }
  241.         pr->ptr += jddp->skip;
  242.         jddp->skip = 0;
  243.     }
  244.     src->next_input_byte = pr->ptr + 1;
  245.     src->bytes_in_buffer = pr->limit - pr->ptr;
  246.     jddp->input_eod = last;
  247.     switch ( ss->phase )
  248.     {
  249.     case 0:                /* not initialized yet */
  250.         if ( (code = gs_jpeg_read_header(ss, TRUE)) < 0 )
  251.             return ERRC;
  252.         pr->ptr = jddp->faked_eoi ? pr->limit : src->next_input_byte-1;
  253.         switch ( code )
  254.         {
  255.         case JPEG_SUSPENDED:
  256.             return 0;
  257.         /*case JPEG_HEADER_OK: */
  258.         }
  259.         /* If we have a ColorTransform parameter, and it's not
  260.          * overridden by an Adobe marker in the data, set colorspace.
  261.          */
  262.         if ( ss->ColorTransform >= 0 &&
  263.             ! jddp->dinfo.saw_Adobe_marker )
  264.         {    switch ( jddp->dinfo.num_components )
  265.             {
  266.             case 3:
  267.                 jddp->dinfo.jpeg_color_space =
  268.                   ss->ColorTransform ? JCS_YCbCr : JCS_RGB;
  269.                 /* out_color_space will default to JCS_RGB */
  270.                 break;
  271.             case 4:
  272.                 jddp->dinfo.jpeg_color_space =
  273.                   ss->ColorTransform ? JCS_YCCK : JCS_CMYK;
  274.                 /* out_color_space will default to JCS_CMYK */
  275.                 break;
  276.             }
  277.         }
  278.         if ( gs_jpeg_start_decompress(ss) < 0 )
  279.             return ERRC;
  280.         ss->scan_line_size = 
  281.           jddp->dinfo.output_width * jddp->dinfo.output_components;
  282.         if ( ss->scan_line_size > (uint) jddp->template.min_out_size )
  283.         {
  284.             /* Create a spare buffer for oversize scanline */
  285.             jddp->scanline_buffer = (byte *)
  286.               gs_malloc(ss->scan_line_size, sizeof(byte),
  287.                     "s_DCTD_process(scanline_buffer)");
  288.             if ( jddp->scanline_buffer == NULL )
  289.                 return ERRC;
  290.         }
  291.         jddp->bytes_in_scanline = 0;
  292.         ss->phase = 1;
  293.         /* falls through */
  294.     case 1:                /* reading data */
  295.     dumpbuffer:
  296.         if ( jddp->bytes_in_scanline != 0 )
  297.         {    uint avail = pw->limit - pw->ptr;
  298.             uint tomove = min(jddp->bytes_in_scanline,
  299.                       avail);
  300.             memcpy(pw->ptr + 1, jddp->scanline_buffer +
  301.                    (ss->scan_line_size - jddp->bytes_in_scanline),
  302.                    tomove);
  303.             pw->ptr += tomove;
  304.             jddp->bytes_in_scanline -= tomove;
  305.             if ( jddp->bytes_in_scanline != 0 )
  306.                 return 1;        /* need more room */
  307.         }
  308.         while ( jddp->dinfo.output_height > jddp->dinfo.output_scanline )
  309.         {    int read;
  310.             byte *samples;
  311.             if ( jddp->scanline_buffer != NULL )
  312.                 samples = jddp->scanline_buffer;
  313.             else
  314.             {    if ( (uint)(pw->limit - pw->ptr) < ss->scan_line_size )
  315.                     return 1;    /* need more room */
  316.                 samples = (byte *)(pw->ptr + 1);
  317.             }
  318.             read = gs_jpeg_read_scanlines(ss, &samples, 1);
  319.             if ( read < 0 )
  320.                 return ERRC;
  321.             pr->ptr = jddp->faked_eoi ? pr->limit : src->next_input_byte-1;
  322.             if ( !read )
  323.                 return 0;        /* need more data */
  324.             if ( jddp->scanline_buffer != NULL )
  325.             {    jddp->bytes_in_scanline = ss->scan_line_size;
  326.                 goto dumpbuffer;
  327.             }
  328.             pw->ptr += ss->scan_line_size;
  329.         }
  330.         ss->phase = 2;
  331.         /* falls through */
  332.     case 2:                /* end of image; scan for EOI */
  333.         if ( (code = gs_jpeg_finish_decompress(ss)) < 0 )
  334.             return ERRC;
  335.         pr->ptr = jddp->faked_eoi ? pr->limit : src->next_input_byte-1;
  336.         if ( code == 0 )
  337.             return 0;
  338.         ss->phase = 3;
  339.         /* falls through */
  340.     case 3:                /* we are DONE */
  341.         return EOFC;
  342.     }
  343.     /* Default case can't happen.... */
  344.     return ERRC;
  345. }
  346.  
  347. /* Release the stream */
  348. private void
  349. s_DCTD_release(stream_state *st)
  350. {    gs_jpeg_destroy(ss);
  351.     if ( ss->data.decompress->scanline_buffer != NULL )
  352.     {
  353.         gs_free(ss->data.decompress->scanline_buffer,
  354.             ss->scan_line_size, sizeof(byte),
  355.             "s_DCTD_release(scanline_buffer)");
  356.     }
  357.     gs_free(ss->data.decompress, 1, sizeof(jpeg_decompress_data),
  358.         "s_DCTD_release");
  359.     /* Switch the template pointer back in case we still need it. */
  360.     st->template = &s_DCTD_template;
  361. }
  362.  
  363. /* Stream template */
  364. const stream_template s_DCTD_template =
  365. {    &st_DCT_state, s_DCTD_init, s_DCTD_process, 2000, 4000, s_DCTD_release
  366. };
  367.  
  368. #undef ss
  369.