[gtk/gamma-shenanigans: 7/13] fix up premultiplication




commit f800cb380af4f839ed85ca4868c9eddc9b6551dd
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Sep 9 06:23:43 2021 -0400

    fix up premultiplication

 gdk/gdkmemorytexture.c | 47 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 6 deletions(-)
---
diff --git a/gdk/gdkmemorytexture.c b/gdk/gdkmemorytexture.c
index 19dc7c7a11..2e4a3cf472 100644
--- a/gdk/gdkmemorytexture.c
+++ b/gdk/gdkmemorytexture.c
@@ -402,6 +402,43 @@ SWIZZLE_FP16(3,2,1,0)
 SWIZZLE_FP16(0,1,2,3)
 SWIZZLE_FP16(3,0,1,2)
 
+#define SWIZZLE_PREMULTIPLY_FP16(A,R,G,B) \
+static void \
+convert_fp16_swizzle_premultiply_ ## A ## R ## G ## B (guchar       *dest_data, \
+                                                       gsize         dest_stride, \
+                                                       const guchar *src_data, \
+                                                       gsize         src_stride, \
+                                                       gsize         width, \
+                                                       gsize         height) \
+{ \
+  gsize x, y; \
+\
+  for (y = 0; y < height; y++) \
+    { \
+      guchar *dest = dest_data; \
+      guint16 *src = (guint16 *)src_data; \
+\
+      for (x = 0; x < width; x++) \
+        { \
+          float c[4]; \
+\
+          half_to_float4 (&src[x * 4], c); \
+\
+          dest[A] = (guchar)(255 * c[3]); \
+          dest[R] = (guchar)(255 * c[0] * c[3]); \
+          dest[G] = (guchar)(255 * c[1] * c[3]); \
+          dest[B] = (guchar)(255 * c[2] * c[3]); \
+        } \
+\
+      dest_data += dest_stride; \
+      src_data += src_stride; \
+    } \
+}
+
+SWIZZLE_PREMULTIPLY_FP16(3,2,1,0)
+SWIZZLE_PREMULTIPLY_FP16(0,1,2,3)
+SWIZZLE_PREMULTIPLY_FP16(3,0,1,2)
+
 static void
 convert_fp16_premultiply (guchar       *dest_data,
                           gsize         dest_stride,
@@ -423,12 +460,9 @@ convert_fp16_premultiply (guchar       *dest_data,
 
           half_to_float4 (&src[x * 4], c);
 
-          if (c[3] != 1.f)
-            {
-              c[1] *= c[3];
-              c[2] *= c[3];
-              c[3] *= c[3];
-            }
+          c[0] *= c[3];
+          c[1] *= c[3];
+          c[2] *= c[3];
 
           float_to_half4 (c, &dest[x + 4]);
         }
@@ -458,6 +492,7 @@ static ConversionFunc converters[GDK_MEMORY_N_FORMATS][3] =
   { convert_swizzle_opaque_3012, convert_swizzle_opaque_0321, convert_swizzle_opaque_3210 },
   { convert_16to8_swizzle_2103, convert_16to8_swizzle_1230, convert_16to8_swizzle_0123 },
   { convert_fp16_swizzle_opaque_3210, convert_fp16_swizzle_opaque_0123, convert_fp16_swizzle_opaque_3012 },
+  { convert_fp16_swizzle_premultiply_3210, convert_fp16_swizzle_premultiply_0123, 
convert_fp16_swizzle_premultiply_3012 },
   { convert_fp16_swizzle_3210, convert_fp16_swizzle_0123, convert_fp16_swizzle_3012 }
 };
 


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