jdatasrc.c

Go to the documentation of this file.
00001 /*
00002  * jdatasrc.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * Modified 2009-2010 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains decompression data source routines for the case of
00010  * reading JPEG data from memory or from a file (or any stdio stream).
00011  * While these routines are sufficient for most applications,
00012  * some will want to use a different source manager.
00013  * IMPORTANT: we assume that fread() will correctly transcribe an array of
00014  * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
00015  * than 8 bits on your machine, you may need to do some tweaking.
00016  */
00017 
00018 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
00019 #include "jinclude.h"
00020 #include "jpeglib.h"
00021 #include "jerror.h"
00022 
00023 
00024 /* Expanded data source object for stdio input */
00025 
00026 typedef struct {
00027   struct jpeg_source_mgr pub;   /* public fields */
00028 
00029   FILE * infile;                /* source stream */
00030   JOCTET * buffer;              /* start of buffer */
00031   boolean start_of_file;        /* have we gotten any data yet? */
00032 } my_source_mgr;
00033 
00034 typedef my_source_mgr * my_src_ptr;
00035 
00036 #define INPUT_BUF_SIZE  4096    /* choose an efficiently fread'able size */
00037 
00038 
00039 /*
00040  * Initialize source --- called by jpeg_read_header
00041  * before any data is actually read.
00042  */
00043 
00044 METHODDEF(void)
00045 init_source (j_decompress_ptr cinfo)
00046 {
00047   my_src_ptr src = (my_src_ptr) cinfo->src;
00048 
00049   /* We reset the empty-input-file flag for each image,
00050    * but we don't clear the input buffer.
00051    * This is correct behavior for reading a series of images from one source.
00052    */
00053   src->start_of_file = TRUE;
00054 }
00055 
00056 METHODDEF(void)
00057 init_mem_source (j_decompress_ptr cinfo)
00058 {
00059   /* no work necessary here */
00060 }
00061 
00062 
00063 /*
00064  * Fill the input buffer --- called whenever buffer is emptied.
00065  *
00066  * In typical applications, this should read fresh data into the buffer
00067  * (ignoring the current state of next_input_byte & bytes_in_buffer),
00068  * reset the pointer & count to the start of the buffer, and return TRUE
00069  * indicating that the buffer has been reloaded.  It is not necessary to
00070  * fill the buffer entirely, only to obtain at least one more byte.
00071  *
00072  * There is no such thing as an EOF return.  If the end of the file has been
00073  * reached, the routine has a choice of ERREXIT() or inserting fake data into
00074  * the buffer.  In most cases, generating a warning message and inserting a
00075  * fake EOI marker is the best course of action --- this will allow the
00076  * decompressor to output however much of the image is there.  However,
00077  * the resulting error message is misleading if the real problem is an empty
00078  * input file, so we handle that case specially.
00079  *
00080  * In applications that need to be able to suspend compression due to input
00081  * not being available yet, a FALSE return indicates that no more data can be
00082  * obtained right now, but more may be forthcoming later.  In this situation,
00083  * the decompressor will return to its caller (with an indication of the
00084  * number of scanlines it has read, if any).  The application should resume
00085  * decompression after it has loaded more data into the input buffer.  Note
00086  * that there are substantial restrictions on the use of suspension --- see
00087  * the documentation.
00088  *
00089  * When suspending, the decompressor will back up to a convenient restart point
00090  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
00091  * indicate where the restart point will be if the current call returns FALSE.
00092  * Data beyond this point must be rescanned after resumption, so move it to
00093  * the front of the buffer rather than discarding it.
00094  */
00095 
00096 METHODDEF(boolean)
00097 fill_input_buffer (j_decompress_ptr cinfo)
00098 {
00099   my_src_ptr src = (my_src_ptr) cinfo->src;
00100   size_t nbytes;
00101 
00102   nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
00103 
00104   if (nbytes <= 0) {
00105     if (src->start_of_file)     /* Treat empty input file as fatal error */
00106       ERREXIT(cinfo, JERR_INPUT_EMPTY);
00107     WARNMS(cinfo, JWRN_JPEG_EOF);
00108     /* Insert a fake EOI marker */
00109     src->buffer[0] = (JOCTET) 0xFF;
00110     src->buffer[1] = (JOCTET) JPEG_EOI;
00111     nbytes = 2;
00112   }
00113 
00114   src->pub.next_input_byte = src->buffer;
00115   src->pub.bytes_in_buffer = nbytes;
00116   src->start_of_file = FALSE;
00117 
00118   return TRUE;
00119 }
00120 
00121 METHODDEF(boolean)
00122 fill_mem_input_buffer (j_decompress_ptr cinfo)
00123 {
00124   static JOCTET mybuffer[4];
00125 
00126   /* The whole JPEG data is expected to reside in the supplied memory
00127    * buffer, so any request for more data beyond the given buffer size
00128    * is treated as an error.
00129    */
00130   WARNMS(cinfo, JWRN_JPEG_EOF);
00131   /* Insert a fake EOI marker */
00132   mybuffer[0] = (JOCTET) 0xFF;
00133   mybuffer[1] = (JOCTET) JPEG_EOI;
00134 
00135   cinfo->src->next_input_byte = mybuffer;
00136   cinfo->src->bytes_in_buffer = 2;
00137 
00138   return TRUE;
00139 }
00140 
00141 
00142 /*
00143  * Skip data --- used to skip over a potentially large amount of
00144  * uninteresting data (such as an APPn marker).
00145  *
00146  * Writers of suspendable-input applications must note that skip_input_data
00147  * is not granted the right to give a suspension return.  If the skip extends
00148  * beyond the data currently in the buffer, the buffer can be marked empty so
00149  * that the next read will cause a fill_input_buffer call that can suspend.
00150  * Arranging for additional bytes to be discarded before reloading the input
00151  * buffer is the application writer's problem.
00152  */
00153 
00154 METHODDEF(void)
00155 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
00156 {
00157   struct jpeg_source_mgr * src = cinfo->src;
00158 
00159   /* Just a dumb implementation for now.  Could use fseek() except
00160    * it doesn't work on pipes.  Not clear that being smart is worth
00161    * any trouble anyway --- large skips are infrequent.
00162    */
00163   if (num_bytes > 0) {
00164     while (num_bytes > (long) src->bytes_in_buffer) {
00165       num_bytes -= (long) src->bytes_in_buffer;
00166       (void) (*src->fill_input_buffer) (cinfo);
00167       /* note we assume that fill_input_buffer will never return FALSE,
00168        * so suspension need not be handled.
00169        */
00170     }
00171     src->next_input_byte += (size_t) num_bytes;
00172     src->bytes_in_buffer -= (size_t) num_bytes;
00173   }
00174 }
00175 
00176 
00177 /*
00178  * An additional method that can be provided by data source modules is the
00179  * resync_to_restart method for error recovery in the presence of RST markers.
00180  * For the moment, this source module just uses the default resync method
00181  * provided by the JPEG library.  That method assumes that no backtracking
00182  * is possible.
00183  */
00184 
00185 
00186 /*
00187  * Terminate source --- called by jpeg_finish_decompress
00188  * after all data has been read.  Often a no-op.
00189  *
00190  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
00191  * application must deal with any cleanup that should happen even
00192  * for error exit.
00193  */
00194 
00195 METHODDEF(void)
00196 term_source (j_decompress_ptr cinfo)
00197 {
00198   /* no work necessary here */
00199 }
00200 
00201 
00202 /*
00203  * Prepare for input from a stdio stream.
00204  * The caller must have already opened the stream, and is responsible
00205  * for closing it after finishing decompression.
00206  */
00207 
00208 GLOBAL(void)
00209 jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
00210 {
00211   my_src_ptr src;
00212 
00213   /* The source object and input buffer are made permanent so that a series
00214    * of JPEG images can be read from the same file by calling jpeg_stdio_src
00215    * only before the first one.  (If we discarded the buffer at the end of
00216    * one image, we'd likely lose the start of the next one.)
00217    * This makes it unsafe to use this manager and a different source
00218    * manager serially with the same JPEG object.  Caveat programmer.
00219    */
00220   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
00221     cinfo->src = (struct jpeg_source_mgr *)
00222       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00223                                   SIZEOF(my_source_mgr));
00224     src = (my_src_ptr) cinfo->src;
00225     src->buffer = (JOCTET *)
00226       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00227                                   INPUT_BUF_SIZE * SIZEOF(JOCTET));
00228   }
00229 
00230   src = (my_src_ptr) cinfo->src;
00231   src->pub.init_source = init_source;
00232   src->pub.fill_input_buffer = fill_input_buffer;
00233   src->pub.skip_input_data = skip_input_data;
00234   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
00235   src->pub.term_source = term_source;
00236   src->infile = infile;
00237   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
00238   src->pub.next_input_byte = NULL; /* until buffer loaded */
00239 }
00240 
00241 
00242 /*
00243  * Prepare for input from a supplied memory buffer.
00244  * The buffer must contain the whole JPEG data.
00245  */
00246 
00247 GLOBAL(void)
00248 jpeg_mem_src (j_decompress_ptr cinfo,
00249               unsigned char * inbuffer, unsigned long insize)
00250 {
00251   struct jpeg_source_mgr * src;
00252 
00253   if (inbuffer == NULL || insize == 0)  /* Treat empty input as fatal error */
00254     ERREXIT(cinfo, JERR_INPUT_EMPTY);
00255 
00256   /* The source object is made permanent so that a series of JPEG images
00257    * can be read from the same buffer by calling jpeg_mem_src only before
00258    * the first one.
00259    */
00260   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
00261     cinfo->src = (struct jpeg_source_mgr *)
00262       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00263                                   SIZEOF(struct jpeg_source_mgr));
00264   }
00265 
00266   src = cinfo->src;
00267   src->init_source = init_mem_source;
00268   src->fill_input_buffer = fill_mem_input_buffer;
00269   src->skip_input_data = skip_input_data;
00270   src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
00271   src->term_source = term_source;
00272   src->bytes_in_buffer = (size_t) insize;
00273   src->next_input_byte = (JOCTET *) inbuffer;
00274 }

Generated on Tue Jul 5 14:13:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1