136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jdatasrc.c 336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 4ab70623eb29e09e67222be5b9e1ea320fe5aa0e9DRC * This file was part of the Independent JPEG Group's software: 5489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane * Copyright (C) 1994-1996, Thomas G. Lane. 65829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * Modified 2009-2011 by Guido Vollbeding. 7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications: 86eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * Copyright (C) 2013, 2016, D. R. Commander. 96eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg 106eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file. 1136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 1236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains decompression data source routines for the case of 13989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * reading JPEG data from memory or from a file (or any stdio stream). 14989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * While these routines are sufficient for most applications, 15989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * some will want to use a different source manager. 1636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * IMPORTANT: we assume that fread() will correctly transcribe an array of 1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * JOCTETs from 8-bit-wide elements on external storage. If char is wider 1836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * than 8 bits on your machine, you may need to do some tweaking. 1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 2036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 2136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jinclude.h" 2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h" 2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jerror.h" 2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 2636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Expanded data source object for stdio input */ 2836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct { 30e5eaf37440b8e337ab150c017df7c03faf846c51DRC struct jpeg_source_mgr pub; /* public fields */ 3136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 326eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis FILE *infile; /* source stream */ 336eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis JOCTET *buffer; /* start of buffer */ 34e5eaf37440b8e337ab150c017df7c03faf846c51DRC boolean start_of_file; /* have we gotten any data yet? */ 3536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_source_mgr; 3636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 376eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidistypedef my_source_mgr *my_src_ptr; 3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 39e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ 4036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 4136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 4236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 4336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize source --- called by jpeg_read_header 4436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * before any data is actually read. 4536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 4636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 47489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 4836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneinit_source (j_decompress_ptr cinfo) 4936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 5036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_src_ptr src = (my_src_ptr) cinfo->src; 5136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 5236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* We reset the empty-input-file flag for each image, 5336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * but we don't clear the input buffer. 5436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This is correct behavior for reading a series of images from one source. 5536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 5636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->start_of_file = TRUE; 5736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 5836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 59ab70623eb29e09e67222be5b9e1ea320fe5aa0e9DRC#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) 60989630f70cf1af69ebfefca8910d1647bf189712Guido VollbedingMETHODDEF(void) 61989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbedinginit_mem_source (j_decompress_ptr cinfo) 62989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding{ 63989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding /* no work necessary here */ 64989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding} 6536a6eec93250e390d1028b3372078810b4428eafDRC#endif 66989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 6736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 6836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 6936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Fill the input buffer --- called whenever buffer is emptied. 7036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 7136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In typical applications, this should read fresh data into the buffer 7236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * (ignoring the current state of next_input_byte & bytes_in_buffer), 7336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * reset the pointer & count to the start of the buffer, and return TRUE 7436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * indicating that the buffer has been reloaded. It is not necessary to 7536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * fill the buffer entirely, only to obtain at least one more byte. 7636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 7736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * There is no such thing as an EOF return. If the end of the file has been 7836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * reached, the routine has a choice of ERREXIT() or inserting fake data into 7936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the buffer. In most cases, generating a warning message and inserting a 8036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * fake EOI marker is the best course of action --- this will allow the 8136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * decompressor to output however much of the image is there. However, 8236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the resulting error message is misleading if the real problem is an empty 8336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * input file, so we handle that case specially. 8436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 8536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In applications that need to be able to suspend compression due to input 8636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * not being available yet, a FALSE return indicates that no more data can be 8736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * obtained right now, but more may be forthcoming later. In this situation, 8836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the decompressor will return to its caller (with an indication of the 8936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * number of scanlines it has read, if any). The application should resume 9036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * decompression after it has loaded more data into the input buffer. Note 9136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * that there are substantial restrictions on the use of suspension --- see 9236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the documentation. 9336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 9436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * When suspending, the decompressor will back up to a convenient restart point 9536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * (typically the start of the current MCU). next_input_byte & bytes_in_buffer 9636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * indicate where the restart point will be if the current call returns FALSE. 9736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Data beyond this point must be rescanned after resumption, so move it to 9836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the front of the buffer rather than discarding it. 9936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 10036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 101489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(boolean) 10236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanefill_input_buffer (j_decompress_ptr cinfo) 10336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 10436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_src_ptr src = (my_src_ptr) cinfo->src; 10536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane size_t nbytes; 10636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 10736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE); 10836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 10936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (nbytes <= 0) { 110e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (src->start_of_file) /* Treat empty input file as fatal error */ 11136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ERREXIT(cinfo, JERR_INPUT_EMPTY); 11236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane WARNMS(cinfo, JWRN_JPEG_EOF); 11336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Insert a fake EOI marker */ 11436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->buffer[0] = (JOCTET) 0xFF; 11536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->buffer[1] = (JOCTET) JPEG_EOI; 11636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane nbytes = 2; 11736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 11836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 11936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.next_input_byte = src->buffer; 12036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.bytes_in_buffer = nbytes; 12136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->start_of_file = FALSE; 12236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 12336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane return TRUE; 12436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 12536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 126ab70623eb29e09e67222be5b9e1ea320fe5aa0e9DRC#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) 127989630f70cf1af69ebfefca8910d1647bf189712Guido VollbedingMETHODDEF(boolean) 128989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbedingfill_mem_input_buffer (j_decompress_ptr cinfo) 129989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding{ 1305829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding static const JOCTET mybuffer[4] = { 1315829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding (JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0 1325829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding }; 133989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 134989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding /* The whole JPEG data is expected to reside in the supplied memory 135989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * buffer, so any request for more data beyond the given buffer size 136989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * is treated as an error. 137989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding */ 138989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding WARNMS(cinfo, JWRN_JPEG_EOF); 1395829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding 140989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding /* Insert a fake EOI marker */ 141989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 142989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding cinfo->src->next_input_byte = mybuffer; 143989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding cinfo->src->bytes_in_buffer = 2; 144989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 145989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding return TRUE; 146989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding} 14736a6eec93250e390d1028b3372078810b4428eafDRC#endif 148989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 14936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 15036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 15136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Skip data --- used to skip over a potentially large amount of 15236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * uninteresting data (such as an APPn marker). 15336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 15436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Writers of suspendable-input applications must note that skip_input_data 15536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * is not granted the right to give a suspension return. If the skip extends 15636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * beyond the data currently in the buffer, the buffer can be marked empty so 15736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * that the next read will cause a fill_input_buffer call that can suspend. 15836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Arranging for additional bytes to be discarded before reloading the input 15936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * buffer is the application writer's problem. 16036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 16136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 162489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 16336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneskip_input_data (j_decompress_ptr cinfo, long num_bytes) 16436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 1656eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis struct jpeg_source_mgr *src = cinfo->src; 16636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 16736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Just a dumb implementation for now. Could use fseek() except 16836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * it doesn't work on pipes. Not clear that being smart is worth 16936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * any trouble anyway --- large skips are infrequent. 17036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 17136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (num_bytes > 0) { 172989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding while (num_bytes > (long) src->bytes_in_buffer) { 173989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding num_bytes -= (long) src->bytes_in_buffer; 174a4ecaacde6d64e1f20b8647546813c17592016c1Guido Vollbeding (void) (*src->fill_input_buffer) (cinfo); 1759ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane /* note we assume that fill_input_buffer will never return FALSE, 1769ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane * so suspension need not be handled. 1779ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane */ 17836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 179989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->next_input_byte += (size_t) num_bytes; 180989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->bytes_in_buffer -= (size_t) num_bytes; 18136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 18236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 18336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 18436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 18536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 18636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * An additional method that can be provided by data source modules is the 18736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * resync_to_restart method for error recovery in the presence of RST markers. 18836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For the moment, this source module just uses the default resync method 18936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * provided by the JPEG library. That method assumes that no backtracking 19036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * is possible. 19136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 19236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 19336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 19436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 19536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Terminate source --- called by jpeg_finish_decompress 19636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * after all data has been read. Often a no-op. 19736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 19836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 19936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * application must deal with any cleanup that should happen even 20036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * for error exit. 20136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 20236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 203489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 20436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneterm_source (j_decompress_ptr cinfo) 20536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 20636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* no work necessary here */ 20736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 20836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 20936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 21036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* 21136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Prepare for input from a stdio stream. 21236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * The caller must have already opened the stream, and is responsible 21336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * for closing it after finishing decompression. 21436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 21536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 216489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void) 2176eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidisjpeg_stdio_src (j_decompress_ptr cinfo, FILE *infile) 21836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 21936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_src_ptr src; 22036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 22136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* The source object and input buffer are made permanent so that a series 22236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * of JPEG images can be read from the same file by calling jpeg_stdio_src 22336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * only before the first one. (If we discarded the buffer at the end of 22436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * one image, we'd likely lose the start of the next one.) 22536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 226e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (cinfo->src == NULL) { /* first time for this JPEG object? */ 22736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cinfo->src = (struct jpeg_source_mgr *) 22836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 2295de454b291f48382648a5d1dc2aa0fca8b5786d4DRC sizeof(my_source_mgr)); 23036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src = (my_src_ptr) cinfo->src; 23136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->buffer = (JOCTET *) 23236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 2335de454b291f48382648a5d1dc2aa0fca8b5786d4DRC INPUT_BUF_SIZE * sizeof(JOCTET)); 2346eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis } else if (cinfo->src->init_source != init_source) { 2356eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis /* It is unsafe to reuse the existing source manager unless it was created 2366eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * by this function. Otherwise, there is no guarantee that the opaque 2376eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * structure is the right size. Note that we could just create a new 2386eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * structure, but the old structure would not be freed until 2396eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * jpeg_destroy_decompress() was called. 2406eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis */ 2416eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis ERREXIT(cinfo, JERR_BUFFER_SIZE); 24236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 24336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 24436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src = (my_src_ptr) cinfo->src; 24536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.init_source = init_source; 24636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.fill_input_buffer = fill_input_buffer; 24736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.skip_input_data = skip_input_data; 24836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 24936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.term_source = term_source; 25036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->infile = infile; 25136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ 25236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane src->pub.next_input_byte = NULL; /* until buffer loaded */ 25336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 254989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 255989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 256ab70623eb29e09e67222be5b9e1ea320fe5aa0e9DRC#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) 257989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding/* 258989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * Prepare for input from a supplied memory buffer. 259989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * The buffer must contain the whole JPEG data. 260989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding */ 261989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 262989630f70cf1af69ebfefca8910d1647bf189712Guido VollbedingGLOBAL(void) 263989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbedingjpeg_mem_src (j_decompress_ptr cinfo, 2646eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis const unsigned char *inbuffer, unsigned long insize) 265989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding{ 2666eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis struct jpeg_source_mgr *src; 267989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 268e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ 269989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding ERREXIT(cinfo, JERR_INPUT_EMPTY); 270989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 271989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding /* The source object is made permanent so that a series of JPEG images 272989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * can be read from the same buffer by calling jpeg_mem_src only before 273989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding * the first one. 274989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding */ 275e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (cinfo->src == NULL) { /* first time for this JPEG object? */ 276989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding cinfo->src = (struct jpeg_source_mgr *) 277989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 2785de454b291f48382648a5d1dc2aa0fca8b5786d4DRC sizeof(struct jpeg_source_mgr)); 2796eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis } else if (cinfo->src->init_source != init_mem_source) { 2806eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis /* It is unsafe to reuse the existing source manager unless it was created 2816eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * by this function. 2826eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis */ 2836eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis ERREXIT(cinfo, JERR_BUFFER_SIZE); 284989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding } 285989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 286989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src = cinfo->src; 287989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->init_source = init_mem_source; 288989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->fill_input_buffer = fill_mem_input_buffer; 289989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->skip_input_data = skip_input_data; 290989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ 291989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->term_source = term_source; 292989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding src->bytes_in_buffer = (size_t) insize; 2936eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis src->next_input_byte = (const JOCTET *) inbuffer; 294989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding} 29536a6eec93250e390d1028b3372078810b4428eafDRC#endif 296