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

Unified Diff: third_party/WebKit/Source/core/html/ImageDataTest.cpp

Issue 2849643002: Implement ImageData::CropRect() (Closed)
Patch Set: Adding MakeCopy() Created 3 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: third_party/WebKit/Source/core/html/ImageDataTest.cpp
diff --git a/third_party/WebKit/Source/core/html/ImageDataTest.cpp b/third_party/WebKit/Source/core/html/ImageDataTest.cpp
index 1cc60e0590e85dccb135c8a7f98d6990f4c9ff6b..1fec53c833a4798068a85a077fc79a3a02b1ac0d 100644
--- a/third_party/WebKit/Source/core/html/ImageDataTest.cpp
+++ b/third_party/WebKit/Source/core/html/ImageDataTest.cpp
@@ -250,15 +250,18 @@ bool ConvertPixelsToColorSpaceAndPixelFormatForTest(
sk_sp<SkColorSpace> src_sk_color_space = nullptr;
if (u8_array) {
- src_sk_color_space = ImageData::GetSkColorSpaceForTest(
- src_color_space, kRGBA8CanvasPixelFormat);
+ src_sk_color_space =
+ CanvasColorParams(src_color_space, kRGBA8CanvasPixelFormat)
+ .GetSkColorSpaceForSkSurfaces();
} else {
- src_sk_color_space = ImageData::GetSkColorSpaceForTest(
- src_color_space, kF16CanvasPixelFormat);
+ src_sk_color_space =
+ CanvasColorParams(src_color_space, kF16CanvasPixelFormat)
+ .GetSkColorSpaceForSkSurfaces();
}
sk_sp<SkColorSpace> dst_sk_color_space =
- ImageData::GetSkColorSpaceForTest(dst_color_space, dst_pixel_format);
+ CanvasColorParams(dst_color_space, dst_pixel_format)
+ .GetSkColorSpaceForSkSurfaces();
// When the input dataArray is in Uint16, we normally should convert the
// values from Little Endian to Big Endian before passing the buffer to
@@ -400,5 +403,233 @@ TEST_F(ImageDataTest, TestGetImageDataInCanvasColorSettings) {
delete[] f32_pixels;
}
+// This test examines ImageData::MakeCopy()
+TEST_F(ImageDataTest, TestMakeCopy) {
+ const int num_image_data_storage_formats = 3;
+ ImageDataStorageFormat image_data_storage_formats[] = {
+ kUint8ClampedArrayStorageFormat, kUint16ArrayStorageFormat,
+ kFloat32ArrayStorageFormat,
+ };
+ String image_data_storage_format_names[] = {
+ kUint8ClampedArrayStorageFormatName, kUint16ArrayStorageFormatName,
+ kFloat32ArrayStorageFormatName,
+ };
+
+ // Source pixels
+ unsigned width = 2;
+ unsigned height = 2;
+ unsigned data_length = width * height * 4;
+ uint8_t* u8_pixels = new uint8_t[data_length];
+ uint16_t* u16_pixels = new uint16_t[data_length];
+ float* f32_pixels = new float[data_length];
+
+ // Fill the pixels with numbers related to their positions
+ unsigned set_value = 0;
+ unsigned index = 0;
+ for (unsigned i = 0; i < height; i++)
+ for (unsigned j = 0; j < width; j++)
+ for (unsigned k = 0; k < 4; k++) {
+ index = i * width * 4 + j * 4 + k;
+ set_value = (i + 1) * (j + 1) * (k + 1);
+ u8_pixels[index] = set_value % 255;
+ u16_pixels[index] = (set_value * 257) % 65535;
+ f32_pixels[index] = (set_value % 255) / 255.0f;
+ }
+
+ // Create ImageData objects
+ DOMArrayBufferView* data_array = nullptr;
+
+ DOMUint8ClampedArray* data_u8 =
+ DOMUint8ClampedArray::Create(u8_pixels, data_length);
+ DCHECK(data_u8);
+ EXPECT_EQ(data_length, data_u8->length());
+ DOMUint16Array* data_u16 = DOMUint16Array::Create(u16_pixels, data_length);
+ DCHECK(data_u16);
+ EXPECT_EQ(data_length, data_u16->length());
+ DOMFloat32Array* data_f32 = DOMFloat32Array::Create(f32_pixels, data_length);
+ DCHECK(data_f32);
+ EXPECT_EQ(data_length, data_f32->length());
+
+ ImageData* image_data = nullptr;
+ ImageData* copy_image_data = nullptr;
+
+ for (int i = 0; i < num_image_data_storage_formats; i++) {
+ if (image_data_storage_formats[i] == kUint8ClampedArrayStorageFormat)
+ data_array = static_cast<DOMArrayBufferView*>(data_u8);
+ else if (image_data_storage_formats[i] == kUint16ArrayStorageFormat)
+ data_array = static_cast<DOMArrayBufferView*>(data_u16);
+ else
+ data_array = static_cast<DOMArrayBufferView*>(data_f32);
+
+ ImageDataColorSettings color_settings;
+ color_settings.setStorageFormat(image_data_storage_format_names[i]);
+ image_data = ImageData::CreateForTest(IntSize(width, height), data_array,
+ &color_settings);
+ copy_image_data = image_data->MakeCopy();
+ EXPECT_TRUE(copy_image_data->Size() == image_data->Size());
+ EXPECT_TRUE(ImageData::CompareImageDataColorSettingsForTest(copy_image_data,
+ image_data));
+
+ int data_size =
+ image_data->Size().Width() * image_data->Size().Height() * 4 *
+ ImageData::StorageFormatDataSize(color_settings.storageFormat());
+
+ if (image_data_storage_formats[i] == kUint8ClampedArrayStorageFormat) {
+ EXPECT_TRUE(std::memcmp(image_data->data()->Data(),
+ copy_image_data->data()->Data(), data_size) == 0);
+ } else if (image_data_storage_formats[i] == kUint16ArrayStorageFormat) {
+ EXPECT_TRUE(
+ std::memcmp(
+ image_data->dataUnion().getAsUint16Array().View()->Data(),
+ copy_image_data->dataUnion().getAsUint16Array().View()->Data(),
+ data_size) == 0);
+ } else {
+ EXPECT_TRUE(
+ std::memcmp(
+ image_data->dataUnion().getAsFloat32Array().View()->Data(),
+ copy_image_data->dataUnion().getAsFloat32Array().View()->Data(),
+ data_size) == 0);
+ }
+ }
+
+ delete[] u8_pixels;
+ delete[] u16_pixels;
+ delete[] f32_pixels;
+}
+
+// This test examines ImageData::MakeSubset()
+TEST_F(ImageDataTest, TestMakeSubset) {
+ const int num_image_data_storage_formats = 3;
+ ImageDataStorageFormat image_data_storage_formats[] = {
+ kUint8ClampedArrayStorageFormat, kUint16ArrayStorageFormat,
+ kFloat32ArrayStorageFormat,
+ };
+ String image_data_storage_format_names[] = {
+ kUint8ClampedArrayStorageFormatName, kUint16ArrayStorageFormatName,
+ kFloat32ArrayStorageFormatName,
+ };
+
+ // Source pixels
+ unsigned width = 40;
+ unsigned height = 40;
+ unsigned data_length = width * height * 4;
+ uint8_t* u8_pixels = new uint8_t[data_length];
+ uint16_t* u16_pixels = new uint16_t[data_length];
+ float* f32_pixels = new float[data_length];
+
+ // Test scenarios
+ const int num_test_cases = 8;
+ const IntRect crop_test_cases[8] = {
+ IntRect(1, 2, 1, 2), IntRect(10, 10, 20, 20), IntRect(10, 10, 40, 40),
+ IntRect(0, 0, 10, 10), IntRect(0, 0, 10, 0), IntRect(0, 0, 0, 10),
+ IntRect(10, 0, 10, 10), IntRect(0, 10, 10, 10),
+ };
+
+ // Fill the pixels with numbers related to their positions
+ unsigned set_value = 0;
+ unsigned expected_value = 0;
+ float fexpected_value = 0;
+ unsigned index = 0;
+ for (unsigned i = 0; i < height; i++)
+ for (unsigned j = 0; j < width; j++)
+ for (unsigned k = 0; k < 4; k++) {
+ index = i * width * 4 + j * 4 + k;
+ set_value = (i + 1) * (j + 1) * (k + 1);
+ u8_pixels[index] = set_value % 255;
+ u16_pixels[index] = (set_value * 257) % 65535;
+ f32_pixels[index] = (set_value % 255) / 255.0f;
+ }
+
+ // Create ImageData objects
+ DOMArrayBufferView* data_array = nullptr;
+
+ DOMUint8ClampedArray* data_u8 =
+ DOMUint8ClampedArray::Create(u8_pixels, data_length);
+ DCHECK(data_u8);
+ EXPECT_EQ(data_length, data_u8->length());
+ DOMUint16Array* data_u16 = DOMUint16Array::Create(u16_pixels, data_length);
+ DCHECK(data_u16);
+ EXPECT_EQ(data_length, data_u16->length());
+ DOMFloat32Array* data_f32 = DOMFloat32Array::Create(f32_pixels, data_length);
+ DCHECK(data_f32);
+ EXPECT_EQ(data_length, data_f32->length());
+
+ ImageData* image_data = nullptr;
+ ImageData* cropped_image_data = nullptr;
+
+ bool test_passed = true;
+ for (int i = 0; i < num_image_data_storage_formats; i++) {
+ if (image_data_storage_formats[i] == kUint8ClampedArrayStorageFormat)
+ data_array = static_cast<DOMArrayBufferView*>(data_u8);
+ else if (image_data_storage_formats[i] == kUint16ArrayStorageFormat)
+ data_array = static_cast<DOMArrayBufferView*>(data_u16);
+ else
+ data_array = static_cast<DOMArrayBufferView*>(data_f32);
+
+ ImageDataColorSettings color_settings;
+ color_settings.setStorageFormat(image_data_storage_format_names[i]);
+ image_data = ImageData::CreateForTest(IntSize(width, height), data_array,
+ &color_settings);
+ for (int j = 0; j < num_test_cases; j++) {
+ // Test the size of the cropped image data
+ IntRect src_rect(IntPoint(), image_data->Size());
+ IntRect crop_rect = Intersection(src_rect, crop_test_cases[j]);
+
+ cropped_image_data = image_data->MakeSubset(crop_test_cases[j]);
+ if (crop_rect.IsEmpty()) {
+ EXPECT_FALSE(cropped_image_data);
+ continue;
+ }
+ EXPECT_TRUE(cropped_image_data->Size() == crop_rect.Size());
+
+ // Test the content
+ for (int k = 0; k < crop_rect.Height(); k++)
+ for (int m = 0; m < crop_rect.Width(); m++)
+ for (int n = 0; n < 4; n++) {
+ index = k * cropped_image_data->Size().Width() * 4 + m * 4 + n;
+ expected_value =
+ (k + crop_rect.X() + 1) * (m + crop_rect.Y() + 1) * (n + 1);
+ if (image_data_storage_formats[i] ==
+ kUint8ClampedArrayStorageFormat)
+ expected_value %= 255;
+ else if (image_data_storage_formats[i] == kUint16ArrayStorageFormat)
+ expected_value = (expected_value * 257) % 65535;
+ else
+ fexpected_value = (expected_value % 255) / 255.0f;
+
+ if (image_data_storage_formats[i] ==
+ kUint8ClampedArrayStorageFormat) {
+ if (cropped_image_data->data()->Data()[index] != expected_value) {
+ test_passed = false;
+ break;
+ }
+ } else if (image_data_storage_formats[i] ==
+ kUint16ArrayStorageFormat) {
+ if (cropped_image_data->dataUnion()
+ .getAsUint16Array()
+ .View()
+ ->Data()[index] != expected_value) {
+ test_passed = false;
+ break;
+ }
+ } else {
+ if (cropped_image_data->dataUnion()
+ .getAsFloat32Array()
+ .View()
+ ->Data()[index] != fexpected_value) {
+ test_passed = false;
+ break;
+ }
+ }
+ }
+ EXPECT_TRUE(test_passed);
+ }
+ }
+
+ delete[] u8_pixels;
+ delete[] u16_pixels;
+ delete[] f32_pixels;
+}
+
} // namspace
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698