]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
changed most Sys_Error calls to Con_Printf with appropriate error returns
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 654747e02ff07d41c1d1a211b26458e5b9a803e8..4e372a85aea5dbf3e2dd04a918ebb61ef0fd1b1e 100644 (file)
--- a/image.c
+++ b/image.c
@@ -311,7 +311,7 @@ LoadTGA
 */
 qbyte *LoadTGA (const 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;
+       int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits;
        qbyte *pixbuf, *image_rgba;
        const qbyte *fin, *enddata;
        TargaHeader targa_header;
        qbyte *pixbuf, *image_rgba;
        const qbyte *fin, *enddata;
        TargaHeader targa_header;
@@ -344,26 +344,17 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
        targa_header.pixel_size = f[16];
        targa_header.attributes = f[17];
 
        targa_header.pixel_size = f[16];
        targa_header.attributes = f[17];
 
+       // advance to end of header
        fin = f + 18;
        fin = f + 18;
-       if (targa_header.id_length != 0)
-               fin += targa_header.id_length;  // skip TARGA image comment
-       if (targa_header.image_type == 2 || targa_header.image_type == 10)
-       {
-               if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
-               {
-                       Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
-                       PrintTargaHeader(&targa_header);
-                       return NULL;
-               }
-       }
-       else if (targa_header.image_type == 1 || targa_header.image_type == 9)
+
+       // skip TARGA image comment (usually 0 bytes)
+       fin += targa_header.id_length;
+
+       // read/skip the colormap if present (note: according to the TARGA spec it
+       // can be present even on truecolor or greyscale images, just not used by
+       // the image data)
+       if (targa_header.colormap_type)
        {
        {
-               if (targa_header.pixel_size != 8)
-               {
-                       Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
-                       PrintTargaHeader(&targa_header);
-                       return NULL;
-               }
                if (targa_header.colormap_length > 256)
                {
                        Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
                if (targa_header.colormap_length > 256)
                {
                        Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
@@ -403,6 +394,26 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
                        return NULL;
                }
        }
                        return NULL;
                }
        }
+
+       // check our pixel_size restrictions according to image_type
+       if (targa_header.image_type == 2 || targa_header.image_type == 10)
+       {
+               if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
+               {
+                       Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
+                       PrintTargaHeader(&targa_header);
+                       return NULL;
+               }
+       }
+       else if (targa_header.image_type == 1 || targa_header.image_type == 9)
+       {
+               if (targa_header.pixel_size != 8)
+               {
+                       Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
+                       PrintTargaHeader(&targa_header);
+                       return NULL;
+               }
+       }
        else if (targa_header.image_type == 3 || targa_header.image_type == 11)
        {
                if (targa_header.pixel_size != 8)
        else if (targa_header.image_type == 3 || targa_header.image_type == 11)
        {
                if (targa_header.pixel_size != 8)
@@ -444,6 +455,14 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
                row_inc = 0;
        }
 
                row_inc = 0;
        }
 
+       // number of attribute bits per pixel, we only support 0 or 8
+       alphabits = targa_header.attributes & 0x0F;
+       if (alphabits != 8 && alphabits != 0)
+       {
+               Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
+               return NULL;
+       }
+
        compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
        x = 0;
        y = 0;
        compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
        x = 0;
        y = 0;
@@ -501,6 +520,8 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
                                                red = green = blue = *fin++;
                                                break;
                                        }
                                                red = green = blue = *fin++;
                                                break;
                                        }
+                                       if (!alphabits)
+                                               alpha = 255;
                                }
                        }
                        *pixbuf++ = red;
                                }
                        }
                        *pixbuf++ = red;
@@ -669,7 +690,10 @@ qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth,
        char basename[MAX_QPATH], name[MAX_QPATH], *c;
        if (developer_memorydebug.integer)
                Mem_CheckSentinelsGlobal();
        char basename[MAX_QPATH], name[MAX_QPATH], *c;
        if (developer_memorydebug.integer)
                Mem_CheckSentinelsGlobal();
-       Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
+       if (developer_texturelogging.integer)
+               Log_Printf("textures.log", "%s\n", filename);
+       strlcpy(basename, filename, sizeof(basename));
+       Image_StripImageExtension(basename, basename); // strip filename 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 == '*')
        // replace *'s with #, so commandline utils don't get confused when dealing with the external files
        for (c = basename;*c;c++)
                if (*c == '*')
@@ -1279,7 +1303,10 @@ Image_Resample
 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
 {
        if (indepth != 1 || outdepth != 1)
 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
 {
        if (indepth != 1 || outdepth != 1)
-               Sys_Error("Image_Resample: 3D resampling not supported\n");
+       {
+               Con_Printf ("Image_Resample: 3D resampling not supported\n");
+               return;
+       }
        if (bytesperpixel == 4)
        {
                if (quality)
        if (bytesperpixel == 4)
        {
                if (quality)
@@ -1295,7 +1322,7 @@ void Image_Resample (const void *indata, int inwidth, int inheight, int indepth,
                        Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
        }
        else
                        Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
        }
        else
-               Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
+               Con_Printf ("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
 }
 
 // in can be the same as out
 }
 
 // in can be the same as out
@@ -1303,7 +1330,10 @@ void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *
 {
        int x, y, nextrow;
        if (*depth != 1 || destdepth != 1)
 {
        int x, y, nextrow;
        if (*depth != 1 || destdepth != 1)
-               Sys_Error("Image_Resample: 3D resampling not supported\n");
+       {
+               Con_Printf ("Image_Resample: 3D resampling not supported\n");
+               return;
+       }
        nextrow = *width * bytesperpixel;
        if (*width > destwidth)
        {
        nextrow = *width * bytesperpixel;
        if (*width > destwidth)
        {
@@ -1344,7 +1374,7 @@ void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *
                                }
                        }
                        else
                                }
                        }
                        else
-                               Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+                               Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
                }
                else
                {
                }
                else
                {
@@ -1379,7 +1409,7 @@ void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *
                                }
                        }
                        else
                                }
                        }
                        else
-                               Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+                               Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
                }
        }
        else
                }
        }
        else
@@ -1420,10 +1450,10 @@ void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *
                                }
                        }
                        else
                                }
                        }
                        else
-                               Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+                               Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
                }
                else
                }
                else
-                       Sys_Error("Image_MipReduce: desired size already achieved\n");
+                       Con_Printf ("Image_MipReduce: desired size already achieved\n");
        }
 }
 
        }
 }
 
@@ -1471,8 +1501,8 @@ void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *ou
                        n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
                        n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
                        */
                        n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
                        n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
                        */
-                       n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
-                       n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
+                       n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
+                       n[1] = ((p2[0] + p2[1] + p2[2]) - (p0[0] + p0[1] + p0[2]));
                        n[2] = ibumpscale;
                        VectorNormalize(n);
                        /*
                        n[2] = ibumpscale;
                        VectorNormalize(n);
                        /*
@@ -1498,7 +1528,8 @@ int image_loadskin(imageskin_t *s, char *shadername)
        qbyte *bumppixels;
        int bumppixels_width, bumppixels_height;
        char name[MAX_QPATH];
        qbyte *bumppixels;
        int bumppixels_width, bumppixels_height;
        char name[MAX_QPATH];
-       Image_StripImageExtension(shadername, name);
+       strlcpy(name, shadername, sizeof(name));
+       Image_StripImageExtension(name, name);
        memset(s, 0, sizeof(*s));
        s->basepixels = loadimagepixels(name, false, 0, 0);
        if (s->basepixels == NULL)
        memset(s, 0, sizeof(*s));
        s->basepixels = loadimagepixels(name, false, 0, 0);
        if (s->basepixels == NULL)