-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*============================================================================== | ||
Copyright 2024 by Roland Rabien | ||
For more information visit www.rabiensoftware.com | ||
==============================================================================*/ | ||
|
||
//============================================================================== | ||
#if JUCE_MAC | ||
juce::Image nsImageToImage (NSImage* nsImage) | ||
{ | ||
if (nsImage != nil) | ||
{ | ||
if (CGImageRef cgImage = [nsImage CGImageForProposedRect:nullptr context:nil hints:nil]) | ||
{ | ||
const auto width = CGImageGetWidth (cgImage); | ||
const auto height = CGImageGetHeight (cgImage); | ||
const auto cgRect = CGRectMake (0, 0, (CGFloat) width, (CGFloat) height); | ||
const juce::Image image (juce::Image::ARGB, (int) width, (int) height, true); | ||
|
||
CGContextDrawImage (juce_getImageContext (image), cgRect, cgImage); | ||
|
||
return image; | ||
} | ||
} | ||
return {}; | ||
} | ||
|
||
NSImage* imageToNSImage (const juce::Image& image) | ||
{ | ||
const auto scaleFactor = 1.0f; | ||
|
||
using ColorSpacePtr = juce::CFUniquePtr<CGColorSpaceRef>; | ||
using ImagePtr = juce::CFUniquePtr<CGImageRef>; | ||
|
||
NSImage* im = [[NSImage alloc] init]; | ||
auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor); | ||
|
||
[im setSize: requiredSize]; | ||
ColorSpacePtr colourSpace { CGColorSpaceCreateWithName (kCGColorSpaceSRGB) }; | ||
ImagePtr imageRef { juce::juce_createCoreGraphicsImage (image, colourSpace.get()) }; | ||
|
||
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef.get()]; | ||
[imageRep setSize: requiredSize]; | ||
[im addRepresentation: imageRep]; | ||
[imageRep release]; | ||
return im; | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*============================================================================== | ||
Copyright 2024 by Roland Rabien | ||
For more information visit www.rabiensoftware.com | ||
==============================================================================*/ | ||
|
||
#pragma once | ||
|
||
//============================================================================== | ||
|
||
#if JUCE_MAC | ||
namespace macOS | ||
{ | ||
juce::Image nsImageToImage (NSImage* nsImage); | ||
|
||
NSImage* imageToNSImage (const juce::Image& image); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters