Blame gtkmm-osx/jpeg-6b/jerror.c

darco 56a656
/*
darco 56a656
 * jerror.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1991-1998, Thomas G. Lane.
darco 56a656
 * This file is part of the Independent JPEG Group's software.
darco 56a656
 * For conditions of distribution and use, see the accompanying README file.
darco 56a656
 *
darco 56a656
 * This file contains simple error-reporting and trace-message routines.
darco 56a656
 * These are suitable for Unix-like systems and others where writing to
darco 56a656
 * stderr is the right thing to do.  Many applications will want to replace
darco 56a656
 * some or all of these routines.
darco 56a656
 *
darco 56a656
 * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
darco 56a656
 * you get a Windows-specific hack to display error messages in a dialog box.
darco 56a656
 * It ain't much, but it beats dropping error messages into the bit bucket,
darco 56a656
 * which is what happens to output to stderr under most Windows C compilers.
darco 56a656
 *
darco 56a656
 * These routines are used by both the compression and decompression code.
darco 56a656
 */
darco 56a656
darco 56a656
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jversion.h"
darco 56a656
#include "jerror.h"
darco 56a656
darco 56a656
#ifdef USE_WINDOWS_MESSAGEBOX
darco 56a656
#include <windows.h></windows.h>
darco 56a656
#endif
darco 56a656
darco 56a656
#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
darco 56a656
#define EXIT_FAILURE  1
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Create the message string table.
darco 56a656
 * We do this from the master message list in jerror.h by re-reading
darco 56a656
 * jerror.h with a suitable definition for macro JMESSAGE.
darco 56a656
 * The message table is made an external symbol just in case any applications
darco 56a656
 * want to refer to it directly.
darco 56a656
 */
darco 56a656
darco 56a656
#ifdef NEED_SHORT_EXTERNAL_NAMES
darco 56a656
#define jpeg_std_message_table	jMsgTable
darco 56a656
#endif
darco 56a656
darco 56a656
#define JMESSAGE(code,string)	string ,
darco 56a656
darco 56a656
const char * const jpeg_std_message_table[] = {
darco 56a656
#include "jerror.h"
darco 56a656
  NULL
darco 56a656
};
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Error exit handler: must not return to caller.
darco 56a656
 *
darco 56a656
 * Applications may override this if they want to get control back after
darco 56a656
 * an error.  Typically one would longjmp somewhere instead of exiting.
darco 56a656
 * The setjmp buffer can be made a private field within an expanded error
darco 56a656
 * handler object.  Note that the info needed to generate an error message
darco 56a656
 * is stored in the error object, so you can generate the message now or
darco 56a656
 * later, at your convenience.
darco 56a656
 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
darco 56a656
 * or jpeg_destroy) at some point.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
error_exit (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  /* Always display the message */
darco 56a656
  (*cinfo->err->output_message) (cinfo);
darco 56a656
darco 56a656
  /* Let the memory manager delete any temp files before we die */
darco 56a656
  jpeg_destroy(cinfo);
darco 56a656
darco 56a656
  exit(EXIT_FAILURE);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Actual output of an error or trace message.
darco 56a656
 * Applications may override this method to send JPEG messages somewhere
darco 56a656
 * other than stderr.
darco 56a656
 *
darco 56a656
 * On Windows, printing to stderr is generally completely useless,
darco 56a656
 * so we provide optional code to produce an error-dialog popup.
darco 56a656
 * Most Windows applications will still prefer to override this routine,
darco 56a656
 * but if they don't, it'll do something at least marginally useful.
darco 56a656
 *
darco 56a656
 * NOTE: to use the library in an environment that doesn't support the
darco 56a656
 * C stdio library, you may have to delete the call to fprintf() entirely,
darco 56a656
 * not just not use this routine.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
output_message (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  char buffer[JMSG_LENGTH_MAX];
darco 56a656
darco 56a656
  /* Create the message */
darco 56a656
  (*cinfo->err->format_message) (cinfo, buffer);
darco 56a656
darco 56a656
#ifdef USE_WINDOWS_MESSAGEBOX
darco 56a656
  /* Display it in a message dialog box */
darco 56a656
  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
darco 56a656
	     MB_OK | MB_ICONERROR);
darco 56a656
#else
darco 56a656
  /* Send it to stderr, adding a newline */
darco 56a656
  fprintf(stderr, "%s\n", buffer);
darco 56a656
#endif
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Decide whether to emit a trace or warning message.
darco 56a656
 * msg_level is one of:
darco 56a656
 *   -1: recoverable corrupt-data warning, may want to abort.
darco 56a656
 *    0: important advisory messages (always display to user).
darco 56a656
 *    1: first level of tracing detail.
darco 56a656
 *    2,3,...: successively more detailed tracing messages.
darco 56a656
 * An application might override this method if it wanted to abort on warnings
darco 56a656
 * or change the policy about which messages to display.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
emit_message (j_common_ptr cinfo, int msg_level)
darco 56a656
{
darco 56a656
  struct jpeg_error_mgr * err = cinfo->err;
darco 56a656
darco 56a656
  if (msg_level < 0) {
darco 56a656
    /* It's a warning message.  Since corrupt files may generate many warnings,
darco 56a656
     * the policy implemented here is to show only the first warning,
darco 56a656
     * unless trace_level >= 3.
darco 56a656
     */
darco 56a656
    if (err->num_warnings == 0 || err->trace_level >= 3)
darco 56a656
      (*err->output_message) (cinfo);
darco 56a656
    /* Always count warnings in num_warnings. */
darco 56a656
    err->num_warnings++;
darco 56a656
  } else {
darco 56a656
    /* It's a trace message.  Show it if trace_level >= msg_level. */
darco 56a656
    if (err->trace_level >= msg_level)
darco 56a656
      (*err->output_message) (cinfo);
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Format a message string for the most recent JPEG error or message.
darco 56a656
 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
darco 56a656
 * characters.  Note that no '\n' character is added to the string.
darco 56a656
 * Few applications should need to override this method.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
format_message (j_common_ptr cinfo, char * buffer)
darco 56a656
{
darco 56a656
  struct jpeg_error_mgr * err = cinfo->err;
darco 56a656
  int msg_code = err->msg_code;
darco 56a656
  const char * msgtext = NULL;
darco 56a656
  const char * msgptr;
darco 56a656
  char ch;
darco 56a656
  boolean isstring;
darco 56a656
darco 56a656
  /* Look up message string in proper table */
darco 56a656
  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
darco 56a656
    msgtext = err->jpeg_message_table[msg_code];
darco 56a656
  } else if (err->addon_message_table != NULL &&
darco 56a656
	     msg_code >= err->first_addon_message &&
darco 56a656
	     msg_code <= err->last_addon_message) {
darco 56a656
    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
darco 56a656
  }
darco 56a656
darco 56a656
  /* Defend against bogus message number */
darco 56a656
  if (msgtext == NULL) {
darco 56a656
    err->msg_parm.i[0] = msg_code;
darco 56a656
    msgtext = err->jpeg_message_table[0];
darco 56a656
  }
darco 56a656
darco 56a656
  /* Check for string parameter, as indicated by %s in the message text */
darco 56a656
  isstring = FALSE;
darco 56a656
  msgptr = msgtext;
darco 56a656
  while ((ch = *msgptr++) != '\0') {
darco 56a656
    if (ch == '%') {
darco 56a656
      if (*msgptr == 's') isstring = TRUE;
darco 56a656
      break;
darco 56a656
    }
darco 56a656
  }
darco 56a656
darco 56a656
  /* Format the message into the passed buffer */
darco 56a656
  if (isstring)
darco 56a656
    sprintf(buffer, msgtext, err->msg_parm.s);
darco 56a656
  else
darco 56a656
    sprintf(buffer, msgtext,
darco 56a656
	    err->msg_parm.i[0], err->msg_parm.i[1],
darco 56a656
	    err->msg_parm.i[2], err->msg_parm.i[3],
darco 56a656
	    err->msg_parm.i[4], err->msg_parm.i[5],
darco 56a656
	    err->msg_parm.i[6], err->msg_parm.i[7]);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Reset error state variables at start of a new image.
darco 56a656
 * This is called during compression startup to reset trace/error
darco 56a656
 * processing to default state, without losing any application-specific
darco 56a656
 * method pointers.  An application might possibly want to override
darco 56a656
 * this method if it has additional error processing state.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
reset_error_mgr (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  cinfo->err->num_warnings = 0;
darco 56a656
  /* trace_level is not reset since it is an application-supplied parameter */
darco 56a656
  cinfo->err->msg_code = 0;	/* may be useful as a flag for "no error" */
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
darco 56a656
 * Typical call is:
darco 56a656
 *	struct jpeg_compress_struct cinfo;
darco 56a656
 *	struct jpeg_error_mgr err;
darco 56a656
 *
darco 56a656
 *	cinfo.err = jpeg_std_error(&err);
darco 56a656
 * after which the application may override some of the methods.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(struct jpeg_error_mgr *)
darco 56a656
jpeg_std_error (struct jpeg_error_mgr * err)
darco 56a656
{
darco 56a656
  err->error_exit = error_exit;
darco 56a656
  err->emit_message = emit_message;
darco 56a656
  err->output_message = output_message;
darco 56a656
  err->format_message = format_message;
darco 56a656
  err->reset_error_mgr = reset_error_mgr;
darco 56a656
darco 56a656
  err->trace_level = 0;		/* default = no tracing */
darco 56a656
  err->num_warnings = 0;	/* no warnings emitted yet */
darco 56a656
  err->msg_code = 0;		/* may be useful as a flag for "no error" */
darco 56a656
darco 56a656
  /* Initialize message table pointers */
darco 56a656
  err->jpeg_message_table = jpeg_std_message_table;
darco 56a656
  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
darco 56a656
darco 56a656
  err->addon_message_table = NULL;
darco 56a656
  err->first_addon_message = 0;	/* for safety */
darco 56a656
  err->last_addon_message = 0;
darco 56a656
darco 56a656
  return err;
darco 56a656
}