blob: 0a30a6b6a0bff16232d4ea84bcac09e04ca9df84 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include <string.h>
6#include <time.h>
7#include <vector>
8
9#include "base/gfx/convolver.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace gfx {
13
14namespace {
15
16// Fills the given filter with impulse functions for the range 0->num_entries.
17 void FillImpulseFilter(int num_entries, ConvolusionFilter1D* filter) {
18 float one = 1.0f;
19 for (int i = 0; i < num_entries; i++)
20 filter->AddFilter(i, &one, 1);
21}
22
23// Filters the given input with the impulse function, and verifies that it
24// does not change.
25void TestImpulseConvolusion(const unsigned char* data, int width, int height) {
26 int byte_count = width * height * 4;
27
28 ConvolusionFilter1D filter_x;
29 FillImpulseFilter(width, &filter_x);
30
31 ConvolusionFilter1D filter_y;
32 FillImpulseFilter(height, &filter_y);
33
34 std::vector<unsigned char> output;
35 output.resize(byte_count);
36 BGRAConvolve2D(data, width * 4, true, filter_x, filter_y, &output[0]);
37
38 // Output should exactly match input.
39 EXPECT_EQ(0, memcmp(data, &output[0], byte_count));
40}
41
42// Fills the destination filter with a box filter averaging every two pixels
43// to produce the output.
44void FillBoxFilter(int size, ConvolusionFilter1D* filter) {
45 const float box[2] = { 0.5, 0.5 };
46 for (int i = 0; i < size; i++)
47 filter->AddFilter(i * 2, box, 2);
48}
49
50} // namespace
51
52// Tests that each pixel, when set and run through the impulse filter, does
53// not change.
54TEST(Convolver, Impulse) {
55 // We pick an "odd" size that is not likely to fit on any boundaries so that
56 // we can see if all the widths and paddings are handled properly.
57 int width = 15;
58 int height = 31;
59 int byte_count = width * height * 4;
60 std::vector<unsigned char> input;
61 input.resize(byte_count);
62
63 unsigned char* input_ptr = &input[0];
64 for (int y = 0; y < height; y++) {
65 for (int x = 0; x < width; x++) {
66 for (int channel = 0; channel < 3; channel++) {
67 memset(input_ptr, 0, byte_count);
68 input_ptr[(y * width + x) * 4 + channel] = 0xff;
69 // Always set the alpha channel or it will attempt to "fix" it for us.
70 input_ptr[(y * width + x) * 4 + 3] = 0xff;
71 TestImpulseConvolusion(input_ptr, width, height);
72 }
73 }
74 }
75}
76
77// Tests that using a box filter to halve an image results in every square of 4
78// pixels in the original get averaged to a pixel in the output.
79TEST(Convolver, Halve) {
80 static const int kSize = 16;
81
82 int src_width = kSize;
83 int src_height = kSize;
84 int src_row_stride = src_width * 4;
85 int src_byte_count = src_row_stride * src_height;
86 std::vector<unsigned char> input;
87 input.resize(src_byte_count);
88
89 int dest_width = src_width / 2;
90 int dest_height = src_height / 2;
91 int dest_byte_count = dest_width * dest_height * 4;
92 std::vector<unsigned char> output;
93 output.resize(dest_byte_count);
94
95 // First fill the array with a bunch of random data.
96 srand(static_cast<unsigned>(time(NULL)));
97 for (int i = 0; i < src_byte_count; i++)
98 input[i] = rand() * 255 / RAND_MAX;
99
100 // Compute the filters.
101 ConvolusionFilter1D filter_x, filter_y;
102 FillBoxFilter(dest_width, &filter_x);
103 FillBoxFilter(dest_height, &filter_y);
104
105 // Do the convolusion.
106 BGRAConvolve2D(&input[0], src_width, true, filter_x, filter_y, &output[0]);
107
108 // Compute the expected results and check, allowing for a small difference
109 // to account for rounding errors.
110 for (int y = 0; y < dest_height; y++) {
111 for (int x = 0; x < dest_width; x++) {
112 for (int channel = 0; channel < 4; channel++) {
113 int src_offset = (y * 2 * src_row_stride + x * 2 * 4) + channel;
114 int value = input[src_offset] + // Top left source pixel.
115 input[src_offset + 4] + // Top right source pixel.
116 input[src_offset + src_row_stride] + // Lower left.
117 input[src_offset + src_row_stride + 4]; // Lower right.
118 value /= 4; // Average.
119 int difference = value - output[(y * dest_width + x) * 4 + channel];
120 EXPECT_TRUE(difference >= -1 || difference <= 1);
121 }
122 }
123 }
124}
125
deanm@google.com71de82c2008-08-13 20:49:15 +0900126} // namespace gfx
license.botf003cfe2008-08-24 09:55:55 +0900127