Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.8 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jdatadst.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains compression data destination routines for the case of
  11. * emitting JPEG data to a file (or any stdio stream). While these routines
  12. * are sufficient for most applications, some will want to use a different
  13. * destination manager.
  14. * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
  15. * JOCTETs into 8-bit-wide elements on external storage. If char is wider
  16. * than 8 bits on your machine, you may need to do some tweaking.
  17. */
  18. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. #include "jerror.h"
  22. /* Expanded data destination object for stdio output */
  23. typedef struct {
  24. struct jpeg_destination_mgr pub; /* public fields */
  25. FILE * outfile; /* target stream */
  26. JOCTET * buffer; /* start of buffer */
  27. } my_destination_mgr;
  28. typedef my_destination_mgr * my_dest_ptr;
  29. #ifdef NIFTY
  30. typedef struct {
  31. struct jpeg_destination_mgr pub; /* public fields */
  32. int bufUsed;
  33. JOCTET *NIFbuffer;
  34. JOCTET *buffer;
  35. } nif_destination_mgr;
  36. typedef nif_destination_mgr *nif_dest_ptr;
  37. #endif
  38. #ifdef NIFTY
  39. //#define OUTPUT_BUF_SIZE 16384 /* choose a much larger in memory buffer */
  40. #define OUTPUT_BUF_SIZE 60000 /* choose a much larger in memory buffer */
  41. #else
  42. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  43. #endif
  44. /*
  45. * Initialize destination --- called by jpeg_start_compress
  46. * before any data is actually written.
  47. */
  48. METHODDEF(void)
  49. init_destination (j_compress_ptr cinfo)
  50. {
  51. #ifdef NIFTY
  52. nif_dest_ptr dest = (nif_dest_ptr) cinfo->dest;
  53. #else
  54. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  55. #endif
  56. /* Allocate the output buffer --- it will be released when done with image */
  57. dest->buffer = (JOCTET *)
  58. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  59. OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
  60. dest->pub.next_output_byte = dest->buffer;
  61. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  62. }
  63. /*
  64. * Empty the output buffer --- called whenever buffer fills up.
  65. *
  66. * In typical applications, this should write the entire output buffer
  67. * (ignoring the current state of next_output_byte & free_in_buffer),
  68. * reset the pointer & count to the start of the buffer, and return TRUE
  69. * indicating that the buffer has been dumped.
  70. *
  71. * In applications that need to be able to suspend compression due to output
  72. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  73. * In this situation, the compressor will return to its caller (possibly with
  74. * an indication that it has not accepted all the supplied scanlines). The
  75. * application should resume compression after it has made more room in the
  76. * output buffer. Note that there are substantial restrictions on the use of
  77. * suspension --- see the documentation.
  78. *
  79. * When suspending, the compressor will back up to a convenient restart point
  80. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  81. * indicate where the restart point will be if the current call returns FALSE.
  82. * Data beyond this point will be regenerated after resumption, so do not
  83. * write it out when emptying the buffer externally.
  84. */
  85. #ifdef NIFTY
  86. METHODDEF (boolean)
  87. empty_output_buffer (j_compress_ptr cinfo)
  88. {
  89. nif_dest_ptr dest = (nif_dest_ptr) cinfo->dest;
  90. cinfo->bytes_in_buffer+=OUTPUT_BUF_SIZE;
  91. (void)memcpy(dest->NIFbuffer+dest->bufUsed, dest->buffer, OUTPUT_BUF_SIZE);
  92. dest->bufUsed += OUTPUT_BUF_SIZE;
  93. dest->pub.next_output_byte = dest->buffer;
  94. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  95. return TRUE;
  96. }
  97. #else
  98. METHODDEF(boolean)
  99. empty_output_buffer (j_compress_ptr cinfo)
  100. {
  101. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  102. if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
  103. (size_t) OUTPUT_BUF_SIZE)
  104. ERREXIT(cinfo, JERR_FILE_WRITE);
  105. dest->pub.next_output_byte = dest->buffer;
  106. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  107. return TRUE;
  108. }
  109. #endif
  110. /*
  111. * Terminate destination --- called by jpeg_finish_compress
  112. * after all data has been written. Usually needs to flush buffer.
  113. *
  114. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  115. * application must deal with any cleanup that should happen even
  116. * for error exit.
  117. */
  118. #ifdef NIFTY
  119. METHODDEF (void)
  120. term_destination (j_compress_ptr cinfo)
  121. {
  122. nif_dest_ptr dest = (nif_dest_ptr) cinfo->dest;
  123. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  124. cinfo->bytes_in_buffer+=datacount;
  125. (void)memcpy(dest->NIFbuffer+dest->bufUsed, dest->buffer, datacount);
  126. }
  127. #else
  128. METHODDEF(void)
  129. term_destination (j_compress_ptr cinfo)
  130. {
  131. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  132. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  133. /* Write any data remaining in the buffer */
  134. if (datacount > 0) {
  135. if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  136. ERREXIT(cinfo, JERR_FILE_WRITE);
  137. }
  138. fflush(dest->outfile);
  139. /* Make sure we wrote the output file OK */
  140. if (ferror(dest->outfile))
  141. ERREXIT(cinfo, JERR_FILE_WRITE);
  142. }
  143. #endif
  144. /*
  145. * Prepare for output to a stdio stream.
  146. * The caller must have already opened the stream, and is responsible
  147. * for closing it after finishing compression.
  148. */
  149. #ifdef NIFTY
  150. GLOBAL (void)
  151. jpeg_mem_dest(j_compress_ptr cinfo, JOCTET *UserBuffer)
  152. {
  153. nif_dest_ptr dest;
  154. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  155. cinfo->dest = (struct jpeg_destination_mgr *)
  156. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  157. SIZEOF(nif_destination_mgr));
  158. }
  159. dest = (nif_dest_ptr) cinfo->dest;
  160. dest->pub.init_destination = init_destination;
  161. dest->pub.empty_output_buffer = empty_output_buffer;
  162. dest->pub.term_destination = term_destination;
  163. dest->NIFbuffer = UserBuffer;
  164. dest->bufUsed = 0;
  165. }
  166. #else
  167. GLOBAL(void)
  168. jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
  169. {
  170. my_dest_ptr dest;
  171. /* The destination object is made permanent so that multiple JPEG images
  172. * can be written to the same file without re-executing jpeg_stdio_dest.
  173. * This makes it dangerous to use this manager and a different destination
  174. * manager serially with the same JPEG object, because their private object
  175. * sizes may be different. Caveat programmer.
  176. */
  177. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  178. cinfo->dest = (struct jpeg_destination_mgr *)
  179. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  180. SIZEOF(my_destination_mgr));
  181. }
  182. dest = (my_dest_ptr) cinfo->dest;
  183. dest->pub.init_destination = init_destination;
  184. dest->pub.empty_output_buffer = empty_output_buffer;
  185. dest->pub.term_destination = term_destination;
  186. dest->outfile = outfile;
  187. }
  188. #endif