[babl] extensions/cairo: replace hard-coded LUT with runtime LUT



commit 529df8ecc41462032da405a4f1bc63f5773e9d2b
Author: Øyvind Kolås <pippin gimp org>
Date:   Wed Nov 16 19:27:28 2016 +0100

    extensions/cairo: replace hard-coded LUT with runtime LUT
    
    The LUT needed recomputation anyways, doing it at runtime permits easier
    tweaking. This results in better precision for the fast path than the
    existing table.

 extensions/Makefile.am    |    2 +-
 extensions/cairo-tables.h | 5027 ---------------------------------------------
 extensions/cairo.c        |   37 +-
 3 files changed, 27 insertions(+), 5039 deletions(-)
---
diff --git a/extensions/Makefile.am b/extensions/Makefile.am
index 7ecb457..0aeb9b7 100644
--- a/extensions/Makefile.am
+++ b/extensions/Makefile.am
@@ -39,7 +39,7 @@ ext_LTLIBRARIES = \
        two-table.la    \
        ycbcr.la
 
-cairo_la_SOURCES = cairo.c cairo-tables.h
+cairo_la_SOURCES = cairo.c
 CIE_la_SOURCES = CIE.c
 simple_la_SOURCES = simple.c
 float_half_la_SOURCES = float-half.c
diff --git a/extensions/cairo.c b/extensions/cairo.c
index 0c4ca78..331ec79 100644
--- a/extensions/cairo.c
+++ b/extensions/cairo.c
@@ -17,10 +17,10 @@
  */
 
 #include <stdlib.h>
+#include <stdio.h>
 #include "babl.h"
 
 #include "base/util.h"
-#include "extensions/cairo-tables.h"
 
 int init (void);
 
@@ -90,20 +90,33 @@ conv_rgbA8_cairo32_le (unsigned char *src, unsigned char *dst, long samples)
   return samples;
 }
 
+static unsigned short linear_to_gamma[65536];
+
+static void init_table(void)
+{
+  unsigned int index;
+  static int done = 0;
+  if (done) return;
+  done = 1;
+
+  for (index = 0; index < 65536 * 1; index++)
+  {
+    float value = index / (65535 * 1.0);
+    linear_to_gamma[index] = babl_linear_to_gamma_2_2 (value) * 65536 * 1;
+  }
+}
+
 static inline unsigned char
 conv_rgbafloat_cairo32_map (float value,
                             float alpha)
 {
-  unsigned short index;
-  float result;
-  if (value < 0.0)
-    return 0;
-  else if (value > 1.0)
+  unsigned int index;
+  if (value <= 0.0)
+    return 0x00;
+  if (value >= 1.0)
     return 0xFF;
-  index = (unsigned short)(value * 0xFFFF);
-  result = linear_to_gamma16[index] / 257.0; /* 65535.0 / 255.0 */
-
-  return (result * alpha) + 0.5f;
+  index = (unsigned int)(value * 65535);
+  return linear_to_gamma[index] * alpha / 257 + 0.5f;
 }
 
 static long
@@ -217,6 +230,8 @@ init (void)
     babl_type ("u8"),
     babl_component ("A"),
     NULL
-  );
+    );
+  
+  init_table();
   return 0;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]