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

Side by Side Diff: third_party/WebKit/Source/core/html/ImageData.cpp

Issue 2849643002: Implement ImageData::CropRect() (Closed)
Patch Set: Adding flipping to ImageData::CopyRect() Created 3 years, 7 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 360
361 // This function is called from unit tests, and all the parameters are supposed 361 // This function is called from unit tests, and all the parameters are supposed
362 // to be validated on the call site. 362 // to be validated on the call site.
363 ImageData* ImageData::CreateForTest( 363 ImageData* ImageData::CreateForTest(
364 const IntSize& size, 364 const IntSize& size,
365 DOMArrayBufferView* buffer_view, 365 DOMArrayBufferView* buffer_view,
366 const ImageDataColorSettings* color_settings) { 366 const ImageDataColorSettings* color_settings) {
367 return new ImageData(size, buffer_view, color_settings); 367 return new ImageData(size, buffer_view, color_settings);
368 } 368 }
369 369
370 // Crops ImageData to the intersect of its size and the given rectangle. If the
371 // intersection is empty or it cannot create the cropped ImageData it returns
372 // nullptr. This function leaves the source ImageData intact. When crop_rect
373 // covers all the ImageData, a copy of the ImageData is returned.
374 ImageData* ImageData::CropRect(const IntRect& crop_rect, bool flip_y) {
375 IntRect src_rect(IntPoint(), size_);
376 const IntRect dst_rect = Intersection(src_rect, crop_rect);
377 if (dst_rect.IsEmpty())
378 return nullptr;
379
380 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
381 DOMArrayBufferView* buffer_view = AllocateAndValidateDataArray(
382 data_size,
383 ImageData::GetImageDataStorageFormat(color_settings_.storageFormat()));
384 if (!buffer_view)
385 return nullptr;
386
387 if (src_rect == dst_rect && !flip_y) {
388 std::memcpy(buffer_view->BufferBase()->Data(), BufferBase()->Data(),
389 data_size * buffer_view->TypeSize());
390 } else {
391 int src_index = 0, dst_index = 0;
392 unsigned data_type_size =
393 ImageData::StorageFormatDataSize(color_settings_.storageFormat());
394 for (int i = 0; i < dst_rect.Height(); i++) {
395 src_index = ((i + dst_rect.X()) * src_rect.Width() + dst_rect.Y()) * 4;
396 dst_index = flip_y ? (dst_rect.Height() - i - 1) * dst_rect.Width() * 4
397 : i * dst_rect.Width() * 4;
398 std::memcpy(
399 static_cast<char*>(buffer_view->BufferBase()->Data()) +
400 dst_index * data_type_size,
401 static_cast<char*>(BufferBase()->Data()) + src_index * data_type_size,
402 dst_rect.Width() * 4 * data_type_size);
403 }
404 }
405 return new ImageData(dst_rect.Size(), buffer_view, &color_settings_);
406 }
407
370 ScriptPromise ImageData::CreateImageBitmap(ScriptState* script_state, 408 ScriptPromise ImageData::CreateImageBitmap(ScriptState* script_state,
371 EventTarget& event_target, 409 EventTarget& event_target,
372 Optional<IntRect> crop_rect, 410 Optional<IntRect> crop_rect,
373 const ImageBitmapOptions& options, 411 const ImageBitmapOptions& options,
374 ExceptionState& exception_state) { 412 ExceptionState& exception_state) {
375 if ((crop_rect && 413 if ((crop_rect &&
376 !ImageBitmap::IsSourceSizeValid(crop_rect->Width(), crop_rect->Height(), 414 !ImageBitmap::IsSourceSizeValid(crop_rect->Width(), crop_rect->Height(),
377 exception_state)) || 415 exception_state)) ||
378 !ImageBitmap::IsSourceSizeValid(BitmapSourceSize().Width(), 416 !ImageBitmap::IsSourceSizeValid(BitmapSourceSize().Width(),
379 BitmapSourceSize().Height(), 417 BitmapSourceSize().Height(),
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 // For ImageData, the color space is only specified by color settings. 634 // For ImageData, the color space is only specified by color settings.
597 // It cannot have a SkColorSpace. This doesn't mean anything. Fix this. 635 // It cannot have a SkColorSpace. This doesn't mean anything. Fix this.
598 sk_sp<SkColorSpace> ImageData::GetSkColorSpace() { 636 sk_sp<SkColorSpace> ImageData::GetSkColorSpace() {
599 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() || 637 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() ||
600 !RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) 638 !RuntimeEnabledFeatures::colorCorrectRenderingEnabled())
601 return nullptr; 639 return nullptr;
602 640
603 return SkColorSpace::MakeSRGB(); 641 return SkColorSpace::MakeSRGB();
604 } 642 }
605 643
606 // This function returns the proper SkColorSpace to color correct the pixels
607 // stored in ImageData before copying to the canvas. For now, it assumes that
608 // both ImageData and canvas use a linear gamma curve.
609 sk_sp<SkColorSpace> ImageData::GetSkColorSpace(
610 const CanvasColorSpace& color_space,
611 const CanvasPixelFormat& pixel_format) {
612 switch (color_space) {
613 case kLegacyCanvasColorSpace:
614 return (gfx::ColorSpace::CreateSRGB()).ToSkColorSpace();
615 case kSRGBCanvasColorSpace:
616 if (pixel_format == kF16CanvasPixelFormat)
617 return (gfx::ColorSpace::CreateSCRGBLinear()).ToSkColorSpace();
618 return (gfx::ColorSpace::CreateSRGB()).ToSkColorSpace();
619 case kRec2020CanvasColorSpace:
620 return (gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020,
621 gfx::ColorSpace::TransferID::LINEAR))
622 .ToSkColorSpace();
623 case kP3CanvasColorSpace:
624 return (gfx::ColorSpace(gfx::ColorSpace::PrimaryID::SMPTEST432_1,
625 gfx::ColorSpace::TransferID::LINEAR))
626 .ToSkColorSpace();
627 }
628 NOTREACHED();
629 return nullptr;
630 }
631
632 sk_sp<SkColorSpace> ImageData::GetSkColorSpaceForTest(
633 const CanvasColorSpace& color_space,
634 const CanvasPixelFormat& pixel_format) {
635 return GetSkColorSpace(color_space, pixel_format);
636 }
637
638 bool ImageData::ImageDataInCanvasColorSettings( 644 bool ImageData::ImageDataInCanvasColorSettings(
639 const CanvasColorSpace& canvas_color_space, 645 const CanvasColorSpace& canvas_color_space,
640 const CanvasPixelFormat& canvas_pixel_format, 646 const CanvasPixelFormat& canvas_pixel_format,
641 std::unique_ptr<uint8_t[]>& converted_pixels) { 647 std::unique_ptr<uint8_t[]>& converted_pixels) {
642 if (!data_ && !data_u16_ && !data_f32_) 648 if (!data_ && !data_u16_ && !data_f32_)
643 return false; 649 return false;
644 650
645 // If canvas and image data are both in the same color space and pixel format 651 // If canvas and image data are both in the same color space and pixel format
646 // is 8-8-8-8, just return the embedded data. 652 // is 8-8-8-8, just return the embedded data.
647 CanvasColorSpace image_data_color_space = 653 CanvasColorSpace image_data_color_space =
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 } else if (data_f32_) { 692 } else if (data_f32_) {
687 src_data = static_cast<void*>(data_f32_->Data()); 693 src_data = static_cast<void*>(data_f32_->Data());
688 DCHECK(src_data); 694 DCHECK(src_data);
689 src_color_format = SkColorSpaceXform::ColorFormat::kRGBA_F32_ColorFormat; 695 src_color_format = SkColorSpaceXform::ColorFormat::kRGBA_F32_ColorFormat;
690 } else { 696 } else {
691 NOTREACHED(); 697 NOTREACHED();
692 } 698 }
693 699
694 sk_sp<SkColorSpace> src_color_space = nullptr; 700 sk_sp<SkColorSpace> src_color_space = nullptr;
695 if (data_) { 701 if (data_) {
696 src_color_space = ImageData::GetSkColorSpace(image_data_color_space, 702 src_color_space =
697 kRGBA8CanvasPixelFormat); 703 CanvasColorParams(image_data_color_space, kRGBA8CanvasPixelFormat)
704 .GetSkColorSpaceForSkSurfaces();
698 } else { 705 } else {
699 src_color_space = ImageData::GetSkColorSpace(image_data_color_space, 706 src_color_space =
700 kF16CanvasPixelFormat); 707 CanvasColorParams(image_data_color_space, kF16CanvasPixelFormat)
708 .GetSkColorSpaceForSkSurfaces();
701 } 709 }
702 710
703 sk_sp<SkColorSpace> dst_color_space = 711 sk_sp<SkColorSpace> dst_color_space =
704 ImageData::GetSkColorSpace(canvas_color_space, canvas_pixel_format); 712 CanvasColorParams(canvas_color_space, canvas_pixel_format)
705 713 .GetSkColorSpaceForSkSurfaces();
706 SkColorSpaceXform::ColorFormat dst_color_format = 714 SkColorSpaceXform::ColorFormat dst_color_format =
707 SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat; 715 SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat;
708 if (canvas_pixel_format == kF16CanvasPixelFormat) 716 if (canvas_pixel_format == kF16CanvasPixelFormat)
709 dst_color_format = SkColorSpaceXform::ColorFormat::kRGBA_F16_ColorFormat; 717 dst_color_format = SkColorSpaceXform::ColorFormat::kRGBA_F16_ColorFormat;
710 718
711 if (SkColorSpace::Equals(src_color_space.get(), dst_color_space.get()) && 719 if (SkColorSpace::Equals(src_color_space.get(), dst_color_space.get()) &&
712 src_color_format == dst_color_format) 720 src_color_format == dst_color_format)
713 return static_cast<unsigned char*>(src_data); 721 return static_cast<unsigned char*>(src_data);
714 722
715 std::unique_ptr<SkColorSpaceXform> xform = 723 std::unique_ptr<SkColorSpaceXform> xform =
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 SECURITY_CHECK(static_cast<unsigned>(size.Width() * size.Height() * 4) <= 787 SECURITY_CHECK(static_cast<unsigned>(size.Width() * size.Height() * 4) <=
780 data_f32_->length()); 788 data_f32_->length());
781 break; 789 break;
782 790
783 default: 791 default:
784 NOTREACHED(); 792 NOTREACHED();
785 } 793 }
786 } 794 }
787 795
788 } // namespace blink 796 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/ImageData.h ('k') | third_party/WebKit/Source/core/html/ImageDataTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698