From be9b6c7cc9197053edccf6be6fcaf03de42ba8f4 Mon Sep 17 00:00:00 2001 From: ewasylishen Date: Tue, 5 Aug 2014 20:41:47 +0000 Subject: [PATCH] Image_LoadImage: read pixel data in 1K chunks instead of calling getc in a loop git-svn-id: http://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@957 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/image.c | 90 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 24 deletions(-) diff --git a/Quake/image.c b/Quake/image.c index 91c4046e..87dc089c 100644 --- a/Quake/image.c +++ b/Quake/image.c @@ -24,6 +24,39 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static char loadfilename[MAX_OSPATH]; //file scope so that error messages can use it +typedef struct stdio_buffer_s { + FILE *f; + unsigned char buffer[1024]; + int size; + int pos; +} stdio_buffer_t; + +static stdio_buffer_t *Buf_Alloc(FILE *f) +{ + stdio_buffer_t *buf = calloc(1, sizeof(stdio_buffer_t)); + buf->f = f; + return buf; +} + +static void Buf_Free(stdio_buffer_t *buf) +{ + free(buf); +} + +static inline int Buf_GetC(stdio_buffer_t *buf) +{ + if (buf->pos >= buf->size) + { + buf->size = fread(buf->buffer, 1, sizeof(buf->buffer), buf->f); + buf->pos = 0; + + if (buf->size == 0) + return EOF; + } + + return buf->buffer[buf->pos++]; +} + /* ============ Image_LoadImage @@ -151,6 +184,7 @@ byte *Image_LoadTGA (FILE *fin, int *width, int *height) byte *targa_rgba; int realrow; //johnfitz -- fix for upside-down targas qboolean upside_down; //johnfitz -- fix for upside-down targas + stdio_buffer_t *buf; targa_header.id_length = fgetc(fin); targa_header.colormap_type = fgetc(fin); @@ -182,6 +216,8 @@ byte *Image_LoadTGA (FILE *fin, int *width, int *height) if (targa_header.id_length != 0) fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment + buf = Buf_Alloc(fin); + if (targa_header.image_type==2) // Uncompressed, RGB images { for(row=rows-1; row>=0; row--) @@ -196,19 +232,19 @@ byte *Image_LoadTGA (FILE *fin, int *width, int *height) switch (targa_header.pixel_size) { case 24: - blue = getc(fin); - green = getc(fin); - red = getc(fin); + blue = Buf_GetC(buf); + green = Buf_GetC(buf); + red = Buf_GetC(buf); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = 255; break; case 32: - blue = getc(fin); - green = getc(fin); - red = getc(fin); - alphabyte = getc(fin); + blue = Buf_GetC(buf); + green = Buf_GetC(buf); + red = Buf_GetC(buf); + alphabyte = Buf_GetC(buf); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; @@ -229,23 +265,23 @@ byte *Image_LoadTGA (FILE *fin, int *width, int *height) //johnfitz for(column=0; column= 0xC0) { runlength = readbyte & 0x3F; - readbyte = fgetc(f); + readbyte = Buf_GetC(buf); } else runlength = 1; @@ -418,6 +458,8 @@ byte *Image_LoadPCX (FILE *f, int *width, int *height) } } } + + Buf_Free(buf); fclose(f);