forked from vova616/screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot_darwin.go
72 lines (60 loc) · 2.01 KB
/
screenshot_darwin.go
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
package screenshot
import (
// #cgo LDFLAGS: -framework CoreGraphics
// #cgo LDFLAGS: -framework CoreFoundation
// #include <CoreGraphics/CoreGraphics.h>
// #include <CoreFoundation/CoreFoundation.h>
"C"
"fmt"
"image"
"math"
"reflect"
"unsafe"
)
func ScreenRect() (image.Rectangle, error) {
displayID := C.CGMainDisplayID()
width := int(C.CGDisplayPixelsWide(displayID))
height := int(C.CGDisplayPixelsHigh(displayID))
return image.Rect(0, 0, width, height), nil
}
func CaptureScreen() (*image.RGBA, error) {
rect, err := ScreenRect()
if err != nil {
return nil, err
}
return CaptureRect(rect)
}
func CaptureRect(rect image.Rectangle) (*image.RGBA, error) {
displayID := C.CGMainDisplayID()
width := int(math.Ceil(float64(C.CGDisplayPixelsWide(displayID))/16) * 16)
// the three variables below are named after their CoreFoundation and
// CoreGraphics types for ease of reference; all are of type uintptr in Go
cgImageRef := C.CGDisplayCreateImage(displayID)
if cgImageRef == 0 {
return nil, fmt.Errorf("CGDisplayCreateImage(%d) returned null", displayID)
}
defer C.CGImageRelease(C.CGImageRef(cgImageRef))
cgDataProviderRef := C.CGImageGetDataProvider(cgImageRef)
if cgDataProviderRef == 0 {
return nil, fmt.Errorf("CGImageGetDataProvider returned null")
}
defer C.CFRelease(C.CFTypeRef(cgDataProviderRef))
cfDataRef := C.CGDataProviderCopyData(cgDataProviderRef)
if cfDataRef == 0 {
return nil, fmt.Errorf("CGDataProviderCopyData returned null")
}
defer C.CFRelease(C.CFTypeRef(cfDataRef))
length := int(C.CFDataGetLength(cfDataRef))
ptr := unsafe.Pointer(C.CFDataGetBytePtr(cfDataRef))
var slice []byte
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
hdrp.Data = uintptr(ptr)
hdrp.Len = length
hdrp.Cap = length
imageBytes := make([]byte, length)
for i := 0; i < length; i += 4 {
imageBytes[i], imageBytes[i+2], imageBytes[i+1], imageBytes[i+3] = slice[i+2], slice[i], slice[i+1], slice[i+3]
}
img := &image.RGBA{Pix: imageBytes, Stride: 4 * width, Rect: rect}
return img, nil
}