read/write calls inside kaffe
Stefan Burstroem
f94sbu at efd.lth.se
Thu Apr 15 14:05:22 PDT 1999
In article <1635.774T1639T10922546 at efd.lth.se> you wrote:
>I'll post the patches later tonight.
Oki, here are some patches that should remove the need for fread in the
gif/png/jpeg decoders.
Could someone try to apply them and see if they work?
And if not please tell me where it fails and I'll try to fix it. (It
doesn't run here yet due to AWT/X problems)
regards, Stefan Burstroem
-----------------------------------------------------------------------
>> Irl: Stefan Burstroem << >> Omnipresence Intl. << >> Irc: Yabba <<
>> Phone: +46 (0)46-211 40 84 << >> EMail: stefan at omnipresence.com <<
-----------------------------------------------------------------------
Index: img.c
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/clib/awt/X/img.c,v
retrieving revision 1.13
diff -c -r1.13 img.c
*** img.c 1999/03/12 00:18:25 1.13
--- img.c 1999/04/16 05:11:04
***************
*** 9,23 ****
*/
#include "toolkit.h"
-
/* interfaces of image conversion functions */
! Image* readGifFile ( FILE* );
Image* readGifData ( unsigned char*, long len );
! Image* readJpegFile ( FILE* );
Image* readJpegData ( unsigned char*, long len );
! Image* readPngFile ( FILE* );
Image* readPngData ( unsigned char*, long len );
/************************************************************************************
--- 9,23 ----
*/
+ #include <fcntl.h> /* For the O_RDONLY flag */
#include "toolkit.h"
/* interfaces of image conversion functions */
! Image* readGifFile ( int );
Image* readGifData ( unsigned char*, long len );
! Image* readJpegFile ( int );
Image* readJpegData ( unsigned char*, long len );
! Image* readPngFile ( int );
Image* readPngData ( unsigned char*, long len );
/************************************************************************************
***************
*** 681,693 ****
Java_java_awt_Toolkit_imgCreateFromFile ( JNIEnv* env, jclass clazz, jstring fileName )
{
Image *img = 0;
! FILE *infile;
char *fn = java2CString( env, X, fileName);
unsigned char sig[SIG_LENGTH];
! if ((infile = fopen( fn, "rb")) != NULL) {
! if ( fread( sig, 1, sizeof(sig), infile) == sizeof(sig) ) {
! rewind( infile); /* some native converters can't skip the signature read */
switch ( imageFormat( sig) ) {
case SIG_GIF:
--- 681,693 ----
Java_java_awt_Toolkit_imgCreateFromFile ( JNIEnv* env, jclass clazz, jstring fileName )
{
Image *img = 0;
! int infile,size,oldPos;
char *fn = java2CString( env, X, fileName);
unsigned char sig[SIG_LENGTH];
! if( KOPEN( fn, O_RDONLY, 0, &infile) == 0) {
! if ( KREAD( infile, sig, sizeof(sig), infile, &size) == 0 && size == sizeof(sig) ) {
! KSEEK( infile, SEEK_SET, 0, &oldPos); /* some native converters can't skip the signature read */
switch ( imageFormat( sig) ) {
case SIG_GIF:
***************
*** 703,709 ****
img = unknownImage;
}
}
! fclose( infile);
}
return img;
--- 703,709 ----
img = unknownImage;
}
}
! KCLOSE( infile);
}
return img;
Index: imggif.c
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/clib/awt/X/imggif.c,v
retrieving revision 1.12
diff -c -r1.12 imggif.c
*** imggif.c 1999/03/22 06:23:09 1.12
--- imggif.c 1999/04/16 05:11:06
***************
*** 182,187 ****
--- 182,209 ----
}
}
+ /**************************************************************************************
+ * kaffe fd IO
+ */
+
+ typedef struct {
+ int fd;
+ } KaffeFDSource;
+
+ int
+ readGifKaffeFD( GifFileType *gf, GifByteType* buf, int length )
+ {
+ KaffeFDSource *psource = (KaffeFDSource*)gf->UserData;
+ int res,outlength;
+
+ res = KREAD(psource->fd,buf,length,&outlength);
+ if(res!=0) {
+ return 0;
+ } else {
+ return outlength;
+ }
+ }
+
#endif /* INCLUDE_GIF */
/**************************************************************************************
***************
*** 189,202 ****
*/
Image*
! readGifFile ( FILE* infile )
{
Image *img = 0;
#if defined(INCLUDE_GIF)
GifFileType *gf;
! if ( !(gf = DGifOpenFileHandle( fileno( infile))) )
return 0;
img = readGif( gf);
--- 211,227 ----
*/
Image*
! readGifFile ( int inFile)
{
Image *img = 0;
#if defined(INCLUDE_GIF)
+ KaffeFDSource fdSrc;
GifFileType *gf;
+
+ fdSrc.fd = inFile;
! if ( !(gf = DGifOpen( &fdSrc, readGifKaffeFD)) )
return 0;
img = readGif( gf);
Index: imgjpeg.c
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/clib/awt/X/imgjpeg.c,v
retrieving revision 1.13
diff -c -r1.13 imgjpeg.c
*** imgjpeg.c 1999/04/05 20:28:58 1.13
--- imgjpeg.c 1999/04/16 05:11:08
***************
*** 70,76 ****
--- 70,87 ----
typedef bufsrc_mgr * bufsrc_ptr;
+ typedef struct {
+ struct jpeg_source_mgr pub; /* public fields */
+ int fd;
+ JOCTET * buffer; /* start of buffer */
+
+ } kaffefdsrc_mgr;
+
+ typedef kaffefdsrc_mgr * kaffefdsrc_ptr;
+
+ #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
+
/*******************************************************************************************
* memory buffer extension for the standard jpeg lib input management (jdatasrc.c)
*/
***************
*** 162,167 ****
--- 173,269 ----
src->buflen = len;
}
+ /*******************************************************************************************
+ * IO functions that uses kaffe's fds to read an image
+ */
+
+ METHODDEF(void)
+ init_kaffefd_source (j_decompress_ptr cinfo)
+ {
+ /* no op, the file is already opened */
+ }
+
+
+ METHODDEF(boolean)
+ get_kaffefd_source (j_decompress_ptr cinfo)
+ {
+ kaffefdsrc_ptr src = (kaffefdsrc_ptr) cinfo->src;
+ size_t nbytes;
+ int res;
+
+ res = KREAD(src->fd, src->buffer, INPUT_BUF_SIZE,&nbytes);
+ if (res != 0 || nbytes == 0) {
+ 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;
+
+ return TRUE;
+ }
+
+
+ METHODDEF(void)
+ skip_kaffefd_source ( j_decompress_ptr cinfo, long num_bytes )
+ {
+ kaffefdsrc_ptr src = (kaffefdsrc_ptr) cinfo->src;
+
+ /* Just a dumb implementation for now. Could use KFSEEK() except
+ * it doesn't work on pipes. Not clear that being smart is worth
+ * any trouble anyway --- large skips are infrequent.
+ */
+ 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);
+ /* note we assume that fill_input_buffer will never return FALSE,
+ * so suspension need not be handled.
+ */
+ }
+ src->pub.next_input_byte += (size_t) num_bytes;
+ src->pub.bytes_in_buffer -= (size_t) num_bytes;
+ }
+ }
+
+
+ METHODDEF(void)
+ term_kaffefd_source (j_decompress_ptr cinfo)
+ {
+ /* No op */
+ }
+
+
+ GLOBAL(void)
+ jpeg_kaffefd_src (j_decompress_ptr cinfo, int infile )
+ {
+ kaffefdsrc_ptr src;
+
+ if (cinfo->src == NULL) { /* first time for this JPEG object? */
+ cinfo->src = (struct jpeg_source_mgr *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ sizeof(kaffefdsrc_mgr));
+ src->buffer = (JOCTET *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ INPUT_BUF_SIZE * sizeof(JOCTET));
+ }
+
+ src = (kaffefdsrc_ptr) cinfo->src;
+ src->pub.init_source = init_kaffefd_source;
+ src->pub.fill_input_buffer = get_kaffefd_source;
+ src->pub.skip_input_data = skip_kaffefd_source;
+ src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
+ src->pub.term_source = term_kaffefd_source;
+
+ src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
+ src->pub.next_input_byte = NULL; /* until buffer loaded */
+
+ src->fd = infile;
+ }
+
/************************************************************************************
***************
*** 199,206 ****
jerr.pub.error_exit = error_exit;
if ( setjmp(jerr.setjmp_buffer)) {
! if ( img )
! Java_java_awt_Toolkit_imgFreeImage( 0, 0, img);
return 0;
}
--- 301,308 ----
jerr.pub.error_exit = error_exit;
if ( setjmp(jerr.setjmp_buffer)) {
! if ( img )
! Java_java_awt_Toolkit_imgFreeImage( 0, 0, img);
return 0;
}
***************
*** 242,248 ****
*/
Image*
! readJpegFile ( FILE* infile )
{
Image *img = 0;
--- 344,350 ----
*/
Image*
! readJpegFile ( int infile )
{
Image *img = 0;
***************
*** 250,256 ****
struct jpeg_decompress_struct cinfo;
jpeg_create_decompress( &cinfo);
! jpeg_stdio_src( &cinfo, infile);
img = readJpeg( &cinfo, MAX_JPEG_COLORS);
--- 352,358 ----
struct jpeg_decompress_struct cinfo;
jpeg_create_decompress( &cinfo);
! jpeg_kaffeio_src( &cinfo, infile);
img = readJpeg( &cinfo, MAX_JPEG_COLORS);
Index: imgpng.c
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/clib/awt/X/imgpng.c,v
retrieving revision 1.13
diff -c -r1.13 imgpng.c
*** imgpng.c 1999/03/22 06:23:09 1.13
--- imgpng.c 1999/04/16 05:11:10
***************
*** 272,278 ****
--- 272,299 ----
png_error( png_ptr, "illegal data read");
}
+ /**************************************************************************************
+ * kaffe fd IO
+ */
+
+ typedef struct {
+ int fd;
+ } KaffeFDSource;
+
+ void
+ KaffeFDRead ( png_structp png_ptr, png_bytep data, png_size_t length )
+ {
+ KaffeFDSource *psource = png_get_io_ptr( png_ptr);
+ int res,outlength;
+
+ res = KREAD(psource->fd,data,length,&outlength);
+
+ if(res!=0 || outlength!=length) {
+ png_error( png_ptr, "illegal data read");
+ }
+ }
+
#endif /* INCLUDE_PNG */
/**************************************************************************************
***************
*** 280,291 ****
*/
Image*
! readPngFile ( FILE* infile )
{
Image *img = 0;
#if defined(INCLUDE_PNG)
png_structp png_ptr;
png_infop info_ptr;
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(png_voidp)0, (png_error_ptr)0, (png_error_ptr)0);
--- 301,315 ----
*/
Image*
! readPngFile ( int infile )
{
Image *img = 0;
#if defined(INCLUDE_PNG)
png_structp png_ptr;
png_infop info_ptr;
+ KaffeFDSource source;
+
+ source.fd = infile;
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(png_voidp)0, (png_error_ptr)0, (png_error_ptr)0);
***************
*** 293,299 ****
return 0;
info_ptr = png_create_info_struct( png_ptr);
! png_init_io( png_ptr, infile);
img = readPng( png_ptr, info_ptr);
--- 317,323 ----
return 0;
info_ptr = png_create_info_struct( png_ptr);
! png_set_read_fn( png_ptr, &source, KaffeFDRead);
img = readPng( png_ptr, info_ptr);
More information about the kaffe
mailing list