jdatadst-tj.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * jdatadst-tj.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) 2011, 2014, 2016, 2019, 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. void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
  29. unsigned long *outsize, boolean alloc);
  30. #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
  31. /* Expanded data destination object for memory output */
  32. typedef struct {
  33. struct jpeg_destination_mgr pub; /* public fields */
  34. unsigned char **outbuffer; /* target buffer */
  35. unsigned long *outsize;
  36. unsigned char *newbuffer; /* newly allocated buffer */
  37. JOCTET *buffer; /* start of buffer */
  38. size_t bufsize;
  39. boolean alloc;
  40. } my_mem_destination_mgr;
  41. typedef my_mem_destination_mgr *my_mem_dest_ptr;
  42. /*
  43. * Initialize destination --- called by jpeg_start_compress
  44. * before any data is actually written.
  45. */
  46. METHODDEF(void)
  47. init_mem_destination(j_compress_ptr cinfo)
  48. {
  49. /* no work necessary here */
  50. }
  51. /*
  52. * Empty the output buffer --- called whenever buffer fills up.
  53. *
  54. * In typical applications, this should write the entire output buffer
  55. * (ignoring the current state of next_output_byte & free_in_buffer),
  56. * reset the pointer & count to the start of the buffer, and return TRUE
  57. * indicating that the buffer has been dumped.
  58. *
  59. * In applications that need to be able to suspend compression due to output
  60. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  61. * In this situation, the compressor will return to its caller (possibly with
  62. * an indication that it has not accepted all the supplied scanlines). The
  63. * application should resume compression after it has made more room in the
  64. * output buffer. Note that there are substantial restrictions on the use of
  65. * suspension --- see the documentation.
  66. *
  67. * When suspending, the compressor will back up to a convenient restart point
  68. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  69. * indicate where the restart point will be if the current call returns FALSE.
  70. * Data beyond this point will be regenerated after resumption, so do not
  71. * write it out when emptying the buffer externally.
  72. */
  73. METHODDEF(boolean)
  74. empty_mem_output_buffer(j_compress_ptr cinfo)
  75. {
  76. size_t nextsize;
  77. JOCTET *nextbuffer;
  78. my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
  79. if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
  80. /* Try to allocate new buffer with double size */
  81. nextsize = dest->bufsize * 2;
  82. nextbuffer = (JOCTET *)malloc(nextsize);
  83. if (nextbuffer == NULL)
  84. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  85. MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
  86. free(dest->newbuffer);
  87. dest->newbuffer = nextbuffer;
  88. dest->pub.next_output_byte = nextbuffer + dest->bufsize;
  89. dest->pub.free_in_buffer = dest->bufsize;
  90. dest->buffer = nextbuffer;
  91. dest->bufsize = nextsize;
  92. return TRUE;
  93. }
  94. /*
  95. * Terminate destination --- called by jpeg_finish_compress
  96. * after all data has been written. Usually needs to flush buffer.
  97. *
  98. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  99. * application must deal with any cleanup that should happen even
  100. * for error exit.
  101. */
  102. METHODDEF(void)
  103. term_mem_destination(j_compress_ptr cinfo)
  104. {
  105. my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
  106. if (dest->alloc) *dest->outbuffer = dest->buffer;
  107. *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
  108. }
  109. /*
  110. * Prepare for output to a memory buffer.
  111. * The caller may supply an own initial buffer with appropriate size.
  112. * Otherwise, or when the actual data output exceeds the given size,
  113. * the library adapts the buffer size as necessary.
  114. * The standard library functions malloc/free are used for allocating
  115. * larger memory, so the buffer is available to the application after
  116. * finishing compression, and then the application is responsible for
  117. * freeing the requested memory.
  118. */
  119. GLOBAL(void)
  120. jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
  121. unsigned long *outsize, boolean alloc)
  122. {
  123. boolean reused = FALSE;
  124. my_mem_dest_ptr dest;
  125. if (outbuffer == NULL || outsize == NULL) /* sanity check */
  126. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  127. /* The destination object is made permanent so that multiple JPEG images
  128. * can be written to the same buffer without re-executing jpeg_mem_dest.
  129. */
  130. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  131. cinfo->dest = (struct jpeg_destination_mgr *)
  132. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  133. sizeof(my_mem_destination_mgr));
  134. dest = (my_mem_dest_ptr)cinfo->dest;
  135. dest->newbuffer = NULL;
  136. dest->buffer = NULL;
  137. } else if (cinfo->dest->init_destination != init_mem_destination) {
  138. /* It is unsafe to reuse the existing destination manager unless it was
  139. * created by this function.
  140. */
  141. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  142. }
  143. dest = (my_mem_dest_ptr)cinfo->dest;
  144. dest->pub.init_destination = init_mem_destination;
  145. dest->pub.empty_output_buffer = empty_mem_output_buffer;
  146. dest->pub.term_destination = term_mem_destination;
  147. if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
  148. reused = TRUE;
  149. dest->outbuffer = outbuffer;
  150. dest->outsize = outsize;
  151. dest->alloc = alloc;
  152. if (*outbuffer == NULL || *outsize == 0) {
  153. if (alloc) {
  154. /* Allocate initial buffer */
  155. dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
  156. if (dest->newbuffer == NULL)
  157. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  158. *outsize = OUTPUT_BUF_SIZE;
  159. } else
  160. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  161. }
  162. dest->pub.next_output_byte = dest->buffer = *outbuffer;
  163. if (!reused)
  164. dest->bufsize = *outsize;
  165. dest->pub.free_in_buffer = dest->bufsize;
  166. }