1 /* |
1 /* |
2 * jdatasrc.c |
2 * jdatasrc.c |
3 * |
3 * |
4 * Copyright (C) 1994-1996, Thomas G. Lane. |
4 * Copyright (C) 1994-1996, Thomas G. Lane. |
|
5 * Modified 2009 by Guido Vollbeding. |
5 * This file is part of the Independent JPEG Group's software. |
6 * This file is part of the Independent JPEG Group's software. |
6 * For conditions of distribution and use, see the accompanying README file. |
7 * For conditions of distribution and use, see the accompanying README file. |
7 * |
8 * |
8 * This file contains decompression data source routines for the case of |
9 * This file contains decompression data source routines for the case of |
9 * reading JPEG data from a file (or any stdio stream). While these routines |
10 * reading JPEG data from memory or from a file (or any stdio stream). |
10 * are sufficient for most applications, some will want to use a different |
11 * While these routines are sufficient for most applications, |
11 * source manager. |
12 * some will want to use a different source manager. |
12 * IMPORTANT: we assume that fread() will correctly transcribe an array of |
13 * IMPORTANT: we assume that fread() will correctly transcribe an array of |
13 * JOCTETs from 8-bit-wide elements on external storage. If char is wider |
14 * JOCTETs from 8-bit-wide elements on external storage. If char is wider |
14 * than 8 bits on your machine, you may need to do some tweaking. |
15 * than 8 bits on your machine, you may need to do some tweaking. |
15 */ |
16 */ |
16 |
17 |
48 /* We reset the empty-input-file flag for each image, |
49 /* We reset the empty-input-file flag for each image, |
49 * but we don't clear the input buffer. |
50 * but we don't clear the input buffer. |
50 * This is correct behavior for reading a series of images from one source. |
51 * This is correct behavior for reading a series of images from one source. |
51 */ |
52 */ |
52 src->start_of_file = TRUE; |
53 src->start_of_file = TRUE; |
|
54 } |
|
55 |
|
56 METHODDEF(void) |
|
57 init_mem_source (j_decompress_ptr cinfo) |
|
58 { |
|
59 /* no work necessary here */ |
53 } |
60 } |
54 |
61 |
55 |
62 |
56 /* |
63 /* |
57 * Fill the input buffer --- called whenever buffer is emptied. |
64 * Fill the input buffer --- called whenever buffer is emptied. |
109 src->start_of_file = FALSE; |
116 src->start_of_file = FALSE; |
110 |
117 |
111 return TRUE; |
118 return TRUE; |
112 } |
119 } |
113 |
120 |
|
121 METHODDEF(boolean) |
|
122 fill_mem_input_buffer (j_decompress_ptr cinfo) |
|
123 { |
|
124 static JOCTET mybuffer[4]; |
|
125 |
|
126 /* The whole JPEG data is expected to reside in the supplied memory |
|
127 * buffer, so any request for more data beyond the given buffer size |
|
128 * is treated as an error. |
|
129 */ |
|
130 WARNMS(cinfo, JWRN_JPEG_EOF); |
|
131 /* Insert a fake EOI marker */ |
|
132 mybuffer[0] = (JOCTET) 0xFF; |
|
133 mybuffer[1] = (JOCTET) JPEG_EOI; |
|
134 |
|
135 cinfo->src->next_input_byte = mybuffer; |
|
136 cinfo->src->bytes_in_buffer = 2; |
|
137 |
|
138 return TRUE; |
|
139 } |
|
140 |
114 |
141 |
115 /* |
142 /* |
116 * Skip data --- used to skip over a potentially large amount of |
143 * Skip data --- used to skip over a potentially large amount of |
117 * uninteresting data (such as an APPn marker). |
144 * uninteresting data (such as an APPn marker). |
118 * |
145 * |
125 */ |
152 */ |
126 |
153 |
127 METHODDEF(void) |
154 METHODDEF(void) |
128 skip_input_data (j_decompress_ptr cinfo, long num_bytes) |
155 skip_input_data (j_decompress_ptr cinfo, long num_bytes) |
129 { |
156 { |
130 my_src_ptr src = (my_src_ptr) cinfo->src; |
157 struct jpeg_source_mgr * src = cinfo->src; |
131 |
158 |
132 /* Just a dumb implementation for now. Could use fseek() except |
159 /* Just a dumb implementation for now. Could use fseek() except |
133 * it doesn't work on pipes. Not clear that being smart is worth |
160 * it doesn't work on pipes. Not clear that being smart is worth |
134 * any trouble anyway --- large skips are infrequent. |
161 * any trouble anyway --- large skips are infrequent. |
135 */ |
162 */ |
136 if (num_bytes > 0) { |
163 if (num_bytes > 0) { |
137 while (num_bytes > (long) src->pub.bytes_in_buffer) { |
164 while (num_bytes > (long) src->bytes_in_buffer) { |
138 num_bytes -= (long) src->pub.bytes_in_buffer; |
165 num_bytes -= (long) src->bytes_in_buffer; |
139 (void) fill_input_buffer(cinfo); |
166 (void) fill_input_buffer(cinfo); |
140 /* note we assume that fill_input_buffer will never return FALSE, |
167 /* note we assume that fill_input_buffer will never return FALSE, |
141 * so suspension need not be handled. |
168 * so suspension need not be handled. |
142 */ |
169 */ |
143 } |
170 } |
144 src->pub.next_input_byte += (size_t) num_bytes; |
171 src->next_input_byte += (size_t) num_bytes; |
145 src->pub.bytes_in_buffer -= (size_t) num_bytes; |
172 src->bytes_in_buffer -= (size_t) num_bytes; |
146 } |
173 } |
147 } |
174 } |
148 |
175 |
149 |
176 |
150 /* |
177 /* |
208 src->pub.term_source = term_source; |
235 src->pub.term_source = term_source; |
209 src->infile = infile; |
236 src->infile = infile; |
210 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ |
237 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ |
211 src->pub.next_input_byte = NULL; /* until buffer loaded */ |
238 src->pub.next_input_byte = NULL; /* until buffer loaded */ |
212 } |
239 } |
|
240 |
|
241 |
|
242 /* |
|
243 * Prepare for input from a supplied memory buffer. |
|
244 * The buffer must contain the whole JPEG data. |
|
245 */ |
|
246 |
|
247 GLOBAL(void) |
|
248 jpeg_mem_src (j_decompress_ptr cinfo, |
|
249 unsigned char * inbuffer, unsigned long insize) |
|
250 { |
|
251 struct jpeg_source_mgr * src; |
|
252 |
|
253 if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ |
|
254 ERREXIT(cinfo, JERR_INPUT_EMPTY); |
|
255 |
|
256 /* The source object is made permanent so that a series of JPEG images |
|
257 * can be read from the same buffer by calling jpeg_mem_src only before |
|
258 * the first one. |
|
259 */ |
|
260 if (cinfo->src == NULL) { /* first time for this JPEG object? */ |
|
261 cinfo->src = (struct jpeg_source_mgr *) |
|
262 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
|
263 SIZEOF(struct jpeg_source_mgr)); |
|
264 } |
|
265 |
|
266 src = cinfo->src; |
|
267 src->init_source = init_mem_source; |
|
268 src->fill_input_buffer = fill_mem_input_buffer; |
|
269 src->skip_input_data = skip_input_data; |
|
270 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ |
|
271 src->term_source = term_source; |
|
272 src->bytes_in_buffer = (size_t) insize; |
|
273 src->next_input_byte = (JOCTET *) inbuffer; |
|
274 } |