| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "cdjpeg.h" |
| 13 |
#include <ctype.h> |
| 14 |
#ifdef NEED_SIGNAL_CATCHER |
| 15 |
#include <signal.h> |
| 16 |
#endif |
| 17 |
#ifdef USE_SETMODE |
| 18 |
#include <fcntl.h> |
| 19 |
|
| 20 |
#include <io.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifdef NEED_SIGNAL_CATCHER |
| 31 |
|
| 32 |
static j_common_ptr sig_cinfo; |
| 33 |
|
| 34 |
void |
| 35 |
signal_catcher (int signum) |
| 36 |
{ |
| 37 |
if (sig_cinfo != NULL) { |
| 38 |
if (sig_cinfo->err != NULL) |
| 39 |
sig_cinfo->err->trace_level = 0; |
| 40 |
jpeg_destroy(sig_cinfo); |
| 41 |
} |
| 42 |
exit(EXIT_FAILURE); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
GLOBAL(void) |
| 47 |
enable_signal_catcher (j_common_ptr cinfo) |
| 48 |
{ |
| 49 |
sig_cinfo = cinfo; |
| 50 |
#ifdef SIGINT |
| 51 |
signal(SIGINT, signal_catcher); |
| 52 |
#endif |
| 53 |
#ifdef SIGTERM |
| 54 |
signal(SIGTERM, signal_catcher); |
| 55 |
#endif |
| 56 |
} |
| 57 |
|
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#ifdef PROGRESS_REPORT |
| 66 |
|
| 67 |
METHODDEF(void) |
| 68 |
progress_monitor (j_common_ptr cinfo) |
| 69 |
{ |
| 70 |
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress; |
| 71 |
int total_passes = prog->pub.total_passes + prog->total_extra_passes; |
| 72 |
int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit); |
| 73 |
|
| 74 |
if (percent_done != prog->percent_done) { |
| 75 |
prog->percent_done = percent_done; |
| 76 |
if (total_passes > 1) { |
| 77 |
fprintf(stderr, "\rPass %d/%d: %3d%% ", |
| 78 |
prog->pub.completed_passes + prog->completed_extra_passes + 1, |
| 79 |
total_passes, percent_done); |
| 80 |
} else { |
| 81 |
fprintf(stderr, "\r %3d%% ", percent_done); |
| 82 |
} |
| 83 |
fflush(stderr); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
GLOBAL(void) |
| 89 |
start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress) |
| 90 |
{ |
| 91 |
|
| 92 |
if (cinfo->err->trace_level == 0) { |
| 93 |
progress->pub.progress_monitor = progress_monitor; |
| 94 |
progress->completed_extra_passes = 0; |
| 95 |
progress->total_extra_passes = 0; |
| 96 |
progress->percent_done = -1; |
| 97 |
cinfo->progress = &progress->pub; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
GLOBAL(void) |
| 103 |
end_progress_monitor (j_common_ptr cinfo) |
| 104 |
{ |
| 105 |
|
| 106 |
if (cinfo->err->trace_level == 0) { |
| 107 |
fprintf(stderr, "\r \r"); |
| 108 |
fflush(stderr); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
GLOBAL(boolean) |
| 122 |
keymatch (char * arg, const char * keyword, int minchars) |
| 123 |
{ |
| 124 |
register int ca, ck; |
| 125 |
register int nmatched = 0; |
| 126 |
|
| 127 |
while ((ca = *arg++) != '\0') { |
| 128 |
if ((ck = *keyword++) == '\0') |
| 129 |
return FALSE; |
| 130 |
if (isupper(ca)) |
| 131 |
ca = tolower(ca); |
| 132 |
if (ca != ck) |
| 133 |
return FALSE; |
| 134 |
nmatched++; |
| 135 |
} |
| 136 |
|
| 137 |
if (nmatched < minchars) |
| 138 |
return FALSE; |
| 139 |
return TRUE; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
GLOBAL(FILE *) |
| 149 |
read_stdin (void) |
| 150 |
{ |
| 151 |
FILE * input_file = stdin; |
| 152 |
|
| 153 |
#ifdef USE_SETMODE |
| 154 |
setmode(fileno(stdin), O_BINARY); |
| 155 |
#endif |
| 156 |
#ifdef USE_FDOPEN |
| 157 |
if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) { |
| 158 |
fprintf(stderr, "Cannot reopen stdin\n"); |
| 159 |
exit(EXIT_FAILURE); |
| 160 |
} |
| 161 |
#endif |
| 162 |
return input_file; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
GLOBAL(FILE *) |
| 167 |
write_stdout (void) |
| 168 |
{ |
| 169 |
FILE * output_file = stdout; |
| 170 |
|
| 171 |
#ifdef USE_SETMODE |
| 172 |
setmode(fileno(stdout), O_BINARY); |
| 173 |
#endif |
| 174 |
#ifdef USE_FDOPEN |
| 175 |
if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) { |
| 176 |
fprintf(stderr, "Cannot reopen stdout\n"); |
| 177 |
exit(EXIT_FAILURE); |
| 178 |
} |
| 179 |
#endif |
| 180 |
return output_file; |
| 181 |
} |