]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
convert lightmaps to sRGB for nice sRGB support
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 462634d094c673d109e11749b73daaf07b1e102e..67c931a4d8a3e213acd7c9185fdda9034ebc7c5f 100644 (file)
--- a/image.c
+++ b/image.c
@@ -219,7 +219,7 @@ unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize, int *miplevel
 
        image_width = pcx.xmax + 1 - pcx.xmin;
        image_height = pcx.ymax + 1 - pcx.ymin;
-       if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
+       if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
        {
                Con_Print("Bad pcx file\n");
                return NULL;
@@ -797,6 +797,7 @@ void Image_StripImageExtension (const char *in, char *out, size_t size_out)
 }
 
 static unsigned char image_linearfromsrgb[256];
+static unsigned char image_srgbfromlinear[256];
 
 void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pin, int numpixels)
 {
@@ -804,7 +805,7 @@ void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pi
        // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
        if (!image_linearfromsrgb[255])
                for (i = 0;i < 256;i++)
-                       image_linearfromsrgb[i] = i < 11 ? (int)(i / 12.92f) : (int)(pow((i/256.0f + 0.055f)/1.0555f, 2.4f)*256.0f);
+                       image_linearfromsrgb[i] = (unsigned char)(Image_LinearFloatFromsRGB(i) * 256.0f);
        for (i = 0;i < numpixels;i++)
        {
                pout[i*4+0] = image_linearfromsrgb[pin[i*4+0]];
@@ -814,6 +815,22 @@ void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pi
        }
 }
 
+void Image_MakesRGBColorsFromLinear(unsigned char *pout, const unsigned char *pin, int numpixels)
+{
+       int i;
+       // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
+       if (!image_srgbfromlinear[255])
+               for (i = 0;i < 256;i++)
+                       image_srgbfromlinear[i] = (unsigned char)bound(0, Image_sRGBFloatFromLinear(i*2) * 128.0f, 255);
+       for (i = 0;i < numpixels;i++)
+       {
+               pout[i*4+0] = image_srgbfromlinear[pin[i*4+0]];
+               pout[i*4+1] = image_srgbfromlinear[pin[i*4+1]];
+               pout[i*4+2] = image_srgbfromlinear[pin[i*4+2]];
+               pout[i*4+3] = pin[i*4+3];
+       }
+}
+
 typedef struct imageformat_s
 {
        const char *formatstring;
@@ -1004,14 +1021,14 @@ unsigned char *loadimagepixelsbgra (const char *filename, qboolean complain, qbo
 }
 
 extern cvar_t gl_picmip;
-rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans, qboolean convertsRGB)
+rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans, qboolean sRGB)
 {
        unsigned char *data;
        rtexture_t *rt;
        int miplevel = R_PicmipForFlags(flags);
-       if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans, convertsRGB, &miplevel)))
+       if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans, false, &miplevel)))
                return 0;
-       rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_BGRA, flags, miplevel, NULL);
+       rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, flags, miplevel, NULL);
        Mem_Free(data);
        return rt;
 }