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/gaborish.h"
7 
8 #include <hwy/base.h>
9 
10 #include "gtest/gtest.h"
11 #include "lib/jxl/convolve.h"
12 #include "lib/jxl/image_ops.h"
13 #include "lib/jxl/image_test_utils.h"
14 
15 namespace jxl {
16 namespace {
17 
18 // weight1,2 need not be normalized.
GaborishKernel(float weight1,float weight2)19 WeightsSymmetric3 GaborishKernel(float weight1, float weight2) {
20   constexpr float weight0 = 1.0f;
21 
22   // Normalize
23   const float mul = 1.0f / (weight0 + 4 * (weight1 + weight2));
24   const float w0 = weight0 * mul;
25   const float w1 = weight1 * mul;
26   const float w2 = weight2 * mul;
27 
28   const WeightsSymmetric3 w = {{HWY_REP4(w0)}, {HWY_REP4(w1)}, {HWY_REP4(w2)}};
29   return w;
30 }
31 
ConvolveGaborish(const ImageF & in,float weight1,float weight2,ThreadPool * pool,ImageF * JXL_RESTRICT out)32 void ConvolveGaborish(const ImageF& in, float weight1, float weight2,
33                       ThreadPool* pool, ImageF* JXL_RESTRICT out) {
34   JXL_CHECK(SameSize(in, *out));
35   Symmetric3(in, Rect(in), GaborishKernel(weight1, weight2), pool, out);
36 }
37 
TestRoundTrip(const Image3F & in,float max_l1)38 void TestRoundTrip(const Image3F& in, float max_l1) {
39   Image3F fwd(in.xsize(), in.ysize());
40   ThreadPool* null_pool = nullptr;
41   ConvolveGaborish(in.Plane(0), 0, 0, null_pool, &fwd.Plane(0));
42   ConvolveGaborish(in.Plane(1), 0, 0, null_pool, &fwd.Plane(1));
43   ConvolveGaborish(in.Plane(2), 0, 0, null_pool, &fwd.Plane(2));
44   GaborishInverse(&fwd, 0.92718927264540152f, null_pool);
45   VerifyRelativeError(in, fwd, max_l1, 1E-4f);
46 }
47 
TEST(GaborishTest,TestZero)48 TEST(GaborishTest, TestZero) {
49   Image3F in(20, 20);
50   ZeroFillImage(&in);
51   TestRoundTrip(in, 0.0f);
52 }
53 
54 // Disabled: large difference.
55 #if 0
56 TEST(GaborishTest, TestDirac) {
57   Image3F in(20, 20);
58   ZeroFillImage(&in);
59   in.PlaneRow(1, 10)[10] = 10.0f;
60   TestRoundTrip(in, 0.26f);
61 }
62 #endif
63 
TEST(GaborishTest,TestFlat)64 TEST(GaborishTest, TestFlat) {
65   Image3F in(20, 20);
66   FillImage(1.0f, &in);
67   TestRoundTrip(in, 1E-5f);
68 }
69 
70 }  // namespace
71 }  // namespace jxl
72