diff --git a/README.md b/README.md index 0275d57..fd43ea0 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# bitmap \ No newline at end of file +# bitmap + +## Use + +```Go +package main + +import ( + "fmt" + + "github.com/go-vgo/robotgo" + "github.com/vcaesar/bitmap" +) + +func main() { + bit := robotgo.CaptureScreen(10, 20, 30, 40) + // use `defer robotgo.FreeBitmap(bit)` to free the bitmap + defer robotgo.FreeBitmap(bit) + + fmt.Println("bitmap...", bit) + img := robotgo.ToImage(bit) + // robotgo.SavePng(img, "test_1.png") + robotgo.Save(img, "test_1.png") + + bit2 := robotgo.ToCBitmap(robotgo.ImgToBitmap(img)) + fx, fy := bitmap.Find(bit2) + fmt.Println("FindBitmap------ ", fx, fy) + robotgo.Move(fx, fy) + + arr := bitmap.FindAll(bit2) + fmt.Println("Find all bitmap: ", arr) + + fx, fy = bitmap.Find(bit) + fmt.Println("FindBitmap------ ", fx, fy) + + bitmap.Save(bit, "test.png") +} +``` \ No newline at end of file diff --git a/base/MMBitmap.h b/base/MMBitmap.h index 744d2d0..dbd7fae 100644 --- a/base/MMBitmap.h +++ b/base/MMBitmap.h @@ -18,11 +18,10 @@ extern "C" #endif struct _MMBitmap { - uint8_t *imageBuffer; /* Pixels stored in Quad I format; i.e., origin is in - * top left. Length should be height * bytewidth. */ - size_t width; /* Never 0, unless image is NULL. */ - size_t height; /* Never 0, unless image is NULL. */ - size_t bytewidth; /* The aligned width (width + padding). */ + uint8_t *imageBuffer; /* Pixels stored in Quad I format; i.e., origin is in top left. Length should be height * bytewidth. */ + int32_t width; /* Never 0, unless image is NULL. */ + int32_t height; /* Never 0, unless image is NULL. */ + int32_t bytewidth; /* The aligned width (width + padding). */ uint8_t bitsPerPixel; /* Should be either 24 or 32. */ uint8_t bytesPerPixel; /* For convenience; should be bitsPerPixel / 8. */ }; @@ -33,9 +32,8 @@ typedef MMBitmap *MMBitmapRef; /* Creates new MMBitmap with the given values. * Follows the Create Rule (caller is responsible for destroy()'ing object). */ -MMBitmapRef createMMBitmap(uint8_t *buffer, size_t width, size_t height, - size_t bytewidth, uint8_t bitsPerPixel, - uint8_t bytesPerPixel); +MMBitmapRef createMMBitmap(uint8_t *buffer, int32_t width, int32_t height, + int32_t bytewidth, uint8_t bitsPerPixel, uint8_t bytesPerPixel); // /* Releases memory occupied by MMBitmap. */ // void destroyMMBitmap(MMBitmapRef bitmap); @@ -50,8 +48,8 @@ MMBitmapRef copyMMBitmap(MMBitmapRef bitmap); * by the caller.), or NULL on error. */ MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect); -#define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && \ - (p).y < (image)->height) +#define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && (p).y < (image)->height) + #define MMBitmapRectInBounds(image, r) \ (((r).origin.x + (r).size.width <= (image)->width) && \ ((r).origin.y + (r).size.height <= (image)->height)) @@ -61,16 +59,14 @@ MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect); /* Get pointer to pixel of MMBitmapRef. No bounds checking is performed (check * yourself before calling this with MMBitmapPointInBounds(). */ #define MMRGBColorRefAtPoint(image, x, y) \ - (MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointMake(x, y))), \ - ((image)->imageBuffer) + (((image)->bytewidth * (y)) \ - + ((x) * (image)->bytesPerPixel))) + (MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointMake(x, y))), \ + ((image)->imageBuffer) + (((image)->bytewidth * (y)) + ((x) * (image)->bytesPerPixel))) /* Dereference pixel of MMBitmapRef. Again, no bounds checking is performed. */ #define MMRGBColorAtPoint(image, x, y) *MMRGBColorRefAtPoint(image, x, y) /* Hex/integer value of color at point. */ -#define MMRGBHexAtPoint(image, x, y) \ - hexFromMMRGB(MMRGBColorAtPoint(image, x, y)) +#define MMRGBHexAtPoint(image, x, y) hexFromMMRGB(MMRGBColorAtPoint(image, x, y)) /* Increment either point.x or point.y depending on the position of point.x. * That is, if x + 1 is >= width, increment y and start x at the beginning. diff --git a/base/bitmap/MMBitmap_c.h b/base/bitmap/MMBitmap_c.h index 6e8e816..5a595f3 100644 --- a/base/bitmap/MMBitmap_c.h +++ b/base/bitmap/MMBitmap_c.h @@ -2,14 +2,9 @@ #include #include -MMBitmapRef createMMBitmap( - uint8_t *buffer, - size_t width, - size_t height, - size_t bytewidth, - uint8_t bitsPerPixel, - uint8_t bytesPerPixel -){ +MMBitmapRef createMMBitmap(uint8_t *buffer, int32_t width, int32_t height, + int32_t bytewidth, uint8_t bitsPerPixel, uint8_t bytesPerPixel +) { MMBitmapRef bitmap = malloc(sizeof(MMBitmap)); if (bitmap == NULL) return NULL; @@ -30,17 +25,13 @@ MMBitmapRef copyMMBitmap(MMBitmapRef bitmap) { if (bitmap->imageBuffer != NULL) { const size_t bufsize = bitmap->height * bitmap->bytewidth; copiedBuf = malloc(bufsize); - if (copiedBuf == NULL) return NULL; + if (copiedBuf == NULL) { return NULL; } memcpy(copiedBuf, bitmap->imageBuffer, bufsize); } - return createMMBitmap(copiedBuf, - bitmap->width, - bitmap->height, - bitmap->bytewidth, - bitmap->bitsPerPixel, - bitmap->bytesPerPixel); + return createMMBitmap(copiedBuf, bitmap->width, bitmap->height, + bitmap->bytewidth, bitmap->bitsPerPixel, bitmap->bytesPerPixel); } MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect) { @@ -58,15 +49,11 @@ MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect) { assert((bufsize + offset) <= (source->bytewidth * source->height)); copiedBuf = malloc(bufsize); - if (copiedBuf == NULL) return NULL; + if (copiedBuf == NULL) { return NULL; } memcpy(copiedBuf, source->imageBuffer + offset, bufsize); - return createMMBitmap(copiedBuf, - rect.size.width, - rect.size.height, - source->bytewidth, - source->bitsPerPixel, - source->bytesPerPixel); + return createMMBitmap(copiedBuf, rect.size.width, rect.size.height, + source->bytewidth, source->bitsPerPixel, source->bytesPerPixel); } } diff --git a/base/bitmap/str_io_c.h b/base/bitmap/str_io_c.h index fa80f8e..d50db7e 100644 --- a/base/bitmap/str_io_c.h +++ b/base/bitmap/str_io_c.h @@ -17,11 +17,9 @@ #define STR_BITS_PER_PIXEL 24 #define STR_BYTES_PER_PIXEL ((STR_BITS_PER_PIXEL) / 8) -#define MAX_DIMENSION_LEN 5 /* Maximum length for [width] or [height] - * in string. */ +#define MAX_DIMENSION_LEN 5 /* Maximum length for [width] or [height] in string. */ -const char *MMBitmapStringErrorString(MMBMPStringError err) -{ +const char *MMBitmapStringErrorString(MMBMPStringError err) { switch (err) { case kMMBMPStringInvalidHeaderError: return "Invalid header for string"; @@ -46,19 +44,14 @@ const char *MMBitmapStringErrorString(MMBMPStringError err) * |len| is set to the length of [width] + the length of [height] + 2, * and true is returned; otherwise, false is returned. */ -static bool getSizeFromString(const uint8_t *buf, size_t buflen, - size_t *width, size_t *height, - size_t *len); +static bool getSizeFromString(const uint8_t *buf, size_t buflen, size_t *width, size_t *height, size_t *len); -MMBitmapRef createMMBitmapFromString(const uint8_t *buffer, size_t buflen, - MMBMPStringError *err) -{ +MMBitmapRef createMMBitmapFromString(const uint8_t *buffer, size_t buflen, MMBMPStringError *err) { uint8_t *decoded, *decompressed; size_t width, height; size_t len, bytewidth; - if (*buffer++ != 'b' || !getSizeFromString(buffer, --buflen, - &width, &height, &len)) { + if (*buffer++ != 'b' || !getSizeFromString(buffer, --buflen, &width, &height, &len)) { if (err != NULL) *err = kMMBMPStringInvalidHeaderError; return NULL; } @@ -96,8 +89,7 @@ MMBitmapRef createMMBitmapFromString(const uint8_t *buffer, size_t buflen, * Caller is responsible for free()'ing returned buffer. */ static uint8_t *createRawBitmapData(MMBitmapRef bitmap); -uint8_t *createStringFromMMBitmap(MMBitmapRef bitmap, MMBMPStringError *err) -{ +uint8_t *createStringFromMMBitmap(MMBitmapRef bitmap, MMBMPStringError *err) { uint8_t *raw, *compressed; uint8_t *ret, *encoded; size_t len, retlen; @@ -110,10 +102,7 @@ uint8_t *createStringFromMMBitmap(MMBitmapRef bitmap, MMBMPStringError *err) return NULL; } - compressed = zlib_compress(raw, - bitmap->width * bitmap->height * - STR_BYTES_PER_PIXEL, - 9, &len); + compressed = zlib_compress(raw, bitmap->width * bitmap->height * STR_BYTES_PER_PIXEL, 9, &len); free(raw); if (compressed == NULL) { if (err != NULL) *err = kMMBMPStringCompressError; @@ -130,20 +119,16 @@ uint8_t *createStringFromMMBitmap(MMBitmapRef bitmap, MMBMPStringError *err) retlen += 3 + (MAX_DIMENSION_LEN * 2); ret = calloc(sizeof(char), (retlen + 1)); snprintf((char *)ret, retlen, "b%lu,%lu,%s", (unsigned long)bitmap->width, - (unsigned long)bitmap->height, - encoded); + (unsigned long)bitmap->height, encoded); ret[retlen] = '\0'; free(encoded); return ret; } -static uint32_t parseDimension(const uint8_t *buf, size_t buflen, - size_t *numlen); +static uint32_t parseDimension(const uint8_t *buf, size_t buflen, size_t *numlen); -static bool getSizeFromString(const uint8_t *buf, size_t buflen, - size_t *width, size_t *height, - size_t *len) -{ +static bool getSizeFromString(const uint8_t *buf, size_t buflen, + size_t *width, size_t *height, size_t *len) { size_t numlen; assert(buf != NULL); assert(width != NULL); @@ -164,8 +149,7 @@ static bool getSizeFromString(const uint8_t *buf, size_t buflen, /* Parses one dimension from string as described in getSizeFromString(). * Returns dimension on success, or 0 on error. */ -static uint32_t parseDimension(const uint8_t *buf, - size_t buflen, size_t *numlen){ +static uint32_t parseDimension(const uint8_t *buf, size_t buflen, size_t *numlen) { char num[MAX_DIMENSION_LEN + 1]; size_t i; // ssize_t len; @@ -184,8 +168,7 @@ static uint32_t parseDimension(const uint8_t *buf, return (uint32_t)atoi(num); } -static uint8_t *createRawBitmapData(MMBitmapRef bitmap) -{ +static uint8_t *createRawBitmapData(MMBitmapRef bitmap) { uint8_t *raw = calloc(STR_BYTES_PER_PIXEL, bitmap->width * bitmap->height); size_t y; diff --git a/base/os.h b/base/os.h index ab46989..2c267c2 100644 --- a/base/os.h +++ b/base/os.h @@ -5,8 +5,7 @@ /* Python versions under 2.5 don't support this macro, but it's not * terribly difficult to replicate: */ #ifndef PyModule_AddIntMacro - #define PyModule_AddIntMacro(module, macro) \ - PyModule_AddIntConstant(module, #macro, macro) + #define PyModule_AddIntMacro(module, macro) PyModule_AddIntConstant(module, #macro, macro) #endif /* PyModule_AddIntMacro */ #if !defined(IS_MACOSX) && defined(__APPLE__) && defined(__MACH__) diff --git a/base/rgb.h b/base/rgb.h index db0e3f7..0810fc4 100644 --- a/base/rgb.h +++ b/base/rgb.h @@ -12,7 +12,6 @@ #include #endif - /* RGB colors in MMBitmaps are stored as BGR for convenience in converting * to/from certain formats (mainly OpenGL). * @@ -46,8 +45,7 @@ typedef uint32_t MMRGBHex; #define RGB_TO_HEX(red, green, blue) (((red) << 16) | ((green) << 8) | (blue)) /* Convenience wrapper for MMRGBColors. */ -H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb) -{ +H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb) { return RGB_TO_HEX(rgb.red, rgb.green, rgb.blue); } @@ -56,8 +54,7 @@ H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb) #define BLUE_FROM_HEX(hex) (hex & 0xFF) /* Converts hexadecimal color to MMRGBColor. */ -H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex) -{ +H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex) { MMRGBColor color; color.red = RED_FROM_HEX(hex); color.green = GREEN_FROM_HEX(hex); @@ -73,9 +70,7 @@ H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex) /* Returns whether two colors are similar within the given range, |tolerance|. * Tolerance can be in the range 0.0f - 1.0f, where 0 denotes the exact * color and 1 denotes any color. */ -H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2, - float tolerance) -{ +H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2, float tolerance) { /* Speedy case */ if (tolerance <= 0.0f) { return MMRGBColorEqualToColor(c1, c2); @@ -87,12 +82,10 @@ H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2, (d2 * d2) + (d3 * d3)) <= (tolerance * 442.0f); } - } /* Identical to MMRGBColorSimilarToColor, only for hex values. */ -H_INLINE int MMRGBHexSimilarToColor(MMRGBHex h1, MMRGBHex h2, float tolerance) -{ +H_INLINE int MMRGBHexSimilarToColor(MMRGBHex h1, MMRGBHex h2, float tolerance) { if (tolerance <= 0.0f) { return h1 == h2; } else { diff --git a/bitmap.go b/bitmap.go index cba39d7..852b8dc 100644 --- a/bitmap.go +++ b/bitmap.go @@ -70,9 +70,9 @@ func ToRobot(bit C.MMBitmapRef) robotgo.CBitmap { func ToC(bit robotgo.Bitmap) C.MMBitmapRef { cbitmap := C.createMMBitmap( (*C.uint8_t)(bit.ImgBuf), - C.size_t(bit.Width), - C.size_t(bit.Height), - C.size_t(bit.Bytewidth), + C.int32_t(bit.Width), + C.int32_t(bit.Height), + C.int32_t(bit.Bytewidth), C.uint8_t(bit.BitsPixel), C.uint8_t(bit.BytesPerPixel), ) @@ -289,8 +289,8 @@ func Open(path string, args ...int) robotgo.CBitmap { return ToRobot(OpenC(path, args...)) } -// FromStr bitmap from string -func FromStr(str string) C.MMBitmapRef { +// FromStrC bitmap from string +func FromStrC(str string) C.MMBitmapRef { cs := C.CString(str) bit := C.bitmap_from_string(cs) C.free(unsafe.Pointer(cs)) @@ -298,6 +298,11 @@ func FromStr(str string) C.MMBitmapRef { return bit } +// FromStr bitmap from string +func FromStr(str string) robotgo.CBitmap { + return ToRobot(FromStrC(str)) +} + func toErr(str *C.char) error { gstr := C.GoString(str) if gstr == "" { diff --git a/c/goBitmap.h b/c/goBitmap.h index 8e70d74..fd785b2 100644 --- a/c/goBitmap.h +++ b/c/goBitmap.h @@ -21,7 +21,7 @@ #include /* Returns false and sets error if |bitmap| is NULL. */ -bool bitmap_ready(MMBitmapRef bitmap){ +bool bitmap_ready(MMBitmapRef bitmap) { if (bitmap == NULL || bitmap->imageBuffer == NULL) { return false; } @@ -35,10 +35,10 @@ bool bitmap_ready(MMBitmapRef bitmap){ // } // } -bool bitmap_copy_to_pboard(MMBitmapRef bitmap){ +bool bitmap_copy_to_pboard(MMBitmapRef bitmap) { MMPasteError err; + if (!bitmap_ready(bitmap)) { return false; } - if (!bitmap_ready(bitmap)) return false; if ((err = copyMMBitmapToPasteboard(bitmap)) != kMMPasteNoError) { return false; } @@ -46,22 +46,18 @@ bool bitmap_copy_to_pboard(MMBitmapRef bitmap){ return true; } -MMBitmapRef bitmap_deepcopy(MMBitmapRef bitmap){ +MMBitmapRef bitmap_deepcopy(MMBitmapRef bitmap) { return bitmap == NULL ? NULL : copyMMBitmap(bitmap); } -MMPoint find_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance){ +MMPoint find_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance) { MMPoint point = {-1, -1}; - // printf("tolenrance=%f\n", tolerance); if (!bitmap_ready(sbit) || !bitmap_ready(bitmap)) { printf("bitmap is not ready yet!\n"); return point; } MMRect rect = MMBitmapGetBounds(sbit); - // printf("x=%d, y=%d, width=%d, height=%d\n", rect.origin.x, - // rect.origin.y, rect.size.width, rect.size.height); - if (findBitmapInRect(bitmap, sbit, &point, rect, tolerance) == 0) { return point; } @@ -69,7 +65,7 @@ MMPoint find_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance){ return point; } -MMPointArrayRef find_every_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance, MMPoint *list){ +MMPointArrayRef find_every_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance, MMPoint *list) { if (!bitmap_ready(bitmap) || !bitmap_ready(sbit)) { return NULL; } MMPoint point; @@ -84,14 +80,14 @@ MMPointArrayRef find_every_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float to return pointArray; } -int count_of_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance){ +int count_of_bitmap(MMBitmapRef bitmap, MMBitmapRef sbit, float tolerance) { if (!bitmap_ready(bitmap) || !bitmap_ready(sbit)) { return 0; } MMRect rect = MMBitmapGetBounds(bitmap); return countOfBitmapInRect(bitmap, sbit, rect, tolerance); } -bool point_in_bounds(MMBitmapRef bitmap, MMPoint point){ +bool point_in_bounds(MMBitmapRef bitmap, MMPoint point) { if (!bitmap_ready(bitmap)) { return NULL; } @@ -103,8 +99,7 @@ bool point_in_bounds(MMBitmapRef bitmap, MMPoint point){ return false; } -MMBitmapRef bitmap_open(char *path, uint16_t ttype){ - // MMImageType type; +MMBitmapRef bitmap_open(char *path, uint16_t ttype) { MMBitmapRef bitmap; MMIOError err; @@ -112,21 +107,19 @@ MMBitmapRef bitmap_open(char *path, uint16_t ttype){ return bitmap; } -MMBitmapRef bitmap_from_string(const char *str){ +MMBitmapRef bitmap_from_string(const char *str) { size_t len = strlen(str); - MMBitmapRef bitmap; MMBMPStringError err; - if ((bitmap = createMMBitmapFromString((unsigned char*)str, len, &err)) - == NULL) { + if ((bitmap = createMMBitmapFromString((unsigned char*)str, len, &err)) == NULL) { return NULL; } return bitmap; } -char *bitmap_save(MMBitmapRef bitmap, char *path, uint16_t type){ +char *bitmap_save(MMBitmapRef bitmap, char *path, uint16_t type) { if (saveMMBitmapToFile(bitmap, path, (MMImageType) type) != 0) { return "Could not save image to file."; } @@ -134,7 +127,7 @@ char *bitmap_save(MMBitmapRef bitmap, char *path, uint16_t type){ return ""; } -char *tostring_bitmap(MMBitmapRef bitmap){ +char *tostring_bitmap(MMBitmapRef bitmap) { char *buf = NULL; MMBMPStringError err; @@ -143,7 +136,7 @@ char *tostring_bitmap(MMBitmapRef bitmap){ } // char out size 200 is enough -bool bitmap_str(MMBitmapRef bitmap, char *out){ +bool bitmap_str(MMBitmapRef bitmap, char *out) { if (!bitmap_ready(bitmap)) { return false; } sprintf(out, "", @@ -155,7 +148,7 @@ bool bitmap_str(MMBitmapRef bitmap, char *out){ return true; } -MMBitmapRef get_portion(MMBitmapRef bit_map, MMRect rect){ +MMBitmapRef get_portion(MMBitmapRef bit_map, MMRect rect) { // MMRect rect; MMBitmapRef portion = NULL; @@ -163,12 +156,10 @@ MMBitmapRef get_portion(MMBitmapRef bit_map, MMRect rect){ return portion; } -MMRGBHex bitmap_get_color(MMBitmapRef bitmap, size_t x, size_t y){ +MMRGBHex bitmap_get_color(MMBitmapRef bitmap, size_t x, size_t y) { if (!bitmap_ready(bitmap)) { return 0; } - MMPoint point; - point = MMPointMake(x, y); - + MMPoint point = MMPointMake(x, y); if (!MMBitmapPointInBounds(bitmap, point)) { return 0; } @@ -176,7 +167,7 @@ MMRGBHex bitmap_get_color(MMBitmapRef bitmap, size_t x, size_t y){ return MMRGBHexAtPoint(bitmap, point.x, point.y); } -MMPoint bitmap_find_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance){ +MMPoint bitmap_find_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance) { MMRect rect = MMBitmapGetBounds(bitmap); MMPoint point = {-1, -1}; @@ -187,7 +178,7 @@ MMPoint bitmap_find_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance){ return point; } -MMPointArrayRef bitmap_find_every_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance, MMPoint *list){ +MMPointArrayRef bitmap_find_every_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance, MMPoint *list) { if (!bitmap_ready(bitmap)) { return NULL; } MMRect rect = MMBitmapGetBounds(bitmap); MMPointArrayRef pointArray; @@ -196,8 +187,8 @@ MMPointArrayRef bitmap_find_every_color(MMBitmapRef bitmap, MMRGBHex color, floa return pointArray; } -int bitmap_count_of_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance){ - if (!bitmap_ready(bitmap)) return 0; +int bitmap_count_of_color(MMBitmapRef bitmap, MMRGBHex color, float tolerance) { + if (!bitmap_ready(bitmap)) { return 0; } MMRect rect = MMBitmapGetBounds(bitmap); return countOfColorsInRect(bitmap, color, rect, tolerance); diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..a3febbf --- /dev/null +++ b/examples/main.go @@ -0,0 +1,185 @@ +// 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 main + +import ( + "fmt" + "log" + + "github.com/go-vgo/robotgo" + "github.com/vcaesar/bitmap" + "github.com/vcaesar/imgo" +) + +func toBitmap(bmp robotgo.CBitmap) { + img := robotgo.ToImage(bmp) + fmt.Println("img: ", img) + imgo.SaveToPNG("test_IMG.png", img) + + gbit := robotgo.ToBitmap(bmp) + fmt.Println("go bitmap", gbit, gbit.Width) + + cbit := robotgo.ToCBitmap(gbit) + // defer robotgo.FreeBitmap(cbit) + log.Println("cbit == bitmap: ", cbit == bmp) +} + +// find color +func findColor(bmp robotgo.CBitmap) { + // find the color in bitmap + color := bitmap.GetColor(bmp, 1, 2) + fmt.Println("color...", color) + + cx, cy := bitmap.FindColor(robotgo.CHex(color), bmp, 1.0) + fmt.Println("pos...", cx, cy) + cx, cy = bitmap.FindColor(robotgo.CHex(color)) + fmt.Println("pos...", cx, cy) + + cx, cy = bitmap.FindColor(0xAADCDC, bmp) + fmt.Println("pos...", cx, cy) + + cx, cy = bitmap.FindColor(0xAADCDC, nil, 0.1) + fmt.Println("pos...", cx, cy) + + cx, cy = bitmap.FindColorCS(0xAADCDC, 388, 179, 300, 300) + fmt.Println("pos...", cx, cy) + + cnt := bitmap.CountColor(0xAADCDC, bmp) + fmt.Println("count...", cnt) + + cnt1 := bitmap.CountColorCS(0xAADCDC, 10, 20, 30, 40) + fmt.Println("count...", cnt1) + + arr := bitmap.FindAllColor(0xAADCDC) + fmt.Println("find all color: ", arr) + for i := 0; i < len(arr); i++ { + fmt.Println("pos is: ", arr[i].X, arr[i].Y) + } +} + +func bitmapString(bmp robotgo.CBitmap) { + // creates bitmap from string by bitmap + bitstr := bitmap.Tostring(bmp) + fmt.Println("bitstr...", bitstr) + + sbitmap := bitmap.FromStr(bitstr) + fmt.Println("bitmap str...", sbitmap) + + bitmap.Save(sbitmap, "teststr.png") +} + +func bitmapTool(bmp robotgo.CBitmap) { + abool := bitmap.PointInBounds(bmp, 1, 2) + fmt.Println("point in bounds...", abool) + + // returns new bitmap object created from a portion of another + bitpos := bitmap.GetPortion(bmp, 10, 10, 11, 10) + fmt.Println(bitpos) + + // saves image to absolute filepath in the given format + bitmap.Save(bmp, "test.png") +} + +func decode() { + img, name, err := robotgo.DecodeImg("test.png") + if err != nil { + log.Println("decode image ", err) + } + fmt.Println("decode test.png", img, name) + + byt, _ := robotgo.OpenImg("test.png") + imgo.SaveByte("test2.png", byt) + + w, h := bitmap.GetSize("test.png") + fmt.Println("image width and hight ", w, h) + w, h, _ = imgo.GetSize("test.png") + fmt.Println("image width and hight ", w, h) + + // convert image + bitmap.Convert("test.png", "test.tif") +} + +func bitmapTest(bmp robotgo.CBitmap) { + bit := robotgo.CaptureScreen(1, 2, 40, 40) + defer robotgo.FreeBitmap(bit) + fmt.Println("CaptureScreen...", bit) + + // searches for needle in bitmap + fx, fy := bitmap.Find(bit, bmp) + fmt.Println("FindBitmap------", fx, fy) + + fx, fy = bitmap.Find(bit) + fmt.Println("FindBitmap------", fx, fy) + + fx, fy = bitmap.Find(bit, nil, 0.2) + fmt.Println("find bitmap: ", fx, fy) + + fx, fy = bitmap.Find(bit, bmp, 0.3) + fmt.Println("find bitmap: ", fx, fy) +} + +func findBitmap(bmp robotgo.CBitmap) { + fx, fy := bitmap.Find(bmp) + fmt.Println("findBitmap: ", fx, fy) + + // open image bitmap + openbit := bitmap.Open("test.tif") + fmt.Println("openBitmap...", openbit) + + fx, fy = bitmap.Find(openbit) + fmt.Println("FindBitmap------", fx, fy) + + fx, fy = bitmap.FindPic("test.tif") + fmt.Println("FindPic------", fx, fy) + + arr := bitmap.FindAll(openbit) + fmt.Println("find all bitmap: ", arr) + for i := 0; i < len(arr); i++ { + fmt.Println("pos is: ", arr[i].X, arr[i].Y) + } +} + +func bitmap1() { + //////////////////////////////////////////////////////////////////////////////// + // Bitmap + //////////////////////////////////////////////////////////////////////////////// + + // gets all of the screen + abitMap := robotgo.CaptureScreen() + fmt.Println("abitMap...", abitMap) + + // gets part of the screen + cbit := robotgo.CaptureScreen(100, 200, 30, 30) + defer robotgo.FreeBitmap(cbit) + fmt.Println("CaptureScreen...", cbit) + + toBitmap(cbit) + + findColor(cbit) + + count := bitmap.Count(abitMap, cbit) + fmt.Println("count...", count) + + bitmapTest(cbit) + findBitmap(cbit) + + bitmapString(cbit) + bitmapTool(cbit) + + decode() + + // free the bitmap + robotgo.FreeBitmap(abitMap) +} + +func main() { + bitmap1() +} diff --git a/go.mod b/go.mod index eadae80..66b222b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/vcaesar/bitmap go 1.17 require ( - github.com/go-vgo/robotgo v1.0.0-beta3 + github.com/go-vgo/robotgo v1.0.0-beta4.1 github.com/vcaesar/tt v0.20.0 ) diff --git a/go.sum b/go.sum index a390849..3d3edd0 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-vgo/robotgo v1.0.0-beta3 h1:jdMuqLkFBLEZ1nJLEiSOPjNZG9vGF2d5rnRSx5RatdA= -github.com/go-vgo/robotgo v1.0.0-beta3/go.mod h1:Qrtib2lSWYnVZKEmt5K6TjXl5I2JAGQ4nbC6Ow6NFic= +github.com/go-vgo/robotgo v1.0.0-beta4.1 h1:781OUMf5u6rXXFVBjrM32ZFs5iw3HBDNXB8Kwrjell8= +github.com/go-vgo/robotgo v1.0.0-beta4.1/go.mod h1:Qrtib2lSWYnVZKEmt5K6TjXl5I2JAGQ4nbC6Ow6NFic= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=