cdjpeg.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * It was modified by The libjpeg-turbo Project to include only code relevant
  7. * to libjpeg-turbo.
  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. #ifdef PROGRESS_REPORT
  25. METHODDEF(void)
  26. progress_monitor(j_common_ptr cinfo)
  27. {
  28. cd_progress_ptr prog = (cd_progress_ptr)cinfo->progress;
  29. int total_passes = prog->pub.total_passes + prog->total_extra_passes;
  30. int percent_done =
  31. (int)(prog->pub.pass_counter * 100L / prog->pub.pass_limit);
  32. if (percent_done != prog->percent_done) {
  33. prog->percent_done = percent_done;
  34. if (total_passes > 1) {
  35. fprintf(stderr, "\rPass %d/%d: %3d%% ",
  36. prog->pub.completed_passes + prog->completed_extra_passes + 1,
  37. total_passes, percent_done);
  38. } else {
  39. fprintf(stderr, "\r %3d%% ", percent_done);
  40. }
  41. fflush(stderr);
  42. }
  43. }
  44. GLOBAL(void)
  45. start_progress_monitor(j_common_ptr cinfo, cd_progress_ptr progress)
  46. {
  47. /* Enable progress display, unless trace output is on */
  48. if (cinfo->err->trace_level == 0) {
  49. progress->pub.progress_monitor = progress_monitor;
  50. progress->completed_extra_passes = 0;
  51. progress->total_extra_passes = 0;
  52. progress->percent_done = -1;
  53. cinfo->progress = &progress->pub;
  54. }
  55. }
  56. GLOBAL(void)
  57. end_progress_monitor(j_common_ptr cinfo)
  58. {
  59. /* Clear away progress display */
  60. if (cinfo->err->trace_level == 0) {
  61. fprintf(stderr, "\r \r");
  62. fflush(stderr);
  63. }
  64. }
  65. #endif
  66. /*
  67. * Case-insensitive matching of possibly-abbreviated keyword switches.
  68. * keyword is the constant keyword (must be lower case already),
  69. * minchars is length of minimum legal abbreviation.
  70. */
  71. GLOBAL(boolean)
  72. keymatch(char *arg, const char *keyword, int minchars)
  73. {
  74. register int ca, ck;
  75. register int nmatched = 0;
  76. while ((ca = *arg++) != '\0') {
  77. if ((ck = *keyword++) == '\0')
  78. return FALSE; /* arg longer than keyword, no good */
  79. if (isupper(ca)) /* force arg to lcase (assume ck is already) */
  80. ca = tolower(ca);
  81. if (ca != ck)
  82. return FALSE; /* no good */
  83. nmatched++; /* count matched characters */
  84. }
  85. /* reached end of argument; fail if it's too short for unique abbrev */
  86. if (nmatched < minchars)
  87. return FALSE;
  88. return TRUE; /* A-OK */
  89. }
  90. /*
  91. * Routines to establish binary I/O mode for stdin and stdout.
  92. * Non-Unix systems often require some hacking to get out of text mode.
  93. */
  94. GLOBAL(FILE *)
  95. read_stdin(void)
  96. {
  97. FILE *input_file = stdin;
  98. #ifdef USE_SETMODE /* need to hack file mode? */
  99. setmode(fileno(stdin), O_BINARY);
  100. #endif
  101. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  102. if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  103. fprintf(stderr, "Cannot reopen stdin\n");
  104. exit(EXIT_FAILURE);
  105. }
  106. #endif
  107. return input_file;
  108. }
  109. GLOBAL(FILE *)
  110. write_stdout(void)
  111. {
  112. FILE *output_file = stdout;
  113. #ifdef USE_SETMODE /* need to hack file mode? */
  114. setmode(fileno(stdout), O_BINARY);
  115. #endif
  116. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  117. if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
  118. fprintf(stderr, "Cannot reopen stdout\n");
  119. exit(EXIT_FAILURE);
  120. }
  121. #endif
  122. return output_file;
  123. }