00001 /* 00002 * memsrc.c 00003 * 00004 * Copyright (C) 1994-1996, Thomas G. Lane. 00005 * This file is part of the Independent JPEG Group's software. 00006 * For conditions of distribution and use, see the accompanying README file. 00007 * 00008 * This file contains decompression data source routines for the case of 00009 * reading JPEG data from a memory buffer that is preloaded with the entire 00010 * JPEG file. This would not seem especially useful at first sight, but 00011 * a number of people have asked for it. 00012 * This is really just a stripped-down version of jdatasrc.c. Comparison 00013 * of this code with jdatasrc.c may be helpful in seeing how to make 00014 * custom source managers for other purposes. 00015 */ 00016 00017 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 00018 #include "jinclude.h" 00019 #include "jpeglib.h" 00020 #include "jerror.h" 00021 00022 00023 /* Expanded data source object for memory input */ 00024 00025 typedef struct { 00026 struct jpeg_source_mgr pub; /* public fields */ 00027 00028 JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */ 00029 } my_source_mgr; 00030 00031 typedef my_source_mgr * my_src_ptr; 00032 00033 00034 /* 00035 * Initialize source --- called by jpeg_read_header 00036 * before any data is actually read. 00037 */ 00038 00039 METHODDEF(void) 00040 init_source (j_decompress_ptr cinfo) 00041 { 00042 /* No work, since jpeg_memory_src set up the buffer pointer and count. 00043 * Indeed, if we want to read multiple JPEG images from one buffer, 00044 * this *must* not do anything to the pointer. 00045 */ 00046 } 00047 00048 00049 /* 00050 * Fill the input buffer --- called whenever buffer is emptied. 00051 * 00052 * In this application, this routine should never be called; if it is called, 00053 * the decompressor has overrun the end of the input buffer, implying we 00054 * supplied an incomplete or corrupt JPEG datastream. A simple error exit 00055 * might be the most appropriate response. 00056 * 00057 * But what we choose to do in this code is to supply dummy EOI markers 00058 * in order to force the decompressor to finish processing and supply 00059 * some sort of output image, no matter how corrupted. 00060 */ 00061 00062 METHODDEF(boolean) 00063 fill_input_buffer (j_decompress_ptr cinfo) 00064 { 00065 my_src_ptr src = (my_src_ptr) cinfo->src; 00066 00067 WARNMS(cinfo, JWRN_JPEG_EOF); 00068 00069 /* Create a fake EOI marker */ 00070 src->eoi_buffer[0] = (JOCTET) 0xFF; 00071 src->eoi_buffer[1] = (JOCTET) JPEG_EOI; 00072 src->pub.next_input_byte = src->eoi_buffer; 00073 src->pub.bytes_in_buffer = 2; 00074 00075 return TRUE; 00076 } 00077 00078 00079 /* 00080 * Skip data --- used to skip over a potentially large amount of 00081 * uninteresting data (such as an APPn marker). 00082 * 00083 * If we overrun the end of the buffer, we let fill_input_buffer deal with 00084 * it. An extremely large skip could cause some time-wasting here, but 00085 * it really isn't supposed to happen ... and the decompressor will never 00086 * skip more than 64K anyway. 00087 */ 00088 00089 METHODDEF(void) 00090 skip_input_data (j_decompress_ptr cinfo, long num_bytes) 00091 { 00092 my_src_ptr src = (my_src_ptr) cinfo->src; 00093 00094 if (num_bytes > 0) { 00095 while (num_bytes > (long) src->pub.bytes_in_buffer) { 00096 num_bytes -= (long) src->pub.bytes_in_buffer; 00097 (void) fill_input_buffer(cinfo); 00098 /* note we assume that fill_input_buffer will never return FALSE, 00099 * so suspension need not be handled. 00100 */ 00101 } 00102 src->pub.next_input_byte += (size_t) num_bytes; 00103 src->pub.bytes_in_buffer -= (size_t) num_bytes; 00104 } 00105 } 00106 00107 00108 /* 00109 * An additional method that can be provided by data source modules is the 00110 * resync_to_restart method for error recovery in the presence of RST markers. 00111 * For the moment, this source module just uses the default resync method 00112 * provided by the JPEG library. That method assumes that no backtracking 00113 * is possible. 00114 */ 00115 00116 00117 /* 00118 * Terminate source --- called by jpeg_finish_decompress 00119 * after all data has been read. Often a no-op. 00120 * 00121 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 00122 * application must deal with any cleanup that should happen even 00123 * for error exit. 00124 */ 00125 00126 METHODDEF(void) 00127 term_source (j_decompress_ptr cinfo) 00128 { 00129 /* no work necessary here */ 00130 } 00131 00132 00133 /* 00134 * Prepare for input from a memory buffer. 00135 */ 00136 00137 GLOBAL(void) 00138 jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize) 00139 { 00140 my_src_ptr src; 00141 00142 /* The source object is made permanent so that a series of JPEG images 00143 * can be read from a single buffer by calling jpeg_memory_src 00144 * only before the first one. 00145 * This makes it unsafe to use this manager and a different source 00146 * manager serially with the same JPEG object. Caveat programmer. 00147 */ 00148 if (cinfo->src == NULL) { /* first time for this JPEG object? */ 00149 cinfo->src = (struct jpeg_source_mgr *) 00150 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 00151 SIZEOF(my_source_mgr)); 00152 } 00153 00154 src = (my_src_ptr) cinfo->src; 00155 src->pub.init_source = init_source; 00156 src->pub.fill_input_buffer = fill_input_buffer; 00157 src->pub.skip_input_data = skip_input_data; 00158 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 00159 src->pub.term_source = term_source; 00160 00161 src->pub.next_input_byte = buffer; 00162 src->pub.bytes_in_buffer = bufsize; 00163 }