8 * Copyright (C) 1991-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains common support routines used by the IJG application
13 * programs (cjpeg, djpeg, jpegtran).
16 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
17 #include <ctype.h> /* to declare isupper(), tolower() */
18 #ifdef NEED_SIGNAL_CATCHER
19 #include <signal.h> /* to declare signal() */
22 #include <fcntl.h> /* to declare setmode()'s parameter macros */
23 /* If you have setmode() but not <io.h>, just delete this line: */
24 #include <io.h> /* to declare setmode() */
29 * Signal catcher to ensure that temporary files are removed before aborting.
30 * NB: for Amiga Manx C this is actually a global routine named _abort();
31 * we put "#define signal_catcher _abort" in jconfig.h. Talk about bogus...
34 #ifdef NEED_SIGNAL_CATCHER
36 static j_common_ptr sig_cinfo
;
38 void /* must be global for Manx C */
39 signal_catcher (int signum
)
41 if (sig_cinfo
!= NULL
) {
42 if (sig_cinfo
->err
!= NULL
) /* turn off trace output */
43 sig_cinfo
->err
->trace_level
= 0;
44 jpeg_destroy(sig_cinfo
); /* clean up memory allocation & temp files */
51 enable_signal_catcher (j_common_ptr cinfo
)
54 #ifdef SIGINT /* not all systems have SIGINT */
55 signal(SIGINT
, signal_catcher
);
57 #ifdef SIGTERM /* not all systems have SIGTERM */
58 signal(SIGTERM
, signal_catcher
);
66 * Optional progress monitor: display a percent-done figure on stderr.
69 #ifdef PROGRESS_REPORT
72 progress_monitor (j_common_ptr cinfo
)
74 cd_progress_ptr prog
= (cd_progress_ptr
) cinfo
->progress
;
75 int total_passes
= prog
->pub
.total_passes
+ prog
->total_extra_passes
;
76 int percent_done
= (int) (prog
->pub
.pass_counter
*100L/prog
->pub
.pass_limit
);
78 if (percent_done
!= prog
->percent_done
) {
79 prog
->percent_done
= percent_done
;
80 if (total_passes
> 1) {
81 fprintf(stderr
, "\rPass %d/%d: %3d%% ",
82 prog
->pub
.completed_passes
+ prog
->completed_extra_passes
+ 1,
83 total_passes
, percent_done
);
85 fprintf(stderr
, "\r %3d%% ", percent_done
);
93 start_progress_monitor (j_common_ptr cinfo
, cd_progress_ptr progress
)
95 /* Enable progress display, unless trace output is on */
96 if (cinfo
->err
->trace_level
== 0) {
97 progress
->pub
.progress_monitor
= progress_monitor
;
98 progress
->completed_extra_passes
= 0;
99 progress
->total_extra_passes
= 0;
100 progress
->percent_done
= -1;
101 cinfo
->progress
= &progress
->pub
;
107 end_progress_monitor (j_common_ptr cinfo
)
109 /* Clear away progress display */
110 if (cinfo
->err
->trace_level
== 0) {
111 fprintf(stderr
, "\r \r");
120 * Case-insensitive matching of possibly-abbreviated keyword switches.
121 * keyword is the constant keyword (must be lower case already),
122 * minchars is length of minimum legal abbreviation.
126 keymatch (char * arg
, const char * keyword
, int minchars
)
129 register int nmatched
= 0;
131 while ((ca
= *arg
++) != '\0') {
132 if ((ck
= *keyword
++) == '\0')
133 return FALSE
; /* arg longer than keyword, no good */
134 if (isupper(ca
)) /* force arg to lcase (assume ck is already) */
137 return FALSE
; /* no good */
138 nmatched
++; /* count matched characters */
140 /* reached end of argument; fail if it's too short for unique abbrev */
141 if (nmatched
< minchars
)
143 return TRUE
; /* A-OK */
148 * Routines to establish binary I/O mode for stdin and stdout.
149 * Non-Unix systems often require some hacking to get out of text mode.
155 FILE * input_file
= stdin
;
157 #ifdef USE_SETMODE /* need to hack file mode? */
158 setmode(fileno(stdin
), O_BINARY
);
160 #ifdef USE_FDOPEN /* need to re-open in binary mode? */
161 if ((input_file
= fdopen(fileno(stdin
), READ_BINARY
)) == NULL
) {
162 fprintf(stderr
, "Cannot reopen stdin\n");
173 FILE * output_file
= stdout
;
175 #ifdef USE_SETMODE /* need to hack file mode? */
176 setmode(fileno(stdout
), O_BINARY
);
178 #ifdef USE_FDOPEN /* need to re-open in binary mode? */
179 if ((output_file
= fdopen(fileno(stdout
), WRITE_BINARY
)) == NULL
) {
180 fprintf(stderr
, "Cannot reopen stdout\n");