jdatadst.c 9.3 KB

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