Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

jmemsrc.c

Go to the documentation of this file.
00001 /*************************************************************************
00008  *
00009  * Source code for Real Time Image Library (libRTImage)
00010  *
00011  * Leeds Vision Group give permission for this code to be copied, modified
00012  * and distributed within the University of Leeds subject to the following
00013  * conditions:-
00014  *
00015  * - The code is not to be used for commercial gain.
00016  * - The code and use thereof will be attributed to the authors where
00017  *   appropriate (including demonstrations which rely on it's use).
00018  * - All modified, distributions of the source files will retain this header.
00019  *
00020  *************************************************************************
00021  * Aphrodite Galata, March 2001
00022  *
00023  */
00024 
00025 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
00026 #include "jinclude.h"
00027 #include "jpeglib.h"
00028 #include "jerror.h"
00029 
00030 
00031 /* Expanded data source object for stdio input */
00032 typedef struct {
00033   struct jpeg_source_mgr pub;   /* public fields */
00034 
00035   unsigned char* membuff;       /* The input buffer */
00036   int location;                 /* Current location in buffer */ 
00037   int membufflength;            /* The length of the input buffer */
00038   JOCTET * buffer;              /* start of buffer */
00039   boolean start_of_buff;        /* have we gotten any data yet? */
00040 } mem_source_mgr;
00041 
00042 typedef mem_source_mgr* mem_src_ptr;
00043 
00044 #define INPUT_BUF_SIZE  4096    /* choose an efficiently fread'able size */
00045 
00046 
00047 METHODDEF(void)
00048 mem_init_source (j_decompress_ptr cinfo)
00049 {
00050   mem_src_ptr src;
00051 
00052   src = (mem_src_ptr) cinfo->src;
00053 
00054   /* We reset the empty-input-file flag for each image,
00055    * but we don't clear the input buffer.
00056    * This is correct behavior for reading a series of images from one source.
00057    */
00058   src->location = 0;
00059   src->start_of_buff = TRUE;
00060 }
00061 
00062 
00063 METHODDEF(boolean)
00064 mem_fill_input_buffer (j_decompress_ptr cinfo)
00065 {
00066   mem_src_ptr src;
00067   size_t bytes_to_read;
00068   size_t nbytes;
00069 
00070   src = (mem_src_ptr) cinfo->src;
00071 
00072   if((src->location)+INPUT_BUF_SIZE >= src->membufflength)
00073     bytes_to_read = src->membufflength - src->location;
00074   else
00075     bytes_to_read = INPUT_BUF_SIZE;
00076 
00077   memcpy(src->buffer, (src->membuff)+(src->location), bytes_to_read);
00078   nbytes = bytes_to_read;
00079   src->location += bytes_to_read;
00080 
00081   if (nbytes <= 0) {
00082     if (src->start_of_buff)     /* Treat empty input file as fatal error */
00083       ERREXIT(cinfo, JERR_INPUT_EMPTY);
00084     WARNMS(cinfo, JWRN_JPEG_EOF);
00085     /* Insert a fake EOI marker */
00086     src->buffer[0] = (JOCTET) 0xFF;
00087     src->buffer[1] = (JOCTET) JPEG_EOI;
00088     nbytes = 2;
00089   }
00090 
00091   src->pub.next_input_byte = src->buffer;
00092   src->pub.bytes_in_buffer = nbytes;
00093   src->start_of_buff = FALSE;
00094 
00095   return TRUE;
00096 }
00097 
00098 
00099 METHODDEF(void)
00100 mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
00101 {
00102   mem_src_ptr src;
00103 
00104   src = (mem_src_ptr) cinfo->src;
00105 
00106   /* Just a dumb implementation for now.  Could use fseek() except
00107    * it doesn't work on pipes.  Not clear that being smart is worth
00108    * any trouble anyway --- large skips are infrequent.
00109    */
00110   if (num_bytes > 0) {
00111     while (num_bytes > (long) src->pub.bytes_in_buffer) {
00112       num_bytes -= (long) src->pub.bytes_in_buffer;
00113       (void) mem_fill_input_buffer(cinfo);
00114       /* note we assume that fill_input_buffer will never return FALSE,
00115        * so suspension need not be handled.
00116        */
00117     }
00118     src->pub.next_input_byte += (size_t) num_bytes;
00119     src->pub.bytes_in_buffer -= (size_t) num_bytes;
00120   }
00121 }
00122 
00123 
00124 METHODDEF(void)
00125 mem_term_source (j_decompress_ptr cinfo)
00126 {
00127   /* no work necessary here */
00128 }
00129 
00130 
00131 GLOBAL(void)
00132 jpeg_mem_src (j_decompress_ptr cinfo, unsigned char *mbuff, int mbufflen)
00133 {
00134   mem_src_ptr src;
00135 
00136   /* The source object and input buffer are made permanent so that a series
00137    * of JPEG images can be read from the same file by calling jpeg_stdio_src
00138    * only before the first one.  (If we discarded the buffer at the end of
00139    * one image, we'd likely lose the start of the next one.)
00140    * This makes it unsafe to use this manager and a different source
00141    * manager serially with the same JPEG object.  Caveat programmer.
00142    */
00143   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
00144     cinfo->src = (struct jpeg_source_mgr *)
00145       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00146                                   SIZEOF(mem_source_mgr));
00147     src = (mem_src_ptr) cinfo->src;
00148     src->buffer = (JOCTET *)
00149       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00150                                   INPUT_BUF_SIZE * SIZEOF(JOCTET));
00151   }
00152 
00153   src = (mem_src_ptr) cinfo->src;
00154   src->pub.init_source = mem_init_source;
00155   src->pub.fill_input_buffer = mem_fill_input_buffer;
00156   src->pub.skip_input_data = mem_skip_input_data;
00157   src->pub.resync_to_restart = jpeg_resync_to_restart;
00158   src->pub.term_source = mem_term_source;
00159   src->membuff = mbuff;
00160   src->membufflength = mbufflen;
00161   src->pub.bytes_in_buffer = 0;    /* forces fill_input_buffer on first read */
00162   src->pub.next_input_byte = NULL; /* until buffer loaded */
00163 }

Generated at Fri Aug 13 17:29:21 2004 for libRTImage by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001