jdatasrc.c 9.0 KB

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