SLIP  1.4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JpegDef.hpp
Go to the documentation of this file.
1 
9 #ifndef JPEGDEF_HPP_
10 #define JPEGDEF_HPP_
11 
16 extern "C" {
17 #include <stdio.h>
18 #include <jpeglib.h>
19 #include <setjmp.h>
20 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
21 #include "jerror.h"
22 }
23 
24 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
25 extern void * malloc JPP((size_t size));
26 extern void free JPP((void *ptr));
27 #endif
28 
29 
30 /*
31  * In ANSI C, and indeed any rational implementation, size_t is also the
32  * type returned by sizeof(). However, it seems there are some irrational
33  * implementations out there, in which sizeof() returns an int even though
34  * size_t is defined as long or unsigned long. To ensure consistent results
35  * we always use this SIZEOF() macro in place of using sizeof() directly.
36  */
37 #define SIZEOF(object) ((size_t) sizeof(object))
38 /*
39  * The modules that use fread() and fwrite() always invoke them through
40  * these macros. On some systems you may need to twiddle the argument casts.
41  * CAUTION: argument order is different from underlying functions!
42  */
43 #define JFREAD(file,buf,sizeofbuf) \
44  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
45 #define JFWRITE(file,buf,sizeofbuf) \
46  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
47 
48 
49 
50 
55 
63 typedef struct {
64  struct jpeg_error_mgr pub; /* "public" fields */
65  jmp_buf setjmp_buffer; /* for return to caller */
66 } my_error_mgr;
67 
68 typedef my_error_mgr * my_error_ptr;
69 
70 
74 inline
75 void my_error_exit(j_common_ptr cinfo)
76 {
77  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
78  my_error_ptr myerr = (my_error_ptr) cinfo->err;
79 
80  /* Always display the message. */
81  /* We could postpone this until after returning, if we chose. */
82  //(*cinfo->err->output_message) (cinfo);
83 
84  /* Return control to the setjmp point */
85  longjmp(myerr->setjmp_buffer, 1);
86 }
87 
88 typedef struct jpeg_decompress_struct my_jpeg_decompress_struct;
89 typedef struct jpeg_compress_struct my_jpeg_compress_struct;
90 
91 
98 
99 
104 typedef struct {
105  struct jpeg_destination_mgr pub; /* public fields */
106  FILE * outfile; /* target stream */
107  JOCTET * buffer; /* start of buffer */
108 } slip_destination_mgr;
109 
110 typedef slip_destination_mgr * slip_dest_ptr;
111 
112 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
113 
118 METHODDEF(void)
119 slip_init_destination (j_compress_ptr cinfo)
120 {
121  slip_dest_ptr dest = (slip_dest_ptr) cinfo->dest;
122 
123  /* Allocate the output buffer --- it will be released when done with image */
124  dest->buffer = (JOCTET *)
125  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
126  OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
127 
128  dest->pub.next_output_byte = dest->buffer;
129  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
130 }
131 
155 METHODDEF(boolean)
156 slip_empty_output_buffer (j_compress_ptr cinfo)
157 {
158  slip_dest_ptr dest = (slip_dest_ptr) cinfo->dest;
159 
160  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
161  (size_t) OUTPUT_BUF_SIZE)
162  ERREXIT(cinfo, JERR_FILE_WRITE);
163 
164  dest->pub.next_output_byte = dest->buffer;
165  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
166 
167  return TRUE;
168 }
169 
170 /*
171  * Terminate destination --- called by jpeg_finish_compress
172  * after all data has been written. Usually needs to flush buffer.
173  *
174  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
175  * application must deal with any cleanup that should happen even
176  * for error exit.
177  */
178 
179 METHODDEF(void)
180 slip_term_destination (j_compress_ptr cinfo)
181 {
182  slip_dest_ptr dest = (slip_dest_ptr) cinfo->dest;
183  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
184 
185  /* Write any data remaining in the buffer */
186  if (datacount > 0) {
187  if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
188  ERREXIT(cinfo, JERR_FILE_WRITE);
189  }
190  fflush(dest->outfile);
191  /* Make sure we wrote the output file OK */
192  if (ferror(dest->outfile))
193  ERREXIT(cinfo, JERR_FILE_WRITE);
194 }
195 
196 
202 GLOBAL(void)
203 slip_jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
204 {
205  slip_dest_ptr dest;
206 
207  /* The destination object is made permanent so that multiple JPEG images
208  * can be written to the same file without re-executing jpeg_stdio_dest.
209  * This makes it dangerous to use this manager and a different destination
210  * manager serially with the same JPEG object, because their private object
211  * sizes may be different. Caveat programmer.
212  */
213  if (cinfo->dest == 0) { /* first time for this JPEG object? */
214  cinfo->dest = (struct jpeg_destination_mgr *)
215  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
216  SIZEOF(slip_destination_mgr));
217  }
218 
219  dest = (slip_dest_ptr) cinfo->dest;
220  dest->pub.init_destination = slip_init_destination;
221  dest->pub.empty_output_buffer = slip_empty_output_buffer;
222  dest->pub.term_destination = slip_term_destination;
223  dest->outfile = outfile;
224 }
225 
226 
227 
234 
235 
236 /* Expanded data source object for stdio input */
237 
238 typedef struct {
239  struct jpeg_source_mgr pub; /* public fields */
240 
241  FILE * infile; /* source stream */
242  JOCTET * buffer; /* start of buffer */
243  boolean start_of_file; /* have we gotten any data yet? */
244 } slip_source_mgr;
245 
246 typedef slip_source_mgr * slip_src_ptr;
247 
248 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
249 
250 
251 /*
252  * Initialize source --- called by jpeg_read_header
253  * before any data is actually read.
254  */
255 
256 METHODDEF(void)
257 slip_init_source (j_decompress_ptr cinfo)
258 {
259  slip_src_ptr src = (slip_src_ptr) cinfo->src;
260 
261  /* We reset the empty-input-file flag for each image,
262  * but we don't clear the input buffer.
263  * This is correct behavior for reading a series of images from one source.
264  */
265  src->start_of_file = TRUE;
266 }
267 
268 /*
269  * Fill the input buffer --- called whenever buffer is emptied.
270  *
271  * In typical applications, this should read fresh data into the buffer
272  * (ignoring the current state of next_input_byte & bytes_in_buffer),
273  * reset the pointer & count to the start of the buffer, and return TRUE
274  * indicating that the buffer has been reloaded. It is not necessary to
275  * fill the buffer entirely, only to obtain at least one more byte.
276  *
277  * There is no such thing as an EOF return. If the end of the file has been
278  * reached, the routine has a choice of ERREXIT() or inserting fake data into
279  * the buffer. In most cases, generating a warning message and inserting a
280  * fake EOI marker is the best course of action --- this will allow the
281  * decompressor to output however much of the image is there. However,
282  * the resulting error message is misleading if the real problem is an empty
283  * input file, so we handle that case specially.
284  *
285  * In applications that need to be able to suspend compression due to input
286  * not being available yet, a FALSE return indicates that no more data can be
287  * obtained right now, but more may be forthcoming later. In this situation,
288  * the decompressor will return to its caller (with an indication of the
289  * number of scanlines it has read, if any). The application should resume
290  * decompression after it has loaded more data into the input buffer. Note
291  * that there are substantial restrictions on the use of suspension --- see
292  * the documentation.
293  *
294  * When suspending, the decompressor will back up to a convenient restart point
295  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
296  * indicate where the restart point will be if the current call returns FALSE.
297  * Data beyond this point must be rescanned after resumption, so move it to
298  * the front of the buffer rather than discarding it.
299  */
300 
301 METHODDEF(boolean)
302 slip_fill_input_buffer (j_decompress_ptr cinfo)
303 {
304  slip_src_ptr src = (slip_src_ptr) cinfo->src;
305  size_t nbytes;
306 
307  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
308 
309  if (nbytes <= 0) {
310  if (src->start_of_file) /* Treat empty input file as fatal error */
311  ERREXIT(cinfo, JERR_INPUT_EMPTY);
312  WARNMS(cinfo, JWRN_JPEG_EOF);
313  /* Insert a fake EOI marker */
314  src->buffer[0] = (JOCTET) 0xFF;
315  src->buffer[1] = (JOCTET) JPEG_EOI;
316  nbytes = 2;
317  }
318 
319  src->pub.next_input_byte = src->buffer;
320  src->pub.bytes_in_buffer = nbytes;
321  src->start_of_file = FALSE;
322 
323  return TRUE;
324 }
325 
326 /*
327  * Skip data --- used to skip over a potentially large amount of
328  * uninteresting data (such as an APPn marker).
329  *
330  * Writers of suspendable-input applications must note that skip_input_data
331  * is not granted the right to give a suspension return. If the skip extends
332  * beyond the data currently in the buffer, the buffer can be marked empty so
333  * that the next read will cause a fill_input_buffer call that can suspend.
334  * Arranging for additional bytes to be discarded before reloading the input
335  * buffer is the application writer's problem.
336  */
337 
338 METHODDEF(void)
339 slip_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
340 {
341  struct jpeg_source_mgr * src = cinfo->src;
342 
343  /* Just a dumb implementation for now. Could use fseek() except
344  * it doesn't work on pipes. Not clear that being smart is worth
345  * any trouble anyway --- large skips are infrequent.
346  */
347  if (num_bytes > 0) {
348  while (num_bytes > (long) src->bytes_in_buffer) {
349  num_bytes -= (long) src->bytes_in_buffer;
350  (void) (*src->fill_input_buffer) (cinfo);
351  /* note we assume that fill_input_buffer will never return FALSE,
352  * so suspension need not be handled.
353  */
354  }
355  src->next_input_byte += (size_t) num_bytes;
356  src->bytes_in_buffer -= (size_t) num_bytes;
357  }
358 }
359 
360 
361 /*
362  * An additional method that can be provided by data source modules is the
363  * resync_to_restart method for error recovery in the presence of RST markers.
364  * For the moment, this source module just uses the default resync method
365  * provided by the JPEG library. That method assumes that no backtracking
366  * is possible.
367  */
368 
369 
370 /*
371  * Terminate source --- called by jpeg_finish_decompress
372  * after all data has been read. Often a no-op.
373  *
374  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
375  * application must deal with any cleanup that should happen even
376  * for error exit.
377  */
378 
379 METHODDEF(void)
380 slip_term_source (j_decompress_ptr cinfo)
381 {
382  /* no work necessary here */
383 }
384 
385 
386 /*
387  * Prepare for input from a stdio stream.
388  * The caller must have already opened the stream, and is responsible
389  * for closing it after finishing decompression.
390  */
391 
392 GLOBAL(void)
393 slip_jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
394 {
395  slip_src_ptr src;
396 
397  /* The source object and input buffer are made permanent so that a series
398  * of JPEG images can be read from the same file by calling jpeg_stdio_src
399  * only before the first one. (If we discarded the buffer at the end of
400  * one image, we'd likely lose the start of the next one.)
401  * This makes it unsafe to use this manager and a different source
402  * manager serially with the same JPEG object. Caveat programmer.
403  */
404  if (cinfo->src == 0) { /* first time for this JPEG object? */
405  cinfo->src = (struct jpeg_source_mgr *)
406  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
407  SIZEOF(slip_source_mgr));
408  src = (slip_src_ptr) cinfo->src;
409  src->buffer = (JOCTET *)
410  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
411  INPUT_BUF_SIZE * SIZEOF(JOCTET));
412  }
413 
414  src = (slip_src_ptr) cinfo->src;
415  src->pub.init_source = slip_init_source;
416  src->pub.fill_input_buffer = slip_fill_input_buffer;
417  src->pub.skip_input_data = slip_skip_input_data;
418  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
419  src->pub.term_source = slip_term_source;
420  src->infile = infile;
421  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
422  src->pub.next_input_byte = 0; /* until buffer loaded */
423 }
424 
429 #endif /* JPEGDEF_HPP_ */
430 
431