tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

enc_gaborish_test.cc (2673B)


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