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