kusano 7d535a
/*
kusano 7d535a
 * cdjpeg.c
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * It was modified by The libjpeg-turbo Project to include only code relevant
shun-iwasawa 82a8f5
 * to libjpeg-turbo.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * 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
shun-iwasawa 82a8f5
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
shun-iwasawa 82a8f5
#include <ctype.h>              /* to declare isupper(), tolower() */</ctype.h>
kusano 7d535a
#ifdef USE_SETMODE
shun-iwasawa 82a8f5
#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>
shun-iwasawa 82a8f5
#include <io.h>                 /* to declare setmode() */</io.h>
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)
shun-iwasawa 82a8f5
progress_monitor(j_common_ptr cinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  cd_progress_ptr prog = (cd_progress_ptr)cinfo->progress;
kusano 7d535a
  int total_passes = prog->pub.total_passes + prog->total_extra_passes;
shun-iwasawa 82a8f5
  int percent_done =
shun-iwasawa 82a8f5
    (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%% ",
shun-iwasawa 82a8f5
              prog->pub.completed_passes + prog->completed_extra_passes + 1,
shun-iwasawa 82a8f5
              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)
shun-iwasawa 82a8f5
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)
shun-iwasawa 82a8f5
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)
shun-iwasawa 82a8f5
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')
shun-iwasawa 82a8f5
      return FALSE;             /* arg longer than keyword, no good */
shun-iwasawa 82a8f5
    if (isupper(ca))            /* force arg to lcase (assume ck is already) */
kusano 7d535a
      ca = tolower(ca);
kusano 7d535a
    if (ca != ck)
shun-iwasawa 82a8f5
      return FALSE;             /* no good */
shun-iwasawa 82a8f5
    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;
shun-iwasawa 82a8f5
  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 *)
shun-iwasawa 82a8f5
read_stdin(void)
kusano 7d535a
{
shun-iwasawa 82a8f5
  FILE *input_file = stdin;
kusano 7d535a
shun-iwasawa 82a8f5
#ifdef USE_SETMODE              /* need to hack file mode? */
kusano 7d535a
  setmode(fileno(stdin), O_BINARY);
kusano 7d535a
#endif
shun-iwasawa 82a8f5
#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 *)
shun-iwasawa 82a8f5
write_stdout(void)
kusano 7d535a
{
shun-iwasawa 82a8f5
  FILE *output_file = stdout;
kusano 7d535a
shun-iwasawa 82a8f5
#ifdef USE_SETMODE              /* need to hack file mode? */
kusano 7d535a
  setmode(fileno(stdout), O_BINARY);
kusano 7d535a
#endif
shun-iwasawa 82a8f5
#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
}