aboutsummaryrefslogtreecommitdiff
path: root/test/add_noise_test.cc
blob: 4fc4e81e636b8f927b15836365609f3f4801896f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include <math.h>
#include <tuple>

#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "third_party/googletest/src/include/gtest/gtest.h"
#include "./vpx_dsp_rtcd.h"
#include "vpx/vpx_integer.h"
#include "vpx_dsp/postproc.h"
#include "vpx_mem/vpx_mem.h"

namespace {

static const int kNoiseSize = 3072;

typedef void (*AddNoiseFunc)(uint8_t *start, const int8_t *noise,
                             int blackclamp, int whiteclamp, int width,
                             int height, int pitch);

typedef std::tuple<double, AddNoiseFunc> AddNoiseTestFPParam;

class AddNoiseTest : public ::testing::Test,
                     public ::testing::WithParamInterface<AddNoiseTestFPParam> {
 public:
  void TearDown() override { libvpx_test::ClearSystemState(); }
  ~AddNoiseTest() override = default;
};

double stddev6(char a, char b, char c, char d, char e, char f) {
  const double n = (a + b + c + d + e + f) / 6.0;
  const double v = ((a - n) * (a - n) + (b - n) * (b - n) + (c - n) * (c - n) +
                    (d - n) * (d - n) + (e - n) * (e - n) + (f - n) * (f - n)) /
                   6.0;
  return sqrt(v);
}

TEST_P(AddNoiseTest, CheckNoiseAdded) {
  const int width = 64;
  const int height = 64;
  const int image_size = width * height;
  int8_t noise[kNoiseSize];
  const int clamp = vpx_setup_noise(GET_PARAM(0), noise, kNoiseSize);
  uint8_t *const s =
      reinterpret_cast<uint8_t *>(vpx_calloc(image_size, sizeof(*s)));
  ASSERT_NE(s, nullptr);
  memset(s, 99, image_size * sizeof(*s));

  ASM_REGISTER_STATE_CHECK(
      GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));

  // Check to make sure we don't end up having either the same or no added
  // noise either vertically or horizontally.
  for (int i = 0; i < image_size - 6 * width - 6; ++i) {
    const double hd = stddev6(s[i] - 99, s[i + 1] - 99, s[i + 2] - 99,
                              s[i + 3] - 99, s[i + 4] - 99, s[i + 5] - 99);
    const double vd = stddev6(s[i] - 99, s[i + width] - 99,
                              s[i + 2 * width] - 99, s[i + 3 * width] - 99,
                              s[i + 4 * width] - 99, s[i + 5 * width] - 99);

    EXPECT_NE(hd, 0);
    EXPECT_NE(vd, 0);
  }

  // Initialize pixels in the image to 255 and check for roll over.
  memset(s, 255, image_size);

  ASM_REGISTER_STATE_CHECK(
      GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));

  // Check to make sure don't roll over.
  for (int i = 0; i < image_size; ++i) {
    EXPECT_GT(static_cast<int>(s[i]), clamp) << "i = " << i;
  }

  // Initialize pixels in the image to 0 and check for roll under.
  memset(s, 0, image_size);

  ASM_REGISTER_STATE_CHECK(
      GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));

  // Check to make sure don't roll under.
  for (int i = 0; i < image_size; ++i) {
    EXPECT_LT(static_cast<int>(s[i]), 255 - clamp) << "i = " << i;
  }

  vpx_free(s);
}

TEST_P(AddNoiseTest, CheckCvsAssembly) {
  const int width = 64;
  const int height = 64;
  const int image_size = width * height;
  int8_t noise[kNoiseSize];
  const int clamp = vpx_setup_noise(4.4, noise, kNoiseSize);

  uint8_t *const s = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
  uint8_t *const d = reinterpret_cast<uint8_t *>(vpx_calloc(image_size, 1));
  ASSERT_NE(s, nullptr);
  ASSERT_NE(d, nullptr);

  memset(s, 99, image_size);
  memset(d, 99, image_size);

  srand(0);
  ASM_REGISTER_STATE_CHECK(
      GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
  srand(0);
  ASM_REGISTER_STATE_CHECK(
      vpx_plane_add_noise_c(d, noise, clamp, clamp, width, height, width));

  for (int i = 0; i < image_size; ++i) {
    EXPECT_EQ(static_cast<int>(s[i]), static_cast<int>(d[i])) << "i = " << i;
  }

  vpx_free(d);
  vpx_free(s);
}

using std::make_tuple;

INSTANTIATE_TEST_SUITE_P(
    C, AddNoiseTest,
    ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_c),
                      make_tuple(4.4, vpx_plane_add_noise_c)));

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AddNoiseTest,
    ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_sse2),
                      make_tuple(4.4, vpx_plane_add_noise_sse2)));
#endif

#if HAVE_MSA
INSTANTIATE_TEST_SUITE_P(
    MSA, AddNoiseTest,
    ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_msa),
                      make_tuple(4.4, vpx_plane_add_noise_msa)));
#endif
}  // namespace