]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
loadimagepixels is a little cleaner now (yes made cleaner by goto...) and dprints...
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 13120cee75a6cf2e8da1f4340075834f8c5b3fe7..aa7f0ef954407f6b413c9e61b06d2643bb64cbcd 100644 (file)
--- a/image.c
+++ b/image.c
@@ -1,6 +1,8 @@
 
 #include "quakedef.h"
 #include "image.h"
+#include "jpeg.h"
+#include "r_shadow.h"
 
 int            image_width;
 int            image_height;
@@ -91,7 +93,7 @@ qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
        const qbyte *palette, *fin, *enddata;
        int x, y, x2, dataByte;
 
-       if (loadsize < sizeof(pcx) + 768)
+       if (loadsize < (int)sizeof(pcx) + 768)
        {
                Con_Printf ("Bad pcx file\n");
                return NULL;
@@ -199,8 +201,22 @@ typedef struct _TargaHeader
 }
 TargaHeader;
 
-TargaHeader            targa_header;
-
+void PrintTargaHeader(TargaHeader *t)
+{
+       Con_Printf("TargaHeader:\n");
+       Con_Printf("uint8 id_length = %i;\n", t->id_length);
+       Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
+       Con_Printf("uint8 image_type = %i;\n", t->image_type);
+       Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
+       Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
+       Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
+       Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
+       Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
+       Con_Printf("uint16 width = %i;\n", t->width);
+       Con_Printf("uint16 height = %i;\n", t->height);
+       Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
+       Con_Printf("uint8 attributes = %i;\n", t->attributes);
+}
 
 /*
 =============
@@ -209,13 +225,17 @@ LoadTGA
 */
 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
 {
-       int x, y, row_inc;
-       unsigned char red, green, blue, alpha, run, runlen;
+       int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen;
        qbyte *pixbuf, *image_rgba;
        const qbyte *fin, *enddata;
+       TargaHeader targa_header;
+       unsigned char palette[256*4], *p;
 
-       if (loadsize < 18+3)
+       if (loadsize < 19)
                return NULL;
+
+       enddata = f + loadsize;
+
        targa_header.id_length = f[0];
        targa_header.colormap_type = f[1];
        targa_header.image_type = f[2];
@@ -234,23 +254,90 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
        targa_header.pixel_size = f[16];
        targa_header.attributes = f[17];
 
-       if (targa_header.image_type != 2 && targa_header.image_type != 10)
+       image_width = targa_header.width;
+       image_height = targa_header.height;
+
+       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_Printf ("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_Printf ("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_Printf ("LoadTGA: only 256 colormap_length supported\n");
+                       PrintTargaHeader(&targa_header);
+                       return NULL;
+               }
+               if (targa_header.colormap_index)
+               {
+                       Con_Printf ("LoadTGA: colormap_index not supported\n");
+                       PrintTargaHeader(&targa_header);
+                       return NULL;
+               }
+               if (targa_header.colormap_size == 24)
+               {
+                       for (x = 0;x < targa_header.colormap_length;x++)
+                       {
+                               palette[x*4+2] = *fin++;
+                               palette[x*4+1] = *fin++;
+                               palette[x*4+0] = *fin++;
+                               palette[x*4+3] = 255;
+                       }
+               }
+               else if (targa_header.colormap_size == 32)
+               {
+                       for (x = 0;x < targa_header.colormap_length;x++)
+                       {
+                               palette[x*4+2] = *fin++;
+                               palette[x*4+1] = *fin++;
+                               palette[x*4+0] = *fin++;
+                               palette[x*4+3] = *fin++;
+                       }
+               }
+               else
+               {
+                       Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size 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)
+               {
+                       Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
+                       PrintTargaHeader(&targa_header);
+                       return NULL;
+               }
+       }
+       else
        {
-               Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
+               Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
+               PrintTargaHeader(&targa_header);
                return NULL;
        }
 
-       if (targa_header.colormap_type != 0     || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
+       if (targa_header.attributes & 0x10)
        {
-               Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
+               Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
                return NULL;
        }
 
-       enddata = f + loadsize;
-
-       image_width = targa_header.width;
-       image_height = targa_header.height;
-
        image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
        if (!image_rgba)
        {
@@ -258,10 +345,6 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
                return NULL;
        }
 
-       fin = f + 18;
-       if (targa_header.id_length != 0)
-               fin += targa_header.id_length;  // skip TARGA image comment
-
        // If bit 5 of attributes isn't set, the image has been stored from bottom to top
        if ((targa_header.attributes & 0x20) == 0)
        {
@@ -274,148 +357,77 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
                row_inc = 0;
        }
 
-       if (targa_header.image_type == 2)
+       compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
+       x = 0;
+       y = 0;
+       red = green = blue = alpha = 255;
+       while (y < image_height)
        {
-               // Uncompressed, RGB images
-               if (targa_header.pixel_size == 24)
+               // decoder is mostly the same whether it's compressed or not
+               readpixelcount = 1000000;
+               runlen = 1000000;
+               if (compressed && fin < enddata)
                {
-                       if (fin + image_width * image_height * 3 <= enddata)
-                       {
-                               for(y = 0;y < image_height;y++)
-                               {
-                                       for(x = 0;x < image_width;x++)
-                                       {
-                                               *pixbuf++ = fin[2];
-                                               *pixbuf++ = fin[1];
-                                               *pixbuf++ = fin[0];
-                                               *pixbuf++ = 255;
-                                               fin += 3;
-                                       }
-                                       pixbuf += row_inc;
-                               }
-                       }
+                       runlen = *fin++;
+                       // high bit indicates this is an RLE compressed run
+                       if (runlen & 0x80)
+                               readpixelcount = 1;
+                       runlen = 1 + (runlen & 0x7f);
                }
-               else
+
+               while((runlen--) && y < image_height)
                {
-                       if (fin + image_width * image_height * 4 <= enddata)
+                       if (readpixelcount > 0)
                        {
-                               for(y = 0;y < image_height;y++)
+                               readpixelcount--;
+                               red = green = blue = alpha = 255;
+                               if (fin < enddata)
                                {
-                                       for(x = 0;x < image_width;x++)
+                                       switch(targa_header.image_type)
                                        {
-                                               *pixbuf++ = fin[2];
-                                               *pixbuf++ = fin[1];
-                                               *pixbuf++ = fin[0];
-                                               *pixbuf++ = fin[3];
-                                               fin += 4;
-                                       }
-                                       pixbuf += row_inc;
-                               }
-                       }
-               }
-       }
-       else if (targa_header.image_type==10)
-       {
-               // Runlength encoded RGB images
-               x = 0;
-               y = 0;
-               while (y < image_height && fin < enddata)
-               {
-                       runlen = *fin++;
-                       if (runlen & 0x80)
-                       {
-                               // RLE compressed run
-                               runlen = 1 + (runlen & 0x7f);
-                               if (targa_header.pixel_size == 24)
-                               {
-                                       if (fin + 3 > enddata)
+                                       case 1:
+                                       case 9:
+                                               // colormapped
+                                               p = palette + (*fin++) * 4;
+                                               red = p[0];
+                                               green = p[1];
+                                               blue = p[2];
+                                               alpha = p[3];
                                                break;
-                                       blue = *fin++;
-                                       green = *fin++;
-                                       red = *fin++;
-                                       alpha = 255;
-                               }
-                               else
-                               {
-                                       if (fin + 4 > enddata)
+                                       case 2:
+                                       case 10:
+                                               // BGR or BGRA
+                                               blue = *fin++;
+                                               if (fin < enddata)
+                                                       green = *fin++;
+                                               if (fin < enddata)
+                                                       red = *fin++;
+                                               if (targa_header.pixel_size == 32 && fin < enddata)
+                                                       alpha = *fin++;
+                                               break;
+                                       case 3:
+                                       case 11:
+                                               // greyscale
+                                               red = green = blue = *fin++;
                                                break;
-                                       blue = *fin++;
-                                       green = *fin++;
-                                       red = *fin++;
-                                       alpha = *fin++;
-                               }
-
-                               while (runlen && y < image_height)
-                               {
-                                       run = runlen;
-                                       if (run > image_width - x)
-                                               run = image_width - x;
-                                       x += run;
-                                       runlen -= run;
-                                       while(run--)
-                                       {
-                                               *pixbuf++ = red;
-                                               *pixbuf++ = green;
-                                               *pixbuf++ = blue;
-                                               *pixbuf++ = alpha;
-                                       }
-                                       if (x == image_width)
-                                       {
-                                               // end of line, advance to next
-                                               x = 0;
-                                               y++;
-                                               pixbuf += row_inc;
                                        }
                                }
                        }
-                       else
+                       *pixbuf++ = red;
+                       *pixbuf++ = green;
+                       *pixbuf++ = blue;
+                       *pixbuf++ = alpha;
+                       x++;
+                       if (x == image_width)
                        {
-                               // RLE uncompressed run
-                               runlen = 1 + (runlen & 0x7f);
-                               while (runlen && y < image_height)
-                               {
-                                       run = runlen;
-                                       if (run > image_width - x)
-                                               run = image_width - x;
-                                       x += run;
-                                       runlen -= run;
-                                       if (targa_header.pixel_size == 24)
-                                       {
-                                               if (fin + run * 3 > enddata)
-                                                       break;
-                                               while(run--)
-                                               {
-                                                       *pixbuf++ = fin[2];
-                                                       *pixbuf++ = fin[1];
-                                                       *pixbuf++ = fin[0];
-                                                       *pixbuf++ = 255;
-                                                       fin += 3;
-                                               }
-                                       }
-                                       else
-                                       {
-                                               if (fin + run * 4 > enddata)
-                                                       break;
-                                               while(run--)
-                                               {
-                                                       *pixbuf++ = fin[2];
-                                                       *pixbuf++ = fin[1];
-                                                       *pixbuf++ = fin[0];
-                                                       *pixbuf++ = fin[3];
-                                                       fin += 4;
-                                               }
-                                       }
-                                       if (x == image_width)
-                                       {
-                                               // end of line, advance to next
-                                               x = 0;
-                                               y++;
-                                               pixbuf += row_inc;
-                                       }
-                               }
+                               // end of line, advance to next
+                               x = 0;
+                               y++;
+                               pixbuf += row_inc;
                        }
                }
        }
+
        return image_rgba;
 }
 
@@ -531,55 +543,89 @@ void Image_StripImageExtension (const char *in, char *out)
 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
 {
        qbyte *f, *data;
-       char basename[256], name[256], *c;
-       Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
+       char basename[MAX_QPATH], name[MAX_QPATH], *c;
+       Image_StripImageExtension(filename, 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 == '*')
                        *c = '#';
+       sprintf (name, "override/%s.tga", basename);
+       f = COM_LoadFile(name, true);
+       if (f)
+       {
+               data = LoadTGA (f, matchwidth, matchheight);
+               goto loaded;
+       }
+       sprintf (name, "override/%s.jpg", basename);
+       f = COM_LoadFile(name, true);
+       if (f)
+       {
+               data = JPEG_LoadImage (f, matchwidth, matchheight);
+               goto loaded;
+       }
        sprintf (name, "textures/%s.tga", basename);
        f = COM_LoadFile(name, true);
        if (f)
        {
                data = LoadTGA (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
+       }
+       sprintf (name, "textures/%s.jpg", basename);
+       f = COM_LoadFile(name, true);
+       if (f)
+       {
+               data = JPEG_LoadImage (f, matchwidth, matchheight);
+               goto loaded;
        }
        sprintf (name, "textures/%s.pcx", basename);
        f = COM_LoadFile(name, true);
        if (f)
        {
                data = LoadPCX (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "%s.tga", basename);
        f = COM_LoadFile(name, true);
        if (f)
        {
                data = LoadTGA (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
+       }
+       sprintf (name, "%s.jpg", basename);
+       f = COM_LoadFile(name, true);
+       if (f)
+       {
+               data = JPEG_LoadImage (f, matchwidth, matchheight);
+               goto loaded;
        }
        sprintf (name, "%s.pcx", basename);
        f = COM_LoadFile(name, true);
        if (f)
        {
                data = LoadPCX (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "%s.lmp", basename);
        f = COM_LoadFile(name, true);
        if (f)
        {
                data = LoadLMP (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        if (complain)
-               Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
+               Con_Printf ("Couldn't load %s.tga, .jpg, .pcx, .lmp\n", filename);
        return NULL;
+loaded:
+       Mem_Free(f);
+       Con_DPrintf("loaded image %s (%dx%d)\n", name);
+       if (image_width == 0 || image_height == 0)
+       {
+               Con_Printf("error loading image %s - it is a %dx%d pixel image!\n", name);
+               if (data != NULL)
+                       Mem_Free(data);
+               data = NULL;
+       }
+       return data;
 }
 
 int image_makemask (const qbyte *in, qbyte *out, int size)
@@ -680,6 +726,22 @@ rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *f
        return rt;
 }
 
+rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
+{
+       qbyte *data, *data2;
+       rtexture_t *rt;
+       if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
+               return 0;
+       data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
+
+       Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
+       rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
+
+       Mem_Free(data2);
+       Mem_Free(data);
+       return rt;
+}
+
 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
 {
        qboolean ret;
@@ -1288,7 +1350,6 @@ void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *
        }
 }
 
-extern cvar_t r_shadow_bumpscale;
 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
 {
        int x, y;
@@ -1297,7 +1358,7 @@ void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *ou
        float iwidth, iheight, ibumpscale, n[3];
        iwidth = 1.0f / width;
        iheight = 1.0f / height;
-       ibumpscale = (255.0f * 3.0f) / (bumpscale * r_shadow_bumpscale.value);
+       ibumpscale = (255.0f * 3.0f) / bumpscale;
        out = outpixels;
        for (y = 0;y < height;y++)
        {
@@ -1345,3 +1406,115 @@ void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *ou
                }
        }
 }
+
+int image_loadskin(imageskin_t *s, char *name)
+{
+       int j;
+       qbyte *bumppixels;
+       int bumppixels_width, bumppixels_height;
+       memset(s, 0, sizeof(*s));
+       s->basepixels = loadimagepixels(name, false, 0, 0);
+       if (s->basepixels == NULL)
+               return false;
+       s->basepixels_width = image_width;
+       s->basepixels_height = image_height;
+
+       bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
+       for (j = 3;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
+               if (s->basepixels[j] < 255)
+                       break;
+       if (j < s->basepixels_width * s->basepixels_height * 4)
+       {
+               s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
+               s->maskpixels_width = s->basepixels_width;
+               s->maskpixels_height = s->basepixels_height;
+               memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
+               for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
+               {
+                       s->maskpixels[j+0] = 255;
+                       s->maskpixels[j+1] = 255;
+                       s->maskpixels[j+2] = 255;
+               }
+       }
+
+       // _luma is supported for tenebrae compatibility
+       // (I think it's a very stupid name, but oh well)
+       if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
+        || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
+       {
+               s->glowpixels_width = image_width;
+               s->glowpixels_height = image_height;
+       }
+       // _norm is the name used by tenebrae
+       // (I don't like the name much)
+       if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
+       {
+               s->nmappixels_width = image_width;
+               s->nmappixels_height = image_height;
+       }
+       else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
+       {
+               bumppixels_width = image_width;
+               bumppixels_height = image_height;
+       }
+       if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
+       {
+               s->glosspixels_width = image_width;
+               s->glosspixels_height = image_height;
+       }
+       if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
+       {
+               s->pantspixels_width = image_width;
+               s->pantspixels_height = image_height;
+       }
+       if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
+       {
+               s->shirtpixels_width = image_width;
+               s->shirtpixels_height = image_height;
+       }
+
+       if (s->nmappixels == NULL)
+       {
+               if (bumppixels != NULL)
+               {
+                       if (r_shadow_bumpscale_bumpmap.value > 0)
+                       {
+                               s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
+                               s->nmappixels_width = bumppixels_width;
+                               s->nmappixels_height = bumppixels_height;
+                               Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
+                       }
+               }
+               else
+               {
+                       if (r_shadow_bumpscale_basetexture.value > 0)
+                       {
+                               s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
+                               s->nmappixels_width = s->basepixels_width;
+                               s->nmappixels_height = s->basepixels_height;
+                               Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
+                       }
+               }
+       }
+       if (bumppixels != NULL)
+               Mem_Free(bumppixels);
+       return true;
+}
+
+void image_freeskin(imageskin_t *s)
+{
+       if (s->basepixels)
+               Mem_Free(s->basepixels);
+       if (s->nmappixels)
+               Mem_Free(s->nmappixels);
+       if (s->glowpixels)
+               Mem_Free(s->glowpixels);
+       if (s->glosspixels)
+               Mem_Free(s->glosspixels);
+       if (s->pantspixels)
+               Mem_Free(s->pantspixels);
+       if (s->shirtpixels)
+               Mem_Free(s->shirtpixels);
+       memset(s, 0, sizeof(*s));
+}
+