Index: third_party/WebKit/Source/core/html/ImageData.cpp |
diff --git a/third_party/WebKit/Source/core/html/ImageData.cpp b/third_party/WebKit/Source/core/html/ImageData.cpp |
index 71f62d53ec0f11a90325a9f64cf1f43c52ad19a3..2336650a80ff52c8754996f4d06bbeafa6501d8a 100644 |
--- a/third_party/WebKit/Source/core/html/ImageData.cpp |
+++ b/third_party/WebKit/Source/core/html/ImageData.cpp |
@@ -367,6 +367,44 @@ ImageData* ImageData::CreateForTest( |
return new ImageData(size, buffer_view, color_settings); |
} |
+// Crops ImageData to the intersect of its size and the given rectangle. If the |
+// intersection is empty or it cannot create the cropped ImageData it returns |
+// nullptr. This function leaves the source ImageData intact. When crop_rect |
+// covers all the ImageData, a copy of the ImageData is returned. |
+ImageData* ImageData::CropRect(const IntRect& crop_rect, bool flip_y) { |
+ IntRect src_rect(IntPoint(), size_); |
+ const IntRect dst_rect = Intersection(src_rect, crop_rect); |
+ if (dst_rect.IsEmpty()) |
+ return nullptr; |
+ |
+ unsigned data_size = 4 * dst_rect.Width() * dst_rect.Height(); |
fserb
2017/04/28 18:54:03
please specify the full type. Here and below. :)
zakerinasab
2017/05/01 14:24:39
By "full type" do you mean "unsigned int"? Using "
Justin Novosad
2017/05/02 19:00:54
Are we sure this arithmetic is safe with respect t
zakerinasab
2017/05/03 14:45:55
It should be, as it is an intersection of the size
|
+ DOMArrayBufferView* buffer_view = AllocateAndValidateDataArray( |
+ data_size, |
+ ImageData::GetImageDataStorageFormat(color_settings_.storageFormat())); |
+ if (!buffer_view) |
+ return nullptr; |
+ |
+ if (src_rect == dst_rect && !flip_y) { |
+ std::memcpy(buffer_view->BufferBase()->Data(), BufferBase()->Data(), |
+ data_size * buffer_view->TypeSize()); |
+ } else { |
+ int src_index = 0, dst_index = 0; |
+ unsigned data_type_size = |
+ ImageData::StorageFormatDataSize(color_settings_.storageFormat()); |
+ for (int i = 0; i < dst_rect.Height(); i++) { |
+ src_index = ((i + dst_rect.X()) * src_rect.Width() + dst_rect.Y()) * 4; |
+ dst_index = flip_y ? (dst_rect.Height() - i - 1) * dst_rect.Width() * 4 |
+ : i * dst_rect.Width() * 4; |
+ std::memcpy( |
+ static_cast<char*>(buffer_view->BufferBase()->Data()) + |
+ dst_index * data_type_size, |
+ static_cast<char*>(BufferBase()->Data()) + src_index * data_type_size, |
+ dst_rect.Width() * 4 * data_type_size); |
+ } |
+ } |
+ return new ImageData(dst_rect.Size(), buffer_view, &color_settings_); |
+} |
+ |
ScriptPromise ImageData::CreateImageBitmap(ScriptState* script_state, |
EventTarget& event_target, |
Optional<IntRect> crop_rect, |
@@ -603,38 +641,6 @@ sk_sp<SkColorSpace> ImageData::GetSkColorSpace() { |
return SkColorSpace::MakeSRGB(); |
} |
-// This function returns the proper SkColorSpace to color correct the pixels |
-// stored in ImageData before copying to the canvas. For now, it assumes that |
-// both ImageData and canvas use a linear gamma curve. |
-sk_sp<SkColorSpace> ImageData::GetSkColorSpace( |
- const CanvasColorSpace& color_space, |
- const CanvasPixelFormat& pixel_format) { |
- switch (color_space) { |
- case kLegacyCanvasColorSpace: |
- return (gfx::ColorSpace::CreateSRGB()).ToSkColorSpace(); |
- case kSRGBCanvasColorSpace: |
- if (pixel_format == kF16CanvasPixelFormat) |
- return (gfx::ColorSpace::CreateSCRGBLinear()).ToSkColorSpace(); |
- return (gfx::ColorSpace::CreateSRGB()).ToSkColorSpace(); |
- case kRec2020CanvasColorSpace: |
- return (gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020, |
- gfx::ColorSpace::TransferID::LINEAR)) |
- .ToSkColorSpace(); |
- case kP3CanvasColorSpace: |
- return (gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1, |
- gfx::ColorSpace::TransferID::LINEAR)) |
- .ToSkColorSpace(); |
- } |
- NOTREACHED(); |
- return nullptr; |
-} |
- |
-sk_sp<SkColorSpace> ImageData::GetSkColorSpaceForTest( |
- const CanvasColorSpace& color_space, |
- const CanvasPixelFormat& pixel_format) { |
- return GetSkColorSpace(color_space, pixel_format); |
-} |
- |
bool ImageData::ImageDataInCanvasColorSettings( |
const CanvasColorSpace& canvas_color_space, |
const CanvasPixelFormat& canvas_pixel_format, |
@@ -693,16 +699,18 @@ bool ImageData::ImageDataInCanvasColorSettings( |
sk_sp<SkColorSpace> src_color_space = nullptr; |
if (data_) { |
- src_color_space = ImageData::GetSkColorSpace(image_data_color_space, |
- kRGBA8CanvasPixelFormat); |
+ src_color_space = |
+ CanvasColorParams(image_data_color_space, kRGBA8CanvasPixelFormat) |
+ .GetSkColorSpaceForSkSurfaces(); |
} else { |
- src_color_space = ImageData::GetSkColorSpace(image_data_color_space, |
- kF16CanvasPixelFormat); |
+ src_color_space = |
+ CanvasColorParams(image_data_color_space, kF16CanvasPixelFormat) |
+ .GetSkColorSpaceForSkSurfaces(); |
} |
sk_sp<SkColorSpace> dst_color_space = |
- ImageData::GetSkColorSpace(canvas_color_space, canvas_pixel_format); |
- |
+ CanvasColorParams(canvas_color_space, canvas_pixel_format) |
+ .GetSkColorSpaceForSkSurfaces(); |
SkColorSpaceXform::ColorFormat dst_color_format = |
SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat; |
if (canvas_pixel_format == kF16CanvasPixelFormat) |