Make libpng treat benign errors as warnings (#2958)
For example, by default libpng will refuse to write a file with
an ICC profile whose PCS illuminant is different from some exact hardcoded string.
diff --git a/apps/shared/avifpng.c b/apps/shared/avifpng.c
index 16abaa1..579f464 100644
--- a/apps/shared/avifpng.c
+++ b/apps/shared/avifpng.c
@@ -677,6 +677,9 @@
fprintf(stderr, "Cannot init libpng (png): %s\n", outputFilename);
goto cleanup;
}
+
+ png_set_benign_errors(png, 1); // Treat benign errors as warnings.
+
info = png_create_info_struct(png);
if (!info) {
fprintf(stderr, "Cannot init libpng (info): %s\n", outputFilename);
diff --git a/tests/data/README.md b/tests/data/README.md
index 48c62ce..bf8e59f 100644
--- a/tests/data/README.md
+++ b/tests/data/README.md
@@ -233,6 +233,15 @@
| 5087 | 0xffe2 APP2 | 612 | `ICC_PROFILE......T........mntrRG chunk 1/1` |
| | | | ... |
+### File [paris_exif_xmp_modified_icc.jpg](paris_exif_xmp_modified_icc.jpg)
+
+
+
+License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE)
+
+Source: Same as paris_exif_xmp_icc.jpg but edited to have libpng report 'PCS illuminant is not D50'
+(the byte at position 0x1440 was changed from 0x2D to 0x2C).
+
### File [paris_extended_xmp.jpg](paris_extended_xmp.jpg)

diff --git a/tests/data/paris_exif_xmp_modified_icc.jpg b/tests/data/paris_exif_xmp_modified_icc.jpg
new file mode 100644
index 0000000..6fac659
--- /dev/null
+++ b/tests/data/paris_exif_xmp_modified_icc.jpg
Binary files differ
diff --git a/tests/test_cmd_metadata.sh b/tests/test_cmd_metadata.sh
index aba1bad..b532610 100755
--- a/tests/test_cmd_metadata.sh
+++ b/tests/test_cmd_metadata.sh
@@ -21,6 +21,7 @@
# Input file paths.
INPUT_PNG="${TESTDATA_DIR}/paris_icc_exif_xmp.png"
INPUT_JPG="${TESTDATA_DIR}/paris_exif_xmp_icc.jpg"
+INPUT_JPG_WEIRD_ICC="${TESTDATA_DIR}/paris_exif_xmp_modified_icc.jpg"
INPUT_ICC="${TESTDATA_DIR}/sRGB2014.icc"
# Output file names.
ENCODED_FILE="avif_test_cmd_metadata_encoded.avif"
@@ -42,7 +43,7 @@
pushd ${TMP_DIR}
# Metadata test.
echo "Testing metadata enc"
- for INPUT in "${INPUT_PNG}" "${INPUT_JPG}"; do
+ for INPUT in "${INPUT_PNG}" "${INPUT_JPG}" "${INPUT_JPG_WEIRD_ICC}"; do
"${AVIFENC}" "${INPUT}" -o "${ENCODED_FILE}"
# Ignoring a metadata chunk should produce a different output file.