OLD | NEW |
(Empty) | |
| 1 // 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> |
| 7 #include <algorithm> |
| 8 #include <numeric> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/time/time.h" |
| 14 #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 |
| 21 namespace skia { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Fills the given filter with impulse functions for the range 0->num_entries. |
| 26 void 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. |
| 34 void 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. |
| 54 void 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. |
| 64 TEST(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. |
| 89 TEST(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. |
| 139 TEST(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, 1.0f, 0.0f, 0.0f, |
| 192 0.0f, 0.0f, 1.0f, 0.0f}; |
| 193 filter.AddFilter(55, factors5, arraysize(factors5)); |
| 194 ASSERT_EQ(6, filter.max_filter()); |
| 195 ASSERT_EQ(5, filter.num_values()); |
| 196 |
| 197 values = filter.FilterForValue(4, &filter_offset, &filter_length); |
| 198 ASSERT_TRUE(values != NULL); |
| 199 ASSERT_EQ(57, filter_offset); // 55 plus 2 leading zeroes |
| 200 ASSERT_EQ(6, filter_length); // 9 - (2 leading + 1 trailing) zeroes |
| 201 |
| 202 // All-zero filters after the first one also work |
| 203 static const float factors6[] = {0.0f}; |
| 204 filter.AddFilter(66, factors6, arraysize(factors6)); |
| 205 ASSERT_EQ(6, filter.max_filter()); |
| 206 ASSERT_EQ(6, filter.num_values()); |
| 207 |
| 208 values = filter.FilterForValue(5, &filter_offset, &filter_length); |
| 209 ASSERT_TRUE(values == NULL); // filter_length == 0 => values is NULL |
| 210 ASSERT_EQ(66, filter_offset); // value passed in |
| 211 ASSERT_EQ(0, filter_length); |
| 212 } |
| 213 |
| 214 void VerifySIMD(unsigned int source_width, |
| 215 unsigned int source_height, |
| 216 unsigned int dest_width, |
| 217 unsigned int dest_height) { |
| 218 float filter[] = {0.05f, -0.15f, 0.6f, 0.6f, -0.15f, 0.05f}; |
| 219 // Preparing convolve coefficients. |
| 220 ConvolutionFilter1D x_filter, y_filter; |
| 221 for (unsigned int p = 0; p < dest_width; ++p) { |
| 222 unsigned int offset = source_width * p / dest_width; |
| 223 EXPECT_LT(offset, source_width); |
| 224 x_filter.AddFilter(offset, filter, |
| 225 std::min<int>(arraysize(filter), source_width - offset)); |
| 226 } |
| 227 x_filter.PaddingForSIMD(); |
| 228 for (unsigned int p = 0; p < dest_height; ++p) { |
| 229 unsigned int offset = source_height * p / dest_height; |
| 230 y_filter.AddFilter(offset, filter, std::min<int>(arraysize(filter), |
| 231 source_height - offset)); |
| 232 } |
| 233 y_filter.PaddingForSIMD(); |
| 234 |
| 235 // Allocate input and output skia bitmap. |
| 236 SkBitmap source, result_c, result_sse; |
| 237 source.allocN32Pixels(source_width, source_height); |
| 238 result_c.allocN32Pixels(dest_width, dest_height); |
| 239 result_sse.allocN32Pixels(dest_width, dest_height); |
| 240 |
| 241 // Randomize source bitmap for testing. |
| 242 unsigned char* src_ptr = static_cast<unsigned char*>(source.getPixels()); |
| 243 for (int y = 0; y < source.height(); y++) { |
| 244 for (unsigned int x = 0; x < source.rowBytes(); x++) |
| 245 src_ptr[x] = rand() % 255; |
| 246 src_ptr += source.rowBytes(); |
| 247 } |
| 248 |
| 249 // Test both cases with different has_alpha. |
| 250 for (int alpha = 0; alpha < 2; alpha++) { |
| 251 // Convolve using C code. |
| 252 base::TimeTicks resize_start; |
| 253 base::TimeDelta delta_c, delta_sse; |
| 254 unsigned char* r1 = static_cast<unsigned char*>(result_c.getPixels()); |
| 255 unsigned char* r2 = static_cast<unsigned char*>(result_sse.getPixels()); |
| 256 |
| 257 resize_start = base::TimeTicks::Now(); |
| 258 BGRAConvolve2D(static_cast<const uint8*>(source.getPixels()), |
| 259 static_cast<int>(source.rowBytes()), (alpha != 0), x_filter, |
| 260 y_filter, static_cast<int>(result_c.rowBytes()), r1, false); |
| 261 delta_c = base::TimeTicks::Now() - resize_start; |
| 262 |
| 263 resize_start = base::TimeTicks::Now(); |
| 264 // Convolve using SSE2 code |
| 265 BGRAConvolve2D(static_cast<const uint8*>(source.getPixels()), |
| 266 static_cast<int>(source.rowBytes()), (alpha != 0), x_filter, |
| 267 y_filter, static_cast<int>(result_sse.rowBytes()), r2, true); |
| 268 delta_sse = base::TimeTicks::Now() - resize_start; |
| 269 |
| 270 // Unfortunately I could not enable the performance check now. |
| 271 // Most bots use debug version, and there are great difference between |
| 272 // the code generation for intrinsic, etc. In release version speed |
| 273 // difference was 150%-200% depend on alpha channel presence; |
| 274 // while in debug version speed difference was 96%-120%. |
| 275 // TODO(jiesun): optimize further until we could enable this for |
| 276 // debug version too. |
| 277 // EXPECT_LE(delta_sse, delta_c); |
| 278 |
| 279 int64 c_us = delta_c.InMicroseconds(); |
| 280 int64 sse_us = delta_sse.InMicroseconds(); |
| 281 VLOG(1) << "from:" << source_width << "x" << source_height |
| 282 << " to:" << dest_width << "x" << dest_height |
| 283 << (alpha ? " with alpha" : " w/o alpha"); |
| 284 VLOG(1) << "c:" << c_us << " sse:" << sse_us; |
| 285 VLOG(1) << "ratio:" << static_cast<float>(c_us) / sse_us; |
| 286 |
| 287 // Comparing result. |
| 288 for (unsigned int i = 0; i < dest_height; i++) { |
| 289 EXPECT_FALSE(memcmp(r1, r2, dest_width * 4)); // RGBA always |
| 290 r1 += result_c.rowBytes(); |
| 291 r2 += result_sse.rowBytes(); |
| 292 } |
| 293 } |
| 294 } |
| 295 |
| 296 TEST(Convolver, VerifySIMDEdgeCases) { |
| 297 srand(static_cast<unsigned int>(time(0))); |
| 298 // Loop over all possible (small) image sizes |
| 299 for (unsigned int width = 1; width < 20; width++) { |
| 300 for (unsigned int height = 1; height < 20; height++) { |
| 301 VerifySIMD(width, height, 8, 8); |
| 302 VerifySIMD(8, 8, width, height); |
| 303 } |
| 304 } |
| 305 } |
| 306 |
| 307 // Verify that lage upscales/downscales produce the same result |
| 308 // with and without SIMD. |
| 309 TEST(Convolver, VerifySIMDPrecision) { |
| 310 int source_sizes[][2] = {{1920, 1080}, {1377, 523}, {325, 241}}; |
| 311 int dest_sizes[][2] = {{1280, 1024}, {177, 123}}; |
| 312 |
| 313 srand(static_cast<unsigned int>(time(0))); |
| 314 |
| 315 // Loop over some specific source and destination dimensions. |
| 316 for (unsigned int i = 0; i < arraysize(source_sizes); ++i) { |
| 317 unsigned int source_width = source_sizes[i][0]; |
| 318 unsigned int source_height = source_sizes[i][1]; |
| 319 for (unsigned int j = 0; j < arraysize(dest_sizes); ++j) { |
| 320 unsigned int dest_width = dest_sizes[j][0]; |
| 321 unsigned int dest_height = dest_sizes[j][1]; |
| 322 VerifySIMD(source_width, source_height, dest_width, dest_height); |
| 323 } |
| 324 } |
| 325 } |
| 326 |
| 327 TEST(Convolver, SeparableSingleConvolution) { |
| 328 static const int kImgWidth = 1024; |
| 329 static const int kImgHeight = 1024; |
| 330 static const int kChannelCount = 3; |
| 331 static const int kStrideSlack = 22; |
| 332 ConvolutionFilter1D filter; |
| 333 const float box[5] = {0.2f, 0.2f, 0.2f, 0.2f, 0.2f}; |
| 334 filter.AddFilter(0, box, 5); |
| 335 |
| 336 // Allocate a source image and set to 0. |
| 337 const int src_row_stride = kImgWidth * kChannelCount + kStrideSlack; |
| 338 int src_byte_count = src_row_stride * kImgHeight; |
| 339 std::vector<unsigned char> input; |
| 340 const int signal_x = kImgWidth / 2; |
| 341 const int signal_y = kImgHeight / 2; |
| 342 input.resize(src_byte_count, 0); |
| 343 // The image has a single impulse pixel in channel 1, smack in the middle. |
| 344 const int non_zero_pixel_index = |
| 345 signal_y * src_row_stride + signal_x * kChannelCount + 1; |
| 346 input[non_zero_pixel_index] = 255; |
| 347 |
| 348 // Destination will be a single channel image with stide matching width. |
| 349 const int dest_row_stride = kImgWidth; |
| 350 const int dest_byte_count = dest_row_stride * kImgHeight; |
| 351 std::vector<unsigned char> output; |
| 352 output.resize(dest_byte_count); |
| 353 |
| 354 // Apply convolution in X. |
| 355 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, filter, |
| 356 SkISize::Make(kImgWidth, kImgHeight), &output[0], |
| 357 dest_row_stride, 0, 1, false); |
| 358 for (int x = signal_x - 2; x <= signal_x + 2; ++x) |
| 359 EXPECT_GT(output[signal_y * dest_row_stride + x], 0); |
| 360 |
| 361 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 3], 0); |
| 362 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 3], 0); |
| 363 |
| 364 // Apply convolution in Y. |
| 365 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, filter, |
| 366 SkISize::Make(kImgWidth, kImgHeight), &output[0], |
| 367 dest_row_stride, 0, 1, false); |
| 368 for (int y = signal_y - 2; y <= signal_y + 2; ++y) |
| 369 EXPECT_GT(output[y * dest_row_stride + signal_x], 0); |
| 370 |
| 371 EXPECT_EQ(output[(signal_y - 3) * dest_row_stride + signal_x], 0); |
| 372 EXPECT_EQ(output[(signal_y + 3) * dest_row_stride + signal_x], 0); |
| 373 |
| 374 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 1], 0); |
| 375 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 1], 0); |
| 376 |
| 377 // The main point of calling this is to invoke the routine on input without |
| 378 // padding. |
| 379 std::vector<unsigned char> output2; |
| 380 output2.resize(dest_byte_count); |
| 381 SingleChannelConvolveX1D(&output[0], dest_row_stride, 0, 1, filter, |
| 382 SkISize::Make(kImgWidth, kImgHeight), &output2[0], |
| 383 dest_row_stride, 0, 1, false); |
| 384 // This should be a result of 2D convolution. |
| 385 for (int x = signal_x - 2; x <= signal_x + 2; ++x) { |
| 386 for (int y = signal_y - 2; y <= signal_y + 2; ++y) |
| 387 EXPECT_GT(output2[y * dest_row_stride + x], 0); |
| 388 } |
| 389 EXPECT_EQ(output2[0], 0); |
| 390 EXPECT_EQ(output2[dest_row_stride - 1], 0); |
| 391 EXPECT_EQ(output2[dest_byte_count - 1], 0); |
| 392 } |
| 393 |
| 394 TEST(Convolver, SeparableSingleConvolutionEdges) { |
| 395 // The purpose of this test is to check if the implementation treats correctly |
| 396 // edges of the image. |
| 397 static const int kImgWidth = 600; |
| 398 static const int kImgHeight = 800; |
| 399 static const int kChannelCount = 3; |
| 400 static const int kStrideSlack = 22; |
| 401 static const int kChannel = 1; |
| 402 ConvolutionFilter1D filter; |
| 403 const float box[5] = {0.2f, 0.2f, 0.2f, 0.2f, 0.2f}; |
| 404 filter.AddFilter(0, box, 5); |
| 405 |
| 406 // Allocate a source image and set to 0. |
| 407 int src_row_stride = kImgWidth * kChannelCount + kStrideSlack; |
| 408 int src_byte_count = src_row_stride * kImgHeight; |
| 409 std::vector<unsigned char> input(src_byte_count); |
| 410 |
| 411 // Draw a frame around the image. |
| 412 for (int i = 0; i < src_byte_count; ++i) { |
| 413 int row = i / src_row_stride; |
| 414 int col = i % src_row_stride / kChannelCount; |
| 415 int channel = i % src_row_stride % kChannelCount; |
| 416 if (channel != kChannel || col > kImgWidth) { |
| 417 input[i] = 255; |
| 418 } else if (row == 0 || col == 0 || col == kImgWidth - 1 || |
| 419 row == kImgHeight - 1) { |
| 420 input[i] = 100; |
| 421 } else if (row == 1 || col == 1 || col == kImgWidth - 2 || |
| 422 row == kImgHeight - 2) { |
| 423 input[i] = 200; |
| 424 } else { |
| 425 input[i] = 0; |
| 426 } |
| 427 } |
| 428 |
| 429 // Destination will be a single channel image with stide matching width. |
| 430 int dest_row_stride = kImgWidth; |
| 431 int dest_byte_count = dest_row_stride * kImgHeight; |
| 432 std::vector<unsigned char> output; |
| 433 output.resize(dest_byte_count); |
| 434 |
| 435 // Apply convolution in X. |
| 436 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, filter, |
| 437 SkISize::Make(kImgWidth, kImgHeight), &output[0], |
| 438 dest_row_stride, 0, 1, false); |
| 439 |
| 440 // Sadly, comparison is not as simple as retaining all values. |
| 441 int invalid_values = 0; |
| 442 const unsigned char first_value = output[0]; |
| 443 EXPECT_NEAR(first_value, 100, 1); |
| 444 for (int i = 0; i < dest_row_stride; ++i) { |
| 445 if (output[i] != first_value) |
| 446 ++invalid_values; |
| 447 } |
| 448 EXPECT_EQ(0, invalid_values); |
| 449 |
| 450 int test_row = 22; |
| 451 EXPECT_NEAR(output[test_row * dest_row_stride], 100, 1); |
| 452 EXPECT_NEAR(output[test_row * dest_row_stride + 1], 80, 1); |
| 453 EXPECT_NEAR(output[test_row * dest_row_stride + 2], 60, 1); |
| 454 EXPECT_NEAR(output[test_row * dest_row_stride + 3], 40, 1); |
| 455 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 1], 100, 1); |
| 456 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 2], 80, 1); |
| 457 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 3], 60, 1); |
| 458 EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 4], 40, 1); |
| 459 |
| 460 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, filter, |
| 461 SkISize::Make(kImgWidth, kImgHeight), &output[0], |
| 462 dest_row_stride, 0, 1, false); |
| 463 |
| 464 int test_column = 42; |
| 465 EXPECT_NEAR(output[test_column], 100, 1); |
| 466 EXPECT_NEAR(output[test_column + dest_row_stride], 80, 1); |
| 467 EXPECT_NEAR(output[test_column + dest_row_stride * 2], 60, 1); |
| 468 EXPECT_NEAR(output[test_column + dest_row_stride * 3], 40, 1); |
| 469 |
| 470 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 1)], 100, 1); |
| 471 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 2)], 80, 1); |
| 472 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 3)], 60, 1); |
| 473 EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 4)], 40, 1); |
| 474 } |
| 475 |
| 476 TEST(Convolver, SetUpGaussianConvolutionFilter) { |
| 477 ConvolutionFilter1D smoothing_filter; |
| 478 ConvolutionFilter1D gradient_filter; |
| 479 SetUpGaussianConvolutionKernel(&smoothing_filter, 4.5f, false); |
| 480 SetUpGaussianConvolutionKernel(&gradient_filter, 3.0f, true); |
| 481 |
| 482 int specified_filter_length; |
| 483 int filter_offset; |
| 484 int filter_length; |
| 485 |
| 486 const ConvolutionFilter1D::Fixed* smoothing_kernel = |
| 487 smoothing_filter.GetSingleFilter(&specified_filter_length, &filter_offset, |
| 488 &filter_length); |
| 489 EXPECT_TRUE(smoothing_kernel); |
| 490 std::vector<float> fp_smoothing_kernel(filter_length); |
| 491 std::transform(smoothing_kernel, smoothing_kernel + filter_length, |
| 492 fp_smoothing_kernel.begin(), |
| 493 ConvolutionFilter1D::FixedToFloat); |
| 494 // Should sum-up to 1 (nearly), and all values whould be in ]0, 1[. |
| 495 EXPECT_NEAR(std::accumulate(fp_smoothing_kernel.begin(), |
| 496 fp_smoothing_kernel.end(), 0.0f), |
| 497 1.0f, 0.01f); |
| 498 EXPECT_GT( |
| 499 *std::min_element(fp_smoothing_kernel.begin(), fp_smoothing_kernel.end()), |
| 500 0.0f); |
| 501 EXPECT_LT( |
| 502 *std::max_element(fp_smoothing_kernel.begin(), fp_smoothing_kernel.end()), |
| 503 1.0f); |
| 504 |
| 505 const ConvolutionFilter1D::Fixed* gradient_kernel = |
| 506 gradient_filter.GetSingleFilter(&specified_filter_length, &filter_offset, |
| 507 &filter_length); |
| 508 EXPECT_TRUE(gradient_kernel); |
| 509 std::vector<float> fp_gradient_kernel(filter_length); |
| 510 std::transform(gradient_kernel, gradient_kernel + filter_length, |
| 511 fp_gradient_kernel.begin(), ConvolutionFilter1D::FixedToFloat); |
| 512 // Should sum-up to 0, and all values whould be in ]-1.5, 1.5[. |
| 513 EXPECT_NEAR(std::accumulate(fp_gradient_kernel.begin(), |
| 514 fp_gradient_kernel.end(), 0.0f), |
| 515 0.0f, 0.01f); |
| 516 EXPECT_GT( |
| 517 *std::min_element(fp_gradient_kernel.begin(), fp_gradient_kernel.end()), |
| 518 -1.5f); |
| 519 EXPECT_LT( |
| 520 *std::min_element(fp_gradient_kernel.begin(), fp_gradient_kernel.end()), |
| 521 0.0f); |
| 522 EXPECT_LT( |
| 523 *std::max_element(fp_gradient_kernel.begin(), fp_gradient_kernel.end()), |
| 524 1.5f); |
| 525 EXPECT_GT( |
| 526 *std::max_element(fp_gradient_kernel.begin(), fp_gradient_kernel.end()), |
| 527 0.0f); |
| 528 } |
| 529 |
| 530 } // namespace skia |
OLD | NEW |