summaryrefslogtreecommitdiff
path: root/platform/macos/src/NSImage+MGLAdditions.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/macos/src/NSImage+MGLAdditions.mm')
-rw-r--r--platform/macos/src/NSImage+MGLAdditions.mm33
1 files changed, 17 insertions, 16 deletions
diff --git a/platform/macos/src/NSImage+MGLAdditions.mm b/platform/macos/src/NSImage+MGLAdditions.mm
index 91c4f7bf66..7500a8a207 100644
--- a/platform/macos/src/NSImage+MGLAdditions.mm
+++ b/platform/macos/src/NSImage+MGLAdditions.mm
@@ -16,33 +16,34 @@
}
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage {
- CGImageRef image = CGImageFromMGLPremultipliedImage(styleImage->image.clone());
+ CGImageRef image = CGImageFromMGLPremultipliedImage(styleImage->getImage().clone());
if (!image) {
return nil;
}
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
- if (self = [self initWithSize:NSMakeSize(styleImage->getWidth(), styleImage->getHeight())]) {
+ CGFloat w = styleImage->getImage().size.width / styleImage->getPixelRatio();
+ CGFloat h = styleImage->getImage().size.height / styleImage->getPixelRatio();
+ if (self = [self initWithSize:NSMakeSize(w, h)]) {
[self addRepresentation:rep];
- [self setTemplate:styleImage->sdf];
+ [self setTemplate:styleImage->isSdf()];
}
return self;
}
-- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
- // Create a bitmap image representation from the image, respecting backing
- // scale factor and any resizing done on the image at runtime.
- // http://www.cocoabuilder.com/archive/cocoa/82430-nsimage-getting-raw-bitmap-data.html#82431
- [self lockFocus];
- NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:{ NSZeroPoint, self.size }];
- [self unlockFocus];
-
- mbgl::PremultipliedImage cPremultipliedImage({ static_cast<uint32_t>(rep.pixelsWide), static_cast<uint32_t>(rep.pixelsHigh) });
- std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.bytes(), cPremultipliedImage.data.get());
- return std::make_unique<mbgl::style::Image>(std::move(cPremultipliedImage),
- (float)(rep.pixelsWide / self.size.width),
- [self isTemplate]);
+- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier {
+ mbgl::PremultipliedImage cPremultipliedImage = self.mgl_premultipliedImage;
+ auto imageWidth = cPremultipliedImage.size.width;
+ return std::make_unique<mbgl::style::Image>([identifier UTF8String],
+ std::move(cPremultipliedImage),
+ (float)(imageWidth / self.size.width),
+ [self isTemplate]);
+}
+
+- (mbgl::PremultipliedImage)mgl_premultipliedImage {
+ CGImageRef ref = [self CGImageForProposedRect:nullptr context:nullptr hints:nullptr];
+ return MGLPremultipliedImageFromCGImage(ref);
}
@end