Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 #include <time.h> | 6 #include <time.h> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 EXPECT_EQ(r1[x], r2[x]); | 317 EXPECT_EQ(r1[x], r2[x]); |
| 318 } | 318 } |
| 319 r1 += result_c.rowBytes(); | 319 r1 += result_c.rowBytes(); |
| 320 r2 += result_sse.rowBytes(); | 320 r2 += result_sse.rowBytes(); |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 } | 323 } |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 TEST(Convolver, SeparableSingleConvolution) { | |
| 328 static const int kSize = 1024; | |
| 329 static const int kChannelCount = 3; | |
| 330 static const int kStrideSlack = 22; | |
| 331 ConvolutionFilter1D filter; | |
| 332 const float box[5] = { 0.2, 0.2, 0.2, 0.2, 0.2 }; | |
| 333 filter.AddFilter(0, box, 5); | |
| 334 | |
| 335 // Allocate a source image and set to 0. | |
| 336 int img_width = kSize; | |
|
Alexei Svitkine (slow)
2013/04/11 18:25:37
Do you need all these temp variables?
e.g. can yo
motek.
2013/04/12 10:50:59
Nah, I don't need them. Copied as such from a rout
| |
| 337 int img_height = kSize; | |
| 338 int src_row_stride = img_width * kChannelCount + kStrideSlack; | |
| 339 int src_byte_count = src_row_stride * img_height; | |
| 340 std::vector<unsigned char> input; | |
| 341 int signal_x = img_width / 2; | |
| 342 int signal_y = img_height / 2; | |
| 343 input.resize(src_byte_count, 0); | |
| 344 // The image has a single impulse pixel in channel 1, smack in the middle. | |
| 345 int non_zero_pixel_index = | |
| 346 signal_y * src_row_stride + signal_x * kChannelCount + 1; | |
| 347 input[non_zero_pixel_index] = 255; | |
| 348 | |
| 349 // Destination will be a single channel image with stide matching width. | |
| 350 int dest_row_stride = img_width; | |
| 351 int dest_byte_count = dest_row_stride * img_height; | |
| 352 std::vector<unsigned char> output; | |
| 353 output.resize(dest_byte_count); | |
| 354 | |
| 355 // Apply convolution in X. | |
| 356 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, | |
| 357 filter, SkISize::Make(img_width, img_height), | |
| 358 &output[0], dest_row_stride, 0, 1, false); | |
| 359 for (int x = signal_x - 2; x <= signal_x + 2; ++x) { | |
|
Alexei Svitkine (slow)
2013/04/11 18:25:37
Nit: No need for {}'s.
motek.
2013/04/12 10:50:59
Done.
| |
| 360 EXPECT_GT(output[signal_y * dest_row_stride + x], 0); | |
| 361 } | |
| 362 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 3], 0); | |
| 363 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 3], 0); | |
| 364 | |
| 365 // Apply convolution in Y. | |
| 366 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, | |
| 367 filter, SkISize::Make(img_width, img_height), | |
| 368 &output[0], dest_row_stride, 0, 1, false); | |
| 369 for (int y = signal_y - 2; y <= signal_y + 2; ++y) { | |
|
Alexei Svitkine (slow)
2013/04/11 18:25:37
Nit: No need for {}'s.
motek.
2013/04/12 10:50:59
Done.
| |
| 370 EXPECT_GT(output[y * dest_row_stride + signal_x], 0); | |
| 371 } | |
| 372 | |
| 373 EXPECT_EQ(output[(signal_y - 3) * dest_row_stride + signal_x], 0); | |
| 374 EXPECT_EQ(output[(signal_y + 3) * dest_row_stride + signal_x], 0); | |
| 375 | |
| 376 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 1], 0); | |
| 377 EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 1], 0); | |
| 378 | |
| 379 // The main point of calling this is to invoke the routine on input without | |
| 380 // padding. | |
| 381 std::vector<unsigned char> output2; | |
| 382 output2.resize(dest_byte_count); | |
| 383 SingleChannelConvolveX1D(&output[0], dest_row_stride, 0, 1, | |
| 384 filter, SkISize::Make(img_width, img_height), | |
| 385 &output2[0], dest_row_stride, 0, 1, false); | |
| 386 // This should be a result of 2D convolution. | |
| 387 for (int x = signal_x - 2; x <= signal_x + 2; ++x) { | |
| 388 for (int y = signal_y - 2; y <= signal_y + 2; ++y) { | |
| 389 EXPECT_GT(output2[y * dest_row_stride + x], 0); | |
| 390 } | |
| 391 } | |
| 392 EXPECT_EQ(output2[0], 0); | |
| 393 EXPECT_EQ(output2[dest_row_stride - 1], 0); | |
| 394 EXPECT_EQ(output2[dest_byte_count - 1], 0); | |
| 395 } | |
| 396 | |
| 397 TEST(Convolver, SeparableSingleConvolutionEdges) { | |
| 398 // The purpose of this test is to check if the implementation treats correctly | |
| 399 // edges of the image. | |
| 400 static const int kImgWidth = 600; | |
| 401 static const int kImgHeight = 800; | |
| 402 static const int kChannelCount = 3; | |
| 403 static const int kStrideSlack = 22; | |
| 404 static const int kChannel = 1; | |
| 405 ConvolutionFilter1D filter; | |
| 406 const float box[5] = { 0.2, 0.2, 0.2, 0.2, 0.2 }; | |
| 407 filter.AddFilter(0, box, 5); | |
| 408 | |
| 409 // Allocate a source image and set to 0. | |
| 410 int src_row_stride = kImgWidth * kChannelCount + kStrideSlack; | |
| 411 int src_byte_count = src_row_stride * kImgHeight; | |
| 412 std::vector<unsigned char> input(src_byte_count); | |
| 413 | |
| 414 // Draw a frame around the image. | |
| 415 for (int i = 0; i < src_byte_count; ++i) { | |
| 416 int row = i / src_row_stride; | |
| 417 int col = i % src_row_stride / kChannelCount; | |
| 418 int channel = i % src_row_stride % kChannelCount; | |
| 419 if (channel != kChannel || col > kImgWidth) { | |
| 420 input[i] = 255; | |
| 421 } else if (row == 0 || col == 0 || | |
| 422 col == kImgWidth - 1 || row == kImgHeight - 1) { | |
| 423 input[i] = 100; | |
| 424 } else if (row == 1 || col == 1 || | |
| 425 col == kImgWidth - 2 || row == kImgHeight - 2) { | |
| 426 input[i] = 200; | |
| 427 } else { | |
| 428 input[i] = 0; | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 // Destination will be a single channel image with stide matching width. | |
| 433 int dest_row_stride = kImgWidth; | |
| 434 int dest_byte_count = dest_row_stride * kImgHeight; | |
| 435 std::vector<unsigned char> output; | |
| 436 output.resize(dest_byte_count); | |
| 437 | |
| 438 // Apply convolution in X. | |
| 439 SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, | |
| 440 filter, SkISize::Make(kImgWidth, kImgHeight), | |
| 441 &output[0], dest_row_stride, 0, 1, false); | |
| 442 | |
| 443 // Sadly, comparison is not as simple as retaining all values. | |
| 444 int invalid_values = 0; | |
| 445 const unsigned char first_value = output[0]; | |
| 446 EXPECT_TRUE(std::abs(100 - first_value) <= 1); | |
| 447 for (int i = 0; i < dest_row_stride; ++i) { | |
| 448 if (output[i] != first_value) | |
| 449 ++invalid_values; | |
| 450 } | |
| 451 EXPECT_EQ(0, invalid_values); | |
| 452 | |
| 453 int test_row = 22; | |
| 454 EXPECT_TRUE(std::abs(output[test_row * dest_row_stride] - 100) <= 1); | |
| 455 EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 1] - 80) <= 1); | |
| 456 EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 2] - 60) <= 1); | |
| 457 EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 3] - 40) <= 1); | |
| 458 EXPECT_TRUE( | |
|
Alexei Svitkine (slow)
2013/04/11 18:25:37
Can you use EXPECT_NEAR() for these?
motek.
2013/04/12 10:50:59
Done.
| |
| 459 std::abs(output[(test_row + 1) * dest_row_stride - 1] - 100) <= 1); | |
| 460 EXPECT_TRUE( | |
| 461 std::abs(output[(test_row + 1) * dest_row_stride - 2] - 80) <= 1); | |
| 462 EXPECT_TRUE( | |
| 463 std::abs(output[(test_row + 1) * dest_row_stride - 3] - 60) <= 1); | |
| 464 EXPECT_TRUE( | |
| 465 std::abs(output[(test_row + 1) * dest_row_stride - 4] - 40) <= 1); | |
| 466 | |
| 467 SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, | |
| 468 filter, SkISize::Make(kImgWidth, kImgHeight), | |
| 469 &output[0], dest_row_stride, 0, 1, false); | |
| 470 | |
| 471 int test_column = 42; | |
| 472 EXPECT_TRUE(std::abs(output[test_column] - 100) <= 1); | |
| 473 EXPECT_TRUE(std::abs(output[test_column + dest_row_stride] - 80) <= 1); | |
| 474 EXPECT_TRUE(std::abs(output[test_column + dest_row_stride * 2] - 60) <= 1); | |
| 475 EXPECT_TRUE(std::abs(output[test_column + dest_row_stride * 3] - 40) <= 1); | |
| 476 | |
| 477 EXPECT_TRUE(std::abs( | |
| 478 output[test_column + dest_row_stride * (kImgHeight - 1)] - 100) <= 1); | |
| 479 EXPECT_TRUE(std::abs( | |
| 480 output[test_column + dest_row_stride * (kImgHeight - 2)] - 80) <= 1); | |
| 481 EXPECT_TRUE(std::abs( | |
| 482 output[test_column + dest_row_stride * (kImgHeight - 3)] - 60) <= 1); | |
| 483 EXPECT_TRUE(std::abs( | |
| 484 output[test_column + dest_row_stride * (kImgHeight - 4)] - 40) <= 1); | |
| 485 } | |
| 486 | |
| 327 } // namespace skia | 487 } // namespace skia |
| OLD | NEW |