jdatadst.c 8.3 KB

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