jmemsrc.c
Go to the documentation of this file.
1 /*
2  * jmemsrc.c
3  * input jpeg data for decompression from in-core memory
4  * Copyright (C) 1997, Toshihiro Matsui, Electrotechnical Laboratory
5  */
6 
7 #include <stdio.h>
8 #include <jpeglib.h>
9 #include <jerror.h>
10 
11 /* Expanded data source object for stdio input */
12 
13 typedef struct {
14  struct jpeg_source_mgr pub; /* public fields */
15 
16  JOCTET * buffer; /* start of buffer */
17  long data_count;
19 
21 
22 /*
23  * Initialize source --- called by jpeg_read_header
24  * before any data is actually read.
25  */
26 
27 METHODDEF(void)
28 init_source (j_decompress_ptr cinfo)
29 {
30  my_src_ptr src = (my_src_ptr) cinfo->src;
31 
32  /* We reset the empty-input-file flag for each image,
33  * but we don't clear the input buffer.
34  * This is correct behavior for reading a series of images from one source.
35  */
36  /* src->start_of_file = TRUE; */
37 }
38 
39 
40 /*
41  * Fill the input buffer --- called whenever buffer is emptied.
42  *
43  * In typical applications, this should read fresh data into the buffer
44  * (ignoring the current state of next_input_byte & bytes_in_buffer),
45  * reset the pointer & count to the start of the buffer, and return TRUE
46  * indicating that the buffer has been reloaded. It is not necessary to
47  * fill the buffer entirely, only to obtain at least one more byte.
48  *
49  * There is no such thing as an EOF return. If the end of the file has been
50  * reached, the routine has a choice of ERREXIT() or inserting fake data into
51  * the buffer. In most cases, generating a warning message and inserting a
52  * fake EOI marker is the best course of action --- this will allow the
53  * decompressor to output however much of the image is there. However,
54  * the resulting error message is misleading if the real problem is an empty
55  * input file, so we handle that case specially.
56  *
57  * In applications that need to be able to suspend compression due to input
58  * not being available yet, a FALSE return indicates that no more data can be
59  * obtained right now, but more may be forthcoming later. In this situation,
60  * the decompressor will return to its caller (with an indication of the
61  * number of scanlines it has read, if any). The application should resume
62  * decompression after it has loaded more data into the input buffer. Note
63  * that there are substantial restrictions on the use of suspension --- see
64  * the documentation.
65  *
66  * When suspending, the decompressor will back up to a convenient restart point
67  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
68  * indicate where the restart point will be if the current call returns FALSE.
69  * Data beyond this point must be rescanned after resumption, so move it to
70  * the front of the buffer rather than discarding it.
71  */
72 
73 METHODDEF(boolean)
74 fill_input_buffer (j_decompress_ptr cinfo)
75 {
76  my_src_ptr src = (my_src_ptr) cinfo->src;
77  size_t nbytes;
78 
79  nbytes = src->data_count;
80 
81  src->pub.next_input_byte = src->buffer;
82  src->pub.bytes_in_buffer = nbytes;
83 
84  return TRUE;
85 }
86 
87 
88 /*
89  * Skip data --- used to skip over a potentially large amount of
90  * uninteresting data (such as an APPn marker).
91  *
92  * Writers of suspendable-input applications must note that skip_input_data
93  * is not granted the right to give a suspension return. If the skip extends
94  * beyond the data currently in the buffer, the buffer can be marked empty so
95  * that the next read will cause a fill_input_buffer call that can suspend.
96  * Arranging for additional bytes to be discarded before reloading the input
97  * buffer is the application writer's problem.
98  */
99 
100 METHODDEF(void)
101 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
102 {
103  my_src_ptr src = (my_src_ptr) cinfo->src;
104 
105  /* Just a dumb implementation for now. Could use fseek() except
106  * it doesn't work on pipes. Not clear that being smart is worth
107  * any trouble anyway --- large skips are infrequent.
108  */
109  if (num_bytes > 0) {
110  if (num_bytes > (long) src->pub.bytes_in_buffer) {
111  fprintf(stderr, "jpeg memory source: skip beyond end-of-data %ld\n", num_bytes);
112  }
113  src->pub.next_input_byte += (size_t) num_bytes;
114  src->pub.bytes_in_buffer -= (size_t) num_bytes;
115  }
116 }
117 
118 
119 /*
120  * An additional method that can be provided by data source modules is the
121  * resync_to_restart method for error recovery in the presence of RST markers.
122  * For the moment, this source module just uses the default resync method
123  * provided by the JPEG library. That method assumes that no backtracking
124  * is possible.
125  */
126 
127 
128 /*
129  * Terminate source --- called by jpeg_finish_decompress
130  * after all data has been read. Often a no-op.
131  *
132  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
133  * application must deal with any cleanup that should happen even
134  * for error exit.
135  */
136 
137 METHODDEF(void)
138 term_source (j_decompress_ptr cinfo)
139 {
140  /* no work necessary here */
141 }
142 
143 
144 /*
145  * Prepare for input from a stdio stream.
146  * The caller must have already opened the stream, and is responsible
147  * for closing it after finishing decompression.
148  */
149 
150 GLOBAL(void)
151 jpeg_memio_src (j_decompress_ptr cinfo, JOCTET *buf, long size)
152 {
153  my_src_ptr src;
154 
155  /* The source object and input buffer are made permanent so that a series
156  * of JPEG images can be read from the same file by calling jpeg_stdio_src
157  * only before the first one. (If we discarded the buffer at the end of
158  * one image, we'd likely lose the start of the next one.)
159  * This makes it unsafe to use this manager and a different source
160  * manager serially with the same JPEG object. Caveat programmer.
161  */
162  if (cinfo->src == NULL) { /* first time for this JPEG object? */
163  cinfo->src = (struct jpeg_source_mgr *)
164  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
165  sizeof(my_source_mgr));
166  src = (my_src_ptr) cinfo->src;
167  }
168 
169  src = (my_src_ptr) cinfo->src;
170 
171  src->buffer = (JOCTET *)buf;
172  src->data_count = size;
173 
174  src->pub.init_source = init_source;
175  src->pub.fill_input_buffer = fill_input_buffer;
176  src->pub.skip_input_data = skip_input_data;
177  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
178  src->pub.term_source = term_source;
179  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
180  src->pub.next_input_byte = NULL; /* until buffer loaded */
181 }
long data_count
Definition: jmemsrc.c:17
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
Definition: jmemsrc.c:101
JOCTET * buffer
Definition: jmemsrc.c:16
init_source(j_decompress_ptr cinfo)
Definition: jmemsrc.c:28
jpeg_memio_src(j_decompress_ptr cinfo, JOCTET *buf, long size)
Definition: jmemsrc.c:151
#define TRUE
Definition: arith.h:19
#define NULL
Definition: transargv.c:8
struct jpeg_source_mgr pub
Definition: jmemsrc.c:14
term_source(j_decompress_ptr cinfo)
Definition: jmemsrc.c:138
my_source_mgr * my_src_ptr
Definition: jmemsrc.c:20
static char buf[CHAR_SIZE]
Definition: helpsub.c:23
if(n==1)
Definition: unixcall.c:491
fill_input_buffer(j_decompress_ptr cinfo)
Definition: jmemsrc.c:74


euslisp
Author(s): Toshihiro Matsui
autogenerated on Fri Feb 21 2020 03:20:54