gdk: Make A8 and A16 formats premultiplied
authorBenjamin Otte <otte@redhat.com>
Fri, 21 Jul 2023 23:29:24 +0000 (01:29 +0200)
committerBenjamin Otte <otte@redhat.com>
Fri, 21 Jul 2023 23:33:44 +0000 (01:33 +0200)
The relevant question here is about details, because we have to choose
if we declare alpha-only formats as having their (nonexistant) color
channels premultiplied or not, so that the code paths using them can do
the right thing.

Because we are premultiplied by default, it makes sense to treat alpha
like that, because then the alpha-only code doesn't need to do
workarounds for straight alpha.

Where this is relevant of course is when expanding the alpha channel
into color channels, where we want to end up with white.
So make sure we do color = alpha there instead of color = 1 like we did
before.

gdk/gdkmemoryformat.c
testsuite/gdk/memorytexture.c

index 40fea6987e9d196ce1a2743ced9b3ead07f85af6..95663e9ba74466f582633a838bc8dce7887297b4 100644 (file)
@@ -69,9 +69,9 @@ name ## _to_float (float        *dest, \
   for (gsize i = 0; i < n; i++) \
     { \
       T *src = (T *) (src_data + i * bpp); \
-      if (G >= 0) dest[0] = (float) src[G] / scale; else dest[0] = 1.0; \
-      dest[1] = dest[2] = dest[0]; \
       if (A >= 0) dest[3] = (float) src[A] / scale; else dest[3] = 1.0; \
+      if (G >= 0) dest[0] = (float) src[G] / scale; else dest[0] = dest[3]; \
+      dest[1] = dest[2] = dest[0]; \
       dest += 4; \
     } \
 } \
@@ -588,22 +588,22 @@ static const GdkMemoryFormatDescription memory_formats[] = {
     g16_from_float,
   },
   [GDK_MEMORY_A8] = {
-    GDK_MEMORY_ALPHA_STRAIGHT,
+    GDK_MEMORY_ALPHA_PREMULTIPLIED,
     1,
     G_ALIGNOF (guchar),
     GDK_MEMORY_U8,
     { 0, 0, 3, 0 },
-    { GL_R8, GL_RED, GL_UNSIGNED_BYTE, { GL_ONE, GL_ONE, GL_ONE, GL_RED } },
+    { GL_R8, GL_RED, GL_UNSIGNED_BYTE, { GL_RED, GL_RED, GL_RED, GL_RED } },
     a8_to_float,
     a8_from_float,
   },
   [GDK_MEMORY_A16] = {
-    GDK_MEMORY_ALPHA_STRAIGHT,
+    GDK_MEMORY_ALPHA_PREMULTIPLIED,
     2,
     G_ALIGNOF (guint16),
     GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
-    { GL_R16, GL_RED, GL_UNSIGNED_SHORT, { GL_ONE, GL_ONE, GL_ONE, GL_RED } },
+    { GL_R16, GL_RED, GL_UNSIGNED_SHORT, { GL_RED, GL_RED, GL_RED, GL_RED } },
     a16_to_float,
     a16_from_float,
   },
index c963be8c5b6cd3d2ea3092cd7d55182dda31d72c..c42bda702452ce99ad774388eeca52f462a3375e 100644 (file)
@@ -287,6 +287,8 @@ gdk_memory_format_is_premultiplied (GdkMemoryFormat format)
     case GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED:
     case GDK_MEMORY_G8A8_PREMULTIPLIED:
     case GDK_MEMORY_G16A16_PREMULTIPLIED:
+    case GDK_MEMORY_A8:
+    case GDK_MEMORY_A16:
     case GDK_MEMORY_A16_FLOAT:
     case GDK_MEMORY_A32_FLOAT:
       return TRUE;
@@ -307,8 +309,6 @@ gdk_memory_format_is_premultiplied (GdkMemoryFormat format)
     case GDK_MEMORY_G8A8:
     case GDK_MEMORY_G16:
     case GDK_MEMORY_G16A16:
-    case GDK_MEMORY_A8:
-    case GDK_MEMORY_A16:
       return FALSE;
 
     case GDK_MEMORY_N_FORMATS: