blob: 441d67143ab683bc2295fa79bc5a81c6bdecc20a [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.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000010 *
11 * This file contains common support routines used by the IJG application
12 * programs (cjpeg, djpeg, jpegtran).
13 */
14
DRCe5eaf372014-05-09 18:00:32 +000015#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
16#include <ctype.h> /* to declare isupper(), tolower() */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000017#ifdef USE_SETMODE
DRCe5eaf372014-05-09 18:00:32 +000018#include <fcntl.h> /* to declare setmode()'s parameter macros */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000019/* If you have setmode() but not <io.h>, just delete this line: */
DRCe5eaf372014-05-09 18:00:32 +000020#include <io.h> /* to declare setmode() */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000021#endif
22
23
24/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000025 * Optional progress monitor: display a percent-done figure on stderr.
26 */
27
28#ifdef PROGRESS_REPORT
29
Thomas G. Lane489583f1996-02-07 00:00:00 +000030METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031progress_monitor (j_common_ptr cinfo)
32{
33 cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
34 int total_passes = prog->pub.total_passes + prog->total_extra_passes;
35 int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
36
37 if (percent_done != prog->percent_done) {
38 prog->percent_done = percent_done;
39 if (total_passes > 1) {
40 fprintf(stderr, "\rPass %d/%d: %3d%% ",
DRCe5eaf372014-05-09 18:00:32 +000041 prog->pub.completed_passes + prog->completed_extra_passes + 1,
42 total_passes, percent_done);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000043 } else {
44 fprintf(stderr, "\r %3d%% ", percent_done);
45 }
46 fflush(stderr);
47 }
48}
49
50
Thomas G. Lane489583f1996-02-07 00:00:00 +000051GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000052start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
53{
54 /* Enable progress display, unless trace output is on */
55 if (cinfo->err->trace_level == 0) {
56 progress->pub.progress_monitor = progress_monitor;
57 progress->completed_extra_passes = 0;
58 progress->total_extra_passes = 0;
59 progress->percent_done = -1;
60 cinfo->progress = &progress->pub;
61 }
62}
63
64
Thomas G. Lane489583f1996-02-07 00:00:00 +000065GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000066end_progress_monitor (j_common_ptr cinfo)
67{
68 /* Clear away progress display */
69 if (cinfo->err->trace_level == 0) {
70 fprintf(stderr, "\r \r");
71 fflush(stderr);
72 }
73}
74
75#endif
76
77
78/*
79 * Case-insensitive matching of possibly-abbreviated keyword switches.
80 * keyword is the constant keyword (must be lower case already),
81 * minchars is length of minimum legal abbreviation.
82 */
83
Thomas G. Lane489583f1996-02-07 00:00:00 +000084GLOBAL(boolean)
DRCbd498032016-02-19 08:53:33 -060085keymatch (char *arg, const char *keyword, int minchars)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000086{
87 register int ca, ck;
88 register int nmatched = 0;
89
90 while ((ca = *arg++) != '\0') {
91 if ((ck = *keyword++) == '\0')
DRCe5eaf372014-05-09 18:00:32 +000092 return FALSE; /* arg longer than keyword, no good */
93 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000094 ca = tolower(ca);
95 if (ca != ck)
DRCe5eaf372014-05-09 18:00:32 +000096 return FALSE; /* no good */
97 nmatched++; /* count matched characters */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000098 }
99 /* reached end of argument; fail if it's too short for unique abbrev */
100 if (nmatched < minchars)
101 return FALSE;
DRCe5eaf372014-05-09 18:00:32 +0000102 return TRUE; /* A-OK */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000103}
104
105
106/*
107 * Routines to establish binary I/O mode for stdin and stdout.
108 * Non-Unix systems often require some hacking to get out of text mode.
109 */
110
Thomas G. Lane489583f1996-02-07 00:00:00 +0000111GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000112read_stdin (void)
113{
114 FILE * input_file = stdin;
115
DRCe5eaf372014-05-09 18:00:32 +0000116#ifdef USE_SETMODE /* need to hack file mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000117 setmode(fileno(stdin), O_BINARY);
118#endif
DRCe5eaf372014-05-09 18:00:32 +0000119#ifdef USE_FDOPEN /* need to re-open in binary mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000120 if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
121 fprintf(stderr, "Cannot reopen stdin\n");
122 exit(EXIT_FAILURE);
123 }
124#endif
125 return input_file;
126}
127
128
Thomas G. Lane489583f1996-02-07 00:00:00 +0000129GLOBAL(FILE *)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000130write_stdout (void)
131{
132 FILE * output_file = stdout;
133
DRCe5eaf372014-05-09 18:00:32 +0000134#ifdef USE_SETMODE /* need to hack file mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000135 setmode(fileno(stdout), O_BINARY);
136#endif
DRCe5eaf372014-05-09 18:00:32 +0000137#ifdef USE_FDOPEN /* need to re-open in binary mode? */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000138 if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
139 fprintf(stderr, "Cannot reopen stdout\n");
140 exit(EXIT_FAILURE);
141 }
142#endif
143 return output_file;
144}