jdatadst.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  25. extern void *malloc(size_t size);
  26. extern void free(void *ptr);
  27. #endif
  28. /* Expanded data destination object for stdio output */
  29. typedef struct {
  30. struct jpeg_destination_mgr pub; /* public fields */
  31. FILE *outfile; /* target stream */
  32. JOCTET *buffer; /* start of buffer */
  33. } my_destination_mgr;
  34. typedef my_destination_mgr *my_dest_ptr;
  35. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  36. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  37. /* Expanded data destination object for memory output */
  38. typedef struct {
  39. struct jpeg_destination_mgr pub; /* public fields */
  40. unsigned char **outbuffer; /* target buffer */
  41. unsigned long *outsize;
  42. unsigned char *newbuffer; /* newly allocated buffer */
  43. JOCTET *buffer; /* start of buffer */
  44. size_t bufsize;
  45. } my_mem_destination_mgr;
  46. typedef my_mem_destination_mgr *my_mem_dest_ptr;
  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. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  64. METHODDEF(void)
  65. init_mem_destination(j_compress_ptr cinfo)
  66. {
  67. /* no work necessary here */
  68. }
  69. #endif
  70. /*
  71. * Empty the output buffer --- called whenever buffer fills up.
  72. *
  73. * In typical applications, this should write the entire output buffer
  74. * (ignoring the current state of next_output_byte & free_in_buffer),
  75. * reset the pointer & count to the start of the buffer, and return TRUE
  76. * indicating that the buffer has been dumped.
  77. *
  78. * In applications that need to be able to suspend compression due to output
  79. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  80. * In this situation, the compressor will return to its caller (possibly with
  81. * an indication that it has not accepted all the supplied scanlines). The
  82. * application should resume compression after it has made more room in the
  83. * output buffer. Note that there are substantial restrictions on the use of
  84. * suspension --- see the documentation.
  85. *
  86. * When suspending, the compressor will back up to a convenient restart point
  87. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  88. * indicate where the restart point will be if the current call returns FALSE.
  89. * Data beyond this point will be regenerated after resumption, so do not
  90. * write it out when emptying the buffer externally.
  91. */
  92. METHODDEF(boolean)
  93. empty_output_buffer(j_compress_ptr cinfo)
  94. {
  95. my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
  96. if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
  97. (size_t)OUTPUT_BUF_SIZE)
  98. ERREXIT(cinfo, JERR_FILE_WRITE);
  99. dest->pub.next_output_byte = dest->buffer;
  100. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  101. return TRUE;
  102. }
  103. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  104. METHODDEF(boolean)
  105. empty_mem_output_buffer(j_compress_ptr cinfo)
  106. {
  107. size_t nextsize;
  108. JOCTET *nextbuffer;
  109. my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
  110. /* Try to allocate new buffer with double size */
  111. nextsize = dest->bufsize * 2;
  112. nextbuffer = (JOCTET *)malloc(nextsize);
  113. if (nextbuffer == NULL)
  114. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  115. MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
  116. free(dest->newbuffer);
  117. dest->newbuffer = nextbuffer;
  118. dest->pub.next_output_byte = nextbuffer + dest->bufsize;
  119. dest->pub.free_in_buffer = dest->bufsize;
  120. dest->buffer = nextbuffer;
  121. dest->bufsize = nextsize;
  122. return TRUE;
  123. }
  124. #endif
  125. /*
  126. * Terminate destination --- called by jpeg_finish_compress
  127. * after all data has been written. Usually needs to flush buffer.
  128. *
  129. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  130. * application must deal with any cleanup that should happen even
  131. * for error exit.
  132. */
  133. METHODDEF(void)
  134. term_destination(j_compress_ptr cinfo)
  135. {
  136. my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
  137. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  138. /* Write any data remaining in the buffer */
  139. if (datacount > 0) {
  140. if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  141. ERREXIT(cinfo, JERR_FILE_WRITE);
  142. }
  143. fflush(dest->outfile);
  144. /* Make sure we wrote the output file OK */
  145. if (ferror(dest->outfile))
  146. ERREXIT(cinfo, JERR_FILE_WRITE);
  147. }
  148. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  149. METHODDEF(void)
  150. term_mem_destination(j_compress_ptr cinfo)
  151. {
  152. my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
  153. *dest->outbuffer = dest->buffer;
  154. *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
  155. }
  156. #endif
  157. /*
  158. * Prepare for output to a stdio stream.
  159. * The caller must have already opened the stream, and is responsible
  160. * for closing it after finishing compression.
  161. */
  162. GLOBAL(void)
  163. jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
  164. {
  165. my_dest_ptr dest;
  166. /* The destination object is made permanent so that multiple JPEG images
  167. * can be written to the same file without re-executing jpeg_stdio_dest.
  168. */
  169. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  170. cinfo->dest = (struct jpeg_destination_mgr *)
  171. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  172. sizeof(my_destination_mgr));
  173. } else if (cinfo->dest->init_destination != init_destination) {
  174. /* It is unsafe to reuse the existing destination manager unless it was
  175. * created by this function. Otherwise, there is no guarantee that the
  176. * opaque structure is the right size. Note that we could just create a
  177. * new structure, but the old structure would not be freed until
  178. * jpeg_destroy_compress() was called.
  179. */
  180. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  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. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  189. /*
  190. * Prepare for output to a memory buffer.
  191. * The caller may supply an own initial buffer with appropriate size.
  192. * Otherwise, or when the actual data output exceeds the given size,
  193. * the library adapts the buffer size as necessary.
  194. * The standard library functions malloc/free are used for allocating
  195. * larger memory, so the buffer is available to the application after
  196. * finishing compression, and then the application is responsible for
  197. * freeing the requested memory.
  198. * Note: An initial buffer supplied by the caller is expected to be
  199. * managed by the application. The library does not free such buffer
  200. * when allocating a larger buffer.
  201. */
  202. GLOBAL(void)
  203. jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer,
  204. unsigned long *outsize)
  205. {
  206. my_mem_dest_ptr dest;
  207. if (outbuffer == NULL || outsize == NULL) /* sanity check */
  208. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  209. /* The destination object is made permanent so that multiple JPEG images
  210. * can be written to the same buffer without re-executing jpeg_mem_dest.
  211. */
  212. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  213. cinfo->dest = (struct jpeg_destination_mgr *)
  214. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  215. sizeof(my_mem_destination_mgr));
  216. } else if (cinfo->dest->init_destination != init_mem_destination) {
  217. /* It is unsafe to reuse the existing destination manager unless it was
  218. * created by this function.
  219. */
  220. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  221. }
  222. dest = (my_mem_dest_ptr)cinfo->dest;
  223. dest->pub.init_destination = init_mem_destination;
  224. dest->pub.empty_output_buffer = empty_mem_output_buffer;
  225. dest->pub.term_destination = term_mem_destination;
  226. dest->outbuffer = outbuffer;
  227. dest->outsize = outsize;
  228. dest->newbuffer = NULL;
  229. if (*outbuffer == NULL || *outsize == 0) {
  230. /* Allocate initial buffer */
  231. dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
  232. if (dest->newbuffer == NULL)
  233. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  234. *outsize = OUTPUT_BUF_SIZE;
  235. }
  236. dest->pub.next_output_byte = dest->buffer = *outbuffer;
  237. dest->pub.free_in_buffer = dest->bufsize = *outsize;
  238. }
  239. #endif