Coverage Report

Created: 2024-05-21 06:24

/src/libjxl/lib/extras/common.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/extras/common.h"
7
8
#include <jxl/codestream_header.h>
9
#include <jxl/types.h>
10
11
#include <cstddef>
12
#include <vector>
13
14
#include "lib/extras/packed_image.h"
15
#include "lib/jxl/base/printf_macros.h"
16
#include "lib/jxl/base/status.h"
17
18
namespace jxl {
19
namespace extras {
20
21
Status SelectFormat(const std::vector<JxlPixelFormat>& accepted_formats,
22
0
                    const JxlBasicInfo& basic_info, JxlPixelFormat* format) {
23
0
  const size_t original_bit_depth = basic_info.bits_per_sample;
24
0
  size_t current_bit_depth = 0;
25
0
  size_t num_alpha_channels = (basic_info.alpha_bits != 0 ? 1 : 0);
26
0
  size_t num_channels = basic_info.num_color_channels + num_alpha_channels;
27
0
  for (;;) {
28
0
    for (const JxlPixelFormat& candidate : accepted_formats) {
29
0
      if (candidate.num_channels != num_channels) continue;
30
0
      const size_t candidate_bit_depth =
31
0
          PackedImage::BitsPerChannel(candidate.data_type);
32
0
      if (
33
          // Candidate bit depth is less than what we have and still enough
34
0
          (original_bit_depth <= candidate_bit_depth &&
35
0
           candidate_bit_depth < current_bit_depth) ||
36
          // Or larger than the too-small bit depth we currently have
37
0
          (current_bit_depth < candidate_bit_depth &&
38
0
           current_bit_depth < original_bit_depth)) {
39
0
        *format = candidate;
40
0
        current_bit_depth = candidate_bit_depth;
41
0
      }
42
0
    }
43
0
    if (current_bit_depth == 0) {
44
0
      if (num_channels > basic_info.num_color_channels) {
45
        // Try dropping the alpha channel.
46
0
        --num_channels;
47
0
        continue;
48
0
      }
49
0
      return JXL_FAILURE("no appropriate format found");
50
0
    }
51
0
    break;
52
0
  }
53
0
  if (current_bit_depth < original_bit_depth) {
54
0
    JXL_WARNING("encoding %" PRIuS "-bit original to %" PRIuS " bits",
55
0
                original_bit_depth, current_bit_depth);
56
0
  }
57
0
  return true;
58
0
}
59
60
}  // namespace extras
61
}  // namespace jxl