mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-03 15:43:55 +00:00
add ToImage examples code
This commit is contained in:
parent
9bbfcc1a55
commit
5528eee031
1
.gitignore
vendored
1
.gitignore
vendored
@ -78,3 +78,4 @@ vendor
|
|||||||
|
|
||||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||||
.glide/
|
.glide/
|
||||||
|
examples/bitmap/test_IMG.png
|
||||||
|
@ -21,6 +21,9 @@ import (
|
|||||||
|
|
||||||
func toBitmap(bmp robotgo.CBitmap) {
|
func toBitmap(bmp robotgo.CBitmap) {
|
||||||
bitmap := robotgo.ToMMBitmapRef(bmp)
|
bitmap := robotgo.ToMMBitmapRef(bmp)
|
||||||
|
img := robotgo.ToImage(bitmap)
|
||||||
|
fmt.Println("img: ", img)
|
||||||
|
imgo.SaveToPNG("test_IMG.png", img)
|
||||||
|
|
||||||
gbit := robotgo.ToBitmap(bitmap)
|
gbit := robotgo.ToBitmap(bitmap)
|
||||||
fmt.Println("go bitmap", gbit, gbit.Width)
|
fmt.Println("go bitmap", gbit, gbit.Width)
|
||||||
|
53
robotgo.go
53
robotgo.go
@ -1107,6 +1107,33 @@ func DecodeImg(path string) (image.Image, string, error) {
|
|||||||
return imgo.DecodeFile(path)
|
return imgo.DecodeFile(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// OpenImg open the image return []byte
|
||||||
func OpenImg(path string) []byte {
|
func OpenImg(path string) []byte {
|
||||||
return imgo.ImgToBytes(path)
|
return imgo.ImgToBytes(path)
|
||||||
@ -1542,29 +1569,3 @@ func ActiveName(name string) error {
|
|||||||
|
|
||||||
return err
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user