jdatasrc-tj.c 6.7 KB

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