00001 /* 00002 * jdatasrc.c 00003 * 00004 * Copyright (C) 1994-1996, Thomas G. Lane. 00005 * Modified 2009 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 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ 00024 00025 METHODDEF(void) 00026 init_mem_source (j_decompress_ptr cinfo) 00027 { 00028 /* no work necessary here */ 00029 } 00030 00031 METHODDEF(boolean) 00032 fill_mem_input_buffer (j_decompress_ptr cinfo) 00033 { 00034 static JOCTET mybuffer[4]; 00035 00036 /* The whole JPEG data is expected to reside in the supplied memory 00037 * buffer, so any request for more data beyond the given buffer size 00038 * is treated as an error. 00039 */ 00040 WARNMS(cinfo, JWRN_JPEG_EOF); 00041 /* Insert a fake EOI marker */ 00042 mybuffer[0] = (JOCTET) 0xFF; 00043 mybuffer[1] = (JOCTET) JPEG_EOI; 00044 00045 cinfo->src->next_input_byte = mybuffer; 00046 cinfo->src->bytes_in_buffer = 2; 00047 00048 return TRUE; 00049 } 00050 00051 /* 00052 * Skip data --- used to skip over a potentially large amount of 00053 * uninteresting data (such as an APPn marker). 00054 * 00055 * Writers of suspendable-input applications must note that skip_input_data 00056 * is not granted the right to give a suspension return. If the skip extends 00057 * beyond the data currently in the buffer, the buffer can be marked empty so 00058 * that the next read will cause a fill_input_buffer call that can suspend. 00059 * Arranging for additional bytes to be discarded before reloading the input 00060 * buffer is the application writer's problem. 00061 */ 00062 00063 METHODDEF(void) 00064 skip_input_data (j_decompress_ptr cinfo, long num_bytes) 00065 { 00066 struct jpeg_source_mgr * src = cinfo->src; 00067 00068 /* Just a dumb implementation for now. Could use fseek() except 00069 * it doesn't work on pipes. Not clear that being smart is worth 00070 * any trouble anyway --- large skips are infrequent. 00071 */ 00072 if ( num_bytes > 0 ) 00073 { 00074 while (num_bytes > (long) src->bytes_in_buffer) 00075 { 00076 num_bytes -= (long) src->bytes_in_buffer; 00077 (void)src->fill_input_buffer(cinfo); 00078 /* note we assume that fill_input_buffer will never return FALSE, 00079 * so suspension need not be handled. 00080 */ 00081 } 00082 src->next_input_byte += (size_t) num_bytes; 00083 src->bytes_in_buffer -= (size_t) num_bytes; 00084 } 00085 } 00086 00087 /* 00088 * An additional method that can be provided by data source modules is the 00089 * resync_to_restart method for error recovery in the presence of RST markers. 00090 * For the moment, this source module just uses the default resync method 00091 * provided by the JPEG library. That method assumes that no backtracking 00092 * is possible. 00093 */ 00094 00095 00096 /* 00097 * Terminate source --- called by jpeg_finish_decompress 00098 * after all data has been read. Often a no-op. 00099 * 00100 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 00101 * application must deal with any cleanup that should happen even 00102 * for error exit. 00103 */ 00104 00105 METHODDEF(void) 00106 term_source (j_decompress_ptr cinfo) 00107 { 00108 /* no work necessary here */ 00109 } 00110 00111 /* 00112 * Prepare for input from a supplied memory buffer. 00113 * The buffer must contain the whole JPEG data. 00114 */ 00115 00116 GLOBAL(void) 00117 jpeg_mem_src (j_decompress_ptr cinfo, 00118 unsigned char * inbuffer, unsigned long insize) 00119 { 00120 struct jpeg_source_mgr * src; 00121 00122 if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ 00123 ERREXIT(cinfo, JERR_INPUT_EMPTY); 00124 00125 /* The source object is made permanent so that a series of JPEG images 00126 * can be read from the same buffer by calling jpeg_mem_src only before 00127 * the first one. 00128 */ 00129 if (cinfo->src == NULL) { /* first time for this JPEG object? */ 00130 cinfo->src = (struct jpeg_source_mgr *) 00131 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 00132 SIZEOF(struct jpeg_source_mgr)); 00133 } 00134 00135 src = cinfo->src; 00136 src->init_source = init_mem_source; 00137 src->fill_input_buffer = fill_mem_input_buffer; 00138 src->skip_input_data = skip_input_data; 00139 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ 00140 src->term_source = term_source; 00141 src->bytes_in_buffer = (size_t) insize; 00142 src->next_input_byte = (JOCTET *) inbuffer; 00143 }