From debdcc31d8c720465e50b2fef39654f783a19db0 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Wed, 8 Sep 2021 13:13:23 -0400 Subject: [PATCH] add ImgToBitmap and RGBAToBitmap support --- go.mod | 2 +- go.sum | 4 ++-- img.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 img.go diff --git a/go.mod b/go.mod index a2717c6..73ae9f9 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934 github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770 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 golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect diff --git a/go.sum b/go.sum index 5972875..d0a7a73 100644 --- a/go.sum +++ b/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/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/imgo v0.12.2 h1:PvIcE/832kDyxou+sE8ClSqXoPVU9xEMY47ieICkWhg= -github.com/vcaesar/imgo v0.12.2/go.mod h1:eJscuTEdc6sVn/hZruy8kWi61xqKHLtbAYyPeYcn+t4= +github.com/vcaesar/imgo v0.20.0 h1:lX7a2vygTri0Z3VYpwoQA2LPvbphFyT5ON4NTqdC4wQ= +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/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/img.go b/img.go new file mode 100644 index 0000000..c526ae3 --- /dev/null +++ b/img.go @@ -0,0 +1,63 @@ +// 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 or the MIT license +// , 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" +) + +// 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) +}