Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Unified Diff: skia/ext/convolver_unittest.cc

Issue 13947013: Complete (but inefficient) implementation of the image retargetting method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed reviewers' remarks. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: skia/ext/convolver_unittest.cc
diff --git a/skia/ext/convolver_unittest.cc b/skia/ext/convolver_unittest.cc
index 377ed8ed3051c20f491ad0336d8bed80a3041b17..567f5df7fc1de5e681edd4ee46ba9951ac1beef8 100644
--- a/skia/ext/convolver_unittest.cc
+++ b/skia/ext/convolver_unittest.cc
@@ -324,4 +324,164 @@ TEST(Convolver, SIMDVerification) {
}
}
+TEST(Convolver, SeparableSingleConvolution) {
+ static const int kSize = 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.
+ 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
+ int img_height = kSize;
+ int src_row_stride = img_width * kChannelCount + kStrideSlack;
+ int src_byte_count = src_row_stride * img_height;
+ std::vector<unsigned char> input;
+ int signal_x = img_width / 2;
+ int signal_y = img_height / 2;
+ input.resize(src_byte_count, 0);
+ // The image has a single impulse pixel in channel 1, smack in the middle.
+ 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.
+ int dest_row_stride = img_width;
+ int dest_byte_count = dest_row_stride * img_height;
+ 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(img_width, img_height),
+ &output[0], dest_row_stride, 0, 1, false);
+ 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.
+ 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(img_width, img_height),
+ &output[0], dest_row_stride, 0, 1, false);
+ 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.
+ 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(img_width, img_height),
+ &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_TRUE(std::abs(output[test_row * dest_row_stride] - 100) <= 1);
+ EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 1] - 80) <= 1);
+ EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 2] - 60) <= 1);
+ EXPECT_TRUE(std::abs(output[test_row * dest_row_stride + 3] - 40) <= 1);
+ 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.
+ std::abs(output[(test_row + 1) * dest_row_stride - 1] - 100) <= 1);
+ EXPECT_TRUE(
+ std::abs(output[(test_row + 1) * dest_row_stride - 2] - 80) <= 1);
+ EXPECT_TRUE(
+ std::abs(output[(test_row + 1) * dest_row_stride - 3] - 60) <= 1);
+ EXPECT_TRUE(
+ std::abs(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_TRUE(std::abs(output[test_column] - 100) <= 1);
+ EXPECT_TRUE(std::abs(output[test_column + dest_row_stride] - 80) <= 1);
+ EXPECT_TRUE(std::abs(output[test_column + dest_row_stride * 2] - 60) <= 1);
+ EXPECT_TRUE(std::abs(output[test_column + dest_row_stride * 3] - 40) <= 1);
+
+ EXPECT_TRUE(std::abs(
+ output[test_column + dest_row_stride * (kImgHeight - 1)] - 100) <= 1);
+ EXPECT_TRUE(std::abs(
+ output[test_column + dest_row_stride * (kImgHeight - 2)] - 80) <= 1);
+ EXPECT_TRUE(std::abs(
+ output[test_column + dest_row_stride * (kImgHeight - 3)] - 60) <= 1);
+ EXPECT_TRUE(std::abs(
+ output[test_column + dest_row_stride * (kImgHeight - 4)] - 40) <= 1);
+}
+
} // namespace skia

Powered by Google App Engine
This is Rietveld 408576698