4 * Copyright (C) 1991-1998, 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 * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
14 * you get a Windows-specific hack to display error messages in a dialog box.
15 * It ain't much, but it beats dropping error messages into the bit bucket,
16 * which is what happens to output to stderr under most Windows C compilers.
18 * These routines are used by both the compression and decompression code.
21 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
23 #define _CRT_SECURE_NO_WARNINGS
30 #ifdef USE_WINDOWS_MESSAGEBOX
34 #ifndef EXIT_FAILURE /* define exit() codes if not provided */
35 #define EXIT_FAILURE 1
40 * Create the message string table.
41 * We do this from the master message list in jerror.h by re-reading
42 * jerror.h with a suitable definition for macro JMESSAGE.
43 * The message table is made an external symbol just in case any applications
44 * want to refer to it directly.
47 #ifdef NEED_SHORT_EXTERNAL_NAMES
48 #define jpeg_std_message_table jMsgTable
51 #define JMESSAGE(code,string) string ,
53 const char * const jpeg_std_message_table[] = {
60 * Error exit handler: must not return to caller.
62 * Applications may override this if they want to get control back after
63 * an error. Typically one would longjmp somewhere instead of exiting.
64 * The setjmp buffer can be made a private field within an expanded error
65 * handler object. Note that the info needed to generate an error message
66 * is stored in the error object, so you can generate the message now or
67 * later, at your convenience.
68 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
69 * or jpeg_destroy) at some point.
73 error_exit (j_common_ptr cinfo)
75 /* Always display the message */
76 (*cinfo->err->output_message) (cinfo);
78 /* Let the memory manager delete any temp files before we die */
86 * Actual output of an error or trace message.
87 * Applications may override this method to send JPEG messages somewhere
90 * On Windows, printing to stderr is generally completely useless,
91 * so we provide optional code to produce an error-dialog popup.
92 * Most Windows applications will still prefer to override this routine,
93 * but if they don't, it'll do something at least marginally useful.
95 * NOTE: to use the library in an environment that doesn't support the
96 * C stdio library, you may have to delete the call to fprintf() entirely,
97 * not just not use this routine.
101 output_message (j_common_ptr cinfo)
103 char buffer[JMSG_LENGTH_MAX];
105 /* Create the message */
106 (*cinfo->err->format_message) (cinfo, buffer);
108 #ifdef USE_WINDOWS_MESSAGEBOX
109 /* Display it in a message dialog box */
110 MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
111 MB_OK | MB_ICONERROR);
113 /* Send it to stderr, adding a newline */
114 fprintf(stderr, "%s\n", buffer);
120 * Decide whether to emit a trace or warning message.
121 * msg_level is one of:
122 * -1: recoverable corrupt-data warning, may want to abort.
123 * 0: important advisory messages (always display to user).
124 * 1: first level of tracing detail.
125 * 2,3,...: successively more detailed tracing messages.
126 * An application might override this method if it wanted to abort on warnings
127 * or change the policy about which messages to display.
131 emit_message (j_common_ptr cinfo, int msg_level)
133 struct jpeg_error_mgr * err = cinfo->err;
136 /* It's a warning message. Since corrupt files may generate many warnings,
137 * the policy implemented here is to show only the first warning,
138 * unless trace_level >= 3.
140 if (err->num_warnings == 0 || err->trace_level >= 3)
141 (*err->output_message) (cinfo);
142 /* Always count warnings in num_warnings. */
145 /* It's a trace message. Show it if trace_level >= msg_level. */
146 if (err->trace_level >= msg_level)
147 (*err->output_message) (cinfo);
153 * Format a message string for the most recent JPEG error or message.
154 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
155 * characters. Note that no '\n' character is added to the string.
156 * Few applications should need to override this method.
160 format_message (j_common_ptr cinfo, char * buffer)
162 struct jpeg_error_mgr * err = cinfo->err;
163 int msg_code = err->msg_code;
164 const char * msgtext = NULL;
169 /* Look up message string in proper table */
170 if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
171 msgtext = err->jpeg_message_table[msg_code];
172 } else if (err->addon_message_table != NULL &&
173 msg_code >= err->first_addon_message &&
174 msg_code <= err->last_addon_message) {
175 msgtext = err->addon_message_table[msg_code - err->first_addon_message];
178 /* Defend against bogus message number */
179 if (msgtext == NULL) {
180 err->msg_parm.i[0] = msg_code;
181 msgtext = err->jpeg_message_table[0];
184 /* Check for string parameter, as indicated by %s in the message text */
187 while ((ch = *msgptr++) != '\0') {
189 if (*msgptr == 's') isstring = TRUE;
194 /* Format the message into the passed buffer */
196 sprintf(buffer, msgtext, err->msg_parm.s);
198 sprintf(buffer, msgtext,
199 err->msg_parm.i[0], err->msg_parm.i[1],
200 err->msg_parm.i[2], err->msg_parm.i[3],
201 err->msg_parm.i[4], err->msg_parm.i[5],
202 err->msg_parm.i[6], err->msg_parm.i[7]);
207 * Reset error state variables at start of a new image.
208 * This is called during compression startup to reset trace/error
209 * processing to default state, without losing any application-specific
210 * method pointers. An application might possibly want to override
211 * this method if it has additional error processing state.
215 reset_error_mgr (j_common_ptr cinfo)
217 cinfo->err->num_warnings = 0;
218 /* trace_level is not reset since it is an application-supplied parameter */
219 cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
224 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
226 * struct jpeg_compress_struct cinfo;
227 * struct jpeg_error_mgr err;
229 * cinfo.err = jpeg_std_error(&err);
230 * after which the application may override some of the methods.
233 GLOBAL(struct jpeg_error_mgr *)
234 jpeg_std_error (struct jpeg_error_mgr * err)
236 err->error_exit = error_exit;
237 err->emit_message = emit_message;
238 err->output_message = output_message;
239 err->format_message = format_message;
240 err->reset_error_mgr = reset_error_mgr;
242 err->trace_level = 0; /* default = no tracing */
243 err->num_warnings = 0; /* no warnings emitted yet */
244 err->msg_code = 0; /* may be useful as a flag for "no error" */
246 /* Initialize message table pointers */
247 err->jpeg_message_table = jpeg_std_message_table;
248 err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
250 err->addon_message_table = NULL;
251 err->first_addon_message = 0; /* for safety */
252 err->last_addon_message = 0;