cdjpeg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * cdjpeg.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains common support routines used by the IJG application
  9. * programs (cjpeg, djpeg, jpegtran).
  10. */
  11. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  12. #include <ctype.h> /* to declare isupper(), tolower() */
  13. #ifdef NEED_SIGNAL_CATCHER
  14. #include <signal.h> /* to declare signal() */
  15. #endif
  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. * Signal catcher to ensure that temporary files are removed before aborting.
  23. * NB: for Amiga Manx C this is actually a global routine named _abort();
  24. * we put "#define signal_catcher _abort" in jconfig.h. Talk about bogus...
  25. */
  26. #ifdef NEED_SIGNAL_CATCHER
  27. static j_common_ptr sig_cinfo;
  28. void /* must be global for Manx C */
  29. signal_catcher (int signum)
  30. {
  31. if (sig_cinfo != NULL) {
  32. if (sig_cinfo->err != NULL) /* turn off trace output */
  33. sig_cinfo->err->trace_level = 0;
  34. jpeg_destroy(sig_cinfo); /* clean up memory allocation & temp files */
  35. }
  36. exit(EXIT_FAILURE);
  37. }
  38. GLOBAL(void)
  39. enable_signal_catcher (j_common_ptr cinfo)
  40. {
  41. sig_cinfo = cinfo;
  42. #ifdef SIGINT /* not all systems have SIGINT */
  43. signal(SIGINT, signal_catcher);
  44. #endif
  45. #ifdef SIGTERM /* not all systems have SIGTERM */
  46. signal(SIGTERM, signal_catcher);
  47. #endif
  48. }
  49. #endif
  50. /*
  51. * Optional progress monitor: display a percent-done figure on stderr.
  52. */
  53. #ifdef PROGRESS_REPORT
  54. METHODDEF(void)
  55. progress_monitor (j_common_ptr cinfo)
  56. {
  57. cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
  58. int total_passes = prog->pub.total_passes + prog->total_extra_passes;
  59. int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
  60. if (percent_done != prog->percent_done) {
  61. prog->percent_done = percent_done;
  62. if (total_passes > 1) {
  63. fprintf(stderr, "\rPass %d/%d: %3d%% ",
  64. prog->pub.completed_passes + prog->completed_extra_passes + 1,
  65. total_passes, percent_done);
  66. } else {
  67. fprintf(stderr, "\r %3d%% ", percent_done);
  68. }
  69. fflush(stderr);
  70. }
  71. }
  72. GLOBAL(void)
  73. start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
  74. {
  75. /* Enable progress display, unless trace output is on */
  76. if (cinfo->err->trace_level == 0) {
  77. progress->pub.progress_monitor = progress_monitor;
  78. progress->completed_extra_passes = 0;
  79. progress->total_extra_passes = 0;
  80. progress->percent_done = -1;
  81. cinfo->progress = &progress->pub;
  82. }
  83. }
  84. GLOBAL(void)
  85. end_progress_monitor (j_common_ptr cinfo)
  86. {
  87. /* Clear away progress display */
  88. if (cinfo->err->trace_level == 0) {
  89. fprintf(stderr, "\r \r");
  90. fflush(stderr);
  91. }
  92. }
  93. #endif
  94. /*
  95. * Case-insensitive matching of possibly-abbreviated keyword switches.
  96. * keyword is the constant keyword (must be lower case already),
  97. * minchars is length of minimum legal abbreviation.
  98. */
  99. GLOBAL(boolean)
  100. keymatch (char * arg, const char * keyword, int minchars)
  101. {
  102. register int ca, ck;
  103. register int nmatched = 0;
  104. while ((ca = *arg++) != '\0') {
  105. if ((ck = *keyword++) == '\0')
  106. return FALSE; /* arg longer than keyword, no good */
  107. if (isupper(ca)) /* force arg to lcase (assume ck is already) */
  108. ca = tolower(ca);
  109. if (ca != ck)
  110. return FALSE; /* no good */
  111. nmatched++; /* count matched characters */
  112. }
  113. /* reached end of argument; fail if it's too short for unique abbrev */
  114. if (nmatched < minchars)
  115. return FALSE;
  116. return TRUE; /* A-OK */
  117. }
  118. /*
  119. * Routines to establish binary I/O mode for stdin and stdout.
  120. * Non-Unix systems often require some hacking to get out of text mode.
  121. */
  122. GLOBAL(FILE *)
  123. read_stdin (void)
  124. {
  125. FILE * input_file = stdin;
  126. #ifdef USE_SETMODE /* need to hack file mode? */
  127. setmode(fileno(stdin), O_BINARY);
  128. #endif
  129. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  130. if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  131. fprintf(stderr, "Cannot reopen stdin\n");
  132. exit(EXIT_FAILURE);
  133. }
  134. #endif
  135. return input_file;
  136. }
  137. GLOBAL(FILE *)
  138. write_stdout (void)
  139. {
  140. FILE * output_file = stdout;
  141. #ifdef USE_SETMODE /* need to hack file mode? */
  142. setmode(fileno(stdout), O_BINARY);
  143. #endif
  144. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  145. if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
  146. fprintf(stderr, "Cannot reopen stdout\n");
  147. exit(EXIT_FAILURE);
  148. }
  149. #endif
  150. return output_file;
  151. }