jmemsrc.c
Go to the documentation of this file.
00001 /*
00002  * jmemsrc.c
00003  * input jpeg data for decompression from in-core memory 
00004  * Copyright (C) 1997, Toshihiro Matsui, Electrotechnical Laboratory
00005  */
00006 
00007 #include <stdio.h>
00008 #include <jpeglib.h>
00009 #include <jerror.h>
00010 
00011 /* Expanded data source object for stdio input */
00012 
00013 typedef struct {
00014   struct jpeg_source_mgr pub;   /* public fields */
00015 
00016   JOCTET * buffer;              /* start of buffer */
00017   long   data_count;
00018 } my_source_mgr;
00019 
00020 typedef my_source_mgr * my_src_ptr;
00021 
00022 /*
00023  * Initialize source --- called by jpeg_read_header
00024  * before any data is actually read.
00025  */
00026 
00027 METHODDEF(void)
00028 init_source (j_decompress_ptr cinfo)
00029 {
00030   my_src_ptr src = (my_src_ptr) cinfo->src;
00031 
00032   /* We reset the empty-input-file flag for each image,
00033    * but we don't clear the input buffer.
00034    * This is correct behavior for reading a series of images from one source.
00035    */
00036   /*  src->start_of_file = TRUE; */
00037 }
00038 
00039 
00040 /*
00041  * Fill the input buffer --- called whenever buffer is emptied.
00042  *
00043  * In typical applications, this should read fresh data into the buffer
00044  * (ignoring the current state of next_input_byte & bytes_in_buffer),
00045  * reset the pointer & count to the start of the buffer, and return TRUE
00046  * indicating that the buffer has been reloaded.  It is not necessary to
00047  * fill the buffer entirely, only to obtain at least one more byte.
00048  *
00049  * There is no such thing as an EOF return.  If the end of the file has been
00050  * reached, the routine has a choice of ERREXIT() or inserting fake data into
00051  * the buffer.  In most cases, generating a warning message and inserting a
00052  * fake EOI marker is the best course of action --- this will allow the
00053  * decompressor to output however much of the image is there.  However,
00054  * the resulting error message is misleading if the real problem is an empty
00055  * input file, so we handle that case specially.
00056  *
00057  * In applications that need to be able to suspend compression due to input
00058  * not being available yet, a FALSE return indicates that no more data can be
00059  * obtained right now, but more may be forthcoming later.  In this situation,
00060  * the decompressor will return to its caller (with an indication of the
00061  * number of scanlines it has read, if any).  The application should resume
00062  * decompression after it has loaded more data into the input buffer.  Note
00063  * that there are substantial restrictions on the use of suspension --- see
00064  * the documentation.
00065  *
00066  * When suspending, the decompressor will back up to a convenient restart point
00067  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
00068  * indicate where the restart point will be if the current call returns FALSE.
00069  * Data beyond this point must be rescanned after resumption, so move it to
00070  * the front of the buffer rather than discarding it.
00071  */
00072 
00073 METHODDEF(boolean)
00074 fill_input_buffer (j_decompress_ptr cinfo)
00075 {
00076   my_src_ptr src = (my_src_ptr) cinfo->src;
00077   size_t nbytes;
00078 
00079   nbytes = src->data_count;
00080 
00081   src->pub.next_input_byte = src->buffer;
00082   src->pub.bytes_in_buffer = nbytes;
00083 
00084   return TRUE;
00085 }
00086 
00087 
00088 /*
00089  * Skip data --- used to skip over a potentially large amount of
00090  * uninteresting data (such as an APPn marker).
00091  *
00092  * Writers of suspendable-input applications must note that skip_input_data
00093  * is not granted the right to give a suspension return.  If the skip extends
00094  * beyond the data currently in the buffer, the buffer can be marked empty so
00095  * that the next read will cause a fill_input_buffer call that can suspend.
00096  * Arranging for additional bytes to be discarded before reloading the input
00097  * buffer is the application writer's problem.
00098  */
00099 
00100 METHODDEF(void)
00101 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
00102 {
00103   my_src_ptr src = (my_src_ptr) cinfo->src;
00104 
00105   /* Just a dumb implementation for now.  Could use fseek() except
00106    * it doesn't work on pipes.  Not clear that being smart is worth
00107    * any trouble anyway --- large skips are infrequent.
00108    */
00109   if (num_bytes > 0) {
00110     if (num_bytes > (long) src->pub.bytes_in_buffer) {
00111       fprintf(stderr, "jpeg memory source: skip beyond end-of-data %ld\n", num_bytes);
00112     }
00113     src->pub.next_input_byte += (size_t) num_bytes;
00114     src->pub.bytes_in_buffer -= (size_t) num_bytes;
00115   }
00116 }
00117 
00118 
00119 /*
00120  * An additional method that can be provided by data source modules is the
00121  * resync_to_restart method for error recovery in the presence of RST markers.
00122  * For the moment, this source module just uses the default resync method
00123  * provided by the JPEG library.  That method assumes that no backtracking
00124  * is possible.
00125  */
00126 
00127 
00128 /*
00129  * Terminate source --- called by jpeg_finish_decompress
00130  * after all data has been read.  Often a no-op.
00131  *
00132  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
00133  * application must deal with any cleanup that should happen even
00134  * for error exit.
00135  */
00136 
00137 METHODDEF(void)
00138 term_source (j_decompress_ptr cinfo)
00139 {
00140   /* no work necessary here */
00141 }
00142 
00143 
00144 /*
00145  * Prepare for input from a stdio stream.
00146  * The caller must have already opened the stream, and is responsible
00147  * for closing it after finishing decompression.
00148  */
00149 
00150 GLOBAL(void)
00151 jpeg_memio_src (j_decompress_ptr cinfo, JOCTET *buf, long size)
00152 {
00153   my_src_ptr src;
00154 
00155   /* The source object and input buffer are made permanent so that a series
00156    * of JPEG images can be read from the same file by calling jpeg_stdio_src
00157    * only before the first one.  (If we discarded the buffer at the end of
00158    * one image, we'd likely lose the start of the next one.)
00159    * This makes it unsafe to use this manager and a different source
00160    * manager serially with the same JPEG object.  Caveat programmer.
00161    */
00162   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
00163     cinfo->src = (struct jpeg_source_mgr *)
00164       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00165                                   sizeof(my_source_mgr));
00166     src = (my_src_ptr) cinfo->src;
00167   }
00168 
00169   src = (my_src_ptr) cinfo->src;
00170 
00171   src->buffer = (JOCTET *)buf;
00172   src->data_count = size;
00173 
00174   src->pub.init_source = init_source;
00175   src->pub.fill_input_buffer = fill_input_buffer;
00176   src->pub.skip_input_data = skip_input_data;
00177   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
00178   src->pub.term_source = term_source;
00179   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
00180   src->pub.next_input_byte = NULL; /* until buffer loaded */
00181 }


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 18:05:53