jdatadst.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. if (dest->newbuffer != NULL)
  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(j_compress_ptr cinfo, unsigned char **outbuffer,
  205. unsigned long *outsize)
  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, JPOOL_PERMANENT,
  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. #endif