1
1
#include " ../screengrab.h"
2
2
#include " ../endian.h"
3
3
#include < stdlib.h> /* malloc() */
4
+ #include < stdio.h> /* printf() */
4
5
5
6
#include < ApplicationServices/ApplicationServices.h>
6
7
#import < Cocoa/Cocoa.h>
7
8
8
- static double getPixelDensity () {
9
- @autoreleasepool
10
- {
11
- NSScreen * mainScreen = [NSScreen
12
- mainScreen ];
13
- if (mainScreen) {
14
- return mainScreen.backingScaleFactor ;
15
- } else {
16
- return 1.0 ;
17
- }
18
- }
19
- }
20
-
21
9
MMBitmapRef copyMMBitmapFromDisplayInRect (MMRect rect) {
22
10
23
11
CGDirectDisplayID displayID = CGMainDisplayID ();
@@ -33,56 +21,37 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) {
33
21
34
22
if (!image) { return NULL ; }
35
23
36
- CFDataRef imageData = CGDataProviderCopyData (CGImageGetDataProvider (image));
24
+ size_t width = CGImageGetWidth (image);
25
+ size_t height = CGImageGetHeight (image);
26
+ CGDataProviderRef provider = CGImageGetDataProvider (image);
27
+
28
+ CFDataRef imageData = CGDataProviderCopyData (provider);
37
29
38
30
if (!imageData) { return NULL ; }
39
31
40
32
long bufferSize = CFDataGetLength (imageData);
41
- size_t bytesPerPixel = (size_t ) (CGImageGetBitsPerPixel (image) / 8 );
42
- double pixelDensity = getPixelDensity ();
43
- long expectedBufferSize = rect.size .width * pixelDensity * rect.size .height * pixelDensity * bytesPerPixel;
44
-
45
- if (expectedBufferSize < bufferSize) {
46
- size_t reportedByteWidth = CGImageGetBytesPerRow (image);
47
- size_t expectedByteWidth = expectedBufferSize / (rect.size .height * pixelDensity);
48
-
49
- uint8_t *buffer = malloc (expectedBufferSize);
33
+ size_t bitsPerPixel = CGImageGetBitsPerPixel (image);
34
+ size_t bytesPerPixel = bitsPerPixel / 8 ;
35
+ size_t bytesPerRow = CGImageGetBytesPerRow (image);
36
+ size_t actualBytesPerRow = width * bytesPerPixel;
37
+ const UInt8 *dataPointer = CFDataGetBytePtr (imageData);
50
38
51
- const uint8_t *dataPointer = CFDataGetBytePtr (imageData);
52
- size_t parts = bufferSize / reportedByteWidth;
39
+ uint8_t *imageDataWithoutPadding = malloc (height * actualBytesPerRow);
53
40
54
- for (size_t idx = 0 ; idx < parts - 1 ; ++idx) {
55
- memcpy (buffer + (idx * expectedByteWidth),
56
- dataPointer + (idx * reportedByteWidth),
57
- expectedByteWidth
58
- );
59
- }
60
-
61
- MMBitmapRef bitmap = createMMBitmap (buffer,
62
- rect.size .width * pixelDensity,
63
- rect.size .height * pixelDensity,
64
- expectedByteWidth,
65
- CGImageGetBitsPerPixel (image),
66
- CGImageGetBitsPerPixel (image) / 8 );
67
-
68
- CFRelease (imageData);
69
- CGImageRelease (image);
70
-
71
- return bitmap;
72
- } else {
73
- uint8_t *buffer = malloc (bufferSize);
74
- CFDataGetBytes (imageData, CFRangeMake (0 , bufferSize), buffer);
75
- MMBitmapRef bitmap = createMMBitmap (buffer,
76
- CGImageGetWidth (image),
77
- CGImageGetHeight (image),
78
- CGImageGetBytesPerRow (image),
79
- CGImageGetBitsPerPixel (image),
80
- CGImageGetBitsPerPixel (image) / 8 );
41
+ for (size_t y = 0 ; y < height; ++y) {
42
+ const UInt8 *rowPtr = dataPointer + y * bytesPerRow;
43
+ memcpy (imageDataWithoutPadding + y * actualBytesPerRow, rowPtr, actualBytesPerRow);
44
+ }
81
45
82
- CFRelease (imageData);
46
+ MMBitmapRef bitmap = createMMBitmap (imageDataWithoutPadding,
47
+ width,
48
+ height,
49
+ actualBytesPerRow,
50
+ bitsPerPixel,
51
+ bytesPerPixel);
83
52
84
- CGImageRelease (image);
53
+ CFRelease (imageData);
54
+ CGImageRelease (image);
85
55
86
- return bitmap;
87
- }
56
+ return bitmap;
88
57
}
0 commit comments