]> 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 0abe8d04c3c66472fd7998b96624ced93c85a67e..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;
@@ -541,8 +543,8 @@ 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 == '*')
@@ -552,52 +554,78 @@ qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth,
        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);
+       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)
@@ -698,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;
@@ -1306,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;
@@ -1315,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++)
        {
@@ -1363,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));
+}
+