png: Correct endianness for big-endian machines
authorSimon McVittie <smcv@debian.org>
Sat, 8 Jan 2022 16:39:36 +0000 (16:39 +0000)
committerJeremy Bicha <jeremy.bicha@canonical.com>
Wed, 9 Feb 2022 18:25:53 +0000 (18:25 +0000)
libpng wants to receive samples in either RGB or RGBA order, whether
each sample is big-endian or not. This resolves test failures in
testsuite/gdk/memorytexture.c (and a lot of reftests) on s390x, and
probably the PowerPC family too.

Modifying the test to show the color in use and write out the PNG bytes
to a file, and running the memorytexture test on s390x, produces a PNG
that loads with the correct color values in GIMP (on an x86_64 machine),
which seems like evidence that this is the correct change and not just
compensating errors.

Bug: https://gitlab.gnome.org/GNOME/gtk/-/issues/4616
Signed-off-by: Simon McVittie <smcv@debian.org>
Forwarded: https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/4357

Gbp-Pq: Name png-Correct-endianness-for-big-endian-machines.patch

gdk/loaders/gdkpng.c

index b998deb144a4c8547f8edb5a96c1b16581786a47..20711210d0b9a9f1c880817bda8b86481df6fa42 100644 (file)
@@ -222,11 +222,7 @@ gdk_load_png (GBytes  *bytes,
     case PNG_COLOR_TYPE_RGB_ALPHA:
       if (depth == 8)
         {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
           format = GDK_MEMORY_R8G8B8A8;
-#elif G_BYTE_ORDER == G_BIG_ENDIAN
-          format = GDK_MEMORY_A8B8G8R8;
-#endif
         }
       else
         {
@@ -236,11 +232,7 @@ gdk_load_png (GBytes  *bytes,
     case PNG_COLOR_TYPE_RGB:
       if (depth == 8)
         {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
           format = GDK_MEMORY_R8G8B8;
-#elif G_BYTE_ORDER == G_BIG_ENDIAN
-          format = GDK_MEMORY_B8G8R8;
-#endif
         }
       else if (depth == 16)
         {
@@ -325,22 +317,14 @@ gdk_save_png (GdkTexture *texture)
     case GDK_MEMORY_A8R8G8B8:
     case GDK_MEMORY_R8G8B8A8:
     case GDK_MEMORY_A8B8G8R8:
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
       format = GDK_MEMORY_R8G8B8A8;
-#elif G_BYTE_ORDER == G_BIG_ENDIAN
-      format = GDK_MEMORY_A8B8G8R8;
-#endif
       png_format = PNG_COLOR_TYPE_RGB_ALPHA;
       depth = 8;
       break;
 
     case GDK_MEMORY_R8G8B8:
     case GDK_MEMORY_B8G8R8:
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
       format = GDK_MEMORY_R8G8B8;
-#elif G_BYTE_ORDER == G_BIG_ENDIAN
-      format = GDK_MEMORY_B8G8R8;
-#endif
       png_format = PNG_COLOR_TYPE_RGB;
       depth = 8;
       break;