home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / SJPEG.C < prev    next >
C/C++ Source or Header  |  1994-07-28  |  8KB  |  279 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. /* sjpeg.c */
  20. /* Ghostscript interface routines for IJG code. */
  21. #include "stdio_.h"
  22. #include "string_.h"
  23. #include "jpeglib.h"
  24. #include "jerror.h"
  25. #include "gx.h"
  26. #include "gserrors.h"
  27. #include "strimpl.h"
  28. #include "sdct.h"
  29. #include "sjpeg.h"
  30.  
  31. /*
  32.  * Error handling routines (these replace corresponding IJG routines from
  33.  * jpeg/jerror.c).  These are used for both compression and decompression.
  34.  * We assume
  35.  * offset_of(jpeg_compress_data, cinfo)==offset_of(jpeg_decompress_data, dinfo)
  36.  */
  37.  
  38. private void
  39. gs_jpeg_error_exit (j_common_ptr cinfo)
  40. {    jpeg_stream_data *jcomdp =
  41.       (jpeg_stream_data *)((char *)cinfo -
  42.                    offset_of(jpeg_compress_data, cinfo));
  43.     longjmp(jcomdp->exit_jmpbuf, 1);
  44. }
  45.  
  46. private void
  47. gs_jpeg_emit_message (j_common_ptr cinfo, int msg_level)
  48. {    if ( msg_level < 0 )
  49.     {    /* GS policy is to ignore IJG warnings when Picky=0,
  50.          * treat them as errors when Picky=1.
  51.          */
  52.         jpeg_stream_data *jcomdp =
  53.           (jpeg_stream_data *)((char *)cinfo -
  54.                        offset_of(jpeg_compress_data, cinfo));
  55.         if ( jcomdp->Picky )
  56.             gs_jpeg_error_exit(cinfo);
  57.     }
  58.     /* Trace messages are always ignored. */
  59. }
  60.  
  61.  
  62. /* Stuff the IJG error message into errorinfo after an error exit. */
  63.  
  64. private int
  65. gs_jpeg_log_error (stream_DCT_state *st)
  66. {    j_common_ptr cinfo = (j_common_ptr) &st->data.compress->cinfo;
  67.     char buffer[JMSG_LENGTH_MAX];
  68.     /* Format the error message */
  69.     (*cinfo->err->format_message)(cinfo, buffer);
  70.     (*st->report_error)((stream_state *)st, buffer);
  71.     return gs_error_ioerror;    /* caller will do return_error() */
  72. }
  73.  
  74.  
  75. /*
  76.  * Interface routines.  This layer of routines exists solely to limit
  77.  * side-effects from using setjmp.
  78.  */
  79.  
  80. int
  81. gs_jpeg_create_compress (stream_DCT_state *st)
  82. {    /* Initialize error handling */
  83.     st->data.compress->cinfo.err = jpeg_std_error(&st->data.common->err);
  84.     st->data.common->err.error_exit = gs_jpeg_error_exit;
  85.     st->data.common->err.emit_message = gs_jpeg_emit_message;
  86.     /* Establish the setjmp return context for gs_jpeg_error_exit to use. */
  87.     if (setjmp(st->data.common->exit_jmpbuf))
  88.         return_error(gs_jpeg_log_error(st));
  89.  
  90.     jpeg_create_compress(&st->data.compress->cinfo);
  91.     return 0;
  92. }
  93.  
  94. int
  95. gs_jpeg_create_decompress (stream_DCT_state *st)
  96. {    /* Initialize error handling */
  97.     st->data.decompress->dinfo.err = jpeg_std_error(&st->data.common->err);
  98.     st->data.common->err.error_exit = gs_jpeg_error_exit;
  99.     st->data.common->err.emit_message = gs_jpeg_emit_message;
  100.     /* Establish the setjmp return context for gs_jpeg_error_exit to use. */
  101.     if (setjmp(st->data.common->exit_jmpbuf))
  102.         return_error(gs_jpeg_log_error(st));
  103.  
  104.     jpeg_create_decompress(&st->data.decompress->dinfo);
  105.     return 0;
  106. }
  107.  
  108. int
  109. gs_jpeg_set_defaults (stream_DCT_state *st)
  110. {    if (setjmp(st->data.common->exit_jmpbuf))
  111.         return_error(gs_jpeg_log_error(st));
  112.     jpeg_set_defaults(&st->data.compress->cinfo);
  113.     return 0;
  114. }
  115.  
  116. int
  117. gs_jpeg_set_colorspace (stream_DCT_state *st,
  118.             J_COLOR_SPACE colorspace)
  119. {    if (setjmp(st->data.common->exit_jmpbuf))
  120.         return_error(gs_jpeg_log_error(st));
  121.     jpeg_set_colorspace(&st->data.compress->cinfo, colorspace);
  122.     return 0;
  123. }
  124.  
  125. int
  126. gs_jpeg_set_linear_quality (stream_DCT_state *st,
  127.                 int scale_factor, boolean force_baseline)
  128. {    if (setjmp(st->data.common->exit_jmpbuf))
  129.         return_error(gs_jpeg_log_error(st));
  130.     jpeg_set_linear_quality(&st->data.compress->cinfo,
  131.                 scale_factor, force_baseline);
  132.     return 0;
  133. }
  134.  
  135. JQUANT_TBL *
  136. gs_jpeg_alloc_quant_table (stream_DCT_state *st)
  137. {    if (setjmp(st->data.common->exit_jmpbuf))
  138.     {    gs_jpeg_log_error(st);
  139.         return NULL;
  140.     }
  141.     return jpeg_alloc_quant_table((j_common_ptr)
  142.                       &st->data.compress->cinfo);
  143. }
  144.  
  145. JHUFF_TBL *
  146. gs_jpeg_alloc_huff_table (stream_DCT_state *st)
  147. {    if (setjmp(st->data.common->exit_jmpbuf))
  148.     {    gs_jpeg_log_error(st);
  149.         return NULL;
  150.     }
  151.     return jpeg_alloc_huff_table((j_common_ptr)
  152.                      &st->data.compress->cinfo);
  153. }
  154.  
  155. int
  156. gs_jpeg_start_compress (stream_DCT_state *st,
  157.             boolean write_all_tables)
  158. {    if (setjmp(st->data.common->exit_jmpbuf))
  159.         return_error(gs_jpeg_log_error(st));
  160.     jpeg_start_compress(&st->data.compress->cinfo, write_all_tables);
  161.     return 0;
  162. }
  163.  
  164. int
  165. gs_jpeg_write_scanlines (stream_DCT_state *st,
  166.              JSAMPARRAY scanlines,
  167.              int num_lines)
  168. {    if (setjmp(st->data.common->exit_jmpbuf))
  169.         return_error(gs_jpeg_log_error(st));
  170.     return (int) jpeg_write_scanlines(&st->data.compress->cinfo,
  171.                       scanlines, (JDIMENSION) num_lines);
  172. }
  173.  
  174. int
  175. gs_jpeg_finish_compress (stream_DCT_state *st)
  176. {    if (setjmp(st->data.common->exit_jmpbuf))
  177.         return_error(gs_jpeg_log_error(st));
  178.     jpeg_finish_compress(&st->data.compress->cinfo);
  179.     return 0;
  180. }
  181.  
  182. int
  183. gs_jpeg_read_header (stream_DCT_state *st,
  184.              boolean require_image)
  185. {    if (setjmp(st->data.common->exit_jmpbuf))
  186.         return_error(gs_jpeg_log_error(st));
  187.     return jpeg_read_header(&st->data.decompress->dinfo, require_image);
  188. }
  189.  
  190. int
  191. gs_jpeg_start_decompress (stream_DCT_state *st)
  192. {    if (setjmp(st->data.common->exit_jmpbuf))
  193.         return_error(gs_jpeg_log_error(st));
  194.     jpeg_start_decompress(&st->data.decompress->dinfo);
  195.     return 0;
  196. }
  197.  
  198. int
  199. gs_jpeg_read_scanlines (stream_DCT_state *st,
  200.             JSAMPARRAY scanlines,
  201.             int max_lines)
  202. {    if (setjmp(st->data.common->exit_jmpbuf))
  203.         return_error(gs_jpeg_log_error(st));
  204.     return (int) jpeg_read_scanlines(&st->data.decompress->dinfo,
  205.                      scanlines, (JDIMENSION) max_lines);
  206. }
  207.  
  208. int
  209. gs_jpeg_finish_decompress (stream_DCT_state *st)
  210. {    if (setjmp(st->data.common->exit_jmpbuf))
  211.         return_error(gs_jpeg_log_error(st));
  212.     return (int) jpeg_finish_decompress(&st->data.decompress->dinfo);
  213. }
  214.  
  215. int
  216. gs_jpeg_destroy (stream_DCT_state *st)
  217. {    if (setjmp(st->data.common->exit_jmpbuf))
  218.         return_error(gs_jpeg_log_error(st));
  219.     jpeg_destroy((j_common_ptr) &st->data.compress->cinfo);
  220.     return 0;
  221. }
  222.  
  223.  
  224. /*
  225.  * These routines replace the low-level memory manager of the IJG library.
  226.  * They pass malloc/free calls to the Ghostscript memory manager.
  227.  * Note we do not need these to be declared in any GS header file.
  228.  */
  229.  
  230. void *
  231. jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
  232. {
  233.   return gs_malloc(1, sizeofobject, "JPEG small internal data allocation");
  234. }
  235.  
  236. void
  237. jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
  238. {
  239.   gs_free(object, 1, sizeofobject, "Freeing JPEG small internal data");
  240. }
  241.  
  242. void FAR *
  243. jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
  244. {
  245.   return gs_malloc(1, sizeofobject, "JPEG large internal data allocation");
  246. }
  247.  
  248. void
  249. jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  250. {
  251.   gs_free(object, 1, sizeofobject, "Freeing JPEG large internal data");
  252. }
  253.  
  254. long
  255. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  256.             long max_bytes_needed, long already_allocated)
  257. {
  258.   return max_bytes_needed;
  259. }
  260.  
  261. void
  262. jpeg_open_backing_store (j_common_ptr cinfo, void * info,
  263.              long total_bytes_needed)
  264. {
  265.   ERREXIT(cinfo, JERR_NO_BACKING_STORE);
  266. }
  267.  
  268. long
  269. jpeg_mem_init (j_common_ptr cinfo)
  270. {
  271.   return 0;            /* just set max_memory_to_use to 0 */
  272. }
  273.  
  274. void
  275. jpeg_mem_term (j_common_ptr cinfo)
  276. {
  277.   /* no work */
  278. }
  279.