Index: skia/ext/convolver_unittest.cc |
diff --git a/skia/ext/convolver_unittest.cc b/skia/ext/convolver_unittest.cc |
index 377ed8ed3051c20f491ad0336d8bed80a3041b17..b6835a85c085a79ec46b37b26db21cdca22482ed 100644 |
--- a/skia/ext/convolver_unittest.cc |
+++ b/skia/ext/convolver_unittest.cc |
@@ -324,4 +324,153 @@ TEST(Convolver, SIMDVerification) { |
} |
} |
+TEST(Convolver, SeparableSingleConvolution) { |
+ static const int kImgWidth = 1024; |
+ static const int kImgHeight = 1024; |
+ static const int kChannelCount = 3; |
+ static const int kStrideSlack = 22; |
+ ConvolutionFilter1D filter; |
+ const float box[5] = { 0.2, 0.2, 0.2, 0.2, 0.2 }; |
+ filter.AddFilter(0, box, 5); |
+ |
+ // Allocate a source image and set to 0. |
+ const int src_row_stride = kImgWidth * kChannelCount + kStrideSlack; |
+ int src_byte_count = src_row_stride * kImgHeight; |
+ std::vector<unsigned char> input; |
+ const int signal_x = kImgWidth / 2; |
+ const int signal_y = kImgHeight / 2; |
+ input.resize(src_byte_count, 0); |
+ // The image has a single impulse pixel in channel 1, smack in the middle. |
+ const int non_zero_pixel_index = |
+ signal_y * src_row_stride + signal_x * kChannelCount + 1; |
+ input[non_zero_pixel_index] = 255; |
+ |
+ // Destination will be a single channel image with stide matching width. |
+ const int dest_row_stride = kImgWidth; |
+ const int dest_byte_count = dest_row_stride * kImgHeight; |
+ std::vector<unsigned char> output; |
+ output.resize(dest_byte_count); |
+ |
+ // Apply convolution in X. |
+ SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, |
+ filter, SkISize::Make(kImgWidth, kImgHeight), |
+ &output[0], dest_row_stride, 0, 1, false); |
+ for (int x = signal_x - 2; x <= signal_x + 2; ++x) |
+ EXPECT_GT(output[signal_y * dest_row_stride + x], 0); |
+ |
+ EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 3], 0); |
+ EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 3], 0); |
+ |
+ // Apply convolution in Y. |
+ SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, |
+ filter, SkISize::Make(kImgWidth, kImgHeight), |
+ &output[0], dest_row_stride, 0, 1, false); |
+ for (int y = signal_y - 2; y <= signal_y + 2; ++y) |
+ EXPECT_GT(output[y * dest_row_stride + signal_x], 0); |
+ |
+ EXPECT_EQ(output[(signal_y - 3) * dest_row_stride + signal_x], 0); |
+ EXPECT_EQ(output[(signal_y + 3) * dest_row_stride + signal_x], 0); |
+ |
+ EXPECT_EQ(output[signal_y * dest_row_stride + signal_x - 1], 0); |
+ EXPECT_EQ(output[signal_y * dest_row_stride + signal_x + 1], 0); |
+ |
+ // The main point of calling this is to invoke the routine on input without |
+ // padding. |
+ std::vector<unsigned char> output2; |
+ output2.resize(dest_byte_count); |
+ SingleChannelConvolveX1D(&output[0], dest_row_stride, 0, 1, |
+ filter, SkISize::Make(kImgWidth, kImgHeight), |
+ &output2[0], dest_row_stride, 0, 1, false); |
+ // This should be a result of 2D convolution. |
+ for (int x = signal_x - 2; x <= signal_x + 2; ++x) { |
+ for (int y = signal_y - 2; y <= signal_y + 2; ++y) |
+ EXPECT_GT(output2[y * dest_row_stride + x], 0); |
+ } |
+ EXPECT_EQ(output2[0], 0); |
+ EXPECT_EQ(output2[dest_row_stride - 1], 0); |
+ EXPECT_EQ(output2[dest_byte_count - 1], 0); |
+} |
+ |
+TEST(Convolver, SeparableSingleConvolutionEdges) { |
+ // The purpose of this test is to check if the implementation treats correctly |
+ // edges of the image. |
+ static const int kImgWidth = 600; |
+ static const int kImgHeight = 800; |
+ static const int kChannelCount = 3; |
+ static const int kStrideSlack = 22; |
+ static const int kChannel = 1; |
+ ConvolutionFilter1D filter; |
+ const float box[5] = { 0.2, 0.2, 0.2, 0.2, 0.2 }; |
+ filter.AddFilter(0, box, 5); |
+ |
+ // Allocate a source image and set to 0. |
+ int src_row_stride = kImgWidth * kChannelCount + kStrideSlack; |
+ int src_byte_count = src_row_stride * kImgHeight; |
+ std::vector<unsigned char> input(src_byte_count); |
+ |
+ // Draw a frame around the image. |
+ for (int i = 0; i < src_byte_count; ++i) { |
+ int row = i / src_row_stride; |
+ int col = i % src_row_stride / kChannelCount; |
+ int channel = i % src_row_stride % kChannelCount; |
+ if (channel != kChannel || col > kImgWidth) { |
+ input[i] = 255; |
+ } else if (row == 0 || col == 0 || |
+ col == kImgWidth - 1 || row == kImgHeight - 1) { |
+ input[i] = 100; |
+ } else if (row == 1 || col == 1 || |
+ col == kImgWidth - 2 || row == kImgHeight - 2) { |
+ input[i] = 200; |
+ } else { |
+ input[i] = 0; |
+ } |
+ } |
+ |
+ // Destination will be a single channel image with stide matching width. |
+ int dest_row_stride = kImgWidth; |
+ int dest_byte_count = dest_row_stride * kImgHeight; |
+ std::vector<unsigned char> output; |
+ output.resize(dest_byte_count); |
+ |
+ // Apply convolution in X. |
+ SingleChannelConvolveX1D(&input[0], src_row_stride, 1, kChannelCount, |
+ filter, SkISize::Make(kImgWidth, kImgHeight), |
+ &output[0], dest_row_stride, 0, 1, false); |
+ |
+ // Sadly, comparison is not as simple as retaining all values. |
+ int invalid_values = 0; |
+ const unsigned char first_value = output[0]; |
+ EXPECT_TRUE(std::abs(100 - first_value) <= 1); |
+ for (int i = 0; i < dest_row_stride; ++i) { |
+ if (output[i] != first_value) |
+ ++invalid_values; |
+ } |
+ EXPECT_EQ(0, invalid_values); |
+ |
+ int test_row = 22; |
+ EXPECT_NEAR(output[test_row * dest_row_stride], 100, 1); |
+ EXPECT_NEAR(output[test_row * dest_row_stride + 1], 80, 1); |
+ EXPECT_NEAR(output[test_row * dest_row_stride + 2], 60, 1); |
+ EXPECT_NEAR(output[test_row * dest_row_stride + 3], 40, 1); |
+ EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 1], 100, 1); |
+ EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 2], 80, 1); |
+ EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 3], 60, 1); |
+ EXPECT_NEAR(output[(test_row + 1) * dest_row_stride - 4], 40, 1); |
+ |
+ SingleChannelConvolveY1D(&input[0], src_row_stride, 1, kChannelCount, |
+ filter, SkISize::Make(kImgWidth, kImgHeight), |
+ &output[0], dest_row_stride, 0, 1, false); |
+ |
+ int test_column = 42; |
+ EXPECT_NEAR(output[test_column], 100, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride], 80, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride * 2], 60, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride * 3], 40, 1); |
+ |
+ EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 1)], 100, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 2)], 80, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 3)], 60, 1); |
+ EXPECT_NEAR(output[test_column + dest_row_stride * (kImgHeight - 4)], 40, 1); |
+} |
+ |
} // namespace skia |