| 40 | } |
| 41 | |
| 42 | avifResult ConvertCommand::Run() { |
| 43 | #if !defined(AVIF_ENABLE_JPEG_GAIN_MAP_CONVERSION) |
| 44 | std::cout << "JPEG gainmap conversion unavailable because avifgainmaputil " |
| 45 | "was not built with libxml2.\n"; |
| 46 | return AVIF_RESULT_NOT_IMPLEMENTED; |
| 47 | #else |
| 48 | const avifPixelFormat pixel_format = |
| 49 | static_cast<avifPixelFormat>(arg_image_read_.pixel_format.value()); |
| 50 | |
| 51 | ImagePtr image(avifImageCreateEmpty()); |
| 52 | if (image == nullptr) { |
| 53 | return AVIF_RESULT_OUT_OF_MEMORY; |
| 54 | } |
| 55 | |
| 56 | if (arg_cicp_.provenance() == argparse::Provenance::SPECIFIED) { |
| 57 | image->colorPrimaries = arg_cicp_.value().color_primaries; |
| 58 | image->transferCharacteristics = arg_cicp_.value().transfer_characteristics; |
| 59 | image->matrixCoefficients = arg_cicp_.value().matrix_coefficients; |
| 60 | } |
| 61 | |
| 62 | avifResult result = |
| 63 | ReadImage(image.get(), arg_input_filename_.value(), pixel_format, |
| 64 | arg_image_read_.depth, arg_image_read_.ignore_profile, |
| 65 | /*ignore_gain_map=*/false, arg_jobs_.jobs.value()); |
| 66 | if (result != AVIF_RESULT_OK) { |
| 67 | std::cout << "Failed to decode image: " << arg_input_filename_; |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | if (image->gainMap && image->gainMap->altICC.size == 0) { |
| 72 | if (image->gainMap->altColorPrimaries == AVIF_COLOR_PRIMARIES_UNSPECIFIED) { |
| 73 | // Assume the alternate image has the same primaries as the base image. |
| 74 | image->gainMap->altColorPrimaries = image->colorPrimaries; |
| 75 | } |
| 76 | if (image->gainMap->altTransferCharacteristics == |
| 77 | AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED) { |
| 78 | // Assume the alternate image is PQ HDR. |
| 79 | image->gainMap->altTransferCharacteristics = |
| 80 | AVIF_TRANSFER_CHARACTERISTICS_PQ; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (image->gainMap == nullptr || image->gainMap->image == nullptr) { |
| 85 | std::cerr << "Input image " << arg_input_filename_ |
| 86 | << " does not contain a gain map\n"; |
| 87 | return AVIF_RESULT_INVALID_ARGUMENT; |
| 88 | } |
| 89 | |
| 90 | image->gainMap->altCLLI = arg_clli_.value(); |
| 91 | |
| 92 | if (arg_swap_base_) { |
| 93 | int depth = arg_image_read_.depth; |
| 94 | if (depth == 0) { |
| 95 | depth = image->gainMap->alternateHdrHeadroom.n == 0 ? 8 : 10; |
| 96 | } |
| 97 | ImagePtr new_base(avifImageCreateEmpty()); |
| 98 | if (new_base == nullptr) { |
| 99 | return AVIF_RESULT_OUT_OF_MEMORY; |
nothing calls this directly
no test coverage detected