cdjpeg.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 5k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*
  2.  * cdjpeg.c
  3.  *
  4.  * Copyright (C) 1991-1995, 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.
  7.  *
  8.  * This file contains common support routines used by the IJG application
  9.  * programs (cjpeg, djpeg, jpegtran).
  10.  */
  11. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  12. #include <ctype.h> /* to declare isupper(), tolower() */
  13. #ifdef NEED_SIGNAL_CATCHER
  14. #include <signal.h> /* to declare signal() */
  15. #endif
  16. #ifdef USE_SETMODE
  17. #include <fcntl.h> /* to declare setmode()'s parameter macros */
  18. /* If you have setmode() but not <io.h>, just delete this line: */
  19. #include <io.h> /* to declare setmode() */
  20. #endif
  21. /*
  22.  * Signal catcher to ensure that temporary files are removed before aborting.
  23.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  24.  * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
  25.  */
  26. #ifdef NEED_SIGNAL_CATCHER
  27. static j_common_ptr sig_cinfo;
  28. GLOBAL void /* must be global for Manx C */
  29. signal_catcher (int signum)
  30. {
  31.   if (sig_cinfo != NULL) {
  32.     if (sig_cinfo->err != NULL) /* turn off trace output */
  33.       sig_cinfo->err->trace_level = 0;
  34.     jpeg_destroy(sig_cinfo); /* clean up memory allocation & temp files */
  35.   }
  36.   exit(EXIT_FAILURE);
  37. }
  38. GLOBAL void
  39. enable_signal_catcher (j_common_ptr cinfo)
  40. {
  41.   sig_cinfo = cinfo;
  42.   signal(SIGINT, signal_catcher);
  43. #ifdef SIGTERM /* not all systems have SIGTERM */
  44.   signal(SIGTERM, signal_catcher);
  45. #endif
  46. }
  47. #endif
  48. /*
  49.  * Optional progress monitor: display a percent-done figure on stderr.
  50.  */
  51. #ifdef PROGRESS_REPORT
  52. METHODDEF void
  53. progress_monitor (j_common_ptr cinfo)
  54. {
  55.   cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
  56.   int total_passes = prog->pub.total_passes + prog->total_extra_passes;
  57.   int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
  58.   if (percent_done != prog->percent_done) {
  59.     prog->percent_done = percent_done;
  60.     if (total_passes > 1) {
  61.       fprintf(stderr, "rPass %d/%d: %3d%% ",
  62.       prog->pub.completed_passes + prog->completed_extra_passes + 1,
  63.       total_passes, percent_done);
  64.     } else {
  65.       fprintf(stderr, "r %3d%% ", percent_done);
  66.     }
  67.     fflush(stderr);
  68.   }
  69. }
  70. GLOBAL void
  71. start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
  72. {
  73.   /* Enable progress display, unless trace output is on */
  74.   if (cinfo->err->trace_level == 0) {
  75.     progress->pub.progress_monitor = progress_monitor;
  76.     progress->completed_extra_passes = 0;
  77.     progress->total_extra_passes = 0;
  78.     progress->percent_done = -1;
  79.     cinfo->progress = &progress->pub;
  80.   }
  81. }
  82. GLOBAL void
  83. end_progress_monitor (j_common_ptr cinfo)
  84. {
  85.   /* Clear away progress display */
  86.   if (cinfo->err->trace_level == 0) {
  87.     fprintf(stderr, "r                r");
  88.     fflush(stderr);
  89.   }
  90. }
  91. #endif
  92. /*
  93.  * Case-insensitive matching of possibly-abbreviated keyword switches.
  94.  * keyword is the constant keyword (must be lower case already),
  95.  * minchars is length of minimum legal abbreviation.
  96.  */
  97. GLOBAL boolean
  98. keymatch (char * arg, const char * keyword, int minchars)
  99. {
  100.   register int ca, ck;
  101.   register int nmatched = 0;
  102.   while ((ca = *arg++) != '') {
  103.     if ((ck = *keyword++) == '')
  104.       return FALSE; /* arg longer than keyword, no good */
  105.     if (isupper(ca)) /* force arg to lcase (assume ck is already) */
  106.       ca = tolower(ca);
  107.     if (ca != ck)
  108.       return FALSE; /* no good */
  109.     nmatched++; /* count matched characters */
  110.   }
  111.   /* reached end of argument; fail if it's too short for unique abbrev */
  112.   if (nmatched < minchars)
  113.     return FALSE;
  114.   return TRUE; /* A-OK */
  115. }
  116. /*
  117.  * Routines to establish binary I/O mode for stdin and stdout.
  118.  * Non-Unix systems often require some hacking to get out of text mode.
  119.  */
  120. GLOBAL FILE *
  121. read_stdin (void)
  122. {
  123.   FILE * input_file = stdin;
  124. #ifdef USE_SETMODE /* need to hack file mode? */
  125.   setmode(fileno(stdin), O_BINARY);
  126. #endif
  127. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  128.   if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  129.     fprintf(stderr, "Cannot reopen stdinn");
  130.     exit(EXIT_FAILURE);
  131.   }
  132. #endif
  133.   return input_file;
  134. }
  135. GLOBAL FILE *
  136. write_stdout (void)
  137. {
  138.   FILE * output_file = stdout;
  139. #ifdef USE_SETMODE /* need to hack file mode? */
  140.   setmode(fileno(stdout), O_BINARY);
  141. #endif
  142. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  143.   if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
  144.     fprintf(stderr, "Cannot reopen stdoutn");
  145.     exit(EXIT_FAILURE);
  146.   }
  147. #endif
  148.   return output_file;
  149. }