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