Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that the gain map metadata is valid on decoding. #2453

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,11 @@ static avifResult avifParseToneMappedImageBox(avifGainMap * gainMap, const uint8
if (writerVersion <= supportedMetadataVersion) {
AVIF_CHECKERR(avifROStreamRemainingBytes(&s) == 0, AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE);
}

if (avifGainMapValidateMetadata(gainMap, diag) != AVIF_RESULT_OK) {
return AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE;
}

return AVIF_RESULT_OK;
}
#endif // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP
Expand Down
12 changes: 12 additions & 0 deletions tests/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,18 @@ Source : same as seine_sdr_gainmap_srgb.avif before commit 10b7232
An image with a `tmap` item (i.e. a gain map) but no 'tmap' brand in the `ftyp` box.
The gain map should be ignored by the decoder since the `tmap` brand is missing.

### File [seine_sdr_gainmap_ngammazero.avif](seine_sdr_gainmap_notmapbrand.avif)
maryla-uc marked this conversation as resolved.
Show resolved Hide resolved

![](seine_sdr_gainmap_notmapbrand.avif)
maryla-uc marked this conversation as resolved.
Show resolved Hide resolved

License: [same as libavif](https://github.com/AOMediaCodec/libavif/blob/main/LICENSE)

Source : same as seine_sdr_gainmap_srgb.avif generated with a modified avifenc that
writes 0 instead of the gain map gamma numerator.

An image with a gain map where the gain map gamma value is zero (invalid).


### File [seine_sdr_gainmap_big_srgb.avif](seine_sdr_gainmap_big_srgb.avif)

![](seine_sdr_gainmap_big_srgb.avif)
Expand Down
Binary file added tests/data/seine_sdr_gainmap_gammazero.avif
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/gtest/avifgainmaptest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,21 @@ TEST(GainMapTest, DecodeInvalidFtyp) {
ASSERT_EQ(decoded->gainMap, nullptr);
}

TEST(GainMapTest, DecodeInvalidGamma) {
const std::string path =
std::string(data_path) + "seine_sdr_gainmap_gammazero.avif";
ImagePtr decoded(avifImageCreateEmpty());
ASSERT_NE(decoded, nullptr);
DecoderPtr decoder(avifDecoderCreate());
ASSERT_NE(decoder, nullptr);
decoder->enableDecodingGainMap = true;
decoder->enableParsingGainMapMetadata = true;
// Fails to decode: the gain map is ignored because the gain map gamma is zero
// (invalid).
ASSERT_EQ(avifDecoderReadFile(decoder.get(), decoded.get(), path.c_str()),
AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE);
}

#define EXPECT_FRACTION_NEAR(numerator, denominator, expected) \
EXPECT_NEAR(std::abs((double)numerator / denominator), expected, \
expected * 0.001);
Expand Down
Loading