jdatasrc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * jdatasrc.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-2011 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 decompression data source routines for the case of
  13. * reading JPEG data from memory or from a file (or any stdio stream).
  14. * While these routines are sufficient for most applications,
  15. * some will want to use a different source manager.
  16. * IMPORTANT: we assume that fread() will correctly transcribe an array of
  17. * JOCTETs from 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. /* Expanded data source object for stdio input */
  25. typedef struct {
  26. struct jpeg_source_mgr pub; /* public fields */
  27. FILE *infile; /* source stream */
  28. JOCTET *buffer; /* start of buffer */
  29. boolean start_of_file; /* have we gotten any data yet? */
  30. } my_source_mgr;
  31. typedef my_source_mgr *my_src_ptr;
  32. #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
  33. /*
  34. * Initialize source --- called by jpeg_read_header
  35. * before any data is actually read.
  36. */
  37. METHODDEF(void)
  38. init_source(j_decompress_ptr cinfo)
  39. {
  40. my_src_ptr src = (my_src_ptr)cinfo->src;
  41. /* We reset the empty-input-file flag for each image,
  42. * but we don't clear the input buffer.
  43. * This is correct behavior for reading a series of images from one source.
  44. */
  45. src->start_of_file = TRUE;
  46. }
  47. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  48. METHODDEF(void)
  49. init_mem_source(j_decompress_ptr cinfo)
  50. {
  51. /* no work necessary here */
  52. }
  53. #endif
  54. /*
  55. * Fill the input buffer --- called whenever buffer is emptied.
  56. *
  57. * In typical applications, this should read fresh data into the buffer
  58. * (ignoring the current state of next_input_byte & bytes_in_buffer),
  59. * reset the pointer & count to the start of the buffer, and return TRUE
  60. * indicating that the buffer has been reloaded. It is not necessary to
  61. * fill the buffer entirely, only to obtain at least one more byte.
  62. *
  63. * There is no such thing as an EOF return. If the end of the file has been
  64. * reached, the routine has a choice of ERREXIT() or inserting fake data into
  65. * the buffer. In most cases, generating a warning message and inserting a
  66. * fake EOI marker is the best course of action --- this will allow the
  67. * decompressor to output however much of the image is there. However,
  68. * the resulting error message is misleading if the real problem is an empty
  69. * input file, so we handle that case specially.
  70. *
  71. * In applications that need to be able to suspend compression due to input
  72. * not being available yet, a FALSE return indicates that no more data can be
  73. * obtained right now, but more may be forthcoming later. In this situation,
  74. * the decompressor will return to its caller (with an indication of the
  75. * number of scanlines it has read, if any). The application should resume
  76. * decompression after it has loaded more data into the input buffer. Note
  77. * that there are substantial restrictions on the use of suspension --- see
  78. * the documentation.
  79. *
  80. * When suspending, the decompressor will back up to a convenient restart point
  81. * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
  82. * indicate where the restart point will be if the current call returns FALSE.
  83. * Data beyond this point must be rescanned after resumption, so move it to
  84. * the front of the buffer rather than discarding it.
  85. */
  86. METHODDEF(boolean)
  87. fill_input_buffer(j_decompress_ptr cinfo)
  88. {
  89. my_src_ptr src = (my_src_ptr)cinfo->src;
  90. size_t nbytes;
  91. nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
  92. if (nbytes <= 0) {
  93. if (src->start_of_file) /* Treat empty input file as fatal error */
  94. ERREXIT(cinfo, JERR_INPUT_EMPTY);
  95. WARNMS(cinfo, JWRN_JPEG_EOF);
  96. /* Insert a fake EOI marker */
  97. src->buffer[0] = (JOCTET)0xFF;
  98. src->buffer[1] = (JOCTET)JPEG_EOI;
  99. nbytes = 2;
  100. }
  101. src->pub.next_input_byte = src->buffer;
  102. src->pub.bytes_in_buffer = nbytes;
  103. src->start_of_file = FALSE;
  104. return TRUE;
  105. }
  106. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  107. METHODDEF(boolean)
  108. fill_mem_input_buffer(j_decompress_ptr cinfo)
  109. {
  110. static const JOCTET mybuffer[4] = {
  111. (JOCTET)0xFF, (JOCTET)JPEG_EOI, 0, 0
  112. };
  113. /* The whole JPEG data is expected to reside in the supplied memory
  114. * buffer, so any request for more data beyond the given buffer size
  115. * is treated as an error.
  116. */
  117. WARNMS(cinfo, JWRN_JPEG_EOF);
  118. /* Insert a fake EOI marker */
  119. cinfo->src->next_input_byte = mybuffer;
  120. cinfo->src->bytes_in_buffer = 2;
  121. return TRUE;
  122. }
  123. #endif
  124. /*
  125. * Skip data --- used to skip over a potentially large amount of
  126. * uninteresting data (such as an APPn marker).
  127. *
  128. * Writers of suspendable-input applications must note that skip_input_data
  129. * is not granted the right to give a suspension return. If the skip extends
  130. * beyond the data currently in the buffer, the buffer can be marked empty so
  131. * that the next read will cause a fill_input_buffer call that can suspend.
  132. * Arranging for additional bytes to be discarded before reloading the input
  133. * buffer is the application writer's problem.
  134. */
  135. METHODDEF(void)
  136. skip_input_data(j_decompress_ptr cinfo, long num_bytes)
  137. {
  138. struct jpeg_source_mgr *src = cinfo->src;
  139. /* Just a dumb implementation for now. Could use fseek() except
  140. * it doesn't work on pipes. Not clear that being smart is worth
  141. * any trouble anyway --- large skips are infrequent.
  142. */
  143. if (num_bytes > 0) {
  144. while (num_bytes > (long)src->bytes_in_buffer) {
  145. num_bytes -= (long)src->bytes_in_buffer;
  146. (void)(*src->fill_input_buffer) (cinfo);
  147. /* note we assume that fill_input_buffer will never return FALSE,
  148. * so suspension need not be handled.
  149. */
  150. }
  151. src->next_input_byte += (size_t)num_bytes;
  152. src->bytes_in_buffer -= (size_t)num_bytes;
  153. }
  154. }
  155. /*
  156. * An additional method that can be provided by data source modules is the
  157. * resync_to_restart method for error recovery in the presence of RST markers.
  158. * For the moment, this source module just uses the default resync method
  159. * provided by the JPEG library. That method assumes that no backtracking
  160. * is possible.
  161. */
  162. /*
  163. * Terminate source --- called by jpeg_finish_decompress
  164. * after all data has been read. Often a no-op.
  165. *
  166. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  167. * application must deal with any cleanup that should happen even
  168. * for error exit.
  169. */
  170. METHODDEF(void)
  171. term_source(j_decompress_ptr cinfo)
  172. {
  173. /* no work necessary here */
  174. }
  175. /*
  176. * Prepare for input from a stdio stream.
  177. * The caller must have already opened the stream, and is responsible
  178. * for closing it after finishing decompression.
  179. */
  180. GLOBAL(void)
  181. jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)
  182. {
  183. my_src_ptr src;
  184. /* The source object and input buffer are made permanent so that a series
  185. * of JPEG images can be read from the same file by calling jpeg_stdio_src
  186. * only before the first one. (If we discarded the buffer at the end of
  187. * one image, we'd likely lose the start of the next one.)
  188. */
  189. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  190. cinfo->src = (struct jpeg_source_mgr *)
  191. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  192. sizeof(my_source_mgr));
  193. src = (my_src_ptr)cinfo->src;
  194. src->buffer = (JOCTET *)
  195. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  196. INPUT_BUF_SIZE * sizeof(JOCTET));
  197. } else if (cinfo->src->init_source != init_source) {
  198. /* It is unsafe to reuse the existing source manager unless it was created
  199. * by this function. Otherwise, there is no guarantee that the opaque
  200. * structure is the right size. Note that we could just create a new
  201. * structure, but the old structure would not be freed until
  202. * jpeg_destroy_decompress() was called.
  203. */
  204. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  205. }
  206. src = (my_src_ptr)cinfo->src;
  207. src->pub.init_source = init_source;
  208. src->pub.fill_input_buffer = fill_input_buffer;
  209. src->pub.skip_input_data = skip_input_data;
  210. src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  211. src->pub.term_source = term_source;
  212. src->infile = infile;
  213. src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  214. src->pub.next_input_byte = NULL; /* until buffer loaded */
  215. }
  216. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  217. /*
  218. * Prepare for input from a supplied memory buffer.
  219. * The buffer must contain the whole JPEG data.
  220. */
  221. GLOBAL(void)
  222. jpeg_mem_src(j_decompress_ptr cinfo, const unsigned char *inbuffer,
  223. unsigned long insize)
  224. {
  225. struct jpeg_source_mgr *src;
  226. if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */
  227. ERREXIT(cinfo, JERR_INPUT_EMPTY);
  228. /* The source object is made permanent so that a series of JPEG images
  229. * can be read from the same buffer by calling jpeg_mem_src only before
  230. * the first one.
  231. */
  232. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  233. cinfo->src = (struct jpeg_source_mgr *)
  234. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  235. sizeof(struct jpeg_source_mgr));
  236. } else if (cinfo->src->init_source != init_mem_source) {
  237. /* It is unsafe to reuse the existing source manager unless it was created
  238. * by this function.
  239. */
  240. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  241. }
  242. src = cinfo->src;
  243. src->init_source = init_mem_source;
  244. src->fill_input_buffer = fill_mem_input_buffer;
  245. src->skip_input_data = skip_input_data;
  246. src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
  247. src->term_source = term_source;
  248. src->bytes_in_buffer = (size_t)insize;
  249. src->next_input_byte = (const JOCTET *)inbuffer;
  250. }
  251. #endif