jdatadst.c 8.5 KB

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