Team Fortress 2 Source Code as on 22/4/2020
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.

267 lines
8.3 KiB

  1. /*
  2. * jdatadst.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2009 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains compression data destination routines for the case of
  10. * emitting JPEG data to memory or to a file (or any stdio stream).
  11. * While these routines are sufficient for most applications,
  12. * some will want to use a different destination manager.
  13. * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
  14. * JOCTETs into 8-bit-wide elements on external storage. If char is wider
  15. * than 8 bits on your machine, you may need to do some tweaking.
  16. */
  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. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  22. extern void * malloc JPP((size_t size));
  23. extern void free JPP((void *ptr));
  24. #endif
  25. /* Expanded data destination object for stdio output */
  26. typedef struct {
  27. struct jpeg_destination_mgr pub; /* public fields */
  28. FILE * outfile; /* target stream */
  29. JOCTET * buffer; /* start of buffer */
  30. } my_destination_mgr;
  31. typedef my_destination_mgr * my_dest_ptr;
  32. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  33. /* Expanded data destination object for memory output */
  34. typedef struct {
  35. struct jpeg_destination_mgr pub; /* public fields */
  36. unsigned char ** outbuffer; /* target buffer */
  37. unsigned long * outsize;
  38. unsigned char * newbuffer; /* newly allocated buffer */
  39. JOCTET * buffer; /* start of buffer */
  40. size_t bufsize;
  41. } my_mem_destination_mgr;
  42. typedef my_mem_destination_mgr * my_mem_dest_ptr;
  43. /*
  44. * Initialize destination --- called by jpeg_start_compress
  45. * before any data is actually written.
  46. */
  47. METHODDEF(void)
  48. init_destination (j_compress_ptr cinfo)
  49. {
  50. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  51. /* Allocate the output buffer --- it will be released when done with image */
  52. dest->buffer = (JOCTET *)
  53. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  54. OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
  55. dest->pub.next_output_byte = dest->buffer;
  56. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  57. }
  58. METHODDEF(void)
  59. init_mem_destination (j_compress_ptr cinfo)
  60. {
  61. /* no work necessary here */
  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. METHODDEF(boolean)
  86. empty_output_buffer (j_compress_ptr cinfo)
  87. {
  88. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  89. if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
  90. (size_t) OUTPUT_BUF_SIZE)
  91. ERREXIT(cinfo, JERR_FILE_WRITE);
  92. dest->pub.next_output_byte = dest->buffer;
  93. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  94. return TRUE;
  95. }
  96. METHODDEF(boolean)
  97. empty_mem_output_buffer (j_compress_ptr cinfo)
  98. {
  99. size_t nextsize;
  100. JOCTET * nextbuffer;
  101. my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
  102. /* Try to allocate new buffer with double size */
  103. nextsize = dest->bufsize * 2;
  104. nextbuffer = (JOCTET *)malloc(nextsize);
  105. if (nextbuffer == NULL)
  106. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  107. MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
  108. if (dest->newbuffer != NULL)
  109. free(dest->newbuffer);
  110. dest->newbuffer = nextbuffer;
  111. dest->pub.next_output_byte = nextbuffer + dest->bufsize;
  112. dest->pub.free_in_buffer = dest->bufsize;
  113. dest->buffer = nextbuffer;
  114. dest->bufsize = nextsize;
  115. return TRUE;
  116. }
  117. /*
  118. * Terminate destination --- called by jpeg_finish_compress
  119. * after all data has been written. Usually needs to flush buffer.
  120. *
  121. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  122. * application must deal with any cleanup that should happen even
  123. * for error exit.
  124. */
  125. METHODDEF(void)
  126. term_destination (j_compress_ptr cinfo)
  127. {
  128. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  129. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  130. /* Write any data remaining in the buffer */
  131. if (datacount > 0) {
  132. if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  133. ERREXIT(cinfo, JERR_FILE_WRITE);
  134. }
  135. fflush(dest->outfile);
  136. /* Make sure we wrote the output file OK */
  137. if (ferror(dest->outfile))
  138. ERREXIT(cinfo, JERR_FILE_WRITE);
  139. }
  140. METHODDEF(void)
  141. term_mem_destination (j_compress_ptr cinfo)
  142. {
  143. my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
  144. *dest->outbuffer = dest->buffer;
  145. *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
  146. }
  147. /*
  148. * Prepare for output to a stdio stream.
  149. * The caller must have already opened the stream, and is responsible
  150. * for closing it after finishing compression.
  151. */
  152. GLOBAL(void)
  153. jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
  154. {
  155. my_dest_ptr dest;
  156. /* The destination object is made permanent so that multiple JPEG images
  157. * can be written to the same file without re-executing jpeg_stdio_dest.
  158. * This makes it dangerous to use this manager and a different destination
  159. * manager serially with the same JPEG object, because their private object
  160. * sizes may be different. Caveat programmer.
  161. */
  162. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  163. cinfo->dest = (struct jpeg_destination_mgr *)
  164. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  165. SIZEOF(my_destination_mgr));
  166. }
  167. dest = (my_dest_ptr) cinfo->dest;
  168. dest->pub.init_destination = init_destination;
  169. dest->pub.empty_output_buffer = empty_output_buffer;
  170. dest->pub.term_destination = term_destination;
  171. dest->outfile = outfile;
  172. }
  173. /*
  174. * Prepare for output to a memory buffer.
  175. * The caller may supply an own initial buffer with appropriate size.
  176. * Otherwise, or when the actual data output exceeds the given size,
  177. * the library adapts the buffer size as necessary.
  178. * The standard library functions malloc/free are used for allocating
  179. * larger memory, so the buffer is available to the application after
  180. * finishing compression, and then the application is responsible for
  181. * freeing the requested memory.
  182. */
  183. GLOBAL(void)
  184. jpeg_mem_dest (j_compress_ptr cinfo,
  185. unsigned char ** outbuffer, unsigned long * outsize)
  186. {
  187. my_mem_dest_ptr dest;
  188. if (outbuffer == NULL || outsize == NULL) /* sanity check */
  189. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  190. /* The destination object is made permanent so that multiple JPEG images
  191. * can be written to the same buffer without re-executing jpeg_mem_dest.
  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_mem_destination_mgr));
  197. }
  198. dest = (my_mem_dest_ptr) cinfo->dest;
  199. dest->pub.init_destination = init_mem_destination;
  200. dest->pub.empty_output_buffer = empty_mem_output_buffer;
  201. dest->pub.term_destination = term_mem_destination;
  202. dest->outbuffer = outbuffer;
  203. dest->outsize = outsize;
  204. dest->newbuffer = NULL;
  205. if (*outbuffer == NULL || *outsize == 0) {
  206. /* Allocate initial buffer */
  207. dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
  208. if (dest->newbuffer == NULL)
  209. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  210. *outsize = OUTPUT_BUF_SIZE;
  211. }
  212. dest->pub.next_output_byte = dest->buffer = *outbuffer;
  213. dest->pub.free_in_buffer = dest->bufsize = *outsize;
  214. }