From c22af9f286f165f6c0a9c8a06d4a742610a47500 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 30 May 2021 13:17:32 -0400 Subject: [PATCH] add more image convert function --- robotgo.go | 92 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 33 deletions(-) diff --git a/robotgo.go b/robotgo.go index e0e2f40..554f64b 100644 --- a/robotgo.go +++ b/robotgo.go @@ -62,6 +62,8 @@ import ( "unsafe" // "syscall" + "encoding/base64" + "image/jpeg" "os/exec" "github.com/go-vgo/robotgo/clipboard" @@ -940,6 +942,63 @@ func TocharBitmap(bit C.MMBitmapRef) *C.char { return strBit } +// ToByteImg convert image.Image to []byte +func ToByteImg(img image.Image) []byte { + buff := bytes.NewBuffer(nil) + jpeg.Encode(buff, img, nil) + + var dist []byte + base64.StdEncoding.Encode(dist, buff.Bytes()) + + return dist +} + +// ToStringImg convert image.Image to string +func ToStringImg(img image.Image) string { + return string(ToByteImg(img)) +} + +// StrToImg convert base64 string to image.Image +func StrToImg(data string) (image.Image, error) { + reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data)) + m, _, err := image.Decode(reader) + + return m, err +} + +// ByteToImg convert []byte to image.Image +func ByteToImg(b []byte) (image.Image, error) { + img, _, err := image.Decode(bytes.NewReader(b)) + return img, 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) + } +} + func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) { pos := C.find_bitmap(bit, sbit, C.float(tolerance)) // fmt.Println("pos----", pos) @@ -1127,39 +1186,6 @@ func DecodeImg(path string) (image.Image, string, error) { return imgo.DecodeFile(path) } -// ByteToImg convert []byte to image.Image -func ByteToImg(b []byte) (image.Image, error) { - img, _, err := image.Decode(bytes.NewReader(b)) - return img, 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) - } -} - // OpenImg open the image return []byte func OpenImg(path string) ([]byte, error) { return imgo.ImgToBytes(path)