jdatadst.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README file.
  10. *
  11. * This file contains compression data destination routines for the case of
  12. * emitting JPEG data to memory or to a file (or any stdio stream).
  13. * While these routines are sufficient for most applications,
  14. * some will want to use a different destination manager.
  15. * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
  16. * JOCTETs into 8-bit-wide elements on external storage. If char is wider
  17. * than 8 bits on your machine, you may need to do some tweaking.
  18. */
  19. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. #include "jerror.h"
  23. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  24. extern void * malloc JPP((size_t size));
  25. extern void free JPP((void *ptr));
  26. #endif
  27. /* Expanded data destination object for stdio output */
  28. typedef struct {
  29. struct jpeg_destination_mgr pub; /* public fields */
  30. FILE * outfile; /* target stream */
  31. JOCTET * buffer; /* start of buffer */
  32. } my_destination_mgr;
  33. typedef my_destination_mgr * my_dest_ptr;
  34. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  35. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  36. /* Expanded data destination object for memory output */
  37. typedef struct {
  38. struct jpeg_destination_mgr pub; /* public fields */
  39. unsigned char ** outbuffer; /* target buffer */
  40. unsigned long * outsize;
  41. unsigned char * newbuffer; /* newly allocated buffer */
  42. JOCTET * buffer; /* start of buffer */
  43. size_t bufsize;
  44. } my_mem_destination_mgr;
  45. typedef my_mem_destination_mgr * my_mem_dest_ptr;
  46. #endif
  47. /*
  48. * Initialize destination --- called by jpeg_start_compress
  49. * before any data is actually written.
  50. */
  51. METHODDEF(void)
  52. init_destination (j_compress_ptr cinfo)
  53. {
  54. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  55. /* Allocate the output buffer --- it will be released when done with image */
  56. dest->buffer = (JOCTET *)
  57. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  58. OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
  59. dest->pub.next_output_byte = dest->buffer;
  60. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  61. }
  62. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  63. METHODDEF(void)
  64. init_mem_destination (j_compress_ptr cinfo)
  65. {
  66. /* no work necessary here */
  67. }
  68. #endif
  69. /*
  70. * Empty the output buffer --- called whenever buffer fills up.
  71. *
  72. * In typical applications, this should write the entire output buffer
  73. * (ignoring the current state of next_output_byte & free_in_buffer),
  74. * reset the pointer & count to the start of the buffer, and return TRUE
  75. * indicating that the buffer has been dumped.
  76. *
  77. * In applications that need to be able to suspend compression due to output
  78. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  79. * In this situation, the compressor will return to its caller (possibly with
  80. * an indication that it has not accepted all the supplied scanlines). The
  81. * application should resume compression after it has made more room in the
  82. * output buffer. Note that there are substantial restrictions on the use of
  83. * suspension --- see the documentation.
  84. *
  85. * When suspending, the compressor will back up to a convenient restart point
  86. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  87. * indicate where the restart point will be if the current call returns FALSE.
  88. * Data beyond this point will be regenerated after resumption, so do not
  89. * write it out when emptying the buffer externally.
  90. */
  91. METHODDEF(boolean)
  92. empty_output_buffer (j_compress_ptr cinfo)
  93. {
  94. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  95. if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
  96. (size_t) OUTPUT_BUF_SIZE)
  97. ERREXIT(cinfo, JERR_FILE_WRITE);
  98. dest->pub.next_output_byte = dest->buffer;
  99. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  100. return TRUE;
  101. }
  102. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  103. METHODDEF(boolean)
  104. empty_mem_output_buffer (j_compress_ptr cinfo)
  105. {
  106. size_t nextsize;
  107. JOCTET * nextbuffer;
  108. my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
  109. /* Try to allocate new buffer with double size */
  110. nextsize = dest->bufsize * 2;
  111. nextbuffer = (JOCTET *) malloc(nextsize);
  112. if (nextbuffer == NULL)
  113. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  114. MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
  115. if (dest->newbuffer != NULL)
  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. * This makes it dangerous to use this manager and a different destination
  169. * manager serially with the same JPEG object, because their private object
  170. * sizes may be different. Caveat programmer.
  171. */
  172. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  173. cinfo->dest = (struct jpeg_destination_mgr *)
  174. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  175. SIZEOF(my_destination_mgr));
  176. }
  177. dest = (my_dest_ptr) cinfo->dest;
  178. dest->pub.init_destination = init_destination;
  179. dest->pub.empty_output_buffer = empty_output_buffer;
  180. dest->pub.term_destination = term_destination;
  181. dest->outfile = outfile;
  182. }
  183. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  184. /*
  185. * Prepare for output to a memory buffer.
  186. * The caller may supply an own initial buffer with appropriate size.
  187. * Otherwise, or when the actual data output exceeds the given size,
  188. * the library adapts the buffer size as necessary.
  189. * The standard library functions malloc/free are used for allocating
  190. * larger memory, so the buffer is available to the application after
  191. * finishing compression, and then the application is responsible for
  192. * freeing the requested memory.
  193. */
  194. GLOBAL(void)
  195. jpeg_mem_dest (j_compress_ptr cinfo,
  196. unsigned char ** outbuffer, unsigned long * outsize)
  197. {
  198. my_mem_dest_ptr dest;
  199. if (outbuffer == NULL || outsize == NULL) /* sanity check */
  200. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  201. /* The destination object is made permanent so that multiple JPEG images
  202. * can be written to the same buffer without re-executing jpeg_mem_dest.
  203. */
  204. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  205. cinfo->dest = (struct jpeg_destination_mgr *)
  206. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  207. SIZEOF(my_mem_destination_mgr));
  208. }
  209. dest = (my_mem_dest_ptr) cinfo->dest;
  210. dest->pub.init_destination = init_mem_destination;
  211. dest->pub.empty_output_buffer = empty_mem_output_buffer;
  212. dest->pub.term_destination = term_mem_destination;
  213. dest->outbuffer = outbuffer;
  214. dest->outsize = outsize;
  215. dest->newbuffer = NULL;
  216. if (*outbuffer == NULL || *outsize == 0) {
  217. /* Allocate initial buffer */
  218. dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
  219. if (dest->newbuffer == NULL)
  220. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  221. *outsize = OUTPUT_BUF_SIZE;
  222. }
  223. dest->pub.next_output_byte = dest->buffer = *outbuffer;
  224. dest->pub.free_in_buffer = dest->bufsize = *outsize;
  225. }
  226. #endif