kusano 7d535a
/*
kusano 7d535a
 * cdjpeg.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains common support routines used by the IJG application
kusano 7d535a
 * programs (cjpeg, djpeg, jpegtran).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
#include <ctype.h>		/* to declare isupper(), tolower() */</ctype.h>
kusano 7d535a
#ifdef NEED_SIGNAL_CATCHER
kusano 7d535a
#include <signal.h>		/* to declare signal() */</signal.h>
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_SETMODE
kusano 7d535a
#include <fcntl.h>		/* to declare setmode()'s parameter macros */</fcntl.h>
kusano 7d535a
/* If you have setmode() but not <io.h>, just delete this line: */</io.h>
kusano 7d535a
#include <io.h>			/* to declare setmode() */</io.h>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Signal catcher to ensure that temporary files are removed before aborting.
kusano 7d535a
 * NB: for Amiga Manx C this is actually a global routine named _abort();
kusano 7d535a
 * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifdef NEED_SIGNAL_CATCHER
kusano 7d535a
kusano 7d535a
static j_common_ptr sig_cinfo;
kusano 7d535a
kusano 7d535a
void				/* must be global for Manx C */
kusano 7d535a
signal_catcher (int signum)
kusano 7d535a
{
kusano 7d535a
  if (sig_cinfo != NULL) {
kusano 7d535a
    if (sig_cinfo->err != NULL) /* turn off trace output */
kusano 7d535a
      sig_cinfo->err->trace_level = 0;
kusano 7d535a
    jpeg_destroy(sig_cinfo);	/* clean up memory allocation & temp files */
kusano 7d535a
  }
kusano 7d535a
  exit(EXIT_FAILURE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
enable_signal_catcher (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  sig_cinfo = cinfo;
kusano 7d535a
#ifdef SIGINT			/* not all systems have SIGINT */
kusano 7d535a
  signal(SIGINT, signal_catcher);
kusano 7d535a
#endif
kusano 7d535a
#ifdef SIGTERM			/* not all systems have SIGTERM */
kusano 7d535a
  signal(SIGTERM, signal_catcher);
kusano 7d535a
#endif
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Optional progress monitor: display a percent-done figure on stderr.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
progress_monitor (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
kusano 7d535a
  int total_passes = prog->pub.total_passes + prog->total_extra_passes;
kusano 7d535a
  int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
kusano 7d535a
kusano 7d535a
  if (percent_done != prog->percent_done) {
kusano 7d535a
    prog->percent_done = percent_done;
kusano 7d535a
    if (total_passes > 1) {
kusano 7d535a
      fprintf(stderr, "\rPass %d/%d: %3d%% ",
kusano 7d535a
	      prog->pub.completed_passes + prog->completed_extra_passes + 1,
kusano 7d535a
	      total_passes, percent_done);
kusano 7d535a
    } else {
kusano 7d535a
      fprintf(stderr, "\r %3d%% ", percent_done);
kusano 7d535a
    }
kusano 7d535a
    fflush(stderr);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
kusano 7d535a
{
kusano 7d535a
  /* Enable progress display, unless trace output is on */
kusano 7d535a
  if (cinfo->err->trace_level == 0) {
kusano 7d535a
    progress->pub.progress_monitor = progress_monitor;
kusano 7d535a
    progress->completed_extra_passes = 0;
kusano 7d535a
    progress->total_extra_passes = 0;
kusano 7d535a
    progress->percent_done = -1;
kusano 7d535a
    cinfo->progress = &progress->pub;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
end_progress_monitor (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* Clear away progress display */
kusano 7d535a
  if (cinfo->err->trace_level == 0) {
kusano 7d535a
    fprintf(stderr, "\r                \r");
kusano 7d535a
    fflush(stderr);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Case-insensitive matching of possibly-abbreviated keyword switches.
kusano 7d535a
 * keyword is the constant keyword (must be lower case already),
kusano 7d535a
 * minchars is length of minimum legal abbreviation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
keymatch (char * arg, const char * keyword, int minchars)
kusano 7d535a
{
kusano 7d535a
  register int ca, ck;
kusano 7d535a
  register int nmatched = 0;
kusano 7d535a
kusano 7d535a
  while ((ca = *arg++) != '\0') {
kusano 7d535a
    if ((ck = *keyword++) == '\0')
kusano 7d535a
      return FALSE;		/* arg longer than keyword, no good */
kusano 7d535a
    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
kusano 7d535a
      ca = tolower(ca);
kusano 7d535a
    if (ca != ck)
kusano 7d535a
      return FALSE;		/* no good */
kusano 7d535a
    nmatched++;			/* count matched characters */
kusano 7d535a
  }
kusano 7d535a
  /* reached end of argument; fail if it's too short for unique abbrev */
kusano 7d535a
  if (nmatched < minchars)
kusano 7d535a
    return FALSE;
kusano 7d535a
  return TRUE;			/* A-OK */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Routines to establish binary I/O mode for stdin and stdout.
kusano 7d535a
 * Non-Unix systems often require some hacking to get out of text mode.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(FILE *)
kusano 7d535a
read_stdin (void)
kusano 7d535a
{
kusano 7d535a
  FILE * input_file = stdin;
kusano 7d535a
kusano 7d535a
#ifdef USE_SETMODE		/* need to hack file mode? */
kusano 7d535a
  setmode(fileno(stdin), O_BINARY);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
kusano 7d535a
  if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
kusano 7d535a
    fprintf(stderr, "Cannot reopen stdin\n");
kusano 7d535a
    exit(EXIT_FAILURE);
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
  return input_file;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(FILE *)
kusano 7d535a
write_stdout (void)
kusano 7d535a
{
kusano 7d535a
  FILE * output_file = stdout;
kusano 7d535a
kusano 7d535a
#ifdef USE_SETMODE		/* need to hack file mode? */
kusano 7d535a
  setmode(fileno(stdout), O_BINARY);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
kusano 7d535a
  if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
kusano 7d535a
    fprintf(stderr, "Cannot reopen stdout\n");
kusano 7d535a
    exit(EXIT_FAILURE);
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
  return output_file;
kusano 7d535a
}