blob: 7cc0d6e832800b872acfd025ed20c044559b63b3 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * cdjpeg.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains common support routines used by the IJG application
11 * programs (cjpeg, djpeg, jpegtran).
12 */
13
DRCe5eaf372014-05-09 18:00:32 +000014#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
15#include <ctype.h> /* to declare isupper(), tolower() */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000016#ifdef USE_SETMODE
DRCe5eaf372014-05-09 18:00:32 +000017#include <fcntl.h> /* to declare setmode()'s parameter macros */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000018/* If you have setmode() but not <io.h>, just delete this line: */
DRCe5eaf372014-05-09 18:00:32 +000019#include <io.h> /* to declare setmode() */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000020#endif
21
22
23/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000024 * Optional progress monitor: display a percent-done figure on stderr.
25 */
26
27#ifdef PROGRESS_REPORT
28
Thomas G. Lane489583f1996-02-07 00:00:00 +000029METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000030progress_monitor (j_common_ptr cinfo)
31{
32 cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
33 int total_passes = prog->pub.total_passes + prog->total_extra_passes;
34 int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
35
36 if (percent_done != prog->percent_done) {
37 prog->percent_done = percent_done;
38 if (total_passes > 1) {
39 fprintf(stderr, "\rPass %d/%d: %3d%% ",
DRCe5eaf372014-05-09 18:00:32 +000040 prog->pub.completed_passes + prog->completed_extra_passes + 1,
41 total_passes, percent_done);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000042 } else {
43 fprintf(stderr, "\r %3d%% ", percent_done);
44 }
45 fflush(stderr);
46 }
47}
48
49
Thomas G. Lane489583f1996-02-07 00:00:00 +000050GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000051start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
52{
53 /* Enable progress display, unless trace output is on */
54 if (cinfo->err->trace_level == 0) {
55 progress->pub.progress_monitor = progress_monitor;
56 progress->completed_extra_passes = 0;
57 progress->total_extra_passes = 0;
58 progress->percent_done = -1;
59 cinfo->progress = &progress->pub;
60 }
61}
62
63
Thomas G. Lane489583f1996-02-07 00:00:00 +000064GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000065end_progress_monitor (j_common_ptr cinfo)
66{
67 /* Clear away progress display */
68 if (cinfo->err->trace_level == 0) {
69 fprintf(stderr, "\r \r");
70 fflush(stderr);
71 }
72}
73
74#endif
75
76
77/*
78 * Case-insensitive matching of possibly-abbreviated keyword switches.
79 * keyword is the constant keyword (must be lower case already),
80 * minchars is length of minimum legal abbreviation.
81 */
82
Thomas G. Lane489583f1996-02-07 00:00:00 +000083GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000084keymatch (char * arg, const char * keyword, int minchars)
85{
86 register int ca, ck;
87 register int nmatched = 0;
88
89 while ((ca = *arg++) != '\0') {
90 if ((ck = *keyword++) == '\0')
DRCe5eaf372014-05-09 18:00:32 +000091 return FALSE; /* arg longer than keyword, no good */
92 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000093 ca = tolower(ca);
94 if (ca != ck)
DRCe5eaf372014-05-09 18:00:32 +000095 return FALSE; /* no good */
96 nmatched++; /* count matched characters */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097 }
98 /* reached end of argument; fail if it's too short for unique abbrev */
99 if (nmatched < minchars)
100 return FALSE;
DRCe5eaf372014-05-09 18:00:32 +0000101 return TRUE; /* A-OK */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000102}
103
104
105/*
106 * Routines to establish binary I/O mode for stdin and stdout.
107 * Non-Unix systems often require some hacking to get out of text mode.
108 */
109
Thomas G. Lane489583f1996-02-07 00:00:00 +0000110GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000111read_stdin (void)
112{
113 FILE * input_file = stdin;
114
DRCe5eaf372014-05-09 18:00:32 +0000115#ifdef USE_SETMODE /* need to hack file mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000116 setmode(fileno(stdin), O_BINARY);
117#endif
DRCe5eaf372014-05-09 18:00:32 +0000118#ifdef USE_FDOPEN /* need to re-open in binary mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000119 if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
120 fprintf(stderr, "Cannot reopen stdin\n");
121 exit(EXIT_FAILURE);
122 }
123#endif
124 return input_file;
125}
126
127
Thomas G. Lane489583f1996-02-07 00:00:00 +0000128GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000129write_stdout (void)
130{
131 FILE * output_file = stdout;
132
DRCe5eaf372014-05-09 18:00:32 +0000133#ifdef USE_SETMODE /* need to hack file mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000134 setmode(fileno(stdout), O_BINARY);
135#endif
DRCe5eaf372014-05-09 18:00:32 +0000136#ifdef USE_FDOPEN /* need to re-open in binary mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000137 if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
138 fprintf(stderr, "Cannot reopen stdout\n");
139 exit(EXIT_FAILURE);
140 }
141#endif
142 return output_file;
143}