sjpegc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (C) 1994, 1997, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: sjpegc.c,v 1.7 2002/03/30 23:55:15 giles Exp $ */
  14. /* Interface routines for IJG code, common to encode/decode. */
  15. #include "stdio_.h"
  16. #include "string_.h"
  17. #include "jpeglib_.h"
  18. #include "jerror_.h"
  19. #include "gx.h"
  20. #include "gserrors.h"
  21. #include "strimpl.h"
  22. #include "sdct.h"
  23. #include "sjpeg.h"
  24. /*
  25. Ghostscript uses a non-public interface to libjpeg in order to
  26. override the library's default memory manager implementation.
  27. Since many users will want to compile Ghostscript using the
  28. shared jpeg library, we provide these prototypes so that a copy
  29. of the libjpeg source distribution is not needed.
  30. The presence of the jmemsys.h header file is detected in
  31. unix-aux.mak, and written to gconfig_.h
  32. */
  33. #include "gconfig_.h"
  34. #ifdef DONT_HAVE_JMEMSYS_H
  35. void *
  36. jpeg_get_small(j_common_ptr cinfo, size_t size);
  37. void
  38. jpeg_free_small(j_common_ptr cinfo, void *object, size_t size);
  39. void FAR *
  40. jpeg_get_large(j_common_ptr cinfo, size_t size);
  41. void
  42. jpeg_free_large(j_common_ptr cinfo, void FAR * object, size_t size);
  43. typedef void *backing_store_ptr;
  44. long
  45. jpeg_mem_available(j_common_ptr cinfo, long min_bytes_needed,
  46. long max_bytes_needed, long already_allocated);
  47. void
  48. jpeg_open_backing_store(j_common_ptr cinfo, backing_store_ptr info,
  49. long total_bytes_needed);
  50. long
  51. jpeg_mem_init(j_common_ptr cinfo);
  52. void
  53. jpeg_mem_term(j_common_ptr cinfo);
  54. #else
  55. #include "jmemsys.h" /* for prototypes */
  56. #endif
  57. private_st_jpeg_block();
  58. /*
  59. * Error handling routines (these replace corresponding IJG routines from
  60. * jpeg/jerror.c). These are used for both compression and decompression.
  61. * We assume
  62. * offset_of(jpeg_compress_data, cinfo)==offset_of(jpeg_decompress_data, dinfo)
  63. */
  64. private void
  65. gs_jpeg_error_exit(j_common_ptr cinfo)
  66. {
  67. jpeg_stream_data *jcomdp =
  68. (jpeg_stream_data *) ((char *)cinfo -
  69. offset_of(jpeg_compress_data, cinfo));
  70. longjmp(jcomdp->exit_jmpbuf, 1);
  71. }
  72. private void
  73. gs_jpeg_emit_message(j_common_ptr cinfo, int msg_level)
  74. {
  75. if (msg_level < 0) { /* GS policy is to ignore IJG warnings when Picky=0,
  76. * treat them as errors when Picky=1.
  77. */
  78. jpeg_stream_data *jcomdp =
  79. (jpeg_stream_data *) ((char *)cinfo -
  80. offset_of(jpeg_compress_data, cinfo));
  81. if (jcomdp->Picky)
  82. gs_jpeg_error_exit(cinfo);
  83. }
  84. /* Trace messages are always ignored. */
  85. }
  86. /*
  87. * This routine initializes the error manager fields in the JPEG object.
  88. * It is based on jpeg_std_error from jpeg/jerror.c.
  89. */
  90. void
  91. gs_jpeg_error_setup(stream_DCT_state * st)
  92. {
  93. struct jpeg_error_mgr *err = &st->data.common->err;
  94. /* Initialize the JPEG compression object with default error handling */
  95. jpeg_std_error(err);
  96. /* Replace some methods with our own versions */
  97. err->error_exit = gs_jpeg_error_exit;
  98. err->emit_message = gs_jpeg_emit_message;
  99. st->data.compress->cinfo.err = err; /* works for decompress case too */
  100. }
  101. /* Stuff the IJG error message into errorinfo after an error exit. */
  102. int
  103. gs_jpeg_log_error(stream_DCT_state * st)
  104. {
  105. j_common_ptr cinfo = (j_common_ptr) & st->data.compress->cinfo;
  106. char buffer[JMSG_LENGTH_MAX];
  107. /* Format the error message */
  108. (*cinfo->err->format_message) (cinfo, buffer);
  109. (*st->report_error) ((stream_state *) st, buffer);
  110. return gs_error_ioerror; /* caller will do return_error() */
  111. }
  112. /*
  113. * Interface routines. This layer of routines exists solely to limit
  114. * side-effects from using setjmp.
  115. */
  116. JQUANT_TBL *
  117. gs_jpeg_alloc_quant_table(stream_DCT_state * st)
  118. {
  119. if (setjmp(st->data.common->exit_jmpbuf)) {
  120. gs_jpeg_log_error(st);
  121. return NULL;
  122. }
  123. return jpeg_alloc_quant_table((j_common_ptr)
  124. & st->data.compress->cinfo);
  125. }
  126. JHUFF_TBL *
  127. gs_jpeg_alloc_huff_table(stream_DCT_state * st)
  128. {
  129. if (setjmp(st->data.common->exit_jmpbuf)) {
  130. gs_jpeg_log_error(st);
  131. return NULL;
  132. }
  133. return jpeg_alloc_huff_table((j_common_ptr)
  134. & st->data.compress->cinfo);
  135. }
  136. int
  137. gs_jpeg_destroy(stream_DCT_state * st)
  138. {
  139. if (setjmp(st->data.common->exit_jmpbuf))
  140. return_error(gs_jpeg_log_error(st));
  141. jpeg_destroy((j_common_ptr) & st->data.compress->cinfo);
  142. return 0;
  143. }
  144. /*
  145. * These routines replace the low-level memory manager of the IJG library.
  146. * They pass malloc/free calls to the Ghostscript memory manager.
  147. * Note we do not need these to be declared in any GS header file.
  148. */
  149. private inline jpeg_compress_data *
  150. cinfo2jcd(j_common_ptr cinfo)
  151. { /* We use the offset of cinfo in jpeg_compress data here, but we */
  152. /* could equally well have used jpeg_decompress_data. */
  153. return (jpeg_compress_data *)
  154. ((byte *)cinfo - offset_of(jpeg_compress_data, cinfo));
  155. }
  156. private void *
  157. jpeg_alloc(j_common_ptr cinfo, size_t size, const char *info)
  158. {
  159. jpeg_compress_data *jcd = cinfo2jcd(cinfo);
  160. gs_memory_t *mem = jcd->memory;
  161. jpeg_block_t *p = gs_alloc_struct_immovable(mem, jpeg_block_t,
  162. &st_jpeg_block, "jpeg_alloc(block)");
  163. void *data = gs_alloc_bytes_immovable(mem, size, info);
  164. if (p == 0 || data == 0) {
  165. gs_free_object(mem, data, info);
  166. gs_free_object(mem, p, "jpeg_alloc(block)");
  167. return 0;
  168. }
  169. p->data = data;
  170. p->next = jcd->blocks;
  171. jcd->blocks = p;
  172. return data;
  173. }
  174. private void
  175. jpeg_free(j_common_ptr cinfo, void *data, const char *info)
  176. {
  177. jpeg_compress_data *jcd = cinfo2jcd(cinfo);
  178. gs_memory_t *mem = jcd->memory;
  179. jpeg_block_t *p = jcd->blocks;
  180. jpeg_block_t **pp = &jcd->blocks;
  181. gs_free_object(mem, data, info);
  182. while(p && p->data != data)
  183. { pp = &p->next;
  184. p = p->next;
  185. }
  186. if(p == 0)
  187. lprintf1("Freeing unrecorded JPEG data 0x%lx!\n", (ulong)data);
  188. else
  189. *pp = p->next;
  190. gs_free_object(mem, p, "jpeg_free(block)");
  191. }
  192. void *
  193. jpeg_get_small(j_common_ptr cinfo, size_t size)
  194. {
  195. return jpeg_alloc(cinfo, size, "JPEG small internal data allocation");
  196. }
  197. void
  198. jpeg_free_small(j_common_ptr cinfo, void *object, size_t size)
  199. {
  200. jpeg_free(cinfo, object, "Freeing JPEG small internal data");
  201. }
  202. void FAR *
  203. jpeg_get_large(j_common_ptr cinfo, size_t size)
  204. {
  205. return jpeg_alloc(cinfo, size, "JPEG large internal data allocation");
  206. }
  207. void
  208. jpeg_free_large(j_common_ptr cinfo, void FAR * object, size_t size)
  209. {
  210. jpeg_free(cinfo, object, "Freeing JPEG large internal data");
  211. }
  212. long
  213. jpeg_mem_available(j_common_ptr cinfo, long min_bytes_needed,
  214. long max_bytes_needed, long already_allocated)
  215. {
  216. return max_bytes_needed;
  217. }
  218. void
  219. jpeg_open_backing_store(j_common_ptr cinfo, backing_store_ptr info,
  220. long total_bytes_needed)
  221. {
  222. ERREXIT(cinfo, JERR_NO_BACKING_STORE);
  223. }
  224. long
  225. jpeg_mem_init(j_common_ptr cinfo)
  226. {
  227. return 0; /* just set max_memory_to_use to 0 */
  228. }
  229. void
  230. jpeg_mem_term(j_common_ptr cinfo)
  231. {
  232. /* no work */
  233. }