cdjpeg.c 3.9 KB

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