convert lightmaps to sRGB for nice sRGB support
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index c556f39..67c931a 100644 (file)
--- a/image.c
+++ b/image.c
@@ -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)
 {
@@ -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;