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 */
18 #include "radiant_jpeglib.h"
22 #ifndef EXIT_FAILURE /* define exit() codes if not provided */
23 #define EXIT_FAILURE 1
28 * Create the message string table.
29 * We do this from the master message list in jerror.h by re-reading
30 * jerror.h with a suitable definition for macro JMESSAGE.
31 * The message table is made an external symbol just in case any applications
32 * want to refer to it directly.
35 #ifdef NEED_SHORT_EXTERNAL_NAMES
36 #define jpeg_std_message_table jMsgTable
39 #define JMESSAGE(code,string) string ,
41 const char * const jpeg_std_message_table[] = {
46 // Rad additions, longjmp out of the LoadJPGBuff
47 GLOBAL jmp_buf rad_loadfailed;
48 GLOBAL char rad_errormsg[JMSG_LENGTH_MAX];
51 * Error exit handler: must not return to caller.
53 * Applications may override this if they want to get control back after
54 * an error. Typically one would longjmp somewhere instead of exiting.
55 * The setjmp buffer can be made a private field within an expanded error
56 * handler object. Note that the info needed to generate an error message
57 * is stored in the error object, so you can generate the message now or
58 * later, at your convenience.
59 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
60 * or jpeg_destroy) at some point.
64 error_exit (j_common_ptr cinfo)
66 // char buffer[JMSG_LENGTH_MAX];
68 /* Create the message */
69 (*cinfo->err->format_message) (cinfo,rad_errormsg);
71 /* Let the memory manager delete any temp files before we die */
74 longjmp( rad_loadfailed, -1 );
79 * Actual output of an error or trace message.
80 * Applications may override this method to send JPEG messages somewhere
85 output_message (j_common_ptr cinfo)
87 char buffer[JMSG_LENGTH_MAX];
89 /* Create the message */
90 (*cinfo->err->format_message) (cinfo, buffer);
92 /* Send it to stderr, adding a newline */
93 printf("%s\n", buffer);
98 * Decide whether to emit a trace or warning message.
99 * msg_level is one of:
100 * -1: recoverable corrupt-data warning, may want to abort.
101 * 0: important advisory messages (always display to user).
102 * 1: first level of tracing detail.
103 * 2,3,...: successively more detailed tracing messages.
104 * An application might override this method if it wanted to abort on warnings
105 * or change the policy about which messages to display.
109 emit_message (j_common_ptr cinfo, int msg_level)
111 struct jpeg_error_mgr * err = cinfo->err;
114 /* It's a warning message. Since corrupt files may generate many warnings,
115 * the policy implemented here is to show only the first warning,
116 * unless trace_level >= 3.
118 if (err->num_warnings == 0 || err->trace_level >= 3)
119 (*err->output_message) (cinfo);
120 /* Always count warnings in num_warnings. */
123 /* It's a trace message. Show it if trace_level >= msg_level. */
124 if (err->trace_level >= msg_level)
125 (*err->output_message) (cinfo);
131 * Format a message string for the most recent JPEG error or message.
132 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
133 * characters. Note that no '\n' character is added to the string.
134 * Few applications should need to override this method.
138 format_message (j_common_ptr cinfo, char * buffer)
140 struct jpeg_error_mgr * err = cinfo->err;
141 int msg_code = err->msg_code;
142 const char * msgtext = NULL;
147 /* Look up message string in proper table */
148 if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
149 msgtext = err->jpeg_message_table[msg_code];
150 } else if (err->addon_message_table != NULL &&
151 msg_code >= err->first_addon_message &&
152 msg_code <= err->last_addon_message) {
153 msgtext = err->addon_message_table[msg_code - err->first_addon_message];
156 /* Defend against bogus message number */
157 if (msgtext == NULL) {
158 err->msg_parm.i[0] = msg_code;
159 msgtext = err->jpeg_message_table[0];
162 /* Check for string parameter, as indicated by %s in the message text */
165 while ((ch = *msgptr++) != '\0') {
167 if (*msgptr == 's') isstring = TRUE;
172 /* Format the message into the passed buffer */
174 sprintf(buffer, msgtext, err->msg_parm.s);
176 sprintf(buffer, msgtext,
177 err->msg_parm.i[0], err->msg_parm.i[1],
178 err->msg_parm.i[2], err->msg_parm.i[3],
179 err->msg_parm.i[4], err->msg_parm.i[5],
180 err->msg_parm.i[6], err->msg_parm.i[7]);
185 * Reset error state variables at start of a new image.
186 * This is called during compression startup to reset trace/error
187 * processing to default state, without losing any application-specific
188 * method pointers. An application might possibly want to override
189 * this method if it has additional error processing state.
193 reset_error_mgr (j_common_ptr cinfo)
195 cinfo->err->num_warnings = 0;
196 /* trace_level is not reset since it is an application-supplied parameter */
197 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
202 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
204 * struct jpeg_compress_struct cinfo;
205 * struct jpeg_error_mgr err;
207 * cinfo.err = jpeg_std_error(&err);
208 * after which the application may override some of the methods.
211 GLOBAL struct jpeg_error_mgr *
212 jpeg_std_error (struct jpeg_error_mgr * err)
214 err->error_exit = error_exit;
215 err->emit_message = emit_message;
216 err->output_message = output_message;
217 err->format_message = format_message;
218 err->reset_error_mgr = reset_error_mgr;
220 err->trace_level = 0; /* default = no tracing */
221 err->num_warnings = 0; /* no warnings emitted yet */
222 err->msg_code = 0; /* may be useful as a flag for "no error" */
224 /* Initialize message table pointers */
225 err->jpeg_message_table = jpeg_std_message_table;
226 err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
228 err->addon_message_table = NULL;
229 err->first_addon_message = 0; /* for safety */
230 err->last_addon_message = 0;