From d863253f1c70448c116b005f34dc127a9524d583 Mon Sep 17 00:00:00 2001 From: Jeyaram Jeyaraj Date: Sat, 17 Dec 2016 15:39:25 -0800 Subject: [PATCH] Add support for opacity and interpolation for drawing images #1556 --- Frameworks/CoreGraphics/CGContext.mm | 19 ++++---- .../CGContextDrawingTests.cpp | 47 +++++++++++++++++++ ...xt.DrawAnImageWithInterpolationQuality.png | 3 ++ ...nImageWithInterpolationQualityAndAlpha.png | 3 ++ ...Image.CGContext.DrawAnImageWithOpacity.png | 3 ++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQuality.png create mode 100644 tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQualityAndAlpha.png create mode 100644 tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithOpacity.png diff --git a/Frameworks/CoreGraphics/CGContext.mm b/Frameworks/CoreGraphics/CGContext.mm index 1ec424ffa9..8b3896b128 100644 --- a/Frameworks/CoreGraphics/CGContext.mm +++ b/Frameworks/CoreGraphics/CGContext.mm @@ -2273,14 +2273,17 @@ void CGContextDrawImage(CGContextRef context, CGRect rect, CGImageRef image) { flipImage = CGAffineTransformTranslate(flipImage, -rect.origin.x, -(rect.origin.y + (rect.size.height / 2.0))); ComPtr commandList; - HRESULT hr = context->DrawToCommandList(_kCGCoordinateModeUserSpace, - &flipImage, - &commandList, - [&](CGContextRef context, ID2D1DeviceContext* deviceContext) { - deviceContext->DrawBitmap(d2dBitmap.Get(), __CGRectToD2D_F(rect)); - return S_OK; - }); - FAIL_FAST_IF_FAILED(hr); + FAIL_FAST_IF_FAILED(context->DrawToCommandList(_kCGCoordinateModeUserSpace, + &flipImage, + &commandList, + [&](CGContextRef context, ID2D1DeviceContext* deviceContext) { + auto& state = context->CurrentGState(); + deviceContext->DrawBitmap(d2dBitmap.Get(), + __CGRectToD2D_F(rect), + state.alpha, + state.GetInterpolationModeForCGImage(refImage.get())); + return S_OK; + })); FAIL_FAST_IF_FAILED(context->DrawImage(commandList.Get())); } diff --git a/tests/UnitTests/CoreGraphics.drawing/CGContextDrawingTests.cpp b/tests/UnitTests/CoreGraphics.drawing/CGContextDrawingTests.cpp index f000ec6b0b..d580575cb0 100644 --- a/tests/UnitTests/CoreGraphics.drawing/CGContextDrawingTests.cpp +++ b/tests/UnitTests/CoreGraphics.drawing/CGContextDrawingTests.cpp @@ -422,6 +422,53 @@ DISABLED_DRAW_TEST_F(CGContext, DrawAnImage, UIKitMimicTest) { CGContextDrawImage(context, bounds, image.get()); } +DRAW_TEST_F(CGContext, DrawAnImageWithOpacity, UIKitMimicTest) { + // Load an Image and draw it into the canvas context + auto drawingConfig = DrawingTestConfig::Get(); + + woc::unique_cf testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png3.9.png")) }; + woc::unique_cf image{ _CGImageCreateFromPNGFile(testFilename.get()) }; + ASSERT_NE(image, nullptr); + + CGContextRef context = GetDrawingContext(); + CGRect bounds = GetDrawingBounds(); + + CGAffineTransform flip = CGAffineTransformMakeScale(1, -1); + CGAffineTransform shift = CGAffineTransformTranslate(flip, 0, bounds.size.height * -1); + CGContextConcatCTM(context, shift); + + CGContextSetAlpha(context, 0.7); + CGContextDrawImage(context, bounds, image.get()); +} + +DRAW_TEST_F(CGContext, DrawAnImageWithInterpolationQuality, UIKitMimicTest) { + auto drawingConfig = DrawingTestConfig::Get(); + + woc::unique_cf testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png3.9.png")) }; + woc::unique_cf image{ _CGImageCreateFromPNGFile(testFilename.get()) }; + ASSERT_NE(image, nullptr); + + CGContextRef context = GetDrawingContext(); + CGRect bounds = GetDrawingBounds(); + + CGContextSetInterpolationQuality(context, kCGInterpolationLow); + CGContextDrawImage(context, bounds, image.get()); +} + +DRAW_TEST_F(CGContext, DrawAnImageWithInterpolationQualityAndAlpha, UIKitMimicTest) { + auto drawingConfig = DrawingTestConfig::Get(); + woc::unique_cf testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png3.9.png")) }; + woc::unique_cf image{ _CGImageCreateFromPNGFile(testFilename.get()) }; + ASSERT_NE(image, nullptr); + + CGContextRef context = GetDrawingContext(); + CGRect bounds = GetDrawingBounds(); + + CGContextSetAlpha(context, 0.25); + CGContextSetInterpolationQuality(context, kCGInterpolationHigh); + CGContextDrawImage(context, bounds, image.get()); +} + DISABLED_DRAW_TEST_F(CGContext, RedBox, UIKitMimicTest) { CGContextRef context = GetDrawingContext(); CGRect bounds = GetDrawingBounds(); diff --git a/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQuality.png b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQuality.png new file mode 100644 index 0000000000..45df0f19f2 --- /dev/null +++ b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQuality.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1465fd318bfefb6fcf3c5a86fb6a8839280e62a0f67559979d73decd373847f8 +size 968387 diff --git a/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQualityAndAlpha.png b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQualityAndAlpha.png new file mode 100644 index 0000000000..92acba72b3 --- /dev/null +++ b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithInterpolationQualityAndAlpha.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2e79c85d2ccd9171a987e4be6066c77267b4ce5271ced20091c0bd0dbee4aec +size 594730 diff --git a/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithOpacity.png b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithOpacity.png new file mode 100644 index 0000000000..acaa5a97cf --- /dev/null +++ b/tests/unittests/CoreGraphics.drawing/data/reference/TestImage.CGContext.DrawAnImageWithOpacity.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b04f28ec2fadb86564e2a406e0208c7d6ba6dbb4dfe74c14969e6d8096414c5b +size 878466