74d9a01
/*
74d9a01
* memsrc.c
74d9a01
*
74d9a01
* Copyright (C) 1994-1996, Thomas G. Lane.
74d9a01
* This file is part of the Independent JPEG Group's software.
74d9a01
* For conditions of distribution and use, see the accompanying README file.
74d9a01
*
74d9a01
* This file contains decompression data source routines for the case of
74d9a01
* reading JPEG data from a memory buffer that is preloaded with the entire
74d9a01
* JPEG file. This would not seem especially useful at first sight, but
74d9a01
* a number of people have asked for it.
74d9a01
* This is really just a stripped-down version of jdatasrc.c. Comparison
74d9a01
* of this code with jdatasrc.c may be helpful in seeing how to make
74d9a01
* custom source managers for other purposes.
74d9a01
*/
74d9a01
74d9a01
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
74d9a01
#include <stdio.h>
74d9a01
#include <jpeglib.h>
74d9a01
#include <jerror.h>
74d9a01
74d9a01
74d9a01
/* Expanded data source object for memory input */
74d9a01
74d9a01
typedef struct {
74d9a01
struct jpeg_source_mgr pub; /* public fields */
74d9a01
74d9a01
JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
74d9a01
} my_source_mgr;
74d9a01
74d9a01
typedef my_source_mgr * my_src_ptr;
74d9a01
74d9a01
74d9a01
/*
74d9a01
* Initialize source --- called by jpeg_read_header
74d9a01
* before any data is actually read.
74d9a01
*/
74d9a01
74d9a01
METHODDEF(void)
74d9a01
init_source (j_decompress_ptr cinfo)
74d9a01
{
74d9a01
/* No work, since jpeg_mem_src set up the buffer pointer and count.
74d9a01
* Indeed, if we want to read multiple JPEG images from one buffer,
74d9a01
* this *must* not do anything to the pointer.
74d9a01
*/
74d9a01
}
74d9a01
74d9a01
74d9a01
/*
74d9a01
* Fill the input buffer --- called whenever buffer is emptied.
74d9a01
*
74d9a01
* In this application, this routine should never be called; if it is called,
74d9a01
* the decompressor has overrun the end of the input buffer, implying we
74d9a01
* supplied an incomplete or corrupt JPEG datastream. A simple error exit
74d9a01
* might be the most appropriate response.
74d9a01
*
74d9a01
* But what we choose to do in this code is to supply dummy EOI markers
74d9a01
* in order to force the decompressor to finish processing and supply
74d9a01
* some sort of output image, no matter how corrupted.
74d9a01
*/
74d9a01
74d9a01
METHODDEF(boolean)
74d9a01
fill_input_buffer (j_decompress_ptr cinfo)
74d9a01
{
74d9a01
my_src_ptr src = (my_src_ptr) cinfo->src;
74d9a01
74d9a01
WARNMS(cinfo, JWRN_JPEG_EOF);
74d9a01
74d9a01
/* Create a fake EOI marker */
74d9a01
src->eoi_buffer[0] = (JOCTET) 0xFF;
74d9a01
src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
74d9a01
src->pub.next_input_byte = src->eoi_buffer;
74d9a01
src->pub.bytes_in_buffer = 2;
74d9a01
74d9a01
return TRUE;
74d9a01
}
74d9a01
74d9a01
74d9a01
/*
74d9a01
* Skip data --- used to skip over a potentially large amount of
74d9a01
* uninteresting data (such as an APPn marker).
74d9a01
*
74d9a01
* If we overrun the end of the buffer, we let fill_input_buffer deal with
74d9a01
* it. An extremely large skip could cause some time-wasting here, but
74d9a01
* it really isn't supposed to happen ... and the decompressor will never
74d9a01
* skip more than 64K anyway.
74d9a01
*/
74d9a01
74d9a01
METHODDEF(void)
74d9a01
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
74d9a01
{
74d9a01
my_src_ptr src = (my_src_ptr) cinfo->src;
74d9a01
74d9a01
if (num_bytes > 0) {
74d9a01
while (num_bytes > (long) src->pub.bytes_in_buffer) {
74d9a01
num_bytes -= (long) src->pub.bytes_in_buffer;
74d9a01
(void) fill_input_buffer(cinfo);
74d9a01
/* note we assume that fill_input_buffer will never return FALSE,
74d9a01
* so suspension need not be handled.
74d9a01
*/
74d9a01
}
74d9a01
src->pub.next_input_byte += (size_t) num_bytes;
74d9a01
src->pub.bytes_in_buffer -= (size_t) num_bytes;
74d9a01
}
74d9a01
}
74d9a01
74d9a01
74d9a01
/*
74d9a01
* An additional method that can be provided by data source modules is the
74d9a01
* resync_to_restart method for error recovery in the presence of RST markers.
74d9a01
* For the moment, this source module just uses the default resync method
74d9a01
* provided by the JPEG library. That method assumes that no backtracking
74d9a01
* is possible.
74d9a01
*/
74d9a01
74d9a01
74d9a01
/*
74d9a01
* Terminate source --- called by jpeg_finish_decompress
74d9a01
* after all data has been read. Often a no-op.
74d9a01
*
74d9a01
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
74d9a01
* application must deal with any cleanup that should happen even
74d9a01
* for error exit.
74d9a01
*/
74d9a01
74d9a01
METHODDEF(void)
74d9a01
term_source (j_decompress_ptr cinfo)
74d9a01
{
74d9a01
/* no work necessary here */
74d9a01
}
74d9a01
74d9a01
74d9a01
/*
74d9a01
* Prepare for input from a memory buffer.
74d9a01
*/
74d9a01
29b605c
//GLOBAL(void)
74d9a01
jpeg_mem_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
74d9a01
{
74d9a01
my_src_ptr src;
74d9a01
74d9a01
/* The source object is made permanent so that a series of JPEG images
74d9a01
* can be read from a single buffer by calling jpeg_mem_src
74d9a01
* only before the first one.
74d9a01
* This makes it unsafe to use this manager and a different source
74d9a01
* manager serially with the same JPEG object. Caveat programmer.
74d9a01
*/
74d9a01
if (cinfo->src == NULL) { /* first time for this JPEG object? */
74d9a01
cinfo->src = (struct jpeg_source_mgr *)
74d9a01
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
74d9a01
sizeof(my_source_mgr));
74d9a01
}
74d9a01
74d9a01
src = (my_src_ptr) cinfo->src;
74d9a01
src->pub.init_source = init_source;
74d9a01
src->pub.fill_input_buffer = fill_input_buffer;
74d9a01
src->pub.skip_input_data = skip_input_data;
74d9a01
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
74d9a01
src->pub.term_source = term_source;
74d9a01
74d9a01
src->pub.next_input_byte = buffer;
74d9a01
src->pub.bytes_in_buffer = bufsize;
74d9a01
}