Skip to content

Commit

Permalink
Support opacity and interpolation for Drawing Images (#1557)
Browse files Browse the repository at this point in the history
* Add support for opacity and interpolation for drawing images #1556
  • Loading branch information
msft-Jeyaram authored Dec 22, 2016
1 parent 0c1e282 commit 6fbd0b0
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
19 changes: 11 additions & 8 deletions Frameworks/CoreGraphics/CGContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<ID2D1CommandList> 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()));
}

Expand Down
49 changes: 48 additions & 1 deletion tests/UnitTests/CoreGraphics.drawing/CGContextDrawingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ DISABLED_DRAW_TEST_F(CGContext, DrawIntoRect, UIKitMimicTest) {
// Draw a portion of an image into a different region.
auto drawingConfig = DrawingTestConfig::Get();

woc::unique_cf<CFStringRef> testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png3.9.png")) };
woc::unique_cf<CFStringRef> testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png1.9.png")) };
woc::unique_cf<CGImageRef> image{ _CGImageCreateFromPNGFile(testFilename.get()) };
ASSERT_NE(image, nullptr);

Expand Down Expand Up @@ -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<CFStringRef> testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png1.9.png")) };
woc::unique_cf<CGImageRef> 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<CFStringRef> testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png1.9.png")) };
woc::unique_cf<CGImageRef> 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<CFStringRef> testFilename{ _CFStringCreateWithStdString(drawingConfig->GetResourcePath("png1.9.png")) };
woc::unique_cf<CGImageRef> 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();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion tests/unittests/CoreGraphics/CGContextTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ virtual void SetUp() {
// Check the canvas context pixel after drawing
dataPtr = static_cast<BYTE*>(CGBitmapContextGetData(context.get()));
ASSERT_NE(dataPtr, nullptr);
EXPECT_EQ(dataPtr[0], 0x97);
// Check the first pixel value of the drawn image.
EXPECT_EQ(dataPtr[0], 0x98);
}

TEST(CGContext, DrawAContextImageIntoAContext) {
Expand Down

0 comments on commit 6fbd0b0

Please sign in to comment.