]> de.git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
Factorized LoadLMP and LoadLMPAs8Bit. Made a bunch of buffers const
authormolivier <molivier@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 4 Apr 2005 05:40:51 +0000 (05:40 +0000)
committermolivier <molivier@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 4 Apr 2005 05:40:51 +0000 (05:40 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5146 d7cf8633-e32d-0410-b094-e92efae38249

image.c
image.h
jpeg.c
jpeg.h

diff --git a/image.c b/image.c
index 02ac9233fbfe786127b4b8f62f176fa5059217c6..86c3d9eeb15c108480be03a124fd130e988bc444 100644 (file)
--- a/image.c
+++ b/image.c
@@ -180,7 +180,7 @@ typedef struct
 LoadPCX
 ============
 */
 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;
 {
        pcx_t pcx;
        qbyte *a, *b, *image_rgba, *pbuf;
@@ -309,11 +309,11 @@ void PrintTargaHeader(TargaHeader *t)
 LoadTGA
 =============
 */
 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;
 {
        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;
 
        TargaHeader targa_header;
        unsigned char palette[256*4], *p;
 
@@ -526,9 +526,9 @@ qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
 LoadLMP
 ============
 */
 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)
        {
 
        if (fs_filesize < 9)
        {
@@ -537,8 +537,8 @@ qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
        }
 
        // parse the very complicated header *chuckle*
        }
 
        // 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);
        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;
        }
 
                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];
 typedef struct
 {
        char            name[32];
@@ -574,7 +588,7 @@ typedef struct
        int                     value;
 } q2wal_t;
 
        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;
 {
        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;
 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;
 struct
 {
        const char *formatstring;
-       qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight);
+       qbyte *(*loadfunc)(const qbyte *f, int matchwidth, int matchheight);
 }
 imageformats[] =
 {
 }
 imageformats[] =
 {
@@ -692,7 +663,7 @@ imageformats[] =
        {"%s.tga", LoadTGA},
        {"%s.jpg", JPEG_LoadImage},
        {"%s.pcx", LoadPCX},
        {"%s.tga", LoadTGA},
        {"%s.jpg", JPEG_LoadImage},
        {"%s.pcx", LoadPCX},
-       {"%s.lmp", LoadLMP},
+       {"%s.lmp", LoadLMPRGBA},
        {NULL, NULL}
 };
 
        {NULL, NULL}
 };
 
diff --git a/image.h b/image.h
index 6a49f5a29a4f6aad6ceaa54c11b0e54830c981d1..023a489c0c3d469e443b75ceb1eac0737a61f916 100644 (file)
--- 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
 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);
 
 
 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 45a2c32b56040932b2e501eca6bf62d383cf6c83..6a28208ababbecfccefa973f1f1bbb4208f1cff7 100644 (file)
--- 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;
 }
 
     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));
 
 {
        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
 ====================
 */
 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;
 {
        struct jpeg_decompress_struct cinfo;
        struct jpeg_error_mgr jerr;
diff --git a/jpeg.h b/jpeg.h
index 787f9c881939a128e0a635cd1dabd35373b6eb82..d6bf4808a48c83ced58d388d3246ca10857fef6e 100644 (file)
--- a/jpeg.h
+++ b/jpeg.h
@@ -27,7 +27,7 @@
 
 qboolean JPEG_OpenLibrary (void);
 void JPEG_CloseLibrary (void);
 
 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);
 
 
 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, qbyte *data);