File: extractgainmap_command.cc

package info (click to toggle)
libavif 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,648 kB
  • sloc: ansic: 30,743; cpp: 14,606; xml: 1,507; sh: 1,296; java: 307; makefile: 57
file content (45 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2023 Google LLC
// SPDX-License-Identifier: BSD-2-Clause

#include "extractgainmap_command.h"

#include "avif/avif_cxx.h"
#include "imageio.h"

namespace avif {

ExtractGainMapCommand::ExtractGainMapCommand()
    : ProgramCommand("extractgainmap",
                     "Save the gain map of an AVIF file as an image") {
  argparse_.add_argument(arg_input_filename_, "input_filename");
  argparse_.add_argument(arg_output_filename_, "output_filename");
  arg_image_encode_.Init(argparse_, /*can_have_alpha=*/false);
}

avifResult ExtractGainMapCommand::Run() {
  DecoderPtr decoder(avifDecoderCreate());
  if (decoder == nullptr) {
    return AVIF_RESULT_OUT_OF_MEMORY;
  }
  decoder->imageContentToDecode = AVIF_IMAGE_CONTENT_GAIN_MAP;

  avifResult result =
      ReadAvif(decoder.get(), arg_input_filename_, /*ignore_profile=*/true);
  if (result != AVIF_RESULT_OK) {
    return result;
  }

  if (decoder->image->gainMap == nullptr ||
      decoder->image->gainMap->image == nullptr) {
    std::cerr << "Input image " << arg_input_filename_
              << " does not contain a gain map\n";
    return AVIF_RESULT_INVALID_ARGUMENT;
  }

  return WriteImage(
      decoder->image->gainMap->image, arg_image_encode_.grid.value().grid_cols,
      arg_image_encode_.grid.value().grid_rows, arg_output_filename_,
      arg_image_encode_.quality, arg_image_encode_.speed);
}

}  // namespace avif