blob: 8d0c852a16d7e94e8b4fea6a687325d29b4806cf [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2012 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.
4
5#include <string.h>
6#include <time.h>
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +01007#include <algorithm>
8#include <numeric>
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include <vector>
10
11#include "base/basictypes.h"
12#include "base/logging.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "skia/ext/convolver.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "third_party/skia/include/core/SkBitmap.h"
17#include "third_party/skia/include/core/SkColorPriv.h"
18#include "third_party/skia/include/core/SkRect.h"
19#include "third_party/skia/include/core/SkTypes.h"
20
21namespace skia {
22
23namespace {
24
25// Fills the given filter with impulse functions for the range 0->num_entries.
26void FillImpulseFilter(int num_entries, ConvolutionFilter1D* filter) {
27 float one = 1.0f;
28 for (int i = 0; i < num_entries; i++)
29 filter->AddFilter(i, &one, 1);
30}
31
32// Filters the given input with the impulse function, and verifies that it
33// does not change.
34void TestImpulseConvolution(const unsigned char* data, int width, int height) {
35 int byte_count = width * height * 4;
36
37 ConvolutionFilter1D filter_x;
38 FillImpulseFilter(width, &filter_x);
39
40 ConvolutionFilter1D filter_y;
41 FillImpulseFilter(height, &filter_y);
42
43 std::vector<unsigned char> output;
44 output.resize(byte_count);
45 BGRAConvolve2D(data, width * 4, true, filter_x, filter_y,
46 filter_x.num_values() * 4, &output[0], false);
47
48 // Output should exactly match input.
49 EXPECT_EQ(0, memcmp(data, &output[0], byte_count));
50}
51
52// Fills the destination filter with a box filter averaging every two pixels
53// to produce the output.
54void FillBoxFilter(int size, ConvolutionFilter1D* filter) {
55 const float box[2] = { 0.5, 0.5 };
56 for (int i = 0; i < size; i++)
57 filter->AddFilter(i * 2, box, 2);
58}
59
60} // namespace
61
62// Tests that each pixel, when set and run through the impulse filter, does
63// not change.
64TEST(Convolver, Impulse) {
65 // We pick an "odd" size that is not likely to fit on any boundaries so that
66 // we can see if all the widths and paddings are handled properly.
67 int width = 15;
68 int height = 31;
69 int byte_count = width * height * 4;
70 std::vector<unsigned char> input;
71 input.resize(byte_count);
72
73 unsigned char* input_ptr = &input[0];
74 for (int y = 0; y < height; y++) {
75 for (int x = 0; x < width; x++) {
76 for (int channel = 0; channel < 3; channel++) {
77 memset(input_ptr, 0, byte_count);
78 input_ptr[(y * width + x) * 4 + channel] = 0xff;
79 // Always set the alpha channel or it will attempt to "fix" it for us.
80 input_ptr[(y * width + x) * 4 + 3] = 0xff;
81 TestImpulseConvolution(input_ptr, width, height);
82 }
83 }
84 }
85}
86
87// Tests that using a box filter to halve an image results in every square of 4
88// pixels in the original get averaged to a pixel in the output.
89TEST(Convolver, Halve) {
90 static const int kSize = 16;
91
92 int src_width = kSize;
93 int src_height = kSize;
94 int src_row_stride = src_width * 4;
95 int src_byte_count = src_row_stride * src_height;
96 std::vector<unsigned char> input;
97 input.resize(src_byte_count);
98
99 int dest_width = src_width / 2;
100 int dest_height = src_height / 2;
101 int dest_byte_count = dest_width * dest_height * 4;
102 std::vector<unsigned char> output;
103 output.resize(dest_byte_count);
104
105 // First fill the array with a bunch of random data.
106 srand(static_cast<unsigned>(time(NULL)));
107 for (int i = 0; i < src_byte_count; i++)
108 input[i] = rand() * 255 / RAND_MAX;
109
110 // Compute the filters.
111 ConvolutionFilter1D filter_x, filter_y;
112 FillBoxFilter(dest_width, &filter_x);
113 FillBoxFilter(dest_height, &filter_y);
114
115 // Do the convolution.
116 BGRAConvolve2D(&input[0], src_width, true, filter_x, filter_y,
117 filter_x.num_values() * 4, &output[0], false);
118
119 // Compute the expected results and check, allowing for a small difference
120 // to account for rounding errors.
121 for (int y = 0; y < dest_height; y++) {
122 for (int x = 0; x < dest_width; x++) {
123 for (int channel = 0; channel < 4; channel++) {
124 int src_offset = (y * 2 * src_row_stride + x * 2 * 4) + channel;
125 int value = input[src_offset] + // Top left source pixel.
126 input[src_offset + 4] + // Top right source pixel.
127 input[src_offset + src_row_stride] + // Lower left.
128 input[src_offset + src_row_stride + 4]; // Lower right.
129 value /= 4; // Average.
130 int difference = value - output[(y * dest_width + x) * 4 + channel];
131 EXPECT_TRUE(difference >= -1 || difference <= 1);
132 }
133 }
134 }
135}
136
137// Tests the optimization in Convolver1D::AddFilter that avoids storing
138// leading/trailing zeroes.
139TEST(Convolver, AddFilter) {
140 skia::ConvolutionFilter1D filter;
141
142 const skia::ConvolutionFilter1D::Fixed* values = NULL;
143 int filter_offset = 0;
144 int filter_length = 0;
145
146 // An all-zero filter is handled correctly, all factors ignored
147 static const float factors1[] = { 0.0f, 0.0f, 0.0f };
148 filter.AddFilter(11, factors1, arraysize(factors1));
149 ASSERT_EQ(0, filter.max_filter());
150 ASSERT_EQ(1, filter.num_values());
151
152 values = filter.FilterForValue(0, &filter_offset, &filter_length);
153 ASSERT_TRUE(values == NULL); // No values => NULL.
154 ASSERT_EQ(11, filter_offset); // Same as input offset.
155 ASSERT_EQ(0, filter_length); // But no factors since all are zeroes.
156
157 // Zeroes on the left are ignored
158 static const float factors2[] = { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f };
159 filter.AddFilter(22, factors2, arraysize(factors2));
160 ASSERT_EQ(4, filter.max_filter());
161 ASSERT_EQ(2, filter.num_values());
162
163 values = filter.FilterForValue(1, &filter_offset, &filter_length);
164 ASSERT_TRUE(values != NULL);
165 ASSERT_EQ(23, filter_offset); // 22 plus 1 leading zero
166 ASSERT_EQ(4, filter_length); // 5 - 1 leading zero
167
168 // Zeroes on the right are ignored
169 static const float factors3[] = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f };
170 filter.AddFilter(33, factors3, arraysize(factors3));
171 ASSERT_EQ(5, filter.max_filter());
172 ASSERT_EQ(3, filter.num_values());
173
174 values = filter.FilterForValue(2, &filter_offset, &filter_length);
175 ASSERT_TRUE(values != NULL);
176 ASSERT_EQ(33, filter_offset); // 33, same as input due to no leading zero
177 ASSERT_EQ(5, filter_length); // 7 - 2 trailing zeroes
178
179 // Zeroes in leading & trailing positions
180 static const float factors4[] = { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f };
181 filter.AddFilter(44, factors4, arraysize(factors4));
182 ASSERT_EQ(5, filter.max_filter()); // No change from existing value.
183 ASSERT_EQ(4, filter.num_values());
184
185 values = filter.FilterForValue(3, &filter_offset, &filter_length);
186 ASSERT_TRUE(values != NULL);
187 ASSERT_EQ(46, filter_offset); // 44 plus 2 leading zeroes
188 ASSERT_EQ(3, filter_length); // 7 - (2 leading + 2 trailing) zeroes
189
190 // Zeroes surrounded by non-zero values are ignored
191 static const float factors5[] = { 0.0f, 0.0f,
192 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
193 0.0f };
194 filter.AddFilter(55, factors5, arraysize(factors5));
195 ASSERT_EQ(6, filter.max_filter());
196 ASSERT_EQ(5, filter.num_values());
197
198 values = filter.FilterForValue(4, &filter_offset, &filter_length);
199 ASSERT_TRUE(values != NULL);
200 ASSERT_EQ(57, filter_offset); // 55 plus 2 leading zeroes
201 ASSERT_EQ(6, filter_length); // 9 - (2 leading + 1 trailing) zeroes
202
203 // All-zero filters after the first one also work
204 static const float factors6[] = { 0.0f };
205 filter.AddFilter(66, factors6, arraysize(factors6));
206 ASSERT_EQ(6, filter.max_filter());
207 ASSERT_EQ(6, filter.num_values());
208
209 values = filter.FilterForValue(5, &filter_offset, &filter_length);
210 ASSERT_TRUE(values == NULL); // filter_length == 0 => values is NULL
211 ASSERT_EQ(66, filter_offset); // value passed in
212 ASSERT_EQ(0, filter_length);
213}
214
Ben Murdochbb1529c2013-08-08 10:24:53 +0100215#if defined(THREAD_SANITIZER)
216// Times out under ThreadSanitizer, http://crbug.com/134400.
217#define MAYBE_SIMDVerification DISABLED_SIMDVerification
218#else
219#define MAYBE_SIMDVerification SIMDVerification
220#endif
221TEST(Convolver, MAYBE_SIMDVerification) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000222 int source_sizes[][2] = {
223 {1,1}, {1,2}, {1,3}, {1,4}, {1,5},
224 {2,1}, {2,2}, {2,3}, {2,4}, {2,5},
225 {3,1}, {3,2}, {3,3}, {3,4}, {3,5},
226 {4,1}, {4,2}, {4,3}, {4,4}, {4,5},
227 {1920, 1080},
228 {720, 480},
229 {1377, 523},
230 {325, 241} };
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000231 int dest_sizes[][2] = { {1280, 1024}, {480, 270}, {177, 123} };
232 float filter[] = { 0.05f, -0.15f, 0.6f, 0.6f, -0.15f, 0.05f };
233
234 srand(static_cast<unsigned int>(time(0)));
235
236 // Loop over some specific source and destination dimensions.
237 for (unsigned int i = 0; i < arraysize(source_sizes); ++i) {
238 unsigned int source_width = source_sizes[i][0];
239 unsigned int source_height = source_sizes[i][1];
240 for (unsigned int j = 0; j < arraysize(dest_sizes); ++j) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000241 unsigned int dest_width = dest_sizes[j][0];
242 unsigned int dest_height = dest_sizes[j][1];
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000243
244 // Preparing convolve coefficients.
245 ConvolutionFilter1D x_filter, y_filter;
246 for (unsigned int p = 0; p < dest_width; ++p) {
247 unsigned int offset = source_width * p / dest_width;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000248 EXPECT_LT(offset, source_width);
249 x_filter.AddFilter(offset, filter,
250 std::min<int>(arraysize(filter),
251 source_width - offset));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000252 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100253 x_filter.PaddingForSIMD();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000254 for (unsigned int p = 0; p < dest_height; ++p) {
255 unsigned int offset = source_height * p / dest_height;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000256 y_filter.AddFilter(offset, filter,
257 std::min<int>(arraysize(filter),
258 source_height - offset));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000259 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100260 y_filter.PaddingForSIMD();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000261
262 // Allocate input and output skia bitmap.
263 SkBitmap source, result_c, result_sse;
264 source.setConfig(SkBitmap::kARGB_8888_Config,
265 source_width, source_height);
266 source.allocPixels();
267 result_c.setConfig(SkBitmap::kARGB_8888_Config,
268 dest_width, dest_height);
269 result_c.allocPixels();
270 result_sse.setConfig(SkBitmap::kARGB_8888_Config,
271 dest_width, dest_height);
272 result_sse.allocPixels();
273
274 // Randomize source bitmap for testing.
275 unsigned char* src_ptr = static_cast<unsigned char*>(source.getPixels());
276 for (int y = 0; y < source.height(); y++) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000277 for (unsigned int x = 0; x < source.rowBytes(); x++)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000278 src_ptr[x] = rand() % 255;
279 src_ptr += source.rowBytes();
280 }
281
282 // Test both cases with different has_alpha.
283 for (int alpha = 0; alpha < 2; alpha++) {
284 // Convolve using C code.
285 base::TimeTicks resize_start;
286 base::TimeDelta delta_c, delta_sse;
287 unsigned char* r1 = static_cast<unsigned char*>(result_c.getPixels());
288 unsigned char* r2 = static_cast<unsigned char*>(result_sse.getPixels());
289
290 resize_start = base::TimeTicks::Now();
291 BGRAConvolve2D(static_cast<const uint8*>(source.getPixels()),
292 static_cast<int>(source.rowBytes()),
293 (alpha != 0), x_filter, y_filter,
294 static_cast<int>(result_c.rowBytes()), r1, false);
295 delta_c = base::TimeTicks::Now() - resize_start;
296
297 resize_start = base::TimeTicks::Now();
298 // Convolve using SSE2 code
299 BGRAConvolve2D(static_cast<const uint8*>(source.getPixels()),
300 static_cast<int>(source.rowBytes()),
301 (alpha != 0), x_filter, y_filter,
302 static_cast<int>(result_sse.rowBytes()), r2, true);
303 delta_sse = base::TimeTicks::Now() - resize_start;
304
305 // Unfortunately I could not enable the performance check now.
306 // Most bots use debug version, and there are great difference between
307 // the code generation for intrinsic, etc. In release version speed
308 // difference was 150%-200% depend on alpha channel presence;
309 // while in debug version speed difference was 96%-120%.
310 // TODO(jiesun): optimize further until we could enable this for
311 // debug version too.
312 // EXPECT_LE(delta_sse, delta_c);
313
314 int64 c_us = delta_c.InMicroseconds();
315 int64 sse_us = delta_sse.InMicroseconds();
316 VLOG(1) << "from:" << source_width << "x" << source_height
317 << " to:" << dest_width << "x" << dest_height
318 << (alpha ? " with alpha" : " w/o alpha");
319 VLOG(1) << "c:" << c_us << " sse:" << sse_us;
320 VLOG(1) << "ratio:" << static_cast<float>(c_us) / sse_us;
321
322 // Comparing result.
323 for (unsigned int i = 0; i < dest_height; i++) {
324 for (unsigned int x = 0; x < dest_width * 4; x++) { // RGBA always.
325 EXPECT_EQ(r1[x], r2[x]);
326 }
327 r1 += result_c.rowBytes();
328 r2 += result_sse.rowBytes();
329 }
330 }
331 }
332 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100333}
334
335TEST(Convolver, SeparableSingleConvolution) {
336 static const int kImgWidth = 1024;
337 static const int kImgHeight = 1024;
338 static const int kChannelCount = 3;
339 static const int kStrideSlack = 22;
340 ConvolutionFilter1D filter;
341 const float box[5] = { 0.2f, 0.2f, 0.2f, 0.2f, 0.2f };
342 filter.AddFilter(0, box, 5);
343
344 // Allocate a source image and set to 0.
345 const int src_row_stride = kImgWidth * kChannelCount + kStrideSlack;
346 int src_byte_count = src_row_stride * kImgHeight;
347 std::vector<unsigned char> input;
348 const int signal_x = kImgWidth / 2;
349 const int signal_y = kImgHeight / 2;
350 input.resize(src_byte_count, 0);
351 // The image has a single impulse pixel in channel 1, smack in the middle.
352 const int non_zero_pixel_index =
353 signal_y * src_row_stride + signal_x * kChannelCount + 1;
354 input[non_zero_pixel_index] = 255;
355
356 // Destination will be a single channel image with stide matching width.
357 const int dest_row_stride = kImgWidth;
358 const int dest_byte_count = dest_row_stride * kImgHeight;
359 std::vector<unsigned char> output;
360 output.resize(dest_byte_count);
361
362 // Apply convolution in X.
363 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount,
364 filter, SkISize::Make(kImgWidth, kImgHeight),
365 &output[0], dest_row_stride, 0, 1, false);
366 for (int x = signal_x - 2; x <= signal_x + 2; ++x)
367 EXPECT_GT(output[signal_y * dest_row_stride + x], 0);
368
369 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 3], 0);
370 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 3], 0);
371
372 // Apply convolution in Y.
373 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount,
374 filter, SkISize::Make(kImgWidth, kImgHeight),
375 &output[0], dest_row_stride, 0, 1, false);
376 for (int y = signal_y - 2; y <= signal_y + 2; ++y)
377 EXPECT_GT(output[y * dest_row_stride + signal_x], 0);
378
379 EXPECT_EQ(output[(signal_y - 3) * dest_row_stride + signal_x], 0);
380 EXPECT_EQ(output[(signal_y + 3) * dest_row_stride + signal_x], 0);
381
382 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 1], 0);
383 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 1], 0);
384
385 // The main point of calling this is to invoke the routine on input without
386 // padding.
387 std::vector<unsigned char> output2;
388 output2.resize(dest_byte_count);
389 SingleChannelConvolveX1D(&output[0], dest_row_stride, 0, 1,
390 filter, SkISize::Make(kImgWidth, kImgHeight),
391 &output2[0], dest_row_stride, 0, 1, false);
392 // This should be a result of 2D convolution.
393 for (int x = signal_x - 2; x <= signal_x + 2; ++x) {
394 for (int y = signal_y - 2; y <= signal_y + 2; ++y)
395 EXPECT_GT(output2[y * dest_row_stride + x], 0);
396 }
397 EXPECT_EQ(output2[0], 0);
398 EXPECT_EQ(output2[dest_row_stride - 1], 0);
399 EXPECT_EQ(output2[dest_byte_count - 1], 0);
400}
401
402TEST(Convolver, SeparableSingleConvolutionEdges) {
403 // The purpose of this test is to check if the implementation treats correctly
404 // edges of the image.
405 static const int kImgWidth = 600;
406 static const int kImgHeight = 800;
407 static const int kChannelCount = 3;
408 static const int kStrideSlack = 22;
409 static const int kChannel = 1;
410 ConvolutionFilter1D filter;
411 const float box[5] = { 0.2f, 0.2f, 0.2f, 0.2f, 0.2f };
412 filter.AddFilter(0, box, 5);
413
414 // Allocate a source image and set to 0.
415 int src_row_stride = kImgWidth * kChannelCount + kStrideSlack;
416 int src_byte_count = src_row_stride * kImgHeight;
417 std::vector<unsigned char> input(src_byte_count);
418
419 // Draw a frame around the image.
420 for (int i = 0; i < src_byte_count; ++i) {
421 int row = i / src_row_stride;
422 int col = i % src_row_stride / kChannelCount;
423 int channel = i % src_row_stride % kChannelCount;
424 if (channel != kChannel || col > kImgWidth) {
425 input[i] = 255;
426 } else if (row == 0 || col == 0 ||
427 col == kImgWidth - 1 || row == kImgHeight - 1) {
428 input[i] = 100;
429 } else if (row == 1 || col == 1 ||
430 col == kImgWidth - 2 || row == kImgHeight - 2) {
431 input[i] = 200;
432 } else {
433 input[i] = 0;
434 }
435 }
436
437 // Destination will be a single channel image with stide matching width.
438 int dest_row_stride = kImgWidth;
439 int dest_byte_count = dest_row_stride * kImgHeight;
440 std::vector<unsigned char> output;
441 output.resize(dest_byte_count);
442
443 // Apply convolution in X.
444 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount,
445 filter, SkISize::Make(kImgWidth, kImgHeight),
446 &output[0], dest_row_stride, 0, 1, false);
447
448 // Sadly, comparison is not as simple as retaining all values.
449 int invalid_values = 0;
450 const unsigned char first_value = output[0];
451 EXPECT_NEAR(first_value, 100, 1);
452 for (int i = 0; i < dest_row_stride; ++i) {
453 if (output[i] != first_value)
454 ++invalid_values;
455 }
456 EXPECT_EQ(0, invalid_values);
457
458 int test_row = 22;
459 EXPECT_NEAR(output[test_row * dest_row_stride], 100, 1);
460 EXPECT_NEAR(output[test_row * dest_row_stride + 1], 80, 1);
461 EXPECT_NEAR(output[test_row * dest_row_stride + 2], 60, 1);
462 EXPECT_NEAR(output[test_row * dest_row_stride + 3], 40, 1);
463 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 1], 100, 1);
464 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 2], 80, 1);
465 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 3], 60, 1);
466 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 4], 40, 1);
467
468 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount,
469 filter, SkISize::Make(kImgWidth, kImgHeight),
470 &output[0], dest_row_stride, 0, 1, false);
471
472 int test_column = 42;
473 EXPECT_NEAR(output[test_column], 100, 1);
474 EXPECT_NEAR(output[test_column + dest_row_stride], 80, 1);
475 EXPECT_NEAR(output[test_column + dest_row_stride * 2], 60, 1);
476 EXPECT_NEAR(output[test_column + dest_row_stride * 3], 40, 1);
477
478 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 1)], 100, 1);
479 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 2)], 80, 1);
480 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 3)], 60, 1);
481 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 4)], 40, 1);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000482}
483
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100484TEST(Convolver, SetUpGaussianConvolutionFilter) {
485 ConvolutionFilter1D smoothing_filter;
486 ConvolutionFilter1D gradient_filter;
487 SetUpGaussianConvolutionKernel(&smoothing_filter, 4.5f, false);
488 SetUpGaussianConvolutionKernel(&gradient_filter, 3.0f, true);
489
490 int specified_filter_length;
491 int filter_offset;
492 int filter_length;
493
494 const ConvolutionFilter1D::Fixed* smoothing_kernel =
495 smoothing_filter.GetSingleFilter(
496 &specified_filter_length, &filter_offset, &filter_length);
497 EXPECT_TRUE(smoothing_kernel);
498 std::vector<float> fp_smoothing_kernel(filter_length);
499 std::transform(smoothing_kernel,
500 smoothing_kernel + filter_length,
501 fp_smoothing_kernel.begin(),
502 ConvolutionFilter1D::FixedToFloat);
503 // Should sum-up to 1 (nearly), and all values whould be in ]0, 1[.
504 EXPECT_NEAR(std::accumulate(
505 fp_smoothing_kernel.begin(), fp_smoothing_kernel.end(), 0.0f),
506 1.0f, 0.01f);
507 EXPECT_GT(*std::min_element(fp_smoothing_kernel.begin(),
508 fp_smoothing_kernel.end()), 0.0f);
509 EXPECT_LT(*std::max_element(fp_smoothing_kernel.begin(),
510 fp_smoothing_kernel.end()), 1.0f);
511
512 const ConvolutionFilter1D::Fixed* gradient_kernel =
513 gradient_filter.GetSingleFilter(
514 &specified_filter_length, &filter_offset, &filter_length);
515 EXPECT_TRUE(gradient_kernel);
516 std::vector<float> fp_gradient_kernel(filter_length);
517 std::transform(gradient_kernel,
518 gradient_kernel + filter_length,
519 fp_gradient_kernel.begin(),
520 ConvolutionFilter1D::FixedToFloat);
521 // Should sum-up to 0, and all values whould be in ]-1.5, 1.5[.
522 EXPECT_NEAR(std::accumulate(
523 fp_gradient_kernel.begin(), fp_gradient_kernel.end(), 0.0f),
524 0.0f, 0.01f);
525 EXPECT_GT(*std::min_element(fp_gradient_kernel.begin(),
526 fp_gradient_kernel.end()), -1.5f);
527 EXPECT_LT(*std::min_element(fp_gradient_kernel.begin(),
528 fp_gradient_kernel.end()), 0.0f);
529 EXPECT_LT(*std::max_element(fp_gradient_kernel.begin(),
530 fp_gradient_kernel.end()), 1.5f);
531 EXPECT_GT(*std::max_element(fp_gradient_kernel.begin(),
532 fp_gradient_kernel.end()), 0.0f);
533}
534
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000535} // namespace skia