]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 72492f87c0a74226361765b784c39e48526a5af8..dbad08ff5b69231a6e8255e1faafe393b8efaa11 100644 (file)
--- a/image.c
+++ b/image.c
@@ -4,6 +4,18 @@
 int            image_width;
 int            image_height;
 
+void Image_GammaRemapRGB(byte *in, byte *out, int pixels, byte *gammar, byte *gammag, byte *gammab)
+{
+       while (pixels--)
+       {
+               out[0] = gammar[in[0]];
+               out[1] = gammag[in[1]];
+               out[2] = gammab[in[2]];
+               in += 3;
+               out += 3;
+       }
+}
+
 // note: pal must be 32bit color
 void Image_Copy8bitRGBA(byte *in, byte *out, int pixels, int *pal)
 {
@@ -42,20 +54,6 @@ void Image_Copy8bitRGBA(byte *in, byte *out, int pixels, int *pal)
                iout[0] = pal[in[0]];
 }
 
-void Image_CopyRGBAGamma(byte *in, byte *out, int pixels)
-{
-       while (pixels--)
-       {
-               out[0] = texgamma[in[0]];
-               out[1] = texgamma[in[1]];
-               out[2] = texgamma[in[2]];
-               out[3] =          in[3] ;
-               in += 4;
-               out += 4;
-       }
-}
-
-
 /*
 =================================================================
 
@@ -88,8 +86,14 @@ LoadPCX
 byte* LoadPCX (byte *f, int matchwidth, int matchheight)
 {
        pcx_t   pcx;
-       byte    *palette, *a, *b, *image_rgba, *fin, *pbuf;
-       int             x, y, dataByte, runLength;
+       byte    *palette, *a, *b, *image_rgba, *fin, *pbuf, *enddata;
+       int             x, y, x2, dataByte;
+
+       if (loadsize < sizeof(pcx) + 768)
+       {
+               Con_Printf ("Bad pcx file\n");
+               return NULL;
+       }
 
        fin = f;
 
@@ -109,53 +113,56 @@ byte* LoadPCX (byte *f, int matchwidth, int matchheight)
        if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
        {
                Con_Printf ("Bad pcx file\n");
-               qfree(f);
                return NULL;
        }
 
        if (matchwidth && (pcx.xmax+1) != matchwidth)
        {
-               qfree(f);
                return NULL;
        }
        if (matchheight && (pcx.ymax+1) != matchheight)
        {
-               qfree(f);
                return NULL;
        }
 
        image_width = pcx.xmax+1;
        image_height = pcx.ymax+1;
 
-       image_rgba = qmalloc(image_width*image_height*4);
+       palette = f + loadsize - 768;
+
+       image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
+       if (!image_rgba)
+       {
+               Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
+               return NULL;
+       }
        pbuf = image_rgba + image_width*image_height*3;
+       enddata = palette;
 
-       for (y = 0;y < image_height;y++)
+       for (y = 0;y < image_height && fin < enddata;y++)
        {
                a = pbuf + y * image_width;
-               for (x = 0;x < image_width;)
+               for (x = 0;x < image_width && fin < enddata;)
                {
                        dataByte = *fin++;
                        if(dataByte >= 0xC0)
                        {
-                               runLength = dataByte & 0x3F;
+                               if (fin >= enddata)
+                                       break;
+                               x2 = x + (dataByte & 0x3F);
                                dataByte = *fin++;
-                               if (runLength)
-                               {
-                                       x += runLength;
-                                       while(runLength--)
-                                               *a++ = dataByte;
-                               }
+                               if (x2 > image_width)
+                                       x2 = image_width; // technically an error
+                               while(x < x2)
+                                       a[x++] = dataByte;
                        }
                        else
-                       {
-                               x++;
-                               *a++ = dataByte;
-                       }
+                               a[x++] = dataByte;
                }
+               while(x < image_width)
+                       a[x++] = 0;
        }
 
-       palette = fin;
        a = image_rgba;
        b = pbuf;
 
@@ -168,7 +175,6 @@ byte* LoadPCX (byte *f, int matchwidth, int matchheight)
                *a++ = 255;
        }
 
-       qfree(f);
        return image_rgba;
 }
 
@@ -200,13 +206,15 @@ LoadTGA
 */
 byte* LoadTGA (byte *f, int matchwidth, int matchheight)
 {
-       int columns, rows, numPixels, row, column;
-       byte *pixbuf, *image_rgba, *fin;
+       int columns, rows, row, column;
+       byte *pixbuf, *image_rgba, *fin, *enddata;
 
+       if (loadsize < 18+3)
+               return NULL;
        targa_header.id_length = f[0];
        targa_header.colormap_type = f[1];
        targa_header.image_type = f[2];
-       
+
        targa_header.colormap_index = f[3] + f[4] * 256;
        targa_header.colormap_length = f[5] + f[6] * 256;
        targa_header.colormap_size = f[7];
@@ -222,21 +230,33 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
        targa_header.attributes = f[17];
 
        if (targa_header.image_type != 2 && targa_header.image_type != 10)
-               Host_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
+       {
+               Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
+               return NULL;
+       }
 
        if (targa_header.colormap_type != 0     || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
-               Host_Error ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
+       {
+               Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
+               return NULL;
+       }
+
+       enddata = f + loadsize;
 
        columns = targa_header.width;
        rows = targa_header.height;
-       numPixels = columns * rows;
 
-       image_rgba = qmalloc(numPixels*4);
+       image_rgba = Mem_Alloc(tempmempool, columns * rows * 4);
+       if (!image_rgba)
+       {
+               Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", columns, rows);
+               return NULL;
+       }
 
        fin = f + 18;
        if (targa_header.id_length != 0)
                fin += targa_header.id_length;  // skip TARGA image comment
-       
+
        if (targa_header.image_type == 2)
        {
                // Uncompressed, RGB images
@@ -248,6 +268,8 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                switch (targa_header.pixel_size)
                                {
                                case 24:
+                                       if (fin + 3 > enddata)
+                                               break;
                                        *pixbuf++ = fin[2];
                                        *pixbuf++ = fin[1];
                                        *pixbuf++ = fin[0];
@@ -255,6 +277,8 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                        fin += 3;
                                        break;
                                case 32:
+                                       if (fin + 4 > enddata)
+                                               break;
                                        *pixbuf++ = fin[2];
                                        *pixbuf++ = fin[1];
                                        *pixbuf++ = fin[0];
@@ -274,6 +298,8 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                        pixbuf = image_rgba + row * columns * 4;
                        for(column = 0;column < columns;)
                        {
+                               if (fin >= enddata)
+                                       goto outofdata;
                                packetHeader = *fin++;
                                packetSize = 1 + (packetHeader & 0x7f);
                                if (packetHeader & 0x80)
@@ -282,19 +308,23 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                        switch (targa_header.pixel_size)
                                        {
                                        case 24:
+                                               if (fin + 3 > enddata)
+                                                       goto outofdata;
                                                blue = *fin++;
                                                green = *fin++;
                                                red = *fin++;
                                                alphabyte = 255;
                                                break;
                                        case 32:
+                                               if (fin + 4 > enddata)
+                                                       goto outofdata;
                                                blue = *fin++;
                                                green = *fin++;
                                                red = *fin++;
                                                alphabyte = *fin++;
                                                break;
                                        }
-       
+
                                        for(j = 0;j < packetSize;j++)
                                        {
                                                *pixbuf++ = red;
@@ -322,6 +352,8 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                                switch (targa_header.pixel_size)
                                                {
                                                case 24:
+                                                       if (fin + 3 > enddata)
+                                                               goto outofdata;
                                                        *pixbuf++ = fin[2];
                                                        *pixbuf++ = fin[1];
                                                        *pixbuf++ = fin[0];
@@ -329,6 +361,8 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                                        fin += 3;
                                                        break;
                                                case 32:
+                                                       if (fin + 4 > enddata)
+                                                               goto outofdata;
                                                        *pixbuf++ = fin[2];
                                                        *pixbuf++ = fin[1];
                                                        *pixbuf++ = fin[0];
@@ -346,17 +380,17 @@ byte* LoadTGA (byte *f, int matchwidth, int matchheight)
                                                        else
                                                                goto breakOut;
                                                        pixbuf = image_rgba + row * columns * 4;
-                                               }                                               
+                                               }
                                        }
                                }
                        }
                        breakOut:;
                }
        }
-       
+outofdata:;
+
        image_width = columns;
        image_height = rows;
-       free(f);
        return image_rgba;
 }
 
@@ -370,30 +404,39 @@ byte* LoadLMP (byte *f, int matchwidth, int matchheight)
        byte    *image_rgba;
        int             width, height;
 
-       // parse the very complicated header *chuckle*
-       width = LittleLong(((int *)f)[0]);
-       height = LittleLong(((int *)f)[1]);
-       if ((unsigned) width > 4096 || (unsigned) height > 4096)
+       if (loadsize < 9)
        {
-               qfree(f);
-               Host_Error("LoadLMP: invalid size\n");
+               Con_Printf("LoadLMP: invalid LMP file\n");
+               return NULL;
        }
-       if (matchwidth && width != matchwidth)
+
+       // parse the very complicated header *chuckle*
+       width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
+       height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
+       if ((unsigned) width > 4096 || (unsigned) height > 4096)
        {
-               qfree(f);
+               Con_Printf("LoadLMP: invalid size\n");
                return NULL;
        }
-       if (matchheight && height != matchheight)
+       if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
+               return NULL;
+
+       if (loadsize < 8 + width * height)
        {
-               qfree(f);
+               Con_Printf("LoadLMP: invalid LMP file\n");
                return NULL;
        }
 
-       image_rgba = qmalloc(width*height*4);
-       Image_Copy8bitRGBA(f + 8, image_rgba, width*height, d_8to24table);
        image_width = width;
        image_height = height;
-       qfree(f);
+
+       image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
+       if (!image_rgba)
+       {
+               Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
+               return NULL;
+       }
+       Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, d_8to24table);
        return image_rgba;
 }
 
@@ -416,34 +459,53 @@ void Image_StripImageExtension (char *in, char *out)
 
 byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight)
 {
-       byte    *f;
-       char    basename[256], name[256];
-       byte    *c;
+       byte    *f, *data;
+       char    basename[256], name[256], *c;
        Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
        // replace *'s with #, so commandline utils don't get confused when dealing with the external files
        for (c = basename;*c;c++)
                if (*c == '*')
                        *c = '#';
        sprintf (name, "textures/%s.tga", basename);
-       f = COM_LoadMallocFile(name, true);
+       f = COM_LoadFile(name, true);
        if (f)
-               return LoadTGA (f, matchwidth, matchheight);
+       {
+               data = LoadTGA (f, matchwidth, matchheight);
+               Mem_Free(f);
+               return data;
+       }
        sprintf (name, "textures/%s.pcx", basename);
-       f = COM_LoadMallocFile(name, true);
+       f = COM_LoadFile(name, true);
        if (f)
-               return LoadPCX (f, matchwidth, matchheight);
+       {
+               data = LoadPCX (f, matchwidth, matchheight);
+               Mem_Free(f);
+               return data;
+       }
        sprintf (name, "%s.tga", basename);
-       f = COM_LoadMallocFile(name, true);
+       f = COM_LoadFile(name, true);
        if (f)
-               return LoadTGA (f, matchwidth, matchheight);
+       {
+               data = LoadTGA (f, matchwidth, matchheight);
+               Mem_Free(f);
+               return data;
+       }
        sprintf (name, "%s.pcx", basename);
-       f = COM_LoadMallocFile(name, true);
+       f = COM_LoadFile(name, true);
        if (f)
-               return LoadPCX (f, matchwidth, matchheight);
+       {
+               data = LoadPCX (f, matchwidth, matchheight);
+               Mem_Free(f);
+               return data;
+       }
        sprintf (name, "%s.lmp", basename);
-       f = COM_LoadMallocFile(name, true);
+       f = COM_LoadFile(name, true);
        if (f)
-               return LoadLMP (f, matchwidth, matchheight);
+       {
+               data = LoadLMP (f, matchwidth, matchheight);
+               Mem_Free(f);
+               return data;
+       }
        if (complain)
                Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
        return NULL;
@@ -475,35 +537,35 @@ byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, in
                return data; // some transparency
        else
        {
-               qfree(data);
+               Mem_Free(data);
                return NULL; // all opaque
        }
 }
 
-rtexture_t *loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
+rtexture_t *loadtextureimage (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
 {
        byte *data;
        rtexture_t *rt;
        if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
                return 0;
-       rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
-       qfree(data);
+       rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
+       Mem_Free(data);
        return rt;
 }
 
-rtexture_t *loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
+rtexture_t *loadtextureimagemask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
 {
        byte *data;
        rtexture_t *rt;
        if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
                return 0;
-       rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
-       qfree(data);
+       rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
+       Mem_Free(data);
        return rt;
 }
 
 rtexture_t *image_masktex;
-rtexture_t *loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
+rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
 {
        int count;
        byte *data;
@@ -512,16 +574,16 @@ rtexture_t *loadtextureimagewithmask (char* filename, int matchwidth, int matchh
        image_masktex = NULL;
        if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
                return 0;
-       rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
+       rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
        count = image_makemask(data, data, image_width * image_height);
        if (count)
        {
-               filename2 = qmalloc(strlen(filename) + 6);
+               filename2 = Mem_Alloc(tempmempool, strlen(filename) + 6);
                sprintf(filename2, "%s_mask", filename);
-               image_masktex = R_LoadTexture (filename2, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
-               qfree(filename2);
+               image_masktex = R_LoadTexture (pool, filename2, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
+               Mem_Free(filename2);
        }
-       qfree(data);
+       Mem_Free(data);
        return rt;
 }
 
@@ -529,7 +591,7 @@ void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *
 {
        byte *buffer, *in, *out, *end;
 
-       buffer = qmalloc(width*height*3 + 18);
+       buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
 
        memset (buffer, 0, 18);
        buffer[2] = 2;          // uncompressed type
@@ -551,7 +613,7 @@ void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *
        }
        COM_WriteFile (filename, buffer, width*height*3 + 18 );
 
-       qfree(buffer);
+       Mem_Free(buffer);
 }
 
 void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
@@ -559,7 +621,7 @@ void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
        int y;
        byte *buffer, *in, *out, *end;
 
-       buffer = qmalloc(width*height*3 + 18);
+       buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
 
        memset (buffer, 0, 18);
        buffer[2] = 2;          // uncompressed type
@@ -584,7 +646,7 @@ void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
        }
        COM_WriteFile (filename, buffer, width*height*3 + 18 );
 
-       qfree(buffer);
+       Mem_Free(buffer);
 }
 
 void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
@@ -592,7 +654,7 @@ void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
        int y;
        byte *buffer, *in, *out, *end;
 
-       buffer = qmalloc(width*height*4 + 18);
+       buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
 
        memset (buffer, 0, 18);
        buffer[2] = 2;          // uncompressed type
@@ -618,7 +680,7 @@ void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
        }
        COM_WriteFile (filename, buffer, width*height*4 + 18 );
 
-       qfree(buffer);
+       Mem_Free(buffer);
 }
 
 qboolean Image_CheckAlpha(byte *data, int size, qboolean rgba)