From 8bd9d978b4c3c249c7adf0b7f645e61b0f4ec513 Mon Sep 17 00:00:00 2001 From: molivier Date: Mon, 4 Apr 2005 05:40:51 +0000 Subject: [PATCH] Factorized LoadLMP and LoadLMPAs8Bit. Made a bunch of buffers const git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5146 d7cf8633-e32d-0410-b094-e92efae38249 --- image.c | 89 +++++++++++++++++++-------------------------------------- image.h | 2 +- jpeg.c | 4 +-- jpeg.h | 2 +- 4 files changed, 34 insertions(+), 63 deletions(-) diff --git a/image.c b/image.c index 02ac9233..86c3d9ee 100644 --- a/image.c +++ b/image.c @@ -180,7 +180,7 @@ typedef struct LoadPCX ============ */ -qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight) +qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight) { pcx_t pcx; qbyte *a, *b, *image_rgba, *pbuf; @@ -309,11 +309,11 @@ void PrintTargaHeader(TargaHeader *t) LoadTGA ============= */ -qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight) +qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight) { int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex; qbyte *pixbuf, *image_rgba; - qbyte *fin, *enddata; + const qbyte *fin, *enddata; TargaHeader targa_header; unsigned char palette[256*4], *p; @@ -526,9 +526,9 @@ qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight) LoadLMP ============ */ -qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight) +qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight, qboolean loadAs8Bit) { - qbyte *image_rgba; + qbyte *image_buffer; if (fs_filesize < 9) { @@ -537,8 +537,8 @@ qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight) } // parse the very complicated header *chuckle* - image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216; - image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216; + image_width = BuffLittleLong(f); + image_height = BuffLittleLong(f + 4); if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0) { Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height); @@ -553,16 +553,30 @@ qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight) return NULL; } - image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4); - if (!image_rgba) + if (loadAs8Bit) { - Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height); - return NULL; + image_buffer = Mem_Alloc(tempmempool, image_width * image_height); + memcpy(image_buffer, f + 8, image_width * image_height); } - Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete); - return image_rgba; + else + { + image_buffer = Mem_Alloc(tempmempool, image_width * image_height * 4); + Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_complete); + } + return image_buffer; +} + +static qbyte *LoadLMPRGBA (const qbyte *f, int matchwidth, int matchheight) +{ + return LoadLMP(f, matchwidth, matchheight, false); } +qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight) +{ + return LoadLMP(f, matchwidth, matchheight, true); +} + + typedef struct { char name[32]; @@ -574,7 +588,7 @@ typedef struct int value; } q2wal_t; -qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight) +qbyte *LoadWAL (const qbyte *f, int matchwidth, int matchheight) { qbyte *image_rgba; const q2wal_t *inwal = (const void *)f; @@ -612,49 +626,6 @@ qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight) } - -/* -============ -LoadLMP -============ -*/ -qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight) -{ - qbyte *image_8bit; - - if (fs_filesize < 9) - { - Con_Print("LoadLMPAs8Bit: invalid LMP file\n"); - return NULL; - } - - // parse the very complicated header *chuckle* - image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216; - image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216; - if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0) - { - Con_Printf("LoadLMPAs8Bit: invalid size %ix%i\n", image_width, image_height); - return NULL; - } - if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight)) - return NULL; - - if (fs_filesize < 8 + image_width * image_height) - { - Con_Print("LoadLMPAs8Bit: invalid LMP file\n"); - return NULL; - } - - image_8bit = Mem_Alloc(tempmempool, image_width * image_height); - if (!image_8bit) - { - Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height); - return NULL; - } - memcpy(image_8bit, f + 8, image_width * image_height); - return image_8bit; -} - void Image_StripImageExtension (const char *in, char *out) { const char *end, *temp; @@ -679,7 +650,7 @@ void Image_StripImageExtension (const char *in, char *out) struct { const char *formatstring; - qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight); + qbyte *(*loadfunc)(const qbyte *f, int matchwidth, int matchheight); } imageformats[] = { @@ -692,7 +663,7 @@ imageformats[] = {"%s.tga", LoadTGA}, {"%s.jpg", JPEG_LoadImage}, {"%s.pcx", LoadPCX}, - {"%s.lmp", LoadLMP}, + {"%s.lmp", LoadLMPRGBA}, {NULL, NULL} }; diff --git a/image.h b/image.h index 6a49f5a2..023a489c 100644 --- a/image.h +++ b/image.h @@ -59,7 +59,7 @@ void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel); // only used by menuplyr coloring -qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight); +qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight); void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale); diff --git a/jpeg.c b/jpeg.c index 45a2c32b..6a28208a 100644 --- a/jpeg.c +++ b/jpeg.c @@ -468,7 +468,7 @@ static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes) cinfo->src->bytes_in_buffer -= num_bytes; } -static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer) +static void JPEG_MemSrc (j_decompress_ptr cinfo, const qbyte *buffer) { cinfo->src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)); @@ -496,7 +496,7 @@ JPEG_LoadImage Load a JPEG image into a RGBA buffer ==================== */ -qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight) +qbyte* JPEG_LoadImage (const qbyte *f, int matchwidth, int matchheight) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; diff --git a/jpeg.h b/jpeg.h index 787f9c88..d6bf4808 100644 --- a/jpeg.h +++ b/jpeg.h @@ -27,7 +27,7 @@ qboolean JPEG_OpenLibrary (void); void JPEG_CloseLibrary (void); -qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight); +qbyte* JPEG_LoadImage (const qbyte *f, int matchwidth, int matchheight); qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, qbyte *data); -- 2.39.2