gdk: Support gray/alpha in PNG loader
authorSophie Herold <sophie@hemio.de>
Fri, 26 May 2023 13:35:57 +0000 (15:35 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 30 May 2023 18:41:01 +0000 (14:41 -0400)
gdk/loaders/gdkpng.c

index d5991fde380670334a53ae263c689494d77599f0..2f6d55d1f9c10f182a059746e225eb3b98499758 100644 (file)
@@ -183,8 +183,7 @@ gdk_load_png (GBytes  *bytes,
   if (color_type == PNG_COLOR_TYPE_PALETTE)
     png_set_palette_to_rgb (png);
 
-  if (color_type == PNG_COLOR_TYPE_GRAY ||
-      color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+  if (color_type == PNG_COLOR_TYPE_GRAY)
     png_set_expand_gray_1_2_4_to_8 (png);
 
   if (png_get_valid (png, info, PNG_INFO_tRNS))
@@ -193,10 +192,6 @@ gdk_load_png (GBytes  *bytes,
   if (depth < 8)
     png_set_packing (png);
 
-  if (color_type == PNG_COLOR_TYPE_GRAY ||
-      color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-    png_set_gray_to_rgb (png);
-
   if (interlace != PNG_INTERLACE_NONE)
     png_set_interlace_handling (png);
 
@@ -239,6 +234,26 @@ gdk_load_png (GBytes  *bytes,
           format = GDK_MEMORY_R16G16B16;
         }
       break;
+    case PNG_COLOR_TYPE_GRAY:
+      if (depth == 8)
+        {
+          format = GDK_MEMORY_G8;
+        }
+      else if (depth == 16)
+        {
+          format = GDK_MEMORY_G16;
+        }
+      break;
+    case PNG_COLOR_TYPE_GRAY_ALPHA:
+      if (depth == 8)
+        {
+          format = GDK_MEMORY_G8A8;
+        }
+      else if (depth == 16)
+        {
+          format = GDK_MEMORY_G16A16;
+        }
+      break;
     default:
       png_destroy_read_struct (&png, &info, NULL);
       g_set_error (error,
@@ -349,6 +364,34 @@ gdk_save_png (GdkTexture *texture)
       depth = 16;
       break;
 
+    case GDK_MEMORY_G8:
+      format = GDK_MEMORY_G8;
+      png_format = PNG_COLOR_TYPE_GRAY;
+      depth = 8;
+      break;
+
+    case GDK_MEMORY_G8A8_PREMULTIPLIED:
+    case GDK_MEMORY_G8A8:
+    case GDK_MEMORY_A8:
+      format = GDK_MEMORY_G8A8;
+      png_format = PNG_COLOR_TYPE_GRAY_ALPHA;
+      depth = 8;
+      break;
+
+    case GDK_MEMORY_G16:
+      format = GDK_MEMORY_G16;
+      png_format = PNG_COLOR_TYPE_GRAY;
+      depth = 16;
+      break;
+
+    case GDK_MEMORY_G16A16_PREMULTIPLIED:
+    case GDK_MEMORY_G16A16:
+    case GDK_MEMORY_A16:
+      format = GDK_MEMORY_G16A16;
+      png_format = PNG_COLOR_TYPE_GRAY_ALPHA;
+      depth = 16;
+      break;
+
     case GDK_MEMORY_N_FORMATS:
     default:
       g_assert_not_reached ();