[Mesa-dev] [PATCH 05/12] main: Gather some common format conversion functions into a single format_utils file

Jason Ekstrand jason at jlekstrand.net
Thu Jul 17 11:04:27 PDT 2014


This initial commit puts all of the RGB <-> sRGB conversion functions in
one place.

Signed-off-by: Jason Ekstrand <jason.ekstrand at intel.com>
---
 src/mesa/Makefile.sources        |   1 +
 src/mesa/main/format_pack.c      | 102 ++++++++++++++-------------------------
 src/mesa/main/format_unpack.c    |  69 ++++++++------------------
 src/mesa/main/format_unpack.h    |   3 --
 src/mesa/main/format_utils.c     |  56 +++++++++++++++++++++
 src/mesa/main/format_utils.h     |  68 ++++++++++++++++++++++++++
 src/mesa/main/texcompress_etc.c  |  20 ++++----
 src/mesa/main/texcompress_s3tc.c |  26 +++++-----
 8 files changed, 204 insertions(+), 141 deletions(-)
 create mode 100644 src/mesa/main/format_utils.c
 create mode 100644 src/mesa/main/format_utils.h

diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources
index f4904fb..a261113 100644
--- a/src/mesa/Makefile.sources
+++ b/src/mesa/Makefile.sources
@@ -48,6 +48,7 @@ MAIN_FILES = \
 	$(SRCDIR)main/formats.c \
 	$(SRCDIR)main/format_pack.c \
 	$(SRCDIR)main/format_unpack.c \
+	$(SRCDIR)main/format_utils.c \
 	$(SRCDIR)main/framebuffer.c \
 	$(SRCDIR)main/get.c \
 	$(SRCDIR)main/genmipmap.c \
diff --git a/src/mesa/main/format_pack.c b/src/mesa/main/format_pack.c
index 6b28592..fb3feb5 100644
--- a/src/mesa/main/format_pack.c
+++ b/src/mesa/main/format_pack.c
@@ -38,6 +38,7 @@
 
 #include "colormac.h"
 #include "format_pack.h"
+#include "format_utils.h"
 #include "macros.h"
 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
@@ -58,39 +59,6 @@ typedef void (*pack_float_rgba_row_func)(GLuint n,
                                          const GLfloat src[][4], void *dst);
 
 
-
-static inline GLfloat
-linear_to_srgb(GLfloat cl)
-{
-   if (cl < 0.0f)
-      return 0.0f;
-   else if (cl < 0.0031308f)
-      return 12.92f * cl;
-   else if (cl < 1.0f)
-      return 1.055f * powf(cl, 0.41666f) - 0.055f;
-   else
-      return 1.0f;
-}
-
-
-static inline GLubyte
-linear_float_to_srgb_ubyte(GLfloat cl)
-{
-   GLubyte res = FLOAT_TO_UBYTE(linear_to_srgb(cl));
-   return res;
-}
-
-
-static inline GLubyte
-linear_ubyte_to_srgb_ubyte(GLubyte cl)
-{
-   GLubyte res = FLOAT_TO_UBYTE(linear_to_srgb(cl / 255.0f));
-   return res;
-}
-
-
-
-
 /*
  * MESA_FORMAT_A8B8G8R8_UNORM
  */
@@ -1043,18 +1011,18 @@ static void
 pack_ubyte_BGR_SRGB8(const GLubyte src[4], void *dst)
 {
    GLubyte *d = ((GLubyte *) dst);
-   d[2] = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
-   d[1] = linear_ubyte_to_srgb_ubyte(src[GCOMP]);
-   d[0] = linear_ubyte_to_srgb_ubyte(src[BCOMP]);
+   d[2] = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   d[1] = _mesa_linear_ubyte_to_srgb_ubyte(src[GCOMP]);
+   d[0] = _mesa_linear_ubyte_to_srgb_ubyte(src[BCOMP]);
 }
 
 static void
 pack_float_BGR_SRGB8(const GLfloat src[4], void *dst)
 {
    GLubyte *d = ((GLubyte *) dst);
-   d[2] = linear_float_to_srgb_ubyte(src[RCOMP]);
-   d[1] = linear_float_to_srgb_ubyte(src[GCOMP]);
-   d[0] = linear_float_to_srgb_ubyte(src[BCOMP]);
+   d[2] = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   d[1] = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   d[0] = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
 }
 
 
@@ -1064,9 +1032,9 @@ static void
 pack_ubyte_A8B8G8R8_SRGB(const GLubyte src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
-   GLubyte r = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
-   GLubyte g = linear_ubyte_to_srgb_ubyte(src[GCOMP]);
-   GLubyte b = linear_ubyte_to_srgb_ubyte(src[BCOMP]);
+   GLubyte r = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   GLubyte g = _mesa_linear_ubyte_to_srgb_ubyte(src[GCOMP]);
+   GLubyte b = _mesa_linear_ubyte_to_srgb_ubyte(src[BCOMP]);
    *d = PACK_COLOR_8888(r, g, b, src[ACOMP]);
 }
 
@@ -1075,9 +1043,9 @@ pack_float_A8B8G8R8_SRGB(const GLfloat src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
    GLubyte r, g, b, a;
-   r = linear_float_to_srgb_ubyte(src[RCOMP]);
-   g = linear_float_to_srgb_ubyte(src[GCOMP]);
-   b = linear_float_to_srgb_ubyte(src[BCOMP]);
+   r = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   g = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   b = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
    UNCLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
    *d = PACK_COLOR_8888(r, g, b, a);
 }
@@ -1089,9 +1057,9 @@ static void
 pack_ubyte_B8G8R8A8_SRGB(const GLubyte src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
-   GLubyte r = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
-   GLubyte g = linear_ubyte_to_srgb_ubyte(src[GCOMP]);
-   GLubyte b = linear_ubyte_to_srgb_ubyte(src[BCOMP]);
+   GLubyte r = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   GLubyte g = _mesa_linear_ubyte_to_srgb_ubyte(src[GCOMP]);
+   GLubyte b = _mesa_linear_ubyte_to_srgb_ubyte(src[BCOMP]);
    *d = PACK_COLOR_8888(src[ACOMP], r, g, b);
 }
 
@@ -1100,9 +1068,9 @@ pack_float_B8G8R8A8_SRGB(const GLfloat src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
    GLubyte r, g, b, a;
-   r = linear_float_to_srgb_ubyte(src[RCOMP]);
-   g = linear_float_to_srgb_ubyte(src[GCOMP]);
-   b = linear_float_to_srgb_ubyte(src[BCOMP]);
+   r = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   g = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   b = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
    UNCLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
    *d = PACK_COLOR_8888(a, r, g, b);
 }
@@ -1114,9 +1082,9 @@ static void
 pack_ubyte_R8G8B8A8_SRGB(const GLubyte src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
-   GLubyte r = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
-   GLubyte g = linear_ubyte_to_srgb_ubyte(src[GCOMP]);
-   GLubyte b = linear_ubyte_to_srgb_ubyte(src[BCOMP]);
+   GLubyte r = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   GLubyte g = _mesa_linear_ubyte_to_srgb_ubyte(src[GCOMP]);
+   GLubyte b = _mesa_linear_ubyte_to_srgb_ubyte(src[BCOMP]);
    *d = PACK_COLOR_8888(src[ACOMP], b, g, r);
 }
 
@@ -1125,9 +1093,9 @@ pack_float_R8G8B8A8_SRGB(const GLfloat src[4], void *dst)
 {
    GLuint *d = ((GLuint *) dst);
    GLubyte r, g, b, a;
-   r = linear_float_to_srgb_ubyte(src[RCOMP]);
-   g = linear_float_to_srgb_ubyte(src[GCOMP]);
-   b = linear_float_to_srgb_ubyte(src[BCOMP]);
+   r = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   g = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   b = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
    UNCLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
    *d = PACK_COLOR_8888(a, b, g, r);
 }
@@ -1139,14 +1107,14 @@ static void
 pack_ubyte_L_SRGB8(const GLubyte src[4], void *dst)
 {
    GLubyte *d = ((GLubyte *) dst);
-   *d = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   *d = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
 }
 
 static void
 pack_float_L_SRGB8(const GLfloat src[4], void *dst)
 {
    GLubyte *d = ((GLubyte *) dst);
-   GLubyte l = linear_float_to_srgb_ubyte(src[RCOMP]);
+   GLubyte l = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
    *d = l;
 }
 
@@ -1157,7 +1125,7 @@ static void
 pack_ubyte_L8A8_SRGB(const GLubyte src[4], void *dst)
 {
    GLushort *d = ((GLushort *) dst);
-   GLubyte l = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
+   GLubyte l = _mesa_linear_ubyte_to_srgb_ubyte(src[RCOMP]);
    *d = PACK_COLOR_88(src[ACOMP], l);
 }
 
@@ -1165,7 +1133,7 @@ static void
 pack_float_L8A8_SRGB(const GLfloat src[4], void *dst)
 {
    GLushort *d = ((GLushort *) dst);
-   GLubyte a, l = linear_float_to_srgb_ubyte(src[RCOMP]);
+   GLubyte a, l = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
    CLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
    *d = PACK_COLOR_88(a, l);
 }
@@ -1742,9 +1710,9 @@ static void
 pack_float_R8G8B8X8_SRGB(const GLfloat src[4], void *dst)
 {
    GLuint *d = (GLuint *) dst;
-   GLubyte r = linear_float_to_srgb_ubyte(src[RCOMP]);
-   GLubyte g = linear_float_to_srgb_ubyte(src[GCOMP]);
-   GLubyte b = linear_float_to_srgb_ubyte(src[BCOMP]);
+   GLubyte r = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   GLubyte g = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   GLubyte b = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
    *d = PACK_COLOR_8888(127, b, g, r);
 }
 
@@ -1892,9 +1860,9 @@ static void
 pack_float_B8G8R8X8_SRGB(const GLfloat src[4], void *dst)
 {
    GLuint *d = (GLuint *) dst;
-   GLubyte r = linear_float_to_srgb_ubyte(src[RCOMP]);
-   GLubyte g = linear_float_to_srgb_ubyte(src[GCOMP]);
-   GLubyte b = linear_float_to_srgb_ubyte(src[BCOMP]);
+   GLubyte r = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[RCOMP]));
+   GLubyte g = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[GCOMP]));
+   GLubyte b = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(src[BCOMP]));
    *d = PACK_COLOR_8888(127, r, g, b);
 }
 
diff --git a/src/mesa/main/format_unpack.c b/src/mesa/main/format_unpack.c
index ad5ea4c..cc1f863 100644
--- a/src/mesa/main/format_unpack.c
+++ b/src/mesa/main/format_unpack.c
@@ -26,6 +26,7 @@
 #include "colormac.h"
 #include "format_unpack.h"
 #include "macros.h"
+#include "format_utils.h"
 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
 
@@ -53,34 +54,6 @@ struct z32f_x24s8
 #define EXPAND_6_8(X)  ( ((X) << 2) | ((X) >> 4) )
 
 
-/**
- * Convert an 8-bit sRGB value from non-linear space to a
- * linear RGB value in [0, 1].
- * Implemented with a 256-entry lookup table.
- */
-GLfloat
-_mesa_nonlinear_to_linear(GLubyte cs8)
-{
-   static GLfloat table[256];
-   static GLboolean tableReady = GL_FALSE;
-   if (!tableReady) {
-      /* compute lookup table now */
-      GLuint i;
-      for (i = 0; i < 256; i++) {
-         const GLfloat cs = UBYTE_TO_FLOAT(i);
-         if (cs <= 0.04045) {
-            table[i] = cs / 12.92f;
-         }
-         else {
-            table[i] = (GLfloat) pow((cs + 0.055) / 1.055, 2.4);
-         }
-      }
-      tableReady = GL_TRUE;
-   }
-   return table[cs8];
-}
-
-
 /**********************************************************************/
 /*  Unpack, returning GLfloat colors                                  */
 /**********************************************************************/
@@ -763,9 +736,9 @@ unpack_BGR_SRGB8(const void *src, GLfloat dst[][4], GLuint n)
    const GLubyte *s = (const GLubyte *) src;
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear(s[i*3+2]);
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear(s[i*3+1]);
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear(s[i*3+0]);
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float(s[i*3+2]);
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float(s[i*3+1]);
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float(s[i*3+0]);
       dst[i][ACOMP] = 1.0F;
    }
 }
@@ -776,9 +749,9 @@ unpack_A8B8G8R8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    const GLuint *s = ((const GLuint *) src);
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 24) );
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 16) & 0xff );
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear( (s[i] >>  8) & 0xff );
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 24) );
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 16) & 0xff );
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >>  8) & 0xff );
       dst[i][ACOMP] = UBYTE_TO_FLOAT( s[i] & 0xff ); /* linear! */
    }
 }
@@ -789,9 +762,9 @@ unpack_B8G8R8A8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    const GLuint *s = ((const GLuint *) src);
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 16) & 0xff );
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear( (s[i] >>  8) & 0xff );
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear( (s[i]      ) & 0xff );
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 16) & 0xff );
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >>  8) & 0xff );
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i]      ) & 0xff );
       dst[i][ACOMP] = UBYTE_TO_FLOAT( s[i] >> 24 ); /* linear! */
    }
 }
@@ -802,9 +775,9 @@ unpack_R8G8B8A8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    const GLuint *s = ((const GLuint *) src);
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear( (s[i]      ) & 0xff );
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear( (s[i] >>  8) & 0xff );
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 16) & 0xff );
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i]      ) & 0xff );
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >>  8) & 0xff );
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 16) & 0xff );
       dst[i][ACOMP] = UBYTE_TO_FLOAT( s[i] >> 24 ); /* linear! */
    }
 }
@@ -817,7 +790,7 @@ unpack_L_SRGB8(const void *src, GLfloat dst[][4], GLuint n)
    for (i = 0; i < n; i++) {
       dst[i][RCOMP] = 
       dst[i][GCOMP] = 
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear(s[i]);
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float(s[i]);
       dst[i][ACOMP] = 1.0F;
    }
 }
@@ -830,7 +803,7 @@ unpack_L8A8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    for (i = 0; i < n; i++) {
       dst[i][RCOMP] =
       dst[i][GCOMP] =
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear(s[i] & 0xff);
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float(s[i] & 0xff);
       dst[i][ACOMP] = UBYTE_TO_FLOAT(s[i] >> 8); /* linear! */
    }
 }
@@ -2124,9 +2097,9 @@ unpack_R8G8B8X8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    const GLuint *s = ((const GLuint *) src);
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear( (s[i]      ) & 0xff );
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear( (s[i] >>  8) & 0xff );
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 16) & 0xff );
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i]      ) & 0xff );
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >>  8) & 0xff );
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 16) & 0xff );
       dst[i][ACOMP] = UBYTE_TO_FLOAT( s[i] >> 24 ); /* linear! */
    }
 }
@@ -2319,9 +2292,9 @@ unpack_B8G8R8X8_SRGB(const void *src, GLfloat dst[][4], GLuint n)
    const GLuint *s = ((const GLuint *) src);
    GLuint i;
    for (i = 0; i < n; i++) {
-      dst[i][RCOMP] = _mesa_nonlinear_to_linear( (s[i] >> 16) & 0xff );
-      dst[i][GCOMP] = _mesa_nonlinear_to_linear( (s[i] >>  8) & 0xff );
-      dst[i][BCOMP] = _mesa_nonlinear_to_linear( (s[i]      ) & 0xff );
+      dst[i][RCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >> 16) & 0xff );
+      dst[i][GCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i] >>  8) & 0xff );
+      dst[i][BCOMP] = _mesa_srgb_ubyte_to_linear_float( (s[i]      ) & 0xff );
       dst[i][ACOMP] = 1.0F;
    }
 }
diff --git a/src/mesa/main/format_unpack.h b/src/mesa/main/format_unpack.h
index 51f97df..eba3c66 100644
--- a/src/mesa/main/format_unpack.h
+++ b/src/mesa/main/format_unpack.h
@@ -25,9 +25,6 @@
 #ifndef FORMAT_UNPACK_H
 #define FORMAT_UNPACK_H
 
-extern GLfloat
-_mesa_nonlinear_to_linear(GLubyte cs8);
-
 extern void
 _mesa_unpack_rgba_row(mesa_format format, GLuint n,
                       const void *src, GLfloat dst[][4]);
diff --git a/src/mesa/main/format_utils.c b/src/mesa/main/format_utils.c
new file mode 100644
index 0000000..241c158
--- /dev/null
+++ b/src/mesa/main/format_utils.c
@@ -0,0 +1,56 @@
+/*
+ * Mesa 3-D graphics library
+ *
+ * Copyright (C) 2014  Intel Corporation  All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "format_utils.h"
+#include "glformats.h"
+
+uint8_t
+_mesa_linear_ubyte_to_srgb_ubyte(uint8_t cl)
+{
+   static uint8_t lut[256];
+   static bool loaded = false;
+   int i;
+
+   if (!loaded) {
+      for (i = 0; i < 256; ++i)
+         lut[i] = FLOAT_TO_UBYTE(_mesa_linear_to_srgb(UBYTE_TO_FLOAT(i)));
+   }
+
+   return lut[cl];
+}
+
+float
+_mesa_srgb_ubyte_to_linear_float(uint8_t cl)
+{
+   static float lut[256];
+   static bool loaded = false;
+   int i;
+
+   if (!loaded) {
+      for (i = 0; i < 256; ++i)
+         lut[i] = _mesa_srgb_to_linear(UBYTE_TO_FLOAT(i));
+   }
+
+   return lut[cl];
+}
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h
new file mode 100644
index 0000000..6af3aa5
--- /dev/null
+++ b/src/mesa/main/format_utils.h
@@ -0,0 +1,68 @@
+/**
+ * \file format_utils.h
+ * A collection of format conversion utility functions.
+ */
+
+/*
+ * Mesa 3-D graphics library
+ *
+ * Copyright (C) 1999-2006  Brian Paul  All Rights Reserved.
+ * Copyright (C) 2014  Intel Corporation  All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef FORMAT_UTILS_H
+#define FORMAT_UTILS_H
+
+#include "macros.h"
+
+/* RGB to sRGB conversion functions */
+
+static inline float
+_mesa_linear_to_srgb(float cl)
+{
+   if (cl < 0.0f)
+      return 0.0f;
+   else if (cl < 0.0031308f)
+      return 12.92f * cl;
+   else if (cl < 1.0f)
+      return 1.055f * powf(cl, 0.41666f) - 0.055f;
+   else
+      return 1.0f;
+}
+
+uint8_t _mesa_linear_ubyte_to_srgb_ubyte(uint8_t cl);
+
+static inline float
+_mesa_srgb_to_linear(float cs)
+{
+   if (cs < 0.0f)
+      return 0.0f;
+   else if (cs < 0.004045)
+      return cs / 12.92f;
+   else if (cs < 1.0f)
+      return pow((cs + 0.055) / 1.055, 2.4);
+   else
+      return 1.0f;
+}
+
+float _mesa_srgb_ubyte_to_linear_float(uint8_t cl);
+
+#endif
diff --git a/src/mesa/main/texcompress_etc.c b/src/mesa/main/texcompress_etc.c
index ae973b0..6ac516f 100644
--- a/src/mesa/main/texcompress_etc.c
+++ b/src/mesa/main/texcompress_etc.c
@@ -42,7 +42,7 @@
 #include "texcompress_etc.h"
 #include "texstore.h"
 #include "macros.h"
-#include "format_unpack.h"
+#include "format_utils.h"
 
 
 struct etc2_block {
@@ -1307,9 +1307,9 @@ fetch_etc2_srgb8(const GLubyte *map,
    etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
                          false /* punchthrough_alpha */);
 
-   texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
-   texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
-   texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
+   texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[0]);
+   texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[1]);
+   texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[2]);
    texel[ACOMP] = 1.0f;
 }
 
@@ -1345,9 +1345,9 @@ fetch_etc2_srgb8_alpha8_eac(const GLubyte *map,
    etc2_rgba8_parse_block(&block, src);
    etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);
 
-   texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
-   texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
-   texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
+   texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[0]);
+   texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[1]);
+   texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[2]);
    texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
 }
 
@@ -1473,9 +1473,9 @@ fetch_etc2_srgb8_punchthrough_alpha1(const GLubyte *map,
                          true /* punchthrough alpha */);
    etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst,
                          true /* punchthrough alpha */);
-   texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]);
-   texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]);
-   texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]);
+   texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[0]);
+   texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[1]);
+   texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(dst[2]);
    texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
 }
 
diff --git a/src/mesa/main/texcompress_s3tc.c b/src/mesa/main/texcompress_s3tc.c
index 894c46d..97c8b8c 100644
--- a/src/mesa/main/texcompress_s3tc.c
+++ b/src/mesa/main/texcompress_s3tc.c
@@ -43,7 +43,7 @@
 #include "texcompress.h"
 #include "texcompress_s3tc.h"
 #include "texstore.h"
-#include "format_unpack.h"
+#include "format_utils.h"
 
 
 #if defined(_WIN32) || defined(WIN32)
@@ -419,9 +419,9 @@ fetch_srgb_dxt1(const GLubyte *map,
    if (fetch_ext_rgb_dxt1) {
       GLubyte tex[4];
       fetch_ext_rgb_dxt1(rowStride, map, i, j, tex);
-      texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
-      texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
-      texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
+      texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[RCOMP]);
+      texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[GCOMP]);
+      texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[BCOMP]);
       texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
    }
    else {
@@ -436,9 +436,9 @@ fetch_srgba_dxt1(const GLubyte *map,
    if (fetch_ext_rgba_dxt1) {
       GLubyte tex[4];
       fetch_ext_rgba_dxt1(rowStride, map, i, j, tex);
-      texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
-      texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
-      texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
+      texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[RCOMP]);
+      texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[GCOMP]);
+      texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[BCOMP]);
       texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
    }
    else {
@@ -453,9 +453,9 @@ fetch_srgba_dxt3(const GLubyte *map,
    if (fetch_ext_rgba_dxt3) {
       GLubyte tex[4];
       fetch_ext_rgba_dxt3(rowStride, map, i, j, tex);
-      texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
-      texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
-      texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
+      texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[RCOMP]);
+      texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[GCOMP]);
+      texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[BCOMP]);
       texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
    }
    else {
@@ -470,9 +470,9 @@ fetch_srgba_dxt5(const GLubyte *map,
    if (fetch_ext_rgba_dxt5) {
       GLubyte tex[4];
       fetch_ext_rgba_dxt5(rowStride, map, i, j, tex);
-      texel[RCOMP] = _mesa_nonlinear_to_linear(tex[RCOMP]);
-      texel[GCOMP] = _mesa_nonlinear_to_linear(tex[GCOMP]);
-      texel[BCOMP] = _mesa_nonlinear_to_linear(tex[BCOMP]);
+      texel[RCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[RCOMP]);
+      texel[GCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[GCOMP]);
+      texel[BCOMP] = _mesa_srgb_ubyte_to_linear_float(tex[BCOMP]);
       texel[ACOMP] = UBYTE_TO_FLOAT(tex[ACOMP]);
    }
    else {
-- 
2.0.1



More information about the mesa-dev mailing list