From 2fb156bdc0cc28227df80e9d59a58547283b50e1 Mon Sep 17 00:00:00 2001 From: Manuel Stoeckl Date: Fri, 6 Aug 2021 18:35:01 -0400 Subject: [PATCH] Read/write 16-bpc PNG data in native endian The png_set_swap function does not affect 8-bpc data. Signed-off-by: Manuel Stoeckl --- src/cairo-png.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/cairo-png.c b/src/cairo-png.c index f576047b1..4b7c34081 100644 --- a/src/cairo-png.c +++ b/src/cairo-png.c @@ -407,6 +407,14 @@ write_png (cairo_surface_t *surface, */ png_write_info (png, info); +#ifndef WORDS_BIGENDIAN + /* libpng treats 16-bit data as big-endian by default. Swapping the + * byte-order on little endian ensures the native-endian data can be + * provided to png_write_image. This does not affect 8-bit data. + */ + png_set_swap (png); +#endif + if (png_color_type == PNG_COLOR_TYPE_RGB_ALPHA) { if (clone->format != CAIRO_FORMAT_RGBA128F) png_set_write_user_transform_fn (png, unpremultiply_data); @@ -702,6 +710,14 @@ read_png (struct png_read_closure_t *png_closure) png_read_info (png, info); +#ifndef WORDS_BIGENDIAN + /* libpng treats 16-bit data as big-endian by default. Swapping the + * byte-order on little endian ensures the native-endian data can be + * provided to png_read_image. This does not affect 8-bit data. + */ + png_set_swap (png); +#endif + png_get_IHDR (png, info, &png_width, &png_height, &depth, &color_type, &interlace, NULL, NULL);