-
Notifications
You must be signed in to change notification settings - Fork 66
/
screenshot_darwin.go
54 lines (44 loc) · 1.4 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
package screenshot
import (
// #cgo LDFLAGS: -framework CoreGraphics
// #cgo LDFLAGS: -framework CoreFoundation
// #include <CoreGraphics/CoreGraphics.h>
// #include <CoreFoundation/CoreFoundation.h>
"C"
"image"
"reflect"
"unsafe"
"math"
)
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)
rawData := C.CGDataProviderCopyData(C.CGImageGetDataProvider(C.CGDisplayCreateImage(displayID)))
length := int(C.CFDataGetLength(rawData))
ptr := unsafe.Pointer(C.CFDataGetBytePtr(rawData))
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]
}
C.CFRelease(C.CFTypeRef(rawData))
img := &image.RGBA{Pix: imageBytes, Stride: 4 * width, Rect: rect}
return img, nil
}