jdatasrc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * jdatasrc.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2009-2015 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. typedef struct {
  23. struct jpeg_source_mgr pub; /* public fields */
  24. FILE * infile; /* source stream */
  25. JOCTET * buffer; /* start of buffer */
  26. boolean start_of_file; /* have we gotten any data yet? */
  27. } my_source_mgr;
  28. typedef my_source_mgr * my_src_ptr;
  29. #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
  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. /* Just a dumb implementation for now. Could use fseek() except
  133. * it doesn't work on pipes. Not clear that being smart is worth
  134. * any trouble anyway --- large skips are infrequent.
  135. */
  136. if (num_bytes > 0) {
  137. while (num_bytes > (long) src->bytes_in_buffer) {
  138. num_bytes -= (long) src->bytes_in_buffer;
  139. (void) (*src->fill_input_buffer) (cinfo);
  140. /* note we assume that fill_input_buffer will never return FALSE,
  141. * so suspension need not be handled.
  142. */
  143. }
  144. src->next_input_byte += (size_t) num_bytes;
  145. src->bytes_in_buffer -= (size_t) num_bytes;
  146. }
  147. }
  148. /*
  149. * An additional method that can be provided by data source modules is the
  150. * resync_to_restart method for error recovery in the presence of RST markers.
  151. * For the moment, this source module just uses the default resync method
  152. * provided by the JPEG library. That method assumes that no backtracking
  153. * is possible.
  154. */
  155. /*
  156. * Terminate source --- called by jpeg_finish_decompress
  157. * after all data has been read. Often a no-op.
  158. *
  159. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  160. * application must deal with any cleanup that should happen even
  161. * for error exit.
  162. */
  163. METHODDEF(void)
  164. term_source (j_decompress_ptr cinfo)
  165. {
  166. /* no work necessary here */
  167. }
  168. /*
  169. * Prepare for input from a stdio stream.
  170. * The caller must have already opened the stream, and is responsible
  171. * for closing it after finishing decompression.
  172. */
  173. GLOBAL(void)
  174. jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
  175. {
  176. my_src_ptr src;
  177. /* The source object and input buffer are made permanent so that a series
  178. * of JPEG images can be read from the same file by calling jpeg_stdio_src
  179. * only before the first one. (If we discarded the buffer at the end of
  180. * one image, we'd likely lose the start of the next one.)
  181. * This makes it unsafe to use this manager and a different source
  182. * manager serially with the same JPEG object. Caveat programmer.
  183. */
  184. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  185. cinfo->src = (struct jpeg_source_mgr *)
  186. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  187. SIZEOF(my_source_mgr));
  188. src = (my_src_ptr) cinfo->src;
  189. src->buffer = (JOCTET *)
  190. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  191. INPUT_BUF_SIZE * SIZEOF(JOCTET));
  192. }
  193. src = (my_src_ptr) cinfo->src;
  194. src->pub.init_source = init_source;
  195. src->pub.fill_input_buffer = fill_input_buffer;
  196. src->pub.skip_input_data = skip_input_data;
  197. src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  198. src->pub.term_source = term_source;
  199. src->infile = infile;
  200. src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  201. src->pub.next_input_byte = NULL; /* until buffer loaded */
  202. }
  203. /*
  204. * Prepare for input from a supplied memory buffer.
  205. * The buffer must contain the whole JPEG data.
  206. */
  207. GLOBAL(void)
  208. jpeg_mem_src (j_decompress_ptr cinfo,
  209. const unsigned char * inbuffer, unsigned long insize)
  210. {
  211. struct jpeg_source_mgr * src;
  212. if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */
  213. ERREXIT(cinfo, JERR_INPUT_EMPTY);
  214. /* The source object is made permanent so that a series of JPEG images
  215. * can be read from the same buffer by calling jpeg_mem_src only before
  216. * the first one.
  217. */
  218. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  219. cinfo->src = (struct jpeg_source_mgr *)
  220. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  221. SIZEOF(struct jpeg_source_mgr));
  222. }
  223. src = cinfo->src;
  224. src->init_source = init_mem_source;
  225. src->fill_input_buffer = fill_mem_input_buffer;
  226. src->skip_input_data = skip_input_data;
  227. src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
  228. src->term_source = term_source;
  229. src->bytes_in_buffer = (size_t) insize;
  230. src->next_input_byte = (const JOCTET *) inbuffer;
  231. }