4
* Copyright (C) 1991-1994, Thomas G. Lane.
5
* This file is part of the Independent JPEG Group's software.
6
* For conditions of distribution and use, see the accompanying README file.
8
* This file contains simple error-reporting and trace-message routines.
9
* These are suitable for Unix-like systems and others where writing to
10
* stderr is the right thing to do. Many applications will want to replace
11
* some or all of these routines.
13
* These routines are used by both the compression and decompression code.
16
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
22
#include "../renderer/tr_local.h"
24
#ifndef EXIT_FAILURE /* define exit() codes if not provided */
25
#define EXIT_FAILURE 1
30
* Create the message string table.
31
* We do this from the master message list in jerror.h by re-reading
32
* jerror.h with a suitable definition for macro JMESSAGE.
33
* The message table is made an external symbol just in case any applications
34
* want to refer to it directly.
37
#ifdef NEED_SHORT_EXTERNAL_NAMES
38
#define jpeg_std_message_table jMsgTable
41
#define JMESSAGE(code,string) string ,
43
const char * const jpeg_std_message_table[] = {
50
* Error exit handler: must not return to caller.
52
* Applications may override this if they want to get control back after
53
* an error. Typically one would longjmp somewhere instead of exiting.
54
* The setjmp buffer can be made a private field within an expanded error
55
* handler object. Note that the info needed to generate an error message
56
* is stored in the error object, so you can generate the message now or
57
* later, at your convenience.
58
* You should make sure that the JPEG object is cleaned up (with jpeg_abort
59
* or jpeg_destroy) at some point.
63
error_exit (j_common_ptr cinfo)
65
char buffer[JMSG_LENGTH_MAX];
67
/* Create the message */
68
(*cinfo->err->format_message) (cinfo, buffer);
70
/* Let the memory manager delete any temp files before we die */
73
ri.Error( ERR_FATAL, "%s\n", buffer );
78
* Actual output of an error or trace message.
79
* Applications may override this method to send JPEG messages somewhere
84
output_message (j_common_ptr cinfo)
86
char buffer[JMSG_LENGTH_MAX];
88
/* Create the message */
89
(*cinfo->err->format_message) (cinfo, buffer);
91
/* Send it to stderr, adding a newline */
92
ri.Printf(PRINT_ALL, "%s\n", buffer);
97
* Decide whether to emit a trace or warning message.
98
* msg_level is one of:
99
* -1: recoverable corrupt-data warning, may want to abort.
100
* 0: important advisory messages (always display to user).
101
* 1: first level of tracing detail.
102
* 2,3,...: successively more detailed tracing messages.
103
* An application might override this method if it wanted to abort on warnings
104
* or change the policy about which messages to display.
108
emit_message (j_common_ptr cinfo, int msg_level)
110
struct jpeg_error_mgr * err = cinfo->err;
113
/* It's a warning message. Since corrupt files may generate many warnings,
114
* the policy implemented here is to show only the first warning,
115
* unless trace_level >= 3.
117
if (err->num_warnings == 0 || err->trace_level >= 3)
118
(*err->output_message) (cinfo);
119
/* Always count warnings in num_warnings. */
122
/* It's a trace message. Show it if trace_level >= msg_level. */
123
if (err->trace_level >= msg_level)
124
(*err->output_message) (cinfo);
130
* Format a message string for the most recent JPEG error or message.
131
* The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
132
* characters. Note that no '\n' character is added to the string.
133
* Few applications should need to override this method.
137
format_message (j_common_ptr cinfo, char * buffer)
139
struct jpeg_error_mgr * err = cinfo->err;
140
int msg_code = err->msg_code;
141
const char * msgtext = NULL;
146
/* Look up message string in proper table */
147
if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
148
msgtext = err->jpeg_message_table[msg_code];
149
} else if (err->addon_message_table != NULL &&
150
msg_code >= err->first_addon_message &&
151
msg_code <= err->last_addon_message) {
152
msgtext = err->addon_message_table[msg_code - err->first_addon_message];
155
/* Defend against bogus message number */
156
if (msgtext == NULL) {
157
err->msg_parm.i[0] = msg_code;
158
msgtext = err->jpeg_message_table[0];
161
/* Check for string parameter, as indicated by %s in the message text */
164
while ((ch = *msgptr++) != '\0') {
166
if (*msgptr == 's') isstring = TRUE;
171
/* Format the message into the passed buffer */
173
sprintf(buffer, msgtext, err->msg_parm.s);
175
sprintf(buffer, msgtext,
176
err->msg_parm.i[0], err->msg_parm.i[1],
177
err->msg_parm.i[2], err->msg_parm.i[3],
178
err->msg_parm.i[4], err->msg_parm.i[5],
179
err->msg_parm.i[6], err->msg_parm.i[7]);
184
* Reset error state variables at start of a new image.
185
* This is called during compression startup to reset trace/error
186
* processing to default state, without losing any application-specific
187
* method pointers. An application might possibly want to override
188
* this method if it has additional error processing state.
192
reset_error_mgr (j_common_ptr cinfo)
194
cinfo->err->num_warnings = 0;
195
/* trace_level is not reset since it is an application-supplied parameter */
196
cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
201
* Fill in the standard error-handling methods in a jpeg_error_mgr object.
203
* struct jpeg_compress_struct cinfo;
204
* struct jpeg_error_mgr err;
206
* cinfo.err = jpeg_std_error(&err);
207
* after which the application may override some of the methods.
210
GLOBAL struct jpeg_error_mgr *
211
jpeg_std_error (struct jpeg_error_mgr * err)
213
err->error_exit = error_exit;
214
err->emit_message = emit_message;
215
err->output_message = output_message;
216
err->format_message = format_message;
217
err->reset_error_mgr = reset_error_mgr;
219
err->trace_level = 0; /* default = no tracing */
220
err->num_warnings = 0; /* no warnings emitted yet */
221
err->msg_code = 0; /* may be useful as a flag for "no error" */
223
/* Initialize message table pointers */
224
err->jpeg_message_table = jpeg_std_message_table;
225
err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
227
err->addon_message_table = NULL;
228
err->first_addon_message = 0; /* for safety */
229
err->last_addon_message = 0;