-
Notifications
You must be signed in to change notification settings - Fork 1
/
cap_macos.cpp
40 lines (32 loc) · 1.41 KB
/
cap_macos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "cap.h"
#include <ApplicationServices/ApplicationServices.h>
#include <iostream>
unsigned char* captureFullScreen(int* width, int* height, int* channels) {
// Capture the screen
CGImageRef screenImageRef = CGDisplayCreateImage(kCGDirectMainDisplay);
if (!screenImageRef) {
std::cerr << "Failed to capture screen" << std::endl;
return nullptr;
}
// Get image properties
*width = static_cast<int>(CGImageGetWidth(screenImageRef));
*height = static_cast<int>(CGImageGetHeight(screenImageRef));
*channels = 4; // Assuming RGBA
// Create a bitmap context
CGContextRef context = CGBitmapContextCreate(NULL, *width, *height, 8, *width * *channels, CGImageGetColorSpace(screenImageRef), kCGImageAlphaPremultipliedLast);
if (!context) {
std::cerr << "Failed to create bitmap context" << std::endl;
CFRelease(screenImageRef);
return nullptr;
}
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), screenImageRef);
// Get the buffer
unsigned char* buffer = (unsigned char*)CGBitmapContextGetData(context);
// Retain the buffer and release the context
unsigned char* bufferCopy = (unsigned char*)malloc(*width * *height * *channels);
memcpy(bufferCopy, buffer, *width * *height * *channels);
CGContextRelease(context);
CFRelease(screenImageRef);
return bufferCopy;
}