/* * jpegsrc.c -- Source module for libjpeg * (C)Copyright 1998, 99 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Sun Mar 5 06:06:39 2000. * $Id: jpegsrc.c,v 1.2 2000/03/22 18:07:01 sian Exp $ * * NOTE: This code needs libjpeg version 6 or later. * * This software is based in part on the work of the Independent JPEG Group * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include "archive.h" #include #include #include #include #include #if BITS_IN_JSAMPLE != 8 # error BITS_IN_JSAMPLE must be 8 #endif typedef struct { struct jpeg_source_mgr pub; /* public fields */ Archive *ar; /* source archive */ JOCTET *buffer; /* start of buffer */ boolean start_of_file; /* have we gotten any data yet? */ } my_source_mgr; typedef my_source_mgr *my_src_ptr; #define INPUT_BUF_SIZE 8192 METHODDEF(void) init_source(j_decompress_ptr cinfo) { my_src_ptr src = (my_src_ptr)cinfo->src; src->start_of_file = TRUE; } METHODDEF(boolean) fill_input_buffer(j_decompress_ptr cinfo) { my_src_ptr src = (my_src_ptr)cinfo->src; size_t nbytes; nbytes = archive_read(src->ar, src->buffer, INPUT_BUF_SIZE); if (nbytes <= 0) { if (src->start_of_file) ERREXIT(cinfo, JERR_INPUT_EMPTY); WARNMS(cinfo, JWRN_JPEG_EOF); /* Insert a fake EOI marker */ src->buffer[0] = (JOCTET)0xff; src->buffer[1] = (JOCTET)JPEG_EOI; nbytes = 2; } src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer = nbytes; src->start_of_file = FALSE; return TRUE; } METHODDEF(void) skip_input_data(j_decompress_ptr cinfo, long num_bytes) { my_src_ptr src = (my_src_ptr)cinfo->src; if (num_bytes > 0) { while (num_bytes > (long)src->pub.bytes_in_buffer) { num_bytes -= (long)src->pub.bytes_in_buffer; (void)fill_input_buffer(cinfo); } src->pub.next_input_byte += (size_t)num_bytes; src->pub.bytes_in_buffer -= (size_t)num_bytes; } } METHODDEF(void) term_source(j_decompress_ptr cinfo) { } GLOBAL(void) jpeg_archive_src(j_decompress_ptr cinfo, Archive *ar) { my_src_ptr src; if (cinfo->src == NULL) { cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr)); src = (my_src_ptr)cinfo->src; src->buffer = (JOCTET *) (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET)); } src = (my_src_ptr)cinfo->src; src->pub.init_source = init_source; src->pub.fill_input_buffer = fill_input_buffer; src->pub.skip_input_data = skip_input_data; src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->pub.term_source = term_source; src->ar = ar; src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ src->pub.next_input_byte = NULL; /* until buffer loaded */ }