]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
corrected the misspelled name GAME_NEXIUZ (and related things) to GAME_NEXUIZ (and...
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 8bb3d4d7df418ef68dc470306ac6deb7bc3f8602..51cde77117bbfc9eb5a53fc1c6c548d1ba72e83c 100644 (file)
--- a/image.c
+++ b/image.c
@@ -2,6 +2,7 @@
 #include "quakedef.h"
 #include "image.h"
 #include "jpeg.h"
+#include "r_shadow.h"
 
 int            image_width;
 int            image_height;
@@ -92,7 +93,7 @@ qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
        const qbyte *palette, *fin, *enddata;
        int x, y, x2, dataByte;
 
-       if (loadsize < (int)sizeof(pcx) + 768)
+       if (fs_filesize < (int)sizeof(pcx) + 768)
        {
                Con_Printf ("Bad pcx file\n");
                return NULL;
@@ -131,7 +132,7 @@ qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
        image_width = pcx.xmax+1;
        image_height = pcx.ymax+1;
 
-       palette = f + loadsize - 768;
+       palette = f + fs_filesize - 768;
 
        image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
        if (!image_rgba)
@@ -230,10 +231,10 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
        TargaHeader targa_header;
        unsigned char palette[256*4], *p;
 
-       if (loadsize < 19)
+       if (fs_filesize < 19)
                return NULL;
 
-       enddata = f + loadsize;
+       enddata = f + fs_filesize;
 
        targa_header.id_length = f[0];
        targa_header.colormap_type = f[1];
@@ -440,7 +441,7 @@ qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
        qbyte *image_rgba;
        int width, height;
 
-       if (loadsize < 9)
+       if (fs_filesize < 9)
        {
                Con_Printf("LoadLMP: invalid LMP file\n");
                return NULL;
@@ -457,7 +458,7 @@ qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
        if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
                return NULL;
 
-       if (loadsize < 8 + width * height)
+       if (fs_filesize < 8 + width * height)
        {
                Con_Printf("LoadLMP: invalid LMP file\n");
                return NULL;
@@ -486,7 +487,7 @@ qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
        qbyte *image_8bit;
        int width, height;
 
-       if (loadsize < 9)
+       if (fs_filesize < 9)
        {
                Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
                return NULL;
@@ -503,7 +504,7 @@ qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
        if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
                return NULL;
 
-       if (loadsize < 8 + width * height)
+       if (fs_filesize < 8 + width * height)
        {
                Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
                return NULL;
@@ -549,80 +550,82 @@ qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth,
                if (*c == '*')
                        *c = '#';
        sprintf (name, "override/%s.tga", basename);
-       f = COM_LoadFile(name, true);
+       f = FS_LoadFile(name, true);
        if (f)
        {
                data = LoadTGA (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "override/%s.jpg", basename);
-       f = COM_LoadFile(name, true);
+       f = FS_LoadFile(name, true);
        if (f)
        {
                data = JPEG_LoadImage (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "textures/%s.tga", basename);
-       f = COM_LoadFile(name, true);
+       f = FS_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);
+       f = FS_LoadFile(name, true);
        if (f)
        {
                data = JPEG_LoadImage (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "textures/%s.pcx", basename);
-       f = COM_LoadFile(name, true);
+       f = FS_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);
+       f = FS_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);
+       f = FS_LoadFile(name, true);
        if (f)
        {
                data = JPEG_LoadImage (f, matchwidth, matchheight);
-               Mem_Free(f);
-               return data;
+               goto loaded;
        }
        sprintf (name, "%s.pcx", basename);
-       f = COM_LoadFile(name, true);
+       f = FS_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);
+       f = FS_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, .jpg, .pcx, .lmp\n", filename);
        return NULL;
+loaded:
+       Mem_Free(f);
+       Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
+       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)
@@ -765,7 +768,7 @@ qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int heig
                *out++ = in[1];
                *out++ = in[0];
        }
-       ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
+       ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
 
        Mem_Free(buffer);
        return ret;
@@ -800,7 +803,7 @@ void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte
                        *out++ = in[0];
                }
        }
-       COM_WriteFile (filename, buffer, width*height*3 + 18 );
+       FS_WriteFile (filename, buffer, width*height*3 + 18 );
 
        Mem_Free(buffer);
 }
@@ -835,7 +838,7 @@ void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyt
                        *out++ = in[3];
                }
        }
-       COM_WriteFile (filename, buffer, width*height*4 + 18 );
+       FS_WriteFile (filename, buffer, width*height*4 + 18 );
 
        Mem_Free(buffer);
 }
@@ -1347,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;
@@ -1356,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++)
        {
@@ -1404,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));
+}
+