| 9 | namespace avif { |
| 10 | |
| 11 | avifResult ChangeBase(const avifImage& image, int depth, |
| 12 | avifPixelFormat yuvFormat, avifImage* swapped) { |
| 13 | if (image.gainMap == nullptr || image.gainMap->image == nullptr) { |
| 14 | return AVIF_RESULT_INVALID_ARGUMENT; |
| 15 | } |
| 16 | |
| 17 | // Copy all metadata (no planes). |
| 18 | avifResult result = avifImageCopy(swapped, &image, /*planes=*/0); |
| 19 | if (result != AVIF_RESULT_OK) { |
| 20 | return result; |
| 21 | } |
| 22 | swapped->depth = depth; |
| 23 | swapped->yuvFormat = yuvFormat; |
| 24 | |
| 25 | if (image.gainMap->alternateHdrHeadroom.d == 0) { |
| 26 | return AVIF_RESULT_INVALID_ARGUMENT; |
| 27 | } |
| 28 | const float headroom = |
| 29 | static_cast<float>(image.gainMap->alternateHdrHeadroom.n) / |
| 30 | image.gainMap->alternateHdrHeadroom.d; |
| 31 | const bool tone_mapping_to_sdr = (headroom == 0.0f); |
| 32 | |
| 33 | swapped->colorPrimaries = image.gainMap->altColorPrimaries; |
| 34 | if (swapped->colorPrimaries == AVIF_COLOR_PRIMARIES_UNSPECIFIED) { |
| 35 | // Default to the same primaries as the base image if unspecified. |
| 36 | swapped->colorPrimaries = image.colorPrimaries; |
| 37 | } |
| 38 | |
| 39 | swapped->transferCharacteristics = image.gainMap->altTransferCharacteristics; |
| 40 | if (swapped->transferCharacteristics == |
| 41 | AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED) { |
| 42 | // Default to PQ for HDR and sRGB for SDR if unspecified. |
| 43 | const avifTransferCharacteristics transfer_characteristics = |
| 44 | static_cast<avifTransferCharacteristics>( |
| 45 | tone_mapping_to_sdr ? AVIF_TRANSFER_CHARACTERISTICS_SRGB |
| 46 | : AVIF_TRANSFER_CHARACTERISTICS_PQ); |
| 47 | swapped->transferCharacteristics = transfer_characteristics; |
| 48 | } |
| 49 | |
| 50 | swapped->matrixCoefficients = image.gainMap->altMatrixCoefficients; |
| 51 | if (swapped->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED) { |
| 52 | // Default to the same matrix as the base image if unspecified. |
| 53 | swapped->matrixCoefficients = image.matrixCoefficients; |
| 54 | } |
| 55 | |
| 56 | avifRGBImage swapped_rgb; |
| 57 | RGBImageCleanup rgb_cleanup(&swapped_rgb); |
| 58 | avifRGBImageSetDefaults(&swapped_rgb, swapped); |
| 59 | |
| 60 | avifContentLightLevelInformationBox clli = image.gainMap->altCLLI; |
| 61 | const bool compute_clli = |
| 62 | !tone_mapping_to_sdr && clli.maxCLL == 0 && clli.maxPALL == 0; |
| 63 | |
| 64 | avifDiagnostics diag; |
| 65 | result = avifImageApplyGainMap(&image, image.gainMap, headroom, |
| 66 | swapped->colorPrimaries, |
| 67 | swapped->transferCharacteristics, &swapped_rgb, |
| 68 | (compute_clli ? &clli : nullptr), &diag); |
no test coverage detected