memsrc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * memsrc.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains decompression data source routines for the case of
  9. * reading JPEG data from a memory buffer that is preloaded with the entire
  10. * JPEG file. This would not seem especially useful at first sight, but
  11. * a number of people have asked for it.
  12. * This is really just a stripped-down version of jdatasrc.c. Comparison
  13. * of this code with jdatasrc.c may be helpful in seeing how to make
  14. * custom source managers for other purposes.
  15. */
  16. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jerror.h"
  20. /* Expanded data source object for memory input */
  21. typedef struct {
  22. struct jpeg_source_mgr pub; /* public fields */
  23. JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
  24. } my_source_mgr;
  25. typedef my_source_mgr * my_src_ptr;
  26. /*
  27. * Initialize source --- called by jpeg_read_header
  28. * before any data is actually read.
  29. */
  30. METHODDEF(void)
  31. init_source (j_decompress_ptr cinfo)
  32. {
  33. /* No work, since jpeg_memory_src set up the buffer pointer and count.
  34. * Indeed, if we want to read multiple JPEG images from one buffer,
  35. * this *must* not do anything to the pointer.
  36. */
  37. }
  38. /*
  39. * Fill the input buffer --- called whenever buffer is emptied.
  40. *
  41. * In this application, this routine should never be called; if it is called,
  42. * the decompressor has overrun the end of the input buffer, implying we
  43. * supplied an incomplete or corrupt JPEG datastream. A simple error exit
  44. * might be the most appropriate response.
  45. *
  46. * But what we choose to do in this code is to supply dummy EOI markers
  47. * in order to force the decompressor to finish processing and supply
  48. * some sort of output image, no matter how corrupted.
  49. */
  50. METHODDEF(boolean)
  51. fill_input_buffer (j_decompress_ptr cinfo)
  52. {
  53. my_src_ptr src = (my_src_ptr) cinfo->src;
  54. WARNMS(cinfo, JWRN_JPEG_EOF);
  55. /* Create a fake EOI marker */
  56. src->eoi_buffer[0] = (JOCTET) 0xFF;
  57. src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
  58. src->pub.next_input_byte = src->eoi_buffer;
  59. src->pub.bytes_in_buffer = 2;
  60. return TRUE;
  61. }
  62. /*
  63. * Skip data --- used to skip over a potentially large amount of
  64. * uninteresting data (such as an APPn marker).
  65. *
  66. * If we overrun the end of the buffer, we let fill_input_buffer deal with
  67. * it. An extremely large skip could cause some time-wasting here, but
  68. * it really isn't supposed to happen ... and the decompressor will never
  69. * skip more than 64K anyway.
  70. */
  71. METHODDEF(void)
  72. skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  73. {
  74. my_src_ptr src = (my_src_ptr) cinfo->src;
  75. if (num_bytes > 0) {
  76. while (num_bytes > (long) src->pub.bytes_in_buffer) {
  77. num_bytes -= (long) src->pub.bytes_in_buffer;
  78. (void) fill_input_buffer(cinfo);
  79. /* note we assume that fill_input_buffer will never return FALSE,
  80. * so suspension need not be handled.
  81. */
  82. }
  83. src->pub.next_input_byte += (size_t) num_bytes;
  84. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  85. }
  86. }
  87. /*
  88. * An additional method that can be provided by data source modules is the
  89. * resync_to_restart method for error recovery in the presence of RST markers.
  90. * For the moment, this source module just uses the default resync method
  91. * provided by the JPEG library. That method assumes that no backtracking
  92. * is possible.
  93. */
  94. /*
  95. * Terminate source --- called by jpeg_finish_decompress
  96. * after all data has been read. Often a no-op.
  97. *
  98. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  99. * application must deal with any cleanup that should happen even
  100. * for error exit.
  101. */
  102. METHODDEF(void)
  103. term_source (j_decompress_ptr cinfo)
  104. {
  105. /* no work necessary here */
  106. }
  107. /*
  108. * Prepare for input from a memory buffer.
  109. */
  110. GLOBAL(void)
  111. jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
  112. {
  113. my_src_ptr src;
  114. /* The source object is made permanent so that a series of JPEG images
  115. * can be read from a single buffer by calling jpeg_memory_src
  116. * only before the first one.
  117. * This makes it unsafe to use this manager and a different source
  118. * manager serially with the same JPEG object. Caveat programmer.
  119. */
  120. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  121. cinfo->src = (struct jpeg_source_mgr *)
  122. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  123. SIZEOF(my_source_mgr));
  124. }
  125. src = (my_src_ptr) cinfo->src;
  126. src->pub.init_source = init_source;
  127. src->pub.fill_input_buffer = fill_input_buffer;
  128. src->pub.skip_input_data = skip_input_data;
  129. src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  130. src->pub.term_source = term_source;
  131. src->pub.next_input_byte = buffer;
  132. src->pub.bytes_in_buffer = bufsize;
  133. }
粤ICP备19079148号