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