Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * cdjpeg.c |
| 3 | * |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 6 | * It was modified by The libjpeg-turbo Project to include only code relevant |
| 7 | * to libjpeg-turbo. |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * This file contains common support routines used by the IJG application |
| 12 | * programs (cjpeg, djpeg, jpegtran). |
| 13 | */ |
| 14 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 15 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 16 | #include <ctype.h> /* to declare isupper(), tolower() */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 17 | #ifdef USE_SETMODE |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 18 | #include <fcntl.h> /* to declare setmode()'s parameter macros */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 19 | /* If you have setmode() but not <io.h>, just delete this line: */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 20 | #include <io.h> /* to declare setmode() */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | |
| 24 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 25 | * Optional progress monitor: display a percent-done figure on stderr. |
| 26 | */ |
| 27 | |
| 28 | #ifdef PROGRESS_REPORT |
| 29 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 30 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 31 | progress_monitor(j_common_ptr cinfo) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 32 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 33 | cd_progress_ptr prog = (cd_progress_ptr)cinfo->progress; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 34 | int total_passes = prog->pub.total_passes + prog->total_extra_passes; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 35 | int percent_done = |
| 36 | (int)(prog->pub.pass_counter * 100L / prog->pub.pass_limit); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 37 | |
| 38 | if (percent_done != prog->percent_done) { |
| 39 | prog->percent_done = percent_done; |
| 40 | if (total_passes > 1) { |
| 41 | fprintf(stderr, "\rPass %d/%d: %3d%% ", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 42 | prog->pub.completed_passes + prog->completed_extra_passes + 1, |
| 43 | total_passes, percent_done); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 44 | } else { |
| 45 | fprintf(stderr, "\r %3d%% ", percent_done); |
| 46 | } |
| 47 | fflush(stderr); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 52 | GLOBAL(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 53 | start_progress_monitor(j_common_ptr cinfo, cd_progress_ptr progress) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 54 | { |
| 55 | /* Enable progress display, unless trace output is on */ |
| 56 | if (cinfo->err->trace_level == 0) { |
| 57 | progress->pub.progress_monitor = progress_monitor; |
| 58 | progress->completed_extra_passes = 0; |
| 59 | progress->total_extra_passes = 0; |
| 60 | progress->percent_done = -1; |
| 61 | cinfo->progress = &progress->pub; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 66 | GLOBAL(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 67 | end_progress_monitor(j_common_ptr cinfo) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 68 | { |
| 69 | /* Clear away progress display */ |
| 70 | if (cinfo->err->trace_level == 0) { |
| 71 | fprintf(stderr, "\r \r"); |
| 72 | fflush(stderr); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Case-insensitive matching of possibly-abbreviated keyword switches. |
| 81 | * keyword is the constant keyword (must be lower case already), |
| 82 | * minchars is length of minimum legal abbreviation. |
| 83 | */ |
| 84 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 85 | GLOBAL(boolean) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 86 | keymatch(char *arg, const char *keyword, int minchars) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 87 | { |
| 88 | register int ca, ck; |
| 89 | register int nmatched = 0; |
| 90 | |
| 91 | while ((ca = *arg++) != '\0') { |
| 92 | if ((ck = *keyword++) == '\0') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 93 | return FALSE; /* arg longer than keyword, no good */ |
| 94 | if (isupper(ca)) /* force arg to lcase (assume ck is already) */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 95 | ca = tolower(ca); |
| 96 | if (ca != ck) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 97 | return FALSE; /* no good */ |
| 98 | nmatched++; /* count matched characters */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 99 | } |
| 100 | /* reached end of argument; fail if it's too short for unique abbrev */ |
| 101 | if (nmatched < minchars) |
| 102 | return FALSE; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 103 | return TRUE; /* A-OK */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | * Routines to establish binary I/O mode for stdin and stdout. |
| 109 | * Non-Unix systems often require some hacking to get out of text mode. |
| 110 | */ |
| 111 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 112 | GLOBAL(FILE *) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 113 | read_stdin(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 114 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 115 | FILE *input_file = stdin; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 116 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 117 | #ifdef USE_SETMODE /* need to hack file mode? */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 118 | setmode(fileno(stdin), O_BINARY); |
| 119 | #endif |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 120 | #ifdef USE_FDOPEN /* need to re-open in binary mode? */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 121 | if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) { |
| 122 | fprintf(stderr, "Cannot reopen stdin\n"); |
| 123 | exit(EXIT_FAILURE); |
| 124 | } |
| 125 | #endif |
| 126 | return input_file; |
| 127 | } |
| 128 | |
| 129 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 130 | GLOBAL(FILE *) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 131 | write_stdout(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 132 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 133 | FILE *output_file = stdout; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 134 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 135 | #ifdef USE_SETMODE /* need to hack file mode? */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 136 | setmode(fileno(stdout), O_BINARY); |
| 137 | #endif |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 138 | #ifdef USE_FDOPEN /* need to re-open in binary mode? */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 139 | if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) { |
| 140 | fprintf(stderr, "Cannot reopen stdout\n"); |
| 141 | exit(EXIT_FAILURE); |
| 142 | } |
| 143 | #endif |
| 144 | return output_file; |
| 145 | } |