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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019