jdatasrc-tj.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * jdatasrc-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-2011 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2011, 2016, 2019, 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. void jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
  25. unsigned long insize);
  26. /*
  27. * Initialize source --- called by jpeg_read_header
  28. * before any data is actually read.
  29. */
  30. METHODDEF(void)
  31. init_mem_source(j_decompress_ptr cinfo)
  32. {
  33. /* no work necessary here */
  34. }
  35. /*
  36. * Fill the input buffer --- called whenever buffer is emptied.
  37. *
  38. * In typical applications, this should read fresh data into the buffer
  39. * (ignoring the current state of next_input_byte & bytes_in_buffer),
  40. * reset the pointer & count to the start of the buffer, and return TRUE
  41. * indicating that the buffer has been reloaded. It is not necessary to
  42. * fill the buffer entirely, only to obtain at least one more byte.
  43. *
  44. * There is no such thing as an EOF return. If the end of the file has been
  45. * reached, the routine has a choice of ERREXIT() or inserting fake data into
  46. * the buffer. In most cases, generating a warning message and inserting a
  47. * fake EOI marker is the best course of action --- this will allow the
  48. * decompressor to output however much of the image is there. However,
  49. * the resulting error message is misleading if the real problem is an empty
  50. * input file, so we handle that case specially.
  51. *
  52. * In applications that need to be able to suspend compression due to input
  53. * not being available yet, a FALSE return indicates that no more data can be
  54. * obtained right now, but more may be forthcoming later. In this situation,
  55. * the decompressor will return to its caller (with an indication of the
  56. * number of scanlines it has read, if any). The application should resume
  57. * decompression after it has loaded more data into the input buffer. Note
  58. * that there are substantial restrictions on the use of suspension --- see
  59. * the documentation.
  60. *
  61. * When suspending, the decompressor will back up to a convenient restart point
  62. * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
  63. * indicate where the restart point will be if the current call returns FALSE.
  64. * Data beyond this point must be rescanned after resumption, so move it to
  65. * the front of the buffer rather than discarding it.
  66. */
  67. METHODDEF(boolean)
  68. fill_mem_input_buffer(j_decompress_ptr cinfo)
  69. {
  70. static const JOCTET mybuffer[4] = {
  71. (JOCTET)0xFF, (JOCTET)JPEG_EOI, 0, 0
  72. };
  73. /* The whole JPEG data is expected to reside in the supplied memory
  74. * buffer, so any request for more data beyond the given buffer size
  75. * is treated as an error.
  76. */
  77. WARNMS(cinfo, JWRN_JPEG_EOF);
  78. /* Insert a fake EOI marker */
  79. cinfo->src->next_input_byte = mybuffer;
  80. cinfo->src->bytes_in_buffer = 2;
  81. return TRUE;
  82. }
  83. /*
  84. * Skip data --- used to skip over a potentially large amount of
  85. * uninteresting data (such as an APPn marker).
  86. *
  87. * Writers of suspendable-input applications must note that skip_input_data
  88. * is not granted the right to give a suspension return. If the skip extends
  89. * beyond the data currently in the buffer, the buffer can be marked empty so
  90. * that the next read will cause a fill_input_buffer call that can suspend.
  91. * Arranging for additional bytes to be discarded before reloading the input
  92. * buffer is the application writer's problem.
  93. */
  94. METHODDEF(void)
  95. skip_input_data(j_decompress_ptr cinfo, long num_bytes)
  96. {
  97. struct jpeg_source_mgr *src = cinfo->src;
  98. /* Just a dumb implementation for now. Could use fseek() except
  99. * it doesn't work on pipes. Not clear that being smart is worth
  100. * any trouble anyway --- large skips are infrequent.
  101. */
  102. if (num_bytes > 0) {
  103. while (num_bytes > (long)src->bytes_in_buffer) {
  104. num_bytes -= (long)src->bytes_in_buffer;
  105. (void)(*src->fill_input_buffer) (cinfo);
  106. /* note we assume that fill_input_buffer will never return FALSE,
  107. * so suspension need not be handled.
  108. */
  109. }
  110. src->next_input_byte += (size_t)num_bytes;
  111. src->bytes_in_buffer -= (size_t)num_bytes;
  112. }
  113. }
  114. /*
  115. * An additional method that can be provided by data source modules is the
  116. * resync_to_restart method for error recovery in the presence of RST markers.
  117. * For the moment, this source module just uses the default resync method
  118. * provided by the JPEG library. That method assumes that no backtracking
  119. * is possible.
  120. */
  121. /*
  122. * Terminate source --- called by jpeg_finish_decompress
  123. * after all data has been read. Often a no-op.
  124. *
  125. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  126. * application must deal with any cleanup that should happen even
  127. * for error exit.
  128. */
  129. METHODDEF(void)
  130. term_source(j_decompress_ptr cinfo)
  131. {
  132. /* no work necessary here */
  133. }
  134. /*
  135. * Prepare for input from a supplied memory buffer.
  136. * The buffer must contain the whole JPEG data.
  137. */
  138. GLOBAL(void)
  139. jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
  140. unsigned long insize)
  141. {
  142. struct jpeg_source_mgr *src;
  143. if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */
  144. ERREXIT(cinfo, JERR_INPUT_EMPTY);
  145. /* The source object is made permanent so that a series of JPEG images
  146. * can be read from the same buffer by calling jpeg_mem_src only before
  147. * the first one.
  148. */
  149. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  150. cinfo->src = (struct jpeg_source_mgr *)
  151. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
  152. sizeof(struct jpeg_source_mgr));
  153. } else if (cinfo->src->init_source != init_mem_source) {
  154. /* It is unsafe to reuse the existing source manager unless it was created
  155. * by this function.
  156. */
  157. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  158. }
  159. src = cinfo->src;
  160. src->init_source = init_mem_source;
  161. src->fill_input_buffer = fill_mem_input_buffer;
  162. src->skip_input_data = skip_input_data;
  163. src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
  164. src->term_source = term_source;
  165. src->bytes_in_buffer = (size_t)insize;
  166. src->next_input_byte = (const JOCTET *)inbuffer;
  167. }