summaryrefslogtreecommitdiff
path: root/gs/src/sjpegc.c
blob: f4661345552771d103dbb70ca341167a00546039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/* sjpegc.c */
/* Interface routines for IJG code, common to encode/decode. */
#include "stdio_.h"
#include "string_.h"
#include "jpeglib.h"
#include "jerror.h"
#include "gx.h"
#include "gserrors.h"
#include "strimpl.h"
#include "sdct.h"
#include "sjpeg.h"

/* gs_jpeg_message_table() is kept in a separate file for arcane reasons.
 * See sjpegerr.c.
 */
const char * const * gs_jpeg_message_table (P1(void));

/*
 * Error handling routines (these replace corresponding IJG routines from
 * jpeg/jerror.c).  These are used for both compression and decompression.
 * We assume
 * offset_of(jpeg_compress_data, cinfo)==offset_of(jpeg_decompress_data, dinfo)
 */

private void
gs_jpeg_error_exit (j_common_ptr cinfo)
{	jpeg_stream_data *jcomdp =
	  (jpeg_stream_data *)((char *)cinfo -
			       offset_of(jpeg_compress_data, cinfo));
	longjmp(jcomdp->exit_jmpbuf, 1);
}

private void
gs_jpeg_emit_message (j_common_ptr cinfo, int msg_level)
{	if ( msg_level < 0 )
	{	/* GS policy is to ignore IJG warnings when Picky=0,
		 * treat them as errors when Picky=1.
		 */
		jpeg_stream_data *jcomdp =
		  (jpeg_stream_data *)((char *)cinfo -
				       offset_of(jpeg_compress_data, cinfo));
		if ( jcomdp->Picky )
			gs_jpeg_error_exit(cinfo);
	}
	/* Trace messages are always ignored. */
}

/*
 * This is an exact copy of format_message from jpeg/jerror.c.
 * We do not use jerror.c in Ghostscript, so we have to duplicate this routine.
 */

private void
gs_jpeg_format_message (j_common_ptr cinfo, char * buffer)
{
  struct jpeg_error_mgr * err = cinfo->err;
  int msg_code = err->msg_code;
  const char * msgtext = NULL;
  const char * msgptr;
  char ch;
  boolean isstring;

  /* Look up message string in proper table */
  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
    msgtext = err->jpeg_message_table[msg_code];
  } else if (err->addon_message_table != NULL &&
	     msg_code >= err->first_addon_message &&
	     msg_code <= err->last_addon_message) {
    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  }

  /* Defend against bogus message number */
  if (msgtext == NULL) {
    err->msg_parm.i[0] = msg_code;
    msgtext = err->jpeg_message_table[0];
  }

  /* Check for string parameter, as indicated by %s in the message text */
  isstring = FALSE;
  msgptr = msgtext;
  while ((ch = *msgptr++) != '\0') {
    if (ch == '%') {
      if (*msgptr == 's') isstring = TRUE;
      break;
    }
  }

  /* Format the message into the passed buffer */
  if (isstring)
    sprintf(buffer, msgtext, err->msg_parm.s);
  else
    sprintf(buffer, msgtext,
	    err->msg_parm.i[0], err->msg_parm.i[1],
	    err->msg_parm.i[2], err->msg_parm.i[3],
	    err->msg_parm.i[4], err->msg_parm.i[5],
	    err->msg_parm.i[6], err->msg_parm.i[7]);
}

/* And this is an exact copy of another routine from jpeg/jerror.c. */

private void
gs_jpeg_reset_error_mgr (j_common_ptr cinfo)
{
  cinfo->err->num_warnings = 0;
  /* trace_level is not reset since it is an application-supplied parameter */
  cinfo->err->msg_code = 0;	/* may be useful as a flag for "no error" */
}

/*
 * This routine initializes the error manager fields in the JPEG object.
 * It is based on jpeg_std_error from jpeg/jerror.c.
 */

void
gs_jpeg_error_setup (stream_DCT_state *st)
{
  struct jpeg_error_mgr * err = &st->data.common->err;

  err->error_exit = gs_jpeg_error_exit;
  err->emit_message = gs_jpeg_emit_message;
  /* We need not set the output_message field since gs_jpeg_emit_message
   * doesn't call it, and the IJG library never calls output_message directly.
   * Setting the format_message field isn't strictly necessary either,
   * since gs_jpeg_log_error calls gs_jpeg_format_message directly.
   */
  err->format_message = gs_jpeg_format_message;
  err->reset_error_mgr = gs_jpeg_reset_error_mgr;

  err->trace_level = 0;		/* default = no tracing */
  err->num_warnings = 0;	/* no warnings emitted yet */
  err->msg_code = 0;		/* may be useful as a flag for "no error" */

  /* Initialize message table pointers */
  err->jpeg_message_table = gs_jpeg_message_table();
  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;

  err->addon_message_table = NULL;
  err->first_addon_message = 0;	/* for safety */
  err->last_addon_message = 0;

  st->data.compress->cinfo.err = err; /* works for decompress case too */
}

/* Stuff the IJG error message into errorinfo after an error exit. */

int
gs_jpeg_log_error (stream_DCT_state *st)
{	j_common_ptr cinfo = (j_common_ptr) &st->data.compress->cinfo;
	char buffer[JMSG_LENGTH_MAX];
	/* Format the error message */
	gs_jpeg_format_message(cinfo, buffer);
	(*st->report_error)((stream_state *)st, buffer);
	return gs_error_ioerror;	/* caller will do return_error() */
}


/*
 * Interface routines.  This layer of routines exists solely to limit
 * side-effects from using setjmp.
 */


JQUANT_TBL *
gs_jpeg_alloc_quant_table (stream_DCT_state *st)
{	if (setjmp(st->data.common->exit_jmpbuf))
	{	gs_jpeg_log_error(st);
		return NULL;
	}
	return jpeg_alloc_quant_table((j_common_ptr)
				      &st->data.compress->cinfo);
}

JHUFF_TBL *
gs_jpeg_alloc_huff_table (stream_DCT_state *st)
{	if (setjmp(st->data.common->exit_jmpbuf))
	{	gs_jpeg_log_error(st);
		return NULL;
	}
	return jpeg_alloc_huff_table((j_common_ptr)
				     &st->data.compress->cinfo);
}

int
gs_jpeg_destroy (stream_DCT_state *st)
{	if (setjmp(st->data.common->exit_jmpbuf))
		return_error(gs_jpeg_log_error(st));
	jpeg_destroy((j_common_ptr) &st->data.compress->cinfo);
	return 0;
}


/*
 * These routines replace the low-level memory manager of the IJG library.
 * They pass malloc/free calls to the Ghostscript memory manager.
 * Note we do not need these to be declared in any GS header file.
 */

void *
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
{
  return gs_malloc(1, sizeofobject, "JPEG small internal data allocation");
}

void
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
{
  gs_free(object, 1, sizeofobject, "Freeing JPEG small internal data");
}

void FAR *
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
{
  return gs_malloc(1, sizeofobject, "JPEG large internal data allocation");
}

void
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
{
  gs_free(object, 1, sizeofobject, "Freeing JPEG large internal data");
}

long
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
		    long max_bytes_needed, long already_allocated)
{
  return max_bytes_needed;
}

void
jpeg_open_backing_store (j_common_ptr cinfo, void * info,
			 long total_bytes_needed)
{
  ERREXIT(cinfo, JERR_NO_BACKING_STORE);
}

long
jpeg_mem_init (j_common_ptr cinfo)
{
  return 0;			/* just set max_memory_to_use to 0 */
}

void
jpeg_mem_term (j_common_ptr cinfo)
{
  /* no work */
}