|
4 | 4 | #include <string>
|
5 | 5 |
|
6 | 6 | #include "avif/avif.h"
|
| 7 | +#include "avifjpeg.h" |
| 8 | +#include "avifpng.h" |
7 | 9 | #include "aviftest_helpers.h"
|
8 | 10 | #include "avifutil.h"
|
9 | 11 | #include "gtest/gtest.h"
|
@@ -289,29 +291,80 @@ TEST(ICCTest, GeneratedICCHash) {
|
289 | 291 | 0);
|
290 | 292 | }
|
291 | 293 |
|
292 |
| -// Verify the invalidity of keeping the ICC profile for a gray image read from |
293 |
| -// an RGB image. |
294 |
| -TEST(ICCTest, RGB2Gray) { |
295 |
| - for (const auto& file_name : |
296 |
| - {"paris_icc_exif_xmp.png", "paris_exif_xmp_icc.jpg"}) { |
297 |
| - const std::string file_path = std::string(data_path) + file_name; |
298 |
| - for (bool ignore_icc : {false, true}) { |
299 |
| - ImagePtr image(avifImageCreateEmpty()); |
300 |
| - // Read the image. |
301 |
| - const avifAppFileFormat file_format = avifReadImage( |
302 |
| - file_path.c_str(), |
303 |
| - /*requestedFormat=*/AVIF_PIXEL_FORMAT_YUV400, |
304 |
| - /*requestedDepth=*/0, |
305 |
| - /*chromaDownsampling=*/AVIF_CHROMA_DOWNSAMPLING_AUTOMATIC, |
306 |
| - /*ignoreColorProfile=*/ignore_icc, /*ignoreExif=*/false, |
307 |
| - /*ignoreXMP=*/false, /*allowChangingCicp=*/true, |
308 |
| - /*ignoreGainMap=*/true, AVIF_DEFAULT_IMAGE_SIZE_LIMIT, image.get(), |
309 |
| - /*outDepth=*/nullptr, /*sourceTiming=*/nullptr, |
310 |
| - /*frameIter=*/nullptr); |
311 |
| - if (ignore_icc) { |
312 |
| - ASSERT_NE(file_format, AVIF_APP_FILE_FORMAT_UNKNOWN); |
| 294 | +// Simpler function to read an image. |
| 295 | +static avifAppFileFormat avifReadImageForRGB2Gray2RGB(const std::string& path, |
| 296 | + avifPixelFormat format, |
| 297 | + bool ignore_icc, |
| 298 | + ImagePtr& image) { |
| 299 | + return avifReadImage( |
| 300 | + path.c_str(), format, /*requestedDepth=*/0, |
| 301 | + /*chromaDownsampling=*/AVIF_CHROMA_DOWNSAMPLING_AUTOMATIC, |
| 302 | + /*ignoreColorProfile=*/ignore_icc, /*ignoreExif=*/false, |
| 303 | + /*ignoreXMP=*/false, /*allowChangingCicp=*/true, |
| 304 | + /*ignoreGainMap=*/true, AVIF_DEFAULT_IMAGE_SIZE_LIMIT, image.get(), |
| 305 | + /*outDepth=*/nullptr, /*sourceTiming=*/nullptr, |
| 306 | + /*frameIter=*/nullptr); |
| 307 | +} |
| 308 | + |
| 309 | +// Verify the invalidity of keeping the ICC profile for a gray/color image read |
| 310 | +// from a color/gray image. |
| 311 | +TEST(ICCTest, RGB2Gray2RGB) { |
| 312 | + constexpr char file_name[] = "paris_icc_exif_xmp.png"; |
| 313 | + const std::string file_path = std::string(data_path) + file_name; |
| 314 | + |
| 315 | + for (auto format : {AVIF_PIXEL_FORMAT_YUV400, AVIF_PIXEL_FORMAT_YUV444}) { |
| 316 | + // Read the ground truth image in the appropriate format. |
| 317 | + ImagePtr image(avifImageCreateEmpty()); |
| 318 | + ASSERT_NE(image, nullptr); |
| 319 | + ASSERT_NE(avifReadImageForRGB2Gray2RGB(file_path, format, |
| 320 | + /*ignore_icc=*/true, image), |
| 321 | + AVIF_APP_FILE_FORMAT_UNKNOWN); |
| 322 | + |
| 323 | + // Add an ICC profile. |
| 324 | + float primariesCoords[8]; |
| 325 | + avifColorPrimariesGetValues(AVIF_COLOR_PRIMARIES_BT709, primariesCoords); |
| 326 | + |
| 327 | + testutil::AvifRwData icc; |
| 328 | + if (format == AVIF_PIXEL_FORMAT_YUV400) { |
| 329 | + EXPECT_EQ(avifGenerateGrayICC(&icc, 2.2f, primariesCoords), AVIF_TRUE); |
| 330 | + } else { |
| 331 | + EXPECT_EQ(avifGenerateRGBICC(&icc, 2.2f, primariesCoords), AVIF_TRUE); |
| 332 | + } |
| 333 | + ASSERT_EQ(avifImageSetProfileICC(image.get(), icc.data, icc.size), |
| 334 | + AVIF_RESULT_OK); |
| 335 | + |
| 336 | + for (const std::string ext : {"png", "jpg"}) { |
| 337 | + // Write the image with the appropriate codec. |
| 338 | + const std::string new_path = |
| 339 | + testing::TempDir() + "tmp_RGB2Gray2RGB." + ext; |
| 340 | + if (ext == "png") { |
| 341 | + ASSERT_EQ( |
| 342 | + avifPNGWrite(new_path.c_str(), image.get(), /*requestedDepth=*/0, |
| 343 | + AVIF_CHROMA_UPSAMPLING_BEST_QUALITY, |
| 344 | + /*compressionLevel=*/0), |
| 345 | + AVIF_TRUE); |
313 | 346 | } else {
|
314 |
| - ASSERT_EQ(file_format, AVIF_APP_FILE_FORMAT_UNKNOWN); |
| 347 | + ASSERT_EQ( |
| 348 | + avifJPEGWrite(new_path.c_str(), image.get(), /*jpegQuality=*/75, |
| 349 | + AVIF_CHROMA_UPSAMPLING_BEST_QUALITY), |
| 350 | + AVIF_TRUE); |
| 351 | + } |
| 352 | + |
| 353 | + for (bool ignore_icc : {false, true}) { |
| 354 | + for (auto new_format : |
| 355 | + {AVIF_PIXEL_FORMAT_YUV400, AVIF_PIXEL_FORMAT_YUV444}) { |
| 356 | + ImagePtr new_image(avifImageCreateEmpty()); |
| 357 | + ASSERT_NE(new_image, nullptr); |
| 358 | + const avifAppFileFormat new_file_format = |
| 359 | + avifReadImageForRGB2Gray2RGB(new_path, new_format, ignore_icc, |
| 360 | + new_image); |
| 361 | + if (format == new_format || ignore_icc) { |
| 362 | + ASSERT_NE(new_file_format, AVIF_APP_FILE_FORMAT_UNKNOWN); |
| 363 | + } else { |
| 364 | + // When formats are different, the ICC cannot be kept. |
| 365 | + ASSERT_EQ(new_file_format, AVIF_APP_FILE_FORMAT_UNKNOWN); |
| 366 | + } |
| 367 | + } |
315 | 368 | }
|
316 | 369 | }
|
317 | 370 | }
|
|
0 commit comments