Coverage Report

Created: 2024-05-21 06:41

/src/libjxl/lib/jxl/enc_comparator.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/jxl/enc_comparator.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstddef>
12
13
#include "lib/jxl/base/compiler_specific.h"
14
#include "lib/jxl/enc_gamma_correct.h"
15
#include "lib/jxl/enc_image_bundle.h"
16
17
namespace jxl {
18
namespace {
19
20
// color is linear, but blending happens in gamma-compressed space using
21
// (gamma-compressed) grayscale background color, alpha image represents
22
// weights of the sRGB colors in the [0 .. (1 << bit_depth) - 1] interval,
23
// output image is in linear space.
24
void AlphaBlend(const Image3F& in, const size_t c, float background_linear,
25
0
                const ImageF& alpha, Image3F* out) {
26
0
  const float background = LinearToSrgb8Direct(background_linear);
27
28
0
  for (size_t y = 0; y < out->ysize(); ++y) {
29
0
    const float* JXL_RESTRICT row_a = alpha.ConstRow(y);
30
0
    const float* JXL_RESTRICT row_i = in.ConstPlaneRow(c, y);
31
0
    float* JXL_RESTRICT row_o = out->PlaneRow(c, y);
32
0
    for (size_t x = 0; x < out->xsize(); ++x) {
33
0
      const float a = row_a[x];
34
0
      if (a <= 0.f) {
35
0
        row_o[x] = background_linear;
36
0
      } else if (a >= 1.f) {
37
0
        row_o[x] = row_i[x];
38
0
      } else {
39
0
        const float w_fg = a;
40
0
        const float w_bg = 1.0f - w_fg;
41
0
        const float fg = w_fg * LinearToSrgb8Direct(row_i[x]);
42
0
        const float bg = w_bg * background;
43
0
        row_o[x] = Srgb8ToLinearDirect(fg + bg);
44
0
      }
45
0
    }
46
0
  }
47
0
}
48
49
0
void AlphaBlend(float background_linear, ImageBundle* io_linear_srgb) {
50
  // No alpha => all opaque.
51
0
  if (!io_linear_srgb->HasAlpha()) return;
52
53
0
  for (size_t c = 0; c < 3; ++c) {
54
0
    AlphaBlend(*io_linear_srgb->color(), c, background_linear,
55
0
               *io_linear_srgb->alpha(), io_linear_srgb->color());
56
0
  }
57
0
}
58
59
float ComputeScoreImpl(const ImageBundle& rgb0, const ImageBundle& rgb1,
60
0
                       Comparator* comparator, ImageF* distmap) {
61
0
  JXL_CHECK(comparator->SetReferenceImage(rgb0));
62
0
  float score;
63
0
  JXL_CHECK(comparator->CompareWith(rgb1, distmap, &score));
64
0
  return score;
65
0
}
66
67
}  // namespace
68
69
Status ComputeScore(const ImageBundle& rgb0, const ImageBundle& rgb1,
70
                    Comparator* comparator, const JxlCmsInterface& cms,
71
                    float* score, ImageF* diffmap, ThreadPool* pool,
72
0
                    bool ignore_alpha) {
73
0
  JxlMemoryManager* memory_manager = rgb0.memory_manager();
74
  // Convert to linear sRGB (unless already in that space)
75
0
  ImageMetadata metadata0 = *rgb0.metadata();
76
0
  ImageBundle store0(memory_manager, &metadata0);
77
0
  const ImageBundle* linear_srgb0;
78
0
  JXL_CHECK(TransformIfNeeded(rgb0, ColorEncoding::LinearSRGB(rgb0.IsGray()),
79
0
                              cms, pool, &store0, &linear_srgb0));
80
0
  ImageMetadata metadata1 = *rgb1.metadata();
81
0
  ImageBundle store1(memory_manager, &metadata1);
82
0
  const ImageBundle* linear_srgb1;
83
0
  JXL_CHECK(TransformIfNeeded(rgb1, ColorEncoding::LinearSRGB(rgb1.IsGray()),
84
0
                              cms, pool, &store1, &linear_srgb1));
85
86
  // No alpha: skip blending, only need a single call to Butteraugli.
87
0
  if (ignore_alpha || (!rgb0.HasAlpha() && !rgb1.HasAlpha())) {
88
0
    *score =
89
0
        ComputeScoreImpl(*linear_srgb0, *linear_srgb1, comparator, diffmap);
90
0
    return true;
91
0
  }
92
93
  // Blend on black and white backgrounds
94
95
0
  const float black = 0.0f;
96
0
  JXL_ASSIGN_OR_RETURN(ImageBundle blended_black0, linear_srgb0->Copy());
97
0
  JXL_ASSIGN_OR_RETURN(ImageBundle blended_black1, linear_srgb1->Copy());
98
0
  AlphaBlend(black, &blended_black0);
99
0
  AlphaBlend(black, &blended_black1);
100
101
0
  const float white = 1.0f;
102
0
  JXL_ASSIGN_OR_RETURN(ImageBundle blended_white0, linear_srgb0->Copy());
103
0
  JXL_ASSIGN_OR_RETURN(ImageBundle blended_white1, linear_srgb1->Copy());
104
105
0
  AlphaBlend(white, &blended_white0);
106
0
  AlphaBlend(white, &blended_white1);
107
108
0
  ImageF diffmap_black;
109
0
  ImageF diffmap_white;
110
0
  const float dist_black = ComputeScoreImpl(blended_black0, blended_black1,
111
0
                                            comparator, &diffmap_black);
112
0
  const float dist_white = ComputeScoreImpl(blended_white0, blended_white1,
113
0
                                            comparator, &diffmap_white);
114
115
  // diffmap and return values are the max of diffmap_black/white.
116
0
  if (diffmap != nullptr) {
117
0
    const size_t xsize = rgb0.xsize();
118
0
    const size_t ysize = rgb0.ysize();
119
0
    JXL_ASSIGN_OR_RETURN(*diffmap,
120
0
                         ImageF::Create(memory_manager, xsize, ysize));
121
0
    for (size_t y = 0; y < ysize; ++y) {
122
0
      const float* JXL_RESTRICT row_black = diffmap_black.ConstRow(y);
123
0
      const float* JXL_RESTRICT row_white = diffmap_white.ConstRow(y);
124
0
      float* JXL_RESTRICT row_out = diffmap->Row(y);
125
0
      for (size_t x = 0; x < xsize; ++x) {
126
0
        row_out[x] = std::max(row_black[x], row_white[x]);
127
0
      }
128
0
    }
129
0
  }
130
0
  *score = std::max(dist_black, dist_white);
131
0
  return true;
132
0
}
133
134
}  // namespace jxl