jdatadst.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * jdatadst.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1996, Thomas G. Lane.
  6. * Modified 2009-2012 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2013, 2016, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains compression data destination routines for the case of
  13. * emitting JPEG data to memory or to a file (or any stdio stream).
  14. * While these routines are sufficient for most applications,
  15. * some will want to use a different destination manager.
  16. * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
  17. * JOCTETs into 8-bit-wide elements on external storage. If char is wider
  18. * than 8 bits on your machine, you may need to do some tweaking.
  19. */
  20. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  21. #include "jinclude.h"
  22. #include "jpeglib.h"
  23. #include "jerror.h"
  24. #include "jpegint.h"
  25. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  26. extern void *malloc (size_t size);
  27. extern void free (void *ptr);
  28. #endif
  29. /* Expanded data destination object for stdio output */
  30. typedef struct {
  31. struct jpeg_destination_mgr pub; /* public fields */
  32. FILE *outfile; /* target stream */
  33. JOCTET *buffer; /* start of buffer */
  34. } my_destination_mgr;
  35. typedef my_destination_mgr *my_dest_ptr;
  36. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  37. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  38. /* Expanded data destination object for memory output */
  39. typedef struct {
  40. struct jpeg_destination_mgr pub; /* public fields */
  41. unsigned char **outbuffer; /* target buffer */
  42. unsigned long *outsize;
  43. unsigned char *newbuffer; /* newly allocated buffer */
  44. JOCTET *buffer; /* start of buffer */
  45. size_t bufsize;
  46. } my_mem_destination_mgr;
  47. typedef my_mem_destination_mgr *my_mem_dest_ptr;
  48. #endif
  49. /*
  50. * Initialize destination --- called by jpeg_start_compress
  51. * before any data is actually written.
  52. */
  53. METHODDEF(void)
  54. init_destination (j_compress_ptr cinfo)
  55. {
  56. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  57. /* Allocate the output buffer --- it will be released when done with image */
  58. dest->buffer = (JOCTET *)
  59. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  60. OUTPUT_BUF_SIZE * sizeof(JOCTET));
  61. dest->pub.next_output_byte = dest->buffer;
  62. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  63. }
  64. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  65. METHODDEF(void)
  66. init_mem_destination (j_compress_ptr cinfo)
  67. {
  68. /* no work necessary here */
  69. }
  70. #endif
  71. /*
  72. * Empty the output buffer --- called whenever buffer fills up.
  73. *
  74. * In typical applications, this should write the entire output buffer
  75. * (ignoring the current state of next_output_byte & free_in_buffer),
  76. * reset the pointer & count to the start of the buffer, and return TRUE
  77. * indicating that the buffer has been dumped.
  78. *
  79. * In applications that need to be able to suspend compression due to output
  80. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  81. * In this situation, the compressor will return to its caller (possibly with
  82. * an indication that it has not accepted all the supplied scanlines). The
  83. * application should resume compression after it has made more room in the
  84. * output buffer. Note that there are substantial restrictions on the use of
  85. * suspension --- see the documentation.
  86. *
  87. * When suspending, the compressor will back up to a convenient restart point
  88. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  89. * indicate where the restart point will be if the current call returns FALSE.
  90. * Data beyond this point will be regenerated after resumption, so do not
  91. * write it out when emptying the buffer externally.
  92. */
  93. METHODDEF(boolean)
  94. empty_output_buffer (j_compress_ptr cinfo)
  95. {
  96. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  97. if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
  98. (size_t) OUTPUT_BUF_SIZE)
  99. ERREXIT(cinfo, JERR_FILE_WRITE);
  100. dest->pub.next_output_byte = dest->buffer;
  101. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  102. return TRUE;
  103. }
  104. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  105. METHODDEF(boolean)
  106. empty_mem_output_buffer (j_compress_ptr cinfo)
  107. {
  108. size_t nextsize;
  109. JOCTET *nextbuffer;
  110. my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
  111. /* Try to allocate new buffer with double size */
  112. nextsize = dest->bufsize * 2;
  113. nextbuffer = (JOCTET *) malloc(nextsize);
  114. if (nextbuffer == NULL)
  115. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  116. MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
  117. free(dest->newbuffer);
  118. dest->newbuffer = nextbuffer;
  119. dest->pub.next_output_byte = nextbuffer + dest->bufsize;
  120. dest->pub.free_in_buffer = dest->bufsize;
  121. dest->buffer = nextbuffer;
  122. dest->bufsize = nextsize;
  123. return TRUE;
  124. }
  125. #endif
  126. /*
  127. * Terminate destination --- called by jpeg_finish_compress
  128. * after all data has been written. Usually needs to flush buffer.
  129. *
  130. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  131. * application must deal with any cleanup that should happen even
  132. * for error exit.
  133. */
  134. METHODDEF(void)
  135. term_destination (j_compress_ptr cinfo)
  136. {
  137. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  138. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  139. /* Write any data remaining in the buffer */
  140. if (datacount > 0) {
  141. if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  142. ERREXIT(cinfo, JERR_FILE_WRITE);
  143. }
  144. fflush(dest->outfile);
  145. /* Make sure we wrote the output file OK */
  146. if (ferror(dest->outfile))
  147. ERREXIT(cinfo, JERR_FILE_WRITE);
  148. }
  149. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  150. METHODDEF(void)
  151. term_mem_destination (j_compress_ptr cinfo)
  152. {
  153. my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
  154. *dest->outbuffer = dest->buffer;
  155. *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
  156. }
  157. #endif
  158. /*
  159. * Prepare for output to a stdio stream.
  160. * The caller must have already opened the stream, and is responsible
  161. * for closing it after finishing compression.
  162. */
  163. GLOBAL(void)
  164. jpeg_stdio_dest (j_compress_ptr cinfo, FILE *outfile)
  165. {
  166. my_dest_ptr dest;
  167. /* The destination object is made permanent so that multiple JPEG images
  168. * can be written to the same file without re-executing jpeg_stdio_dest.
  169. */
  170. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  171. cinfo->dest = (struct jpeg_destination_mgr *)
  172. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  173. sizeof(my_destination_mgr));
  174. } else if (cinfo->dest->init_destination != init_destination) {
  175. /* It is unsafe to reuse the existing destination manager unless it was
  176. * created by this function. Otherwise, there is no guarantee that the
  177. * opaque structure is the right size. Note that we could just create a
  178. * new structure, but the old structure would not be freed until
  179. * jpeg_destroy_compress() was called.
  180. */
  181. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  182. }
  183. dest = (my_dest_ptr) cinfo->dest;
  184. dest->pub.init_destination = init_destination;
  185. dest->pub.empty_output_buffer = empty_output_buffer;
  186. dest->pub.term_destination = term_destination;
  187. dest->outfile = outfile;
  188. }
  189. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  190. /*
  191. * Prepare for output to a memory buffer.
  192. * The caller may supply an own initial buffer with appropriate size.
  193. * Otherwise, or when the actual data output exceeds the given size,
  194. * the library adapts the buffer size as necessary.
  195. * The standard library functions malloc/free are used for allocating
  196. * larger memory, so the buffer is available to the application after
  197. * finishing compression, and then the application is responsible for
  198. * freeing the requested memory.
  199. * Note: An initial buffer supplied by the caller is expected to be
  200. * managed by the application. The library does not free such buffer
  201. * when allocating a larger buffer.
  202. */
  203. GLOBAL(void)
  204. jpeg_mem_dest_internal (j_compress_ptr cinfo,
  205. unsigned char **outbuffer, unsigned long *outsize, int pool_id)
  206. {
  207. my_mem_dest_ptr dest;
  208. if (outbuffer == NULL || outsize == NULL) /* sanity check */
  209. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  210. /* The destination object is made permanent so that multiple JPEG images
  211. * can be written to the same buffer without re-executing jpeg_mem_dest.
  212. */
  213. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  214. cinfo->dest = (struct jpeg_destination_mgr *)
  215. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, pool_id,
  216. sizeof(my_mem_destination_mgr));
  217. } else if (cinfo->dest->init_destination != init_mem_destination) {
  218. /* It is unsafe to reuse the existing destination manager unless it was
  219. * created by this function.
  220. */
  221. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  222. }
  223. dest = (my_mem_dest_ptr) cinfo->dest;
  224. dest->pub.init_destination = init_mem_destination;
  225. dest->pub.empty_output_buffer = empty_mem_output_buffer;
  226. dest->pub.term_destination = term_mem_destination;
  227. dest->outbuffer = outbuffer;
  228. dest->outsize = outsize;
  229. dest->newbuffer = NULL;
  230. if (*outbuffer == NULL || *outsize == 0) {
  231. /* Allocate initial buffer */
  232. dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
  233. if (dest->newbuffer == NULL)
  234. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  235. *outsize = OUTPUT_BUF_SIZE;
  236. }
  237. dest->pub.next_output_byte = dest->buffer = *outbuffer;
  238. dest->pub.free_in_buffer = dest->bufsize = *outsize;
  239. }
  240. GLOBAL(void)
  241. jpeg_mem_dest (j_compress_ptr cinfo,
  242. unsigned char **outbuffer, unsigned long *outsize)
  243. {
  244. /* The destination object is made permanent so that multiple JPEG images
  245. * can be written to the same file without re-executing jpeg_stdio_dest.
  246. */
  247. jpeg_mem_dest_internal(cinfo, outbuffer, outsize, JPOOL_PERMANENT);
  248. }
  249. #endif