Leaked source code of windows server 2003
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.

256 lines
7.6 KiB

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