Back to index

lightning-sunbird  0.9+nobinonly
cdjpeg.c
Go to the documentation of this file.
00001 /*
00002  * cdjpeg.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains common support routines used by the IJG application
00009  * programs (cjpeg, djpeg, jpegtran).
00010  */
00011 
00012 #include "cdjpeg.h"         /* Common decls for cjpeg/djpeg applications */
00013 #include <ctype.h>          /* to declare isupper(), tolower() */
00014 #ifdef NEED_SIGNAL_CATCHER
00015 #include <signal.h>         /* to declare signal() */
00016 #endif
00017 #ifdef USE_SETMODE
00018 #include <fcntl.h>          /* to declare setmode()'s parameter macros */
00019 /* If you have setmode() but not <io.h>, just delete this line: */
00020 #include <io.h>                    /* to declare setmode() */
00021 #endif
00022 
00023 
00024 /*
00025  * Signal catcher to ensure that temporary files are removed before aborting.
00026  * NB: for Amiga Manx C this is actually a global routine named _abort();
00027  * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
00028  */
00029 
00030 #ifdef NEED_SIGNAL_CATCHER
00031 
00032 static j_common_ptr sig_cinfo;
00033 
00034 void                        /* must be global for Manx C */
00035 signal_catcher (int signum)
00036 {
00037   if (sig_cinfo != NULL) {
00038     if (sig_cinfo->err != NULL) /* turn off trace output */
00039       sig_cinfo->err->trace_level = 0;
00040     jpeg_destroy(sig_cinfo);       /* clean up memory allocation & temp files */
00041   }
00042   exit(EXIT_FAILURE);
00043 }
00044 
00045 
00046 GLOBAL(void)
00047 enable_signal_catcher (j_common_ptr cinfo)
00048 {
00049   sig_cinfo = cinfo;
00050 #ifdef SIGINT               /* not all systems have SIGINT */
00051   signal(SIGINT, signal_catcher);
00052 #endif
00053 #ifdef SIGTERM                     /* not all systems have SIGTERM */
00054   signal(SIGTERM, signal_catcher);
00055 #endif
00056 }
00057 
00058 #endif
00059 
00060 
00061 /*
00062  * Optional progress monitor: display a percent-done figure on stderr.
00063  */
00064 
00065 #ifdef PROGRESS_REPORT
00066 
00067 METHODDEF(void)
00068 progress_monitor (j_common_ptr cinfo)
00069 {
00070   cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
00071   int total_passes = prog->pub.total_passes + prog->total_extra_passes;
00072   int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
00073 
00074   if (percent_done != prog->percent_done) {
00075     prog->percent_done = percent_done;
00076     if (total_passes > 1) {
00077       fprintf(stderr, "\rPass %d/%d: %3d%% ",
00078              prog->pub.completed_passes + prog->completed_extra_passes + 1,
00079              total_passes, percent_done);
00080     } else {
00081       fprintf(stderr, "\r %3d%% ", percent_done);
00082     }
00083     fflush(stderr);
00084   }
00085 }
00086 
00087 
00088 GLOBAL(void)
00089 start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
00090 {
00091   /* Enable progress display, unless trace output is on */
00092   if (cinfo->err->trace_level == 0) {
00093     progress->pub.progress_monitor = progress_monitor;
00094     progress->completed_extra_passes = 0;
00095     progress->total_extra_passes = 0;
00096     progress->percent_done = -1;
00097     cinfo->progress = &progress->pub;
00098   }
00099 }
00100 
00101 
00102 GLOBAL(void)
00103 end_progress_monitor (j_common_ptr cinfo)
00104 {
00105   /* Clear away progress display */
00106   if (cinfo->err->trace_level == 0) {
00107     fprintf(stderr, "\r                \r");
00108     fflush(stderr);
00109   }
00110 }
00111 
00112 #endif
00113 
00114 
00115 /*
00116  * Case-insensitive matching of possibly-abbreviated keyword switches.
00117  * keyword is the constant keyword (must be lower case already),
00118  * minchars is length of minimum legal abbreviation.
00119  */
00120 
00121 GLOBAL(boolean)
00122 keymatch (char * arg, const char * keyword, int minchars)
00123 {
00124   register int ca, ck;
00125   register int nmatched = 0;
00126 
00127   while ((ca = *arg++) != '\0') {
00128     if ((ck = *keyword++) == '\0')
00129       return FALSE;         /* arg longer than keyword, no good */
00130     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
00131       ca = tolower(ca);
00132     if (ca != ck)
00133       return FALSE;         /* no good */
00134     nmatched++;                    /* count matched characters */
00135   }
00136   /* reached end of argument; fail if it's too short for unique abbrev */
00137   if (nmatched < minchars)
00138     return FALSE;
00139   return TRUE;                     /* A-OK */
00140 }
00141 
00142 
00143 /*
00144  * Routines to establish binary I/O mode for stdin and stdout.
00145  * Non-Unix systems often require some hacking to get out of text mode.
00146  */
00147 
00148 GLOBAL(FILE *)
00149 read_stdin (void)
00150 {
00151   FILE * input_file = stdin;
00152 
00153 #ifdef USE_SETMODE          /* need to hack file mode? */
00154   setmode(fileno(stdin), O_BINARY);
00155 #endif
00156 #ifdef USE_FDOPEN           /* need to re-open in binary mode? */
00157   if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
00158     fprintf(stderr, "Cannot reopen stdin\n");
00159     exit(EXIT_FAILURE);
00160   }
00161 #endif
00162   return input_file;
00163 }
00164 
00165 
00166 GLOBAL(FILE *)
00167 write_stdout (void)
00168 {
00169   FILE * output_file = stdout;
00170 
00171 #ifdef USE_SETMODE          /* need to hack file mode? */
00172   setmode(fileno(stdout), O_BINARY);
00173 #endif
00174 #ifdef USE_FDOPEN           /* need to re-open in binary mode? */
00175   if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
00176     fprintf(stderr, "Cannot reopen stdout\n");
00177     exit(EXIT_FAILURE);
00178   }
00179 #endif
00180   return output_file;
00181 }