mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-06 16:53:56 +00:00
Merge pull request #363 from go-vgo/bitmap-pr
add ImgToBitmap and RGBAToBitmap support
This commit is contained in:
commit
c55761e55f
@ -368,6 +368,6 @@ Some discussions and questions, please see [issues/228](https://github.com/go-vg
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Robotgo is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
|
Robotgo is primarily distributed under the terms of "both the MIT license and the Apache License (Version 2.0)", with portions covered by various BSD-like licenses.
|
||||||
|
|
||||||
See [LICENSE-APACHE](http://www.apache.org/licenses/LICENSE-2.0), [LICENSE-MIT](https://github.com/go-vgo/robotgo/blob/master/LICENSE).
|
See [LICENSE-APACHE](http://www.apache.org/licenses/LICENSE-2.0), [LICENSE-MIT](https://github.com/go-vgo/robotgo/blob/master/LICENSE).
|
||||||
|
2
go.mod
2
go.mod
@ -9,7 +9,7 @@ require (
|
|||||||
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934
|
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934
|
||||||
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770
|
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770
|
||||||
github.com/vcaesar/gops v0.21.1
|
github.com/vcaesar/gops v0.21.1
|
||||||
github.com/vcaesar/imgo v0.12.2
|
github.com/vcaesar/imgo v0.20.0
|
||||||
github.com/vcaesar/tt v0.11.0
|
github.com/vcaesar/tt v0.11.0
|
||||||
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
|
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
|
||||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
|
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
|
||||||
|
4
go.sum
4
go.sum
@ -36,8 +36,8 @@ github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2bi
|
|||||||
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
|
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
|
||||||
github.com/vcaesar/gops v0.21.1 h1:a8ZCbho+K3RP1+hlkGCSWih84Iu3hjXYKwZW3970hkg=
|
github.com/vcaesar/gops v0.21.1 h1:a8ZCbho+K3RP1+hlkGCSWih84Iu3hjXYKwZW3970hkg=
|
||||||
github.com/vcaesar/gops v0.21.1/go.mod h1:upqQTBNSvazU+AvHwXY7VPdJscLoOqOxw+vkmJKnjEk=
|
github.com/vcaesar/gops v0.21.1/go.mod h1:upqQTBNSvazU+AvHwXY7VPdJscLoOqOxw+vkmJKnjEk=
|
||||||
github.com/vcaesar/imgo v0.12.2 h1:PvIcE/832kDyxou+sE8ClSqXoPVU9xEMY47ieICkWhg=
|
github.com/vcaesar/imgo v0.20.0 h1:lX7a2vygTri0Z3VYpwoQA2LPvbphFyT5ON4NTqdC4wQ=
|
||||||
github.com/vcaesar/imgo v0.12.2/go.mod h1:eJscuTEdc6sVn/hZruy8kWi61xqKHLtbAYyPeYcn+t4=
|
github.com/vcaesar/imgo v0.20.0/go.mod h1:eJscuTEdc6sVn/hZruy8kWi61xqKHLtbAYyPeYcn+t4=
|
||||||
github.com/vcaesar/tt v0.11.0 h1:obQecjgbnAxxC6OYGY6yDvhGRW2PR5wD8Ma2uJH3WGA=
|
github.com/vcaesar/tt v0.11.0 h1:obQecjgbnAxxC6OYGY6yDvhGRW2PR5wD8Ma2uJH3WGA=
|
||||||
github.com/vcaesar/tt v0.11.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
|
github.com/vcaesar/tt v0.11.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
129
img.go
Normal file
129
img.go
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
package robotgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/vcaesar/imgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DecodeImg decode the image to image.Image and return
|
||||||
|
func DecodeImg(path string) (image.Image, string, error) {
|
||||||
|
return imgo.DecodeFile(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenImg open the image return []byte
|
||||||
|
func OpenImg(path string) ([]byte, error) {
|
||||||
|
return imgo.ImgToBytes(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveImg save the image by []byte
|
||||||
|
func SaveImg(b []byte, path string) error {
|
||||||
|
return imgo.Save(path, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SavePng save the image by image.Image
|
||||||
|
func SavePng(img image.Image, path string) error {
|
||||||
|
return imgo.SaveToPNG(path, img)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToByteImg convert image.Image to []byte
|
||||||
|
func ToByteImg(img image.Image) []byte {
|
||||||
|
return imgo.ToByteImg(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
return imgo.StrToImg(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByteToImg convert []byte to image.Image
|
||||||
|
func ByteToImg(b []byte) (image.Image, error) {
|
||||||
|
return imgo.ByteToImg(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RGBAToBitmap convert the standard image.RGBA to Bitmap
|
||||||
|
func RGBAToBitmap(r1 *image.RGBA) (bit Bitmap) {
|
||||||
|
bit.Width = r1.Bounds().Size().X
|
||||||
|
bit.Height = r1.Bounds().Size().Y
|
||||||
|
bit.Bytewidth = r1.Stride
|
||||||
|
|
||||||
|
src := ToUint8p(r1.Pix)
|
||||||
|
bit.ImgBuf = src
|
||||||
|
|
||||||
|
bit.BitsPixel = 32
|
||||||
|
bit.BytesPerPixel = 32 / 8
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImgToBitmap convert the standard image.Image to Bitmap
|
||||||
|
func ImgToBitmap(m image.Image) (bit Bitmap) {
|
||||||
|
bit.Width = m.Bounds().Size().X
|
||||||
|
bit.Height = m.Bounds().Size().Y
|
||||||
|
|
||||||
|
pix, stride, _ := imgo.EncodeImg(m)
|
||||||
|
bit.Bytewidth = stride
|
||||||
|
|
||||||
|
src := ToUint8p(pix)
|
||||||
|
bit.ImgBuf = src
|
||||||
|
//
|
||||||
|
bit.BitsPixel = 32
|
||||||
|
bit.BytesPerPixel = 32 / 8
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUint8p convert the []uint8 to uint8 pointer
|
||||||
|
func ToUint8p(dst []uint8) *uint8 {
|
||||||
|
src := make([]uint8, len(dst)+10)
|
||||||
|
for i := 0; i < len(dst)-4; i += 4 {
|
||||||
|
src[i+3] = dst[i+3]
|
||||||
|
src[i] = dst[i+2]
|
||||||
|
src[i+1] = dst[i+1]
|
||||||
|
src[i+2] = dst[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr := unsafe.Pointer(&src[0])
|
||||||
|
return (*uint8)(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToRGBAGo convert Bitmap to standard image.RGBA
|
||||||
|
func ToRGBAGo(bmp1 Bitmap) *image.RGBA {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
77
robotgo.go
77
robotgo.go
@ -51,7 +51,6 @@ package robotgo
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
|
|
||||||
@ -64,12 +63,9 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
// "syscall"
|
// "syscall"
|
||||||
"encoding/base64"
|
|
||||||
"image/jpeg"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/go-vgo/robotgo/clipboard"
|
"github.com/go-vgo/robotgo/clipboard"
|
||||||
"github.com/vcaesar/imgo"
|
|
||||||
"github.com/vcaesar/tt"
|
"github.com/vcaesar/tt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -951,36 +947,6 @@ func TocharBitmap(bit C.MMBitmapRef) *C.char {
|
|||||||
return strBit
|
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
|
// ToImage convert C.MMBitmapRef to standard image.Image
|
||||||
func ToImage(bit C.MMBitmapRef) image.Image {
|
func ToImage(bit C.MMBitmapRef) image.Image {
|
||||||
return ToRGBA(bit)
|
return ToRGBA(bit)
|
||||||
@ -989,28 +955,7 @@ func ToImage(bit C.MMBitmapRef) image.Image {
|
|||||||
// ToRGBA convert C.MMBitmapRef to standard image.RGBA
|
// ToRGBA convert C.MMBitmapRef to standard image.RGBA
|
||||||
func ToRGBA(bit C.MMBitmapRef) *image.RGBA {
|
func ToRGBA(bit C.MMBitmapRef) *image.RGBA {
|
||||||
bmp1 := ToBitmap(bit)
|
bmp1 := ToBitmap(bit)
|
||||||
img1 := image.NewRGBA(image.Rect(0, 0, bmp1.Width, bmp1.Height))
|
return ToRGBAGo(bmp1)
|
||||||
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) {
|
func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) {
|
||||||
@ -1195,26 +1140,6 @@ func OpenBitmap(gpath string, args ...int) C.MMBitmapRef {
|
|||||||
return bit
|
return bit
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeImg decode the image to image.Image and return
|
|
||||||
func DecodeImg(path string) (image.Image, string, error) {
|
|
||||||
return imgo.DecodeFile(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OpenImg open the image return []byte
|
|
||||||
func OpenImg(path string) ([]byte, error) {
|
|
||||||
return imgo.ImgToBytes(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaveImg save the image by []byte
|
|
||||||
func SaveImg(b []byte, path string) error {
|
|
||||||
return imgo.Save(path, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SavePng save the image by image.Image
|
|
||||||
func SavePng(img image.Image, path string) error {
|
|
||||||
return imgo.SaveToPNG(path, img)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BitmapStr bitmap from string
|
// BitmapStr bitmap from string
|
||||||
func BitmapStr(str string) C.MMBitmapRef {
|
func BitmapStr(str string) C.MMBitmapRef {
|
||||||
return BitmapFromStr(str)
|
return BitmapFromStr(str)
|
||||||
|
Loading…
Reference in New Issue
Block a user