Commit 7e96b50d for libheif
commit 7e96b50d1f7c146e3bb8c0ca5ec3e31d2f362a10
Author: Dirk Farin <dirk.farin@gmail.com>
Date: Mon Oct 26 08:54:59 2026 +0100
PNG loader: fix decoding of 16 bit gray+alpha images
16 bit grayscale PNGs with alpha (two channels) did not match the
grayscale branch and fell through to the RGB branch, which read four
samples per pixel from rows holding only two. This produced a wrong
image and a heap buffer over-read past the end of the row buffer.
Route them to the 16 bit grayscale branch, whose alpha path was
unreachable before, and use the alpha plane stride when writing alpha.
diff --git a/heifio/decoder_png.cc b/heifio/decoder_png.cc
index 7985cc67..7285c178 100644
--- a/heifio/decoder_png.cc
+++ b/heifio/decoder_png.cc
@@ -382,7 +382,7 @@ heif_error loadPNG(const char* filename, int output_bit_depth, InputImage *input
}
}
}
- else if (band == 1) {
+ else if (band == 1 || (band == 2 && bit_depth > 8)) {
assert(bit_depth > 8);
err = heif_image_create((int) width, (int) height,
@@ -418,7 +418,7 @@ heif_error loadPNG(const char* filename, int output_bit_depth, InputImage *input
uint16_t va = (uint16_t) (((p[2] << 8) | p[3]) >> bdShift);
py[x + y * y_stride] = vp;
- pa[x + y * y_stride] = va;
+ pa[x + y * a_stride] = va;
p += 4;
}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 10f0e2dc..6a785e5d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -233,6 +233,12 @@ else()
message(INFO " Disabling the TIFF decoder test because libtiff was not found")
endif ()
+if (PNG_FOUND)
+ add_heifio_test(pngdecode)
+else()
+ message(INFO " Disabling the PNG decoder test because libpng was not found")
+endif ()
+
if (ENABLE_PLUGIN_LOADING)
get_directory_property(ALL_TESTS TESTS)
set_tests_properties(${ALL_TESTS} PROPERTIES ENVIRONMENT "LIBHEIF_PLUGIN_PATH=${CMAKE_BINARY_DIR}/libheif/plugins")
diff --git a/tests/data/heifio/gray_alpha16.png b/tests/data/heifio/gray_alpha16.png
new file mode 100644
index 00000000..a120663e
Binary files /dev/null and b/tests/data/heifio/gray_alpha16.png differ
diff --git a/tests/pngdecode.cc b/tests/pngdecode.cc
new file mode 100644
index 00000000..269b35cb
--- /dev/null
+++ b/tests/pngdecode.cc
@@ -0,0 +1,72 @@
+/*
+ libheifio PNG decode unit tests
+
+ MIT License
+
+ Copyright (c) 2026 Dirk Farin <dirk.farin@gmail.com>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+// All test images are 8x4 pixels.
+
+#include "catch_amalgamated.hpp"
+#include "heifio/decoder.h"
+#include "heifio/decoder_png.h"
+#include "test_utils.h"
+#include "libheif/heif.h"
+
+#include <cstdint>
+#include <string>
+
+static constexpr int width = 8;
+static constexpr int height = 4;
+
+static InputImage load(const char* filename, int output_bit_depth)
+{
+ InputImage input_image;
+ std::string path = get_path_for_heifio_test_file(filename);
+ heif_error err = loadPNG(path.c_str(), output_bit_depth, &input_image);
+ REQUIRE(err.code == heif_error_Ok);
+ REQUIRE(input_image.image != nullptr);
+ REQUIRE(heif_image_get_primary_width(input_image.image.get()) == width);
+ REQUIRE(heif_image_get_primary_height(input_image.image.get()) == height);
+ return input_image;
+}
+
+
+// 16 bit gray+alpha used to be decoded as if it had four channels.
+// Pixel (x,y) has gray value 0x1000*x+y and alpha 0x2000*x+0x100*y.
+TEST_CASE("Grayscale+alpha 16 bit") {
+ InputImage input_image = load("gray_alpha16.png", 16);
+ const heif_image* image = input_image.image.get();
+ REQUIRE(heif_image_get_colorspace(image) == heif_colorspace_monochrome);
+ REQUIRE(heif_image_get_chroma_format(image) == heif_chroma_monochrome);
+ REQUIRE(heif_image_has_channel(image, heif_channel_Alpha));
+
+ size_t y_stride, a_stride;
+ const uint8_t* py = heif_image_get_plane_readonly2(image, heif_channel_Y, &y_stride);
+ const uint8_t* pa = heif_image_get_plane_readonly2(image, heif_channel_Alpha, &a_stride);
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++) {
+ INFO("x=" << x << " y=" << y);
+ REQUIRE(*(const uint16_t*) (py + y * y_stride + x * 2) == 0x1000 * x + y);
+ REQUIRE(*(const uint16_t*) (pa + y * a_stride + x * 2) == 0x2000 * x + 0x100 * y);
+ }
+}