Skip to content

Commit

Permalink
add more image convert function
Browse files Browse the repository at this point in the history
  • Loading branch information
vcaesar committed May 30, 2021
1 parent bdec2bf commit c22af9f
Showing 1 changed file with 59 additions and 33 deletions.
92 changes: 59 additions & 33 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import (
"unsafe"

// "syscall"
"encoding/base64"
"image/jpeg"
"os/exec"

"github.com/go-vgo/robotgo/clipboard"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c22af9f

Please sign in to comment.