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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020