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
150
151
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string.h>
#include <time.h>
#include <vector>
#include "base/gfx/convolver.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gfx {
namespace {
// Fills the given filter with impulse functions for the range 0->num_entries.
void FillImpulseFilter(int num_entries, ConvolusionFilter1D* filter) {
float one = 1.0f;
for (int i = 0; i < num_entries; i++)
filter->AddFilter(i, &one, 1);
}
// Filters the given input with the impulse function, and verifies that it
// does not change.
void TestImpulseConvolusion(const unsigned char* data, int width, int height) {
int byte_count = width * height * 4;
ConvolusionFilter1D filter_x;
FillImpulseFilter(width, &filter_x);
ConvolusionFilter1D filter_y;
FillImpulseFilter(height, &filter_y);
std::vector<unsigned char> output;
output.resize(byte_count);
BGRAConvolve2D(data, width * 4, true, filter_x, filter_y, &output[0]);
// Output should exactly match input.
EXPECT_EQ(0, memcmp(data, &output[0], byte_count));
}
// Fills the destination filter with a box filter averaging every two pixels
// to produce the output.
void FillBoxFilter(int size, ConvolusionFilter1D* filter) {
const float box[2] = { 0.5, 0.5 };
for (int i = 0; i < size; i++)
filter->AddFilter(i * 2, box, 2);
}
} // namespace
// Tests that each pixel, when set and run through the impulse filter, does
// not change.
TEST(Convolver, Impulse) {
// We pick an "odd" size that is not likely to fit on any boundaries so that
// we can see if all the widths and paddings are handled properly.
int width = 15;
int height = 31;
int byte_count = width * height * 4;
std::vector<unsigned char> input;
input.resize(byte_count);
unsigned char* input_ptr = &input[0];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int channel = 0; channel < 3; channel++) {
memset(input_ptr, 0, byte_count);
input_ptr[(y * width + x) * 4 + channel] = 0xff;
// Always set the alpha channel or it will attempt to "fix" it for us.
input_ptr[(y * width + x) * 4 + 3] = 0xff;
TestImpulseConvolusion(input_ptr, width, height);
}
}
}
}
// Tests that using a box filter to halve an image results in every square of 4
// pixels in the original get averaged to a pixel in the output.
TEST(Convolver, Halve) {
static const int kSize = 16;
int src_width = kSize;
int src_height = kSize;
int src_row_stride = src_width * 4;
int src_byte_count = src_row_stride * src_height;
std::vector<unsigned char> input;
input.resize(src_byte_count);
int dest_width = src_width / 2;
int dest_height = src_height / 2;
int dest_byte_count = dest_width * dest_height * 4;
std::vector<unsigned char> output;
output.resize(dest_byte_count);
// First fill the array with a bunch of random data.
srand(static_cast<unsigned>(time(NULL)));
for (int i = 0; i < src_byte_count; i++)
input[i] = rand() * 255 / RAND_MAX;
// Compute the filters.
ConvolusionFilter1D filter_x, filter_y;
FillBoxFilter(dest_width, &filter_x);
FillBoxFilter(dest_height, &filter_y);
// Do the convolusion.
BGRAConvolve2D(&input[0], src_width, true, filter_x, filter_y, &output[0]);
// Compute the expected results and check, allowing for a small difference
// to account for rounding errors.
for (int y = 0; y < dest_height; y++) {
for (int x = 0; x < dest_width; x++) {
for (int channel = 0; channel < 4; channel++) {
int src_offset = (y * 2 * src_row_stride + x * 2 * 4) + channel;
int value = input[src_offset] + // Top left source pixel.
input[src_offset + 4] + // Top right source pixel.
input[src_offset + src_row_stride] + // Lower left.
input[src_offset + src_row_stride + 4]; // Lower right.
value /= 4; // Average.
int difference = value - output[(y * dest_width + x) * 4 + channel];
EXPECT_TRUE(difference >= -1 || difference <= 1);
}
}
}
}
} // namespace gfx
|