Merge pull request #556 from go-vgo/bitmap-pr

Add: add capture multi screen default support in windows
This commit is contained in:
Evans 2023-01-19 10:10:45 -08:00 committed by GitHub
commit b20007917d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 21 deletions

View File

@ -72,6 +72,9 @@ var (
// DisplayID set the screen display id // DisplayID set the screen display id
DisplayID = -1 DisplayID = -1
// NotPid used the hwnd not pid in windows
NotPid bool
) )
type ( type (
@ -324,7 +327,11 @@ func CaptureScreen(args ...int) CBitmap {
h = C.int32_t(rect.H) h = C.int32_t(rect.H)
} }
bit := C.capture_screen(x, y, w, h, C.int32_t(displayId)) isPid := 0
if NotPid || len(args) > 5 {
isPid = 1
}
bit := C.capture_screen(x, y, w, h, C.int32_t(displayId), C.int8_t(isPid))
return CBitmap(bit) return CBitmap(bit)
} }

View File

@ -51,7 +51,7 @@ MMRGBHex get_px_color(int32_t x, int32_t y, int32_t display_id) {
return color; return color;
} }
bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1), display_id); bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1), display_id, 0);
color = MMRGBHexAtPoint(bitmap, 0, 0); color = MMRGBHexAtPoint(bitmap, 0, 0);
destroyMMBitmap(bitmap); destroyMMBitmap(bitmap);
@ -114,8 +114,8 @@ void bitmap_dealloc(MMBitmapRef bitmap) {
} }
// capture_screen capture screen // capture_screen capture screen
MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h, int32_t display_id) { MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h, int32_t display_id, int8_t isPid) {
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h), display_id); MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h), display_id, isPid);
return bitmap; return bitmap;
} }

View File

@ -13,7 +13,7 @@
#include <string.h> #include <string.h>
#endif #endif
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id) { MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id, int8_t isPid) {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
MMBitmapRef bitmap = NULL; MMBitmapRef bitmap = NULL;
uint8_t *buffer = NULL; uint8_t *buffer = NULL;
@ -24,8 +24,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
displayID = CGMainDisplayID(); displayID = CGMainDisplayID();
} }
CGImageRef image = CGDisplayCreateImageForRect(displayID, MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
CGRectMake(rect.origin.x, rect.origin.y, rect.size.w, rect.size.h)); CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
if (!image) { return NULL; } if (!image) { return NULL; }
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image)); CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
@ -52,14 +52,14 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
display = XGetMainDisplay(); display = XGetMainDisplay();
} }
MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
XImage *image = XGetImage(display, XDefaultRootWindow(display), XImage *image = XGetImage(display, XDefaultRootWindow(display),
(int)rect.origin.x, (int)rect.origin.y, (int)o.x, (int)o.y, (unsigned int)s.w, (unsigned int)s.h, AllPlanes, ZPixmap);
(unsigned int)rect.size.w, (unsigned int)rect.size.h, AllPlanes, ZPixmap);
XCloseDisplay(display); XCloseDisplay(display);
if (image == NULL) { return NULL; } if (image == NULL) { return NULL; }
bitmap = createMMBitmap_c((uint8_t *)image->data, bitmap = createMMBitmap_c((uint8_t *)image->data,
rect.size.w, rect.size.h, (size_t)image->bytes_per_line, s.w, s.h, (size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8); (uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
image->data = NULL; /* Steal ownership of bitmap data so we don't have to copy it. */ image->data = NULL; /* Steal ownership of bitmap data so we don't have to copy it. */
XDestroyImage(image); XDestroyImage(image);
@ -72,29 +72,34 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
HBITMAP dib; HBITMAP dib;
BITMAPINFO bi; BITMAPINFO bi;
int32_t x = rect.origin.x, y = rect.origin.y;
int32_t w = rect.size.w, h = rect.size.h;
/* Initialize bitmap info. */ /* Initialize bitmap info. */
bi.bmiHeader.biSize = sizeof(bi.bmiHeader); bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = (long)rect.size.w; bi.bmiHeader.biWidth = (long) w;
bi.bmiHeader.biHeight = -(long)rect.size.h; /* Non-cartesian, please */ bi.bmiHeader.biHeight = -(long) h; /* Non-cartesian, please */
bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.w * rect.size.h); bi.bmiHeader.biSizeImage = (DWORD)(4 * w * h);
bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0; bi.bmiHeader.biClrImportant = 0;
HWND hwnd; HWND hwnd;
if (display_id == -1) { if (display_id == -1 || isPid == 0) {
screen = GetDC(NULL); /* Get entire screen */ // screen = GetDC(NULL); /* Get entire screen */
hwnd = GetDesktopWindow();
} else { } else {
hwnd = (HWND) (uintptr) display_id; hwnd = (HWND) (uintptr) display_id;
screen = GetDC(hwnd);
} }
screen = GetDC(hwnd);
if (screen == NULL) { return NULL; } if (screen == NULL) { return NULL; }
// Todo: // Todo: Use DXGI
screenMem = CreateCompatibleDC(screen); screenMem = CreateCompatibleDC(screen);
/* Get screen data in display device context. */ /* Get screen data in display device context. */
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0); dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);
@ -102,8 +107,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
/* Copy the data into a bitmap struct. */ /* Copy the data into a bitmap struct. */
BOOL b = (screenMem == NULL) || BOOL b = (screenMem == NULL) ||
SelectObject(screenMem, dib) == NULL || SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem, (int)0, (int)0, (int)rect.size.w, (int)rect.size.h, !BitBlt(screenMem, (int)0, (int)0, (int)w, (int)h, screen, x, y, SRCCOPY);
screen, rect.origin.x, rect.origin.y, SRCCOPY);
if (b) { if (b) {
/* Error copying data. */ /* Error copying data. */
ReleaseDC(hwnd, screen); ReleaseDC(hwnd, screen);
@ -113,8 +117,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
return NULL; return NULL;
} }
bitmap = createMMBitmap_c(NULL, rect.size.w, rect.size.h, 4 * rect.size.w, bitmap = createMMBitmap_c(NULL, w, h, 4 * w, (uint8_t)bi.bmiHeader.biBitCount, 4);
(uint8_t)bi.bmiHeader.biBitCount, 4);
/* Copy the data to our pixel buffer. */ /* Copy the data to our pixel buffer. */
if (bitmap != NULL) { if (bitmap != NULL) {