diff --git a/bitmap.go b/bitmap.go index e6e1674..ca300bc 100644 --- a/bitmap.go +++ b/bitmap.go @@ -26,7 +26,6 @@ package robotgo import "C" import ( - "image" "unsafe" "github.com/vcaesar/tt" @@ -41,18 +40,13 @@ import ( |______/ |__| |__| |__| |__| /__/ \__\ | _| */ -// ToBitmap trans C.MMBitmapRef to Bitmap -func ToBitmap(bit C.MMBitmapRef) Bitmap { - bitmap := Bitmap{ - ImgBuf: (*uint8)(bit.imageBuffer), - Width: int(bit.width), - Height: int(bit.height), - Bytewidth: int(bit.bytewidth), - BitsPixel: uint8(bit.bitsPerPixel), - BytesPerPixel: uint8(bit.bytesPerPixel), - } +// SaveCapture capture screen and save +func SaveCapture(spath string, args ...int) string { + bit := CaptureScreen(args...) - return bitmap + err := SaveBitmap(bit, spath) + FreeBitmap(bit) + return err } // ToCBitmap trans Bitmap to C.MMBitmapRef @@ -69,6 +63,11 @@ func ToCBitmap(bit Bitmap) C.MMBitmapRef { return cbitmap } +// ToMMBitmapRef trans CBitmap to C.MMBitmapRef +func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef { + return C.MMBitmapRef(bit) +} + // ToBitmapBytes saves Bitmap to bitmap format in bytes func ToBitmapBytes(bit C.MMBitmapRef) []byte { var len C.size_t @@ -82,11 +81,6 @@ func ToBitmapBytes(bit C.MMBitmapRef) []byte { return bs } -// ToMMBitmapRef trans CBitmap to C.MMBitmapRef -func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef { - return C.MMBitmapRef(bit) -} - // TostringBitmap tostring bitmap to string func TostringBitmap(bit C.MMBitmapRef) string { strBit := C.tostring_bitmap(bit) @@ -99,17 +93,6 @@ func TocharBitmap(bit C.MMBitmapRef) *C.char { return strBit } -// ToImage convert C.MMBitmapRef to standard image.Image -func ToImage(bit C.MMBitmapRef) image.Image { - return ToRGBA(bit) -} - -// ToRGBA convert C.MMBitmapRef to standard image.RGBA -func ToRGBA(bit C.MMBitmapRef) *image.RGBA { - bmp1 := ToBitmap(bit) - return ToRGBAGo(bmp1) -} - func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) { pos := C.find_bitmap(bit, sbit, C.float(tolerance)) // fmt.Println("pos----", pos) @@ -337,7 +320,7 @@ func GetPortion(bit C.MMBitmapRef, x, y, w, h int) C.MMBitmapRef { // Convert convert the bitmap // // robotgo.Convert(opath, spath string, type int) -func Convert(opath, spath string, args ...int) { +func Convert(opath, spath string, args ...int) string { var mtype = 1 if len(args) > 0 { mtype = args[0] @@ -346,13 +329,7 @@ func Convert(opath, spath string, args ...int) { // C.CString() bitmap := OpenBitmap(opath) // fmt.Println("a----", bit_map) - SaveBitmap(bitmap, spath, mtype) -} - -// FreeBitmap free and dealloc the C bitmap -func FreeBitmap(bitmap C.MMBitmapRef) { - // C.destroyMMBitmap(bitmap) - C.bitmap_dealloc(bitmap) + return SaveBitmap(bitmap, spath, mtype) } // FreeBitmapArr free and dealloc the C bitmap array diff --git a/bitmap/goBitmap.h b/bitmap/goBitmap.h index 429fa39..0a205c3 100644 --- a/bitmap/goBitmap.h +++ b/bitmap/goBitmap.h @@ -26,12 +26,12 @@ bool bitmap_ready(MMBitmapRef bitmap){ return true; } -void bitmap_dealloc(MMBitmapRef bitmap){ - if (bitmap != NULL) { - destroyMMBitmap(bitmap); - bitmap = NULL; - } -} +// void bitmap_dealloc(MMBitmapRef bitmap){ +// if (bitmap != NULL) { +// destroyMMBitmap(bitmap); +// bitmap = NULL; +// } +// } bool bitmap_copy_to_pboard(MMBitmapRef bitmap){ MMPasteError err; diff --git a/cmd/color-pos/main.go b/cmd/color-pos/main.go index 4383a45..b4ec4f9 100644 --- a/cmd/color-pos/main.go +++ b/cmd/color-pos/main.go @@ -7,6 +7,7 @@ import ( ) func colorPicker() { + // click the left mouse button to get the value m := robotgo.AddEvent("mleft") if m { x, y := robotgo.GetMousePos() diff --git a/robotgo.go b/robotgo.go index f22a7cc..22387cb 100644 --- a/robotgo.go +++ b/robotgo.go @@ -371,12 +371,35 @@ func CaptureImg(args ...int) image.Image { return ToImage(bit) } -// SaveCapture capture screen and save -func SaveCapture(spath string, args ...int) { - bit := CaptureScreen(args...) +// FreeBitmap free and dealloc the C bitmap +func FreeBitmap(bitmap C.MMBitmapRef) { + // C.destroyMMBitmap(bitmap) + C.bitmap_dealloc(bitmap) +} - SaveBitmap(bit, spath) - FreeBitmap(bit) +// ToBitmap trans C.MMBitmapRef to Bitmap +func ToBitmap(bit C.MMBitmapRef) Bitmap { + bitmap := Bitmap{ + ImgBuf: (*uint8)(bit.imageBuffer), + Width: int(bit.width), + Height: int(bit.height), + Bytewidth: int(bit.bytewidth), + BitsPixel: uint8(bit.bitsPerPixel), + BytesPerPixel: uint8(bit.bytesPerPixel), + } + + return bitmap +} + +// ToImage convert C.MMBitmapRef to standard image.Image +func ToImage(bit C.MMBitmapRef) image.Image { + return ToRGBA(bit) +} + +// ToRGBA convert C.MMBitmapRef to standard image.RGBA +func ToRGBA(bit C.MMBitmapRef) *image.RGBA { + bmp1 := ToBitmap(bit) + return ToRGBAGo(bmp1) } /* diff --git a/screen/goScreen.h b/screen/goScreen.h index b2d6471..dd22a10 100644 --- a/screen/goScreen.h +++ b/screen/goScreen.h @@ -95,6 +95,13 @@ char* get_XDisplay_name(){ #endif } +void bitmap_dealloc(MMBitmapRef bitmap){ + if (bitmap != NULL) { + destroyMMBitmap(bitmap); + bitmap = NULL; + } +} + // capture_screen capture screen MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h){ // if () {