4 * Copyright (C) 1994-1996, Thomas G. Lane.
\r
5 * This file is part of the Independent JPEG Group's software.
\r
6 * For conditions of distribution and use, see the accompanying README file.
\r
8 * This file contains decompression data source routines for the case of
\r
9 * reading JPEG data from a memory buffer that is preloaded with the entire
\r
10 * JPEG file. This would not seem especially useful at first sight, but
\r
11 * a number of people have asked for it.
\r
12 * This is really just a stripped-down version of jdatasrc.c. Comparison
\r
13 * of this code with jdatasrc.c may be helpful in seeing how to make
\r
14 * custom source managers for other purposes.
\r
17 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
\r
18 #include "jinclude.h"
\r
19 #include "jpeglib.h"
\r
23 /* Expanded data source object for memory input */
\r
26 * \brief very low level C 'structure', used to decode jpeg file
\r
27 * Should not appear in the Doxygen supplied documentation
\r
30 struct jpeg_source_mgr pub; /* public fields */
\r
31 JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
\r
34 typedef my_source_mgr * my_src_ptr;
\r
38 * Initialize source --- called by jpeg_read_header
\r
39 * before any data is actually read.
\r
43 init_source (j_decompress_ptr cinfo)
\r
45 /* No work, since jpeg_memory_src set up the buffer pointer and count.
\r
46 * Indeed, if we want to read multiple JPEG images from one buffer,
\r
47 * this *must* not do anything to the pointer.
\r
54 * Fill the input buffer --- called whenever buffer is emptied.
\r
56 * In this application, this routine should never be called; if it is called,
\r
57 * the decompressor has overrun the end of the input buffer, implying we
\r
58 * supplied an incomplete or corrupt JPEG datastream. A simple error exit
\r
59 * might be the most appropriate response.
\r
61 * But what we choose to do in this code is to supply dummy EOI markers
\r
62 * in order to force the decompressor to finish processing and supply
\r
63 * some sort of output image, no matter how corrupted.
\r
67 fill_input_buffer (j_decompress_ptr cinfo)
\r
69 my_src_ptr src = (my_src_ptr) cinfo->src;
\r
71 WARNMS(cinfo, JWRN_JPEG_EOF);
\r
73 /* Create a fake EOI marker */
\r
74 src->eoi_buffer[0] = (JOCTET) 0xFF;
\r
75 src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
\r
76 src->pub.next_input_byte = src->eoi_buffer;
\r
77 src->pub.bytes_in_buffer = 2;
\r
84 * Skip data --- used to skip over a potentially large amount of
\r
85 * uninteresting data (such as an APPn marker).
\r
87 * If we overrun the end of the buffer, we let fill_input_buffer deal with
\r
88 * it. An extremely large skip could cause some time-wasting here, but
\r
89 * it really isn't supposed to happen ... and the decompressor will never
\r
90 * skip more than 64K anyway.
\r
94 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
\r
96 my_src_ptr src = (my_src_ptr) cinfo->src;
\r
98 if (num_bytes > 0) {
\r
99 while (num_bytes > (long) src->pub.bytes_in_buffer) {
\r
100 num_bytes -= (long) src->pub.bytes_in_buffer;
\r
101 (void) fill_input_buffer(cinfo);
\r
102 /* note we assume that fill_input_buffer will never return FALSE,
\r
103 * so suspension need not be handled.
\r
106 src->pub.next_input_byte += (size_t) num_bytes;
\r
107 src->pub.bytes_in_buffer -= (size_t) num_bytes;
\r
113 * An additional method that can be provided by data source modules is the
\r
114 * resync_to_restart method for error recovery in the presence of RST markers.
\r
115 * For the moment, this source module just uses the default resync method
\r
116 * provided by the JPEG library. That method assumes that no backtracking
\r
122 * Terminate source --- called by jpeg_finish_decompress
\r
123 * after all data has been read. Often a no-op.
\r
125 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
\r
126 * application must deal with any cleanup that should happen even
\r
131 term_source (j_decompress_ptr cinfo)
\r
133 /* no work necessary here */
\r
139 * Prepare for input from a memory buffer.
\r
143 jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
\r
147 /* The source object is made permanent so that a series of JPEG images
\r
148 * can be read from a single buffer by calling jpeg_memory_src
\r
149 * only before the first one.
\r
150 * This makes it unsafe to use this manager and a different source
\r
151 * manager serially with the same JPEG object. Caveat programmer.
\r
153 if (cinfo->src == NULL) { /* first time for this JPEG object? */
\r
154 cinfo->src = (struct jpeg_source_mgr *)
\r
155 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
\r
156 SIZEOF(my_source_mgr));
\r
159 src = (my_src_ptr) cinfo->src;
\r
160 src->pub.init_source = init_source;
\r
161 src->pub.fill_input_buffer = fill_input_buffer;
\r
162 src->pub.skip_input_data = skip_input_data;
\r
163 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
\r
164 src->pub.term_source = term_source;
\r
166 src->pub.next_input_byte = buffer;
\r
167 src->pub.bytes_in_buffer = bufsize;
\r