From fe3b4b3331a11edf9a471253869d822c419fd40e Mon Sep 17 00:00:00 2001 From: rocket049 Date: Thu, 29 Oct 2020 13:05:40 +0800 Subject: [PATCH] add func ToImage: convert C.MMBitmapRef to standard image.Image --- robotgo.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/robotgo.go b/robotgo.go index ee291ef..c06b51e 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1542,3 +1542,29 @@ func ActiveName(name string) error { return err } + +// ToImage convert C.MMBitmapRef to standard image.Image +func ToImage(bit C.MMBitmapRef) image.Image { + bmp1 := ToBitmap(bit) + img1 := image.NewRGBA(image.Rect(0, 0, bmp1.Width, bmp1.Height)) + img1.Pix = make([]uint8, bmp1.Bytewidth*bmp1.Height) + copyToVUint8A(img1.Pix, bmp1.ImgBuf) + img1.Stride = bmp1.Bytewidth + return img1 +} + +func val(p *uint8, n int) uint8 { + addr := uintptr(unsafe.Pointer(p)) + addr += uintptr(n) + p1 := (*uint8)(unsafe.Pointer(addr)) + return *p1 +} + +func copyToVUint8A(dst []uint8, src *uint8) { + for i := 0; i < len(dst)-4; i += 4 { + dst[i] = val(src, i+2) + dst[i+1] = val(src, i+1) + dst[i+2] = val(src, i) + dst[i+3] = val(src, i+3) + } +}