yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
clipboard.mm
Go to the documentation of this file.
1#include "clipboard.h"
2
3#include <iostream>
4#include <vector>
5
6#ifdef TARGET_OS_MAC
7#import <Cocoa/Cocoa.h>
8
9void yaze::app::core::CopyImageToClipboard(const std::vector<uint8_t>& pngData) {
10 NSData* data = [NSData dataWithBytes:pngData.data() length:pngData.size()];
11 NSImage* image = [[NSImage alloc] initWithData:data];
12
13 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
14 [pasteboard clearContents];
15 [pasteboard writeObjects:@[ image ]];
16}
17
18void yaze::app::core::GetImageFromClipboard(std::vector<uint8_t>& pixel_data, int& width, int& height) {
19 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
20 NSArray* classArray = [NSArray arrayWithObject:[NSImage class]];
21 NSDictionary* options = [NSDictionary dictionary];
22
23 NSImage* image = [pasteboard readObjectsForClasses:classArray options:options].firstObject;
24 if (!image) {
25 width = height = 0;
26 return;
27 }
28
29 // Assuming the image is in an RGBA format
30 CGImageRef cgImage = [image CGImageForProposedRect:nil context:nil hints:nil];
31 width = (int)CGImageGetWidth(cgImage);
32 height = (int)CGImageGetHeight(cgImage);
33
34 size_t bytesPerRow = 4 * width;
35 size_t totalBytes = bytesPerRow * height;
36 pixel_data.resize(totalBytes);
37
38 CGContextRef context = CGBitmapContextCreate(
39 pixel_data.data(), width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(),
40 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
41 CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
42 CGContextRelease(context);
43}
44
45#endif
void CopyImageToClipboard(const std::vector< uint8_t > &data)
Definition clipboard.cc:10
void GetImageFromClipboard(std::vector< uint8_t > &data, int &width, int &height)
Definition clipboard.cc:11