@@ -451,6 +451,74 @@ TEST(GainMapTest, EncodeDecodeGrid) {
451451 // .write(reinterpret_cast<char*>(encoded.data), encoded.size);
452452}
453453
454+ void MakeTestImage (const std::string& path, uint32_t grid_cols,
455+ uint32_t grid_rows, uint32_t cell_width,
456+ uint32_t cell_height, uint32_t gain_map_grid_cols,
457+ uint32_t gain_map_grid_rows, uint32_t gain_map_cell_width,
458+ uint32_t gain_map_cell_height, uint8_t tmap_version = 0 ,
459+ uint16_t minimum_version = 0 , uint16_t writer_version = 0 ,
460+ bool add_extra_bytes = false ) {
461+ std::vector<ImagePtr> cells;
462+ std::vector<const avifImage*> cell_ptrs;
463+ std::vector<ImagePtr> gain_map_cells;
464+ std::vector<const avifImage*> gain_map_ptrs;
465+
466+ avifGainMapMetadata gain_map_metadata =
467+ GetTestGainMapMetadata (/* base_rendition_is_hdr=*/ true );
468+ for (uint32_t i = 0 ; i < grid_cols * grid_rows; ++i) {
469+ ImagePtr image =
470+ testutil::CreateImage (cell_width, cell_height, /* depth=*/ 10 ,
471+ AVIF_PIXEL_FORMAT_YUV444, AVIF_PLANES_ALL);
472+ ASSERT_NE (image, nullptr );
473+ image->gainMap = avifGainMapCreate ();
474+ image->gainMap ->metadata = gain_map_metadata;
475+ image->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_PQ;
476+ image->gainMap ->altDepth = 8 ;
477+ image->gainMap ->altPlaneCount = 3 ;
478+ image->gainMap ->altColorPrimaries = AVIF_COLOR_PRIMARIES_SRGB;
479+ image->gainMap ->altTransferCharacteristics =
480+ AVIF_TRANSFER_CHARACTERISTICS_SRGB;
481+ image->gainMap ->altMatrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT601;
482+ testutil::FillImageGradient (image.get ());
483+
484+ cell_ptrs.push_back (image.get ());
485+ cells.push_back (std::move (image));
486+ }
487+ for (uint32_t i = 0 ; i < gain_map_grid_cols * gain_map_grid_rows; ++i) {
488+ ImagePtr gain_map = testutil::CreateImage (
489+ gain_map_cell_width, gain_map_cell_height, /* depth=*/ 8 ,
490+ AVIF_PIXEL_FORMAT_YUV420, AVIF_PLANES_YUV);
491+ ASSERT_NE (gain_map, nullptr );
492+ gain_map->gainMap = avifGainMapCreate ();
493+ gain_map->gainMap ->metadata = gain_map_metadata;
494+ testutil::FillImageGradient (gain_map.get ());
495+ gain_map_ptrs.push_back (gain_map.get ());
496+ gain_map_cells.push_back (std::move (gain_map));
497+ }
498+
499+ EncoderPtr encoder (avifEncoderCreate ());
500+ ASSERT_NE (encoder, nullptr );
501+ encoder->speed = 10 ;
502+ avifEncoderInternalOptions internalOptions;
503+ internalOptions.tmapVersion = tmap_version;
504+ internalOptions.tmapMinimumVersion = minimum_version;
505+ internalOptions.tmapWriterVersion = writer_version;
506+ internalOptions.tmapAddExtraBytes = add_extra_bytes;
507+ avifEncoderSetInternalOptions (encoder.get (), &internalOptions);
508+ testutil::AvifRwData encoded;
509+ avifResult result = avifEncoderAddImageGridInternal (
510+ encoder.get (), grid_cols, grid_rows, cell_ptrs.data (), gain_map_grid_cols,
511+ gain_map_grid_rows, gain_map_ptrs.data (), AVIF_ADD_IMAGE_FLAG_SINGLE);
512+ ASSERT_EQ (result, AVIF_RESULT_OK)
513+ << avifResultToString (result) << " " << encoder->diag .error ;
514+ result = avifEncoderFinish (encoder.get (), &encoded);
515+ ASSERT_EQ (result, AVIF_RESULT_OK)
516+ << avifResultToString (result) << " " << encoder->diag .error ;
517+
518+ std::ofstream (path, std::ios::binary)
519+ .write (reinterpret_cast <char *>(encoded.data ), encoded.size );
520+ }
521+
454522TEST (GainMapTest, InvalidGrid) {
455523 std::vector<ImagePtr> cells;
456524 std::vector<const avifImage*> cell_ptrs;
@@ -1162,6 +1230,66 @@ TEST(GainMapTest, CreateTestImages) {
11621230 encoded_small_gainmap.size );
11631231 }
11641232 }
1233+
1234+ if (kUpdateTestImages ) {
1235+ MakeTestImage (
1236+ std::string (data_path) + " color_grid_gainmap_different_grid.avif" ,
1237+ /* grid_cols=*/ 4 ,
1238+ /* grid_rows=*/ 3 ,
1239+ /* cell_width=*/ 128 , /* cell_heigh=*/ 200 ,
1240+ /* gain_map_grid_cols=*/ 2 , /* gain_map_grid_rows=*/ 2 ,
1241+ /* gain_map_cell_width=*/ 64 , /* gain_map_cell_height=*/ 80 );
1242+ MakeTestImage (
1243+ std::string (data_path) + " color_grid_alpha_grid_gainmap_nogrid.avif" ,
1244+ /* grid_cols=*/ 4 ,
1245+ /* grid_rows=*/ 3 ,
1246+ /* cell_width=*/ 128 , /* cell_heigh=*/ 200 ,
1247+ /* gain_map_grid_cols=*/ 1 , /* gain_map_grid_rows=*/ 1 ,
1248+ /* gain_map_cell_width=*/ 64 , /* gain_map_cell_height=*/ 80 );
1249+ MakeTestImage (
1250+ std::string (data_path) + " color_nogrid_alpha_nogrid_gainmap_grid.avif" ,
1251+ /* grid_cols=*/ 1 ,
1252+ /* grid_rows=*/ 1 ,
1253+ /* cell_width=*/ 128 , /* cell_heigh=*/ 200 ,
1254+ /* gain_map_grid_cols=*/ 2 , /* gain_map_grid_rows=*/ 2 ,
1255+ /* gain_map_cell_width=*/ 64 , /* gain_map_cell_height=*/ 80 );
1256+ MakeTestImage (std::string (data_path) + " unsupported_gainmap_version.avif" ,
1257+ /* grid_cols=*/ 1 ,
1258+ /* grid_rows=*/ 1 ,
1259+ /* cell_width=*/ 100 , /* cell_heigh=*/ 100 ,
1260+ /* gain_map_grid_cols=*/ 1 , /* gain_map_grid_rows=*/ 1 ,
1261+ /* gain_map_cell_width=*/ 50 , /* gain_map_cell_height=*/ 50 ,
1262+ /* tmap_version=*/ 99 , /* minimum_version=*/ 0 ,
1263+ /* writer_version=*/ 0 );
1264+ MakeTestImage (
1265+ std::string (data_path) + " unsupported_gainmap_minimum_version.avif" ,
1266+ /* grid_cols=*/ 1 ,
1267+ /* grid_rows=*/ 1 ,
1268+ /* cell_width=*/ 100 , /* cell_heigh=*/ 100 ,
1269+ /* gain_map_grid_cols=*/ 1 , /* gain_map_grid_rows=*/ 1 ,
1270+ /* gain_map_cell_width=*/ 50 , /* gain_map_cell_height=*/ 50 ,
1271+ /* tmap_version=*/ 0 , /* minimum_version=*/ 99 , /* writer_version=*/ 99 );
1272+ MakeTestImage (
1273+ std::string (data_path) +
1274+ " unsupported_gainmap_writer_version_with_extra_bytes.avif" ,
1275+ /* grid_cols=*/ 1 ,
1276+ /* grid_rows=*/ 1 ,
1277+ /* cell_width=*/ 100 , /* cell_heigh=*/ 100 ,
1278+ /* gain_map_grid_cols=*/ 1 , /* gain_map_grid_rows=*/ 1 ,
1279+ /* gain_map_cell_width=*/ 50 , /* gain_map_cell_height=*/ 50 ,
1280+ /* tmap_version=*/ 0 , /* minimum_version=*/ 0 , /* writer_version=*/ 99 ,
1281+ /* add_extra_bytes=*/ 1 );
1282+ MakeTestImage (std::string (data_path) +
1283+ " supported_gainmap_writer_version_with_extra_bytes.avif" ,
1284+ /* grid_cols=*/ 1 ,
1285+ /* grid_rows=*/ 1 ,
1286+ /* cell_width=*/ 100 , /* cell_heigh=*/ 100 ,
1287+ /* gain_map_grid_cols=*/ 1 , /* gain_map_grid_rows=*/ 1 ,
1288+ /* gain_map_cell_width=*/ 50 , /* gain_map_cell_height=*/ 50 ,
1289+ /* tmap_version=*/ 0 , /* minimum_version=*/ 0 ,
1290+ /* writer_version=*/ 0 ,
1291+ /* add_extra_bytes=*/ 1 );
1292+ }
11651293}
11661294
11671295class ToneMapTest
0 commit comments