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