blob: a3d6e067555497d4056ba0b51f125a5836d4a3eb [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * cdjpeg.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1991-1996, Thomas G. Lane.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00005 * 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
12#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
13#include <ctype.h> /* to declare isupper(), tolower() */
14#ifdef NEED_SIGNAL_CATCHER
15#include <signal.h> /* to declare signal() */
16#endif
17#ifdef USE_SETMODE
18#include <fcntl.h> /* to declare setmode()'s parameter macros */
19/* If you have setmode() but not <io.h>, just delete this line: */
20#include <io.h> /* to declare setmode() */
21#endif
22
23
24/*
25 * Signal catcher to ensure that temporary files are removed before aborting.
26 * NB: for Amiga Manx C this is actually a global routine named _abort();
27 * we put "#define signal_catcher _abort" in jconfig.h. Talk about bogus...
28 */
29
30#ifdef NEED_SIGNAL_CATCHER
31
32static j_common_ptr sig_cinfo;
33
Thomas G. Lane489583f1996-02-07 00:00:00 +000034void /* must be global for Manx C */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000035signal_catcher (int signum)
36{
37 if (sig_cinfo != NULL) {
38 if (sig_cinfo->err != NULL) /* turn off trace output */
39 sig_cinfo->err->trace_level = 0;
40 jpeg_destroy(sig_cinfo); /* clean up memory allocation & temp files */
41 }
42 exit(EXIT_FAILURE);
43}
44
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047enable_signal_catcher (j_common_ptr cinfo)
48{
49 sig_cinfo = cinfo;
50 signal(SIGINT, signal_catcher);
51#ifdef SIGTERM /* not all systems have SIGTERM */
52 signal(SIGTERM, signal_catcher);
53#endif
54}
55
56#endif
57
58
59/*
60 * Optional progress monitor: display a percent-done figure on stderr.
61 */
62
63#ifdef PROGRESS_REPORT
64
Thomas G. Lane489583f1996-02-07 00:00:00 +000065METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000066progress_monitor (j_common_ptr cinfo)
67{
68 cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
69 int total_passes = prog->pub.total_passes + prog->total_extra_passes;
70 int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
71
72 if (percent_done != prog->percent_done) {
73 prog->percent_done = percent_done;
74 if (total_passes > 1) {
75 fprintf(stderr, "\rPass %d/%d: %3d%% ",
76 prog->pub.completed_passes + prog->completed_extra_passes + 1,
77 total_passes, percent_done);
78 } else {
79 fprintf(stderr, "\r %3d%% ", percent_done);
80 }
81 fflush(stderr);
82 }
83}
84
85
Thomas G. Lane489583f1996-02-07 00:00:00 +000086GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000087start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
88{
89 /* Enable progress display, unless trace output is on */
90 if (cinfo->err->trace_level == 0) {
91 progress->pub.progress_monitor = progress_monitor;
92 progress->completed_extra_passes = 0;
93 progress->total_extra_passes = 0;
94 progress->percent_done = -1;
95 cinfo->progress = &progress->pub;
96 }
97}
98
99
Thomas G. Lane489583f1996-02-07 00:00:00 +0000100GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000101end_progress_monitor (j_common_ptr cinfo)
102{
103 /* Clear away progress display */
104 if (cinfo->err->trace_level == 0) {
105 fprintf(stderr, "\r \r");
106 fflush(stderr);
107 }
108}
109
110#endif
111
112
113/*
114 * Case-insensitive matching of possibly-abbreviated keyword switches.
115 * keyword is the constant keyword (must be lower case already),
116 * minchars is length of minimum legal abbreviation.
117 */
118
Thomas G. Lane489583f1996-02-07 00:00:00 +0000119GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000120keymatch (char * arg, const char * keyword, int minchars)
121{
122 register int ca, ck;
123 register int nmatched = 0;
124
125 while ((ca = *arg++) != '\0') {
126 if ((ck = *keyword++) == '\0')
127 return FALSE; /* arg longer than keyword, no good */
128 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
129 ca = tolower(ca);
130 if (ca != ck)
131 return FALSE; /* no good */
132 nmatched++; /* count matched characters */
133 }
134 /* reached end of argument; fail if it's too short for unique abbrev */
135 if (nmatched < minchars)
136 return FALSE;
137 return TRUE; /* A-OK */
138}
139
140
141/*
142 * Routines to establish binary I/O mode for stdin and stdout.
143 * Non-Unix systems often require some hacking to get out of text mode.
144 */
145
Thomas G. Lane489583f1996-02-07 00:00:00 +0000146GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147read_stdin (void)
148{
149 FILE * input_file = stdin;
150
151#ifdef USE_SETMODE /* need to hack file mode? */
152 setmode(fileno(stdin), O_BINARY);
153#endif
154#ifdef USE_FDOPEN /* need to re-open in binary mode? */
155 if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
156 fprintf(stderr, "Cannot reopen stdin\n");
157 exit(EXIT_FAILURE);
158 }
159#endif
160 return input_file;
161}
162
163
Thomas G. Lane489583f1996-02-07 00:00:00 +0000164GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000165write_stdout (void)
166{
167 FILE * output_file = stdout;
168
169#ifdef USE_SETMODE /* need to hack file mode? */
170 setmode(fileno(stdout), O_BINARY);
171#endif
172#ifdef USE_FDOPEN /* need to re-open in binary mode? */
173 if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
174 fprintf(stderr, "Cannot reopen stdout\n");
175 exit(EXIT_FAILURE);
176 }
177#endif
178 return output_file;
179}