cdjpeg.c 4.3 KB

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