Repository navigation
Expand file tree
/
Copy pathscreengrab.c
More file actions
157 lines (128 loc) · 4.23 KB
/
Copy pathscreengrab.c
File metadata and controls
157 lines (128 loc) · 4.23 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "screengrab.h"
#include "bmp_io.h"
#include "endian.h"
#include <stdlib.h> /* malloc() */
#if defined(IS_MACOSX)
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <ApplicationServices/ApplicationServices.h>
#elif defined(USE_X11)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "xdisplay.h"
#elif defined(IS_WINDOWS)
#include <string.h>
#endif
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
{
#if defined(IS_MACOSX)
/**
* Prevent misaligned pixels by rounding down to a multiple of 16.
* http://stackoverflow.com/a/17389894/2233771
*/
if (rect.size.width > 16) {
rect.size.width &= 0xFFFFFFF0;
}
MMBitmapRef bitmap = NULL;
uint8_t *buffer = NULL;
size_t bufferSize = 0;
CGDirectDisplayID displayID = CGMainDisplayID();
CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGRectMake(rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height));
if (!image) { return NULL; }
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
if (!imageData) { return NULL; }
bufferSize = CFDataGetLength(imageData);
buffer = malloc(bufferSize);
CFDataGetBytes(imageData, CFRangeMake(0,bufferSize), buffer);
bitmap = createMMBitmap(buffer,
CGImageGetWidth(image),
CGImageGetHeight(image),
CGImageGetBytesPerRow(image),
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);
CFRelease(imageData);
CGImageRelease(image);
return bitmap;
#elif defined(USE_X11)
MMBitmapRef bitmap;
Display *display = XOpenDisplay(NULL);
XImage *image = XGetImage(display,
XDefaultRootWindow(display),
(int)rect.origin.x,
(int)rect.origin.y,
(unsigned int)rect.size.width,
(unsigned int)rect.size.height,
AllPlanes, ZPixmap);
XCloseDisplay(display);
if (image == NULL) return NULL;
bitmap = createMMBitmap((uint8_t *)image->data,
rect.size.width,
rect.size.height,
(size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel,
(uint8_t)image->bits_per_pixel / 8);
image->data = NULL; /* Steal ownership of bitmap data so we don't have to
* copy it. */
XDestroyImage(image);
return bitmap;
#elif defined(IS_WINDOWS)
MMBitmapRef bitmap;
void *data;
HDC screen = NULL, screenMem = NULL;
HBITMAP dib;
BITMAPINFO bi;
/* Initialize bitmap info. */
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = (long)rect.size.width;
bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.width * rect.size.height);
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
screen = GetDC(NULL); /* Get entire screen */
if (screen == NULL) return NULL;
/* Get screen data in display device context. */
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);
/* Copy the data into a bitmap struct. */
if ((screenMem = CreateCompatibleDC(screen)) == NULL ||
SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem,
(int)0,
(int)0,
(int)rect.size.width,
(int)rect.size.height,
screen,
rect.origin.x,
rect.origin.y,
SRCCOPY)) {
/* Error copying data. */
ReleaseDC(NULL, screen);
DeleteObject(dib);
if (screenMem != NULL) DeleteDC(screenMem);
return NULL;
}
bitmap = createMMBitmap(NULL,
rect.size.width,
rect.size.height,
4 * rect.size.width,
(uint8_t)bi.bmiHeader.biBitCount,
4);
/* Copy the data to our pixel buffer. */
if (bitmap != NULL) {
bitmap->imageBuffer = malloc(bitmap->bytewidth * bitmap->height);
memcpy(bitmap->imageBuffer, data, bitmap->bytewidth * bitmap->height);
}
ReleaseDC(NULL, screen);
DeleteObject(dib);
DeleteDC(screenMem);
return bitmap;
#endif
}