From a244110982d1451044fd7275546f2085a91017b5 Mon Sep 17 00:00:00 2001 From: kbinani Date: Thu, 29 Sep 2016 20:16:20 +0900 Subject: [PATCH] gofmt -s -w -l . --- example/example.go | 2 +- internal/util/util.go | 4 +- internal/xwindow/xwindow.go | 39 +++--- screenshot_darwin.go | 6 +- screenshot_freebsd.go | 2 +- screenshot_linux.go | 2 +- screenshot_windows.go | 270 ++++++++++++++++++------------------ 7 files changed, 162 insertions(+), 163 deletions(-) diff --git a/example/example.go b/example/example.go index d6b416f..1cb725e 100644 --- a/example/example.go +++ b/example/example.go @@ -1,11 +1,11 @@ package main import ( + "fmt" "github.com/kbinani/screenshot" "image" "image/png" "os" - "fmt" ) // save *image.RGBA to filePath with PNG format. diff --git a/internal/util/util.go b/internal/util/util.go index 24a9432..042be9f 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -1,8 +1,8 @@ package util import ( - "image" "errors" + "image" ) func CreateImage(rect image.Rectangle) (img *image.RGBA, e error) { @@ -14,7 +14,7 @@ func CreateImage(rect image.Rectangle) (img *image.RGBA, e error) { if err == nil { e = nil } - } () + }() // image.NewRGBA may panic if rect is too large. img = image.NewRGBA(rect) diff --git a/internal/xwindow/xwindow.go b/internal/xwindow/xwindow.go index c866429..82fc2cf 100644 --- a/internal/xwindow/xwindow.go +++ b/internal/xwindow/xwindow.go @@ -1,24 +1,24 @@ package xwindow import ( - "fmt" "errors" - "image" - "image/color" - "github.com/kbinani/screenshot/internal/util" + "fmt" "github.com/BurntSushi/xgb" - "github.com/BurntSushi/xgb/xproto" "github.com/BurntSushi/xgb/xinerama" + "github.com/BurntSushi/xgb/xproto" + "github.com/kbinani/screenshot/internal/util" + "image" + "image/color" ) func Capture(x, y, width, height int) (img *image.RGBA, e error) { - defer func () { + defer func() { err := recover() if err != nil { img = nil e = errors.New(fmt.Sprintf("%v", err)) } - } () + }() c, err := xgb.NewConn() if err != nil { return nil, err @@ -37,7 +37,7 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { screen := xproto.Setup(c).DefaultScreen(c) wholeScreenBounds := image.Rect(0, 0, int(screen.WidthInPixels), int(screen.HeightInPixels)) - targetBounds := image.Rect(x + x0, y + y0, x + x0 + width, y + y0 + height) + targetBounds := image.Rect(x+x0, y+y0, x+x0+width, y+y0+height) intersect := wholeScreenBounds.Intersect(targetBounds) rect := image.Rect(0, 0, width, height) @@ -51,7 +51,7 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { for iy := 0; iy < height; iy++ { j := index for ix := 0; ix < width; ix++ { - img.Pix[j + 3] = 255 + img.Pix[j+3] = 255 j += 4 } index += img.Stride @@ -59,8 +59,8 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { if !intersect.Empty() { xImg, err := xproto.GetImage(c, xproto.ImageFormatZPixmap, xproto.Drawable(screen.Root), - int16(intersect.Min.X), int16(intersect.Min.Y), - uint16(intersect.Dx()), uint16(intersect.Dy()), 0xffffffff).Reply() + int16(intersect.Min.X), int16(intersect.Min.Y), + uint16(intersect.Dx()), uint16(intersect.Dy()), 0xffffffff).Reply() if err != nil { return nil, err } @@ -69,10 +69,10 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { offset := 0 for iy := intersect.Min.Y; iy < intersect.Max.Y; iy++ { for ix := intersect.Min.X; ix < intersect.Max.X; ix++ { - r := xImg.Data[offset + 2] - g := xImg.Data[offset + 1] + r := xImg.Data[offset+2] + g := xImg.Data[offset+1] b := xImg.Data[offset] - img.SetRGBA(ix - (x + x0), iy - (y + y0), color.RGBA{r, g, b, 255}) + img.SetRGBA(ix-(x+x0), iy-(y+y0), color.RGBA{r, g, b, 255}) offset += 4 } } @@ -82,12 +82,12 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { } func NumActiveDisplays() (num int) { - defer func () { + defer func() { e := recover() if e != nil { num = 0 } - } () + }() c, err := xgb.NewConn() if err != nil { @@ -110,12 +110,12 @@ func NumActiveDisplays() (num int) { } func GetDisplayBounds(displayIndex int) (rect image.Rectangle) { - defer func () { + defer func() { e := recover() if e != nil { rect = image.ZR } - } () + }() c, err := xgb.NewConn() if err != nil { @@ -143,7 +143,6 @@ func GetDisplayBounds(displayIndex int) (rect image.Rectangle) { y := int(screen.YOrg) - y0 w := int(screen.Width) h := int(screen.Height) - rect = image.Rect(x, y, x + w, y + h) + rect = image.Rect(x, y, x+w, y+h) return rect } - diff --git a/screenshot_darwin.go b/screenshot_darwin.go index 5cd43df..21c8268 100644 --- a/screenshot_darwin.go +++ b/screenshot_darwin.go @@ -8,10 +8,10 @@ package screenshot import "C" import ( - "image" "errors" - "unsafe" "github.com/kbinani/screenshot/internal/util" + "image" + "unsafe" ) func Capture(x, y, width, height int) (*image.RGBA, error) { @@ -42,5 +42,5 @@ func GetDisplayBounds(displayIndex int) image.Rectangle { w = 0 h = 0 C.GetDisplayBounds(C.int(displayIndex), &x, &y, &w, &h) - return image.Rect(int(x), int(y), int(x + w), int(y + h)) + return image.Rect(int(x), int(y), int(x+w), int(y+h)) } diff --git a/screenshot_freebsd.go b/screenshot_freebsd.go index 222c2f5..a21661d 100644 --- a/screenshot_freebsd.go +++ b/screenshot_freebsd.go @@ -1,8 +1,8 @@ package screenshot import ( - "image" "github.com/kbinani/screenshot/internal/xwindow" + "image" ) // Capture returns screen capture of specified desktop region. diff --git a/screenshot_linux.go b/screenshot_linux.go index 222c2f5..a21661d 100644 --- a/screenshot_linux.go +++ b/screenshot_linux.go @@ -1,8 +1,8 @@ package screenshot import ( - "image" "github.com/kbinani/screenshot/internal/xwindow" + "image" ) // Capture returns screen capture of specified desktop region. diff --git a/screenshot_windows.go b/screenshot_windows.go index c6c6dbb..e3169d2 100644 --- a/screenshot_windows.go +++ b/screenshot_windows.go @@ -1,135 +1,135 @@ -package screenshot - -import ( - "image" - "unsafe" - "syscall" - "errors" - win "github.com/lxn/win" - "github.com/kbinani/screenshot/internal/util" -) - -var ( - libUser32, _ = syscall.LoadLibrary("user32.dll") - funcGetDesktopWindow, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "GetDesktopWindow") - funcEnumDisplayMonitors, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "EnumDisplayMonitors") -) - -func Capture(x, y, width, height int) (*image.RGBA, error) { - rect := image.Rect(0, 0, width, height) - img, err := util.CreateImage(rect) - if err != nil { - return nil, err - } - - hwnd := getDesktopWindow() - hdc := win.GetDC(hwnd) - if hdc == 0 { - return nil, errors.New("GetDC failed") - } - defer win.ReleaseDC(hwnd, hdc) - - memory_device := win.CreateCompatibleDC(hdc) - if memory_device == 0 { - return nil, errors.New("CreateCompatibleDC failed") - } - defer win.DeleteDC(memory_device) - - bitmap := win.CreateCompatibleBitmap(hdc, int32(width), int32(height)) - if bitmap == 0 { - return nil, errors.New("CreateCompatibleBitmap failed") - } - defer win.DeleteObject(win.HGDIOBJ(bitmap)) - - var header win.BITMAPINFOHEADER - header.BiSize = uint32(unsafe.Sizeof(header)) - header.BiPlanes = 1 - header.BiBitCount = 32 - header.BiWidth = int32(width) - header.BiHeight = int32(-height) - header.BiCompression = win.BI_RGB - header.BiSizeImage = 0 - - old := win.SelectObject(memory_device, win.HGDIOBJ(bitmap)) - if old == 0 { - return nil, errors.New("SelectObject failed") - } - defer win.SelectObject(memory_device, old) - - if !win.BitBlt(memory_device, 0, 0, int32(width), int32(height), hdc, int32(x), int32(y), win.SRCCOPY) { - return nil, errors.New("BitBlt failed") - } - - if win.GetDIBits(hdc, bitmap, 0, uint32(height), (*byte)(unsafe.Pointer(&img.Pix[0])), (*win.BITMAPINFO)(unsafe.Pointer(&header)), win.DIB_RGB_COLORS) == 0 { - return nil, errors.New("GetDIBits failed") - } - - i := 0 - for y := 0; y < height; y++ { - for x := 0; x < width; x++ { - // BGRA => RGBA, and set A to 255 - img.Pix[i], img.Pix[i + 2], img.Pix[i + 3] = img.Pix[i + 2], img.Pix[i], 255 - - i += 4 - } - } - - return img, nil -} - -func NumActiveDisplays() int { - var count int = 0 - enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(unsafe.Pointer(&count))) - return count -} - -func GetDisplayBounds(displayIndex int) image.Rectangle { - var ctx getMonitorBoundsContext - ctx.Index = displayIndex - ctx.Count = 0 - enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(unsafe.Pointer(&ctx))) - return image.Rect( - int(ctx.Rect.Left), int(ctx.Rect.Top), - int(ctx.Rect.Right - ctx.Rect.Left), int(ctx.Rect.Bottom - ctx.Rect.Top)) -} - -func getDesktopWindow() win.HWND { - ret, _, _ := syscall.Syscall(funcGetDesktopWindow, 0, 0, 0, 0) - return win.HWND(ret) -} - -func enumDisplayMonitors(hdc win.HDC, lprcClip *win.RECT, lpfnEnum uintptr, dwData uintptr) bool { - ret, _, _ := syscall.Syscall6(funcEnumDisplayMonitors, 4, - uintptr(hdc), - uintptr(unsafe.Pointer(lprcClip)), - lpfnEnum, - dwData, - 0, - 0) - return int(ret) != 0 -} - -func countupMonitorCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { - var count *int - count = (*int)(unsafe.Pointer(dwData)) - *count = *count + 1 - return uintptr(1) -} - -type getMonitorBoundsContext struct { - Index int - Rect win.RECT - Count int -} - -func getMonitorBoundsCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { - var ctx *getMonitorBoundsContext - ctx = (*getMonitorBoundsContext)(unsafe.Pointer(dwData)) - if ctx.Count == ctx.Index { - ctx.Rect = *lprcMonitor - return uintptr(0) - } else { - ctx.Count = ctx.Count + 1 - return uintptr(1) - } -} +package screenshot + +import ( + "errors" + "github.com/kbinani/screenshot/internal/util" + win "github.com/lxn/win" + "image" + "syscall" + "unsafe" +) + +var ( + libUser32, _ = syscall.LoadLibrary("user32.dll") + funcGetDesktopWindow, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "GetDesktopWindow") + funcEnumDisplayMonitors, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "EnumDisplayMonitors") +) + +func Capture(x, y, width, height int) (*image.RGBA, error) { + rect := image.Rect(0, 0, width, height) + img, err := util.CreateImage(rect) + if err != nil { + return nil, err + } + + hwnd := getDesktopWindow() + hdc := win.GetDC(hwnd) + if hdc == 0 { + return nil, errors.New("GetDC failed") + } + defer win.ReleaseDC(hwnd, hdc) + + memory_device := win.CreateCompatibleDC(hdc) + if memory_device == 0 { + return nil, errors.New("CreateCompatibleDC failed") + } + defer win.DeleteDC(memory_device) + + bitmap := win.CreateCompatibleBitmap(hdc, int32(width), int32(height)) + if bitmap == 0 { + return nil, errors.New("CreateCompatibleBitmap failed") + } + defer win.DeleteObject(win.HGDIOBJ(bitmap)) + + var header win.BITMAPINFOHEADER + header.BiSize = uint32(unsafe.Sizeof(header)) + header.BiPlanes = 1 + header.BiBitCount = 32 + header.BiWidth = int32(width) + header.BiHeight = int32(-height) + header.BiCompression = win.BI_RGB + header.BiSizeImage = 0 + + old := win.SelectObject(memory_device, win.HGDIOBJ(bitmap)) + if old == 0 { + return nil, errors.New("SelectObject failed") + } + defer win.SelectObject(memory_device, old) + + if !win.BitBlt(memory_device, 0, 0, int32(width), int32(height), hdc, int32(x), int32(y), win.SRCCOPY) { + return nil, errors.New("BitBlt failed") + } + + if win.GetDIBits(hdc, bitmap, 0, uint32(height), (*byte)(unsafe.Pointer(&img.Pix[0])), (*win.BITMAPINFO)(unsafe.Pointer(&header)), win.DIB_RGB_COLORS) == 0 { + return nil, errors.New("GetDIBits failed") + } + + i := 0 + for y := 0; y < height; y++ { + for x := 0; x < width; x++ { + // BGRA => RGBA, and set A to 255 + img.Pix[i], img.Pix[i+2], img.Pix[i+3] = img.Pix[i+2], img.Pix[i], 255 + + i += 4 + } + } + + return img, nil +} + +func NumActiveDisplays() int { + var count int = 0 + enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(unsafe.Pointer(&count))) + return count +} + +func GetDisplayBounds(displayIndex int) image.Rectangle { + var ctx getMonitorBoundsContext + ctx.Index = displayIndex + ctx.Count = 0 + enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(unsafe.Pointer(&ctx))) + return image.Rect( + int(ctx.Rect.Left), int(ctx.Rect.Top), + int(ctx.Rect.Right-ctx.Rect.Left), int(ctx.Rect.Bottom-ctx.Rect.Top)) +} + +func getDesktopWindow() win.HWND { + ret, _, _ := syscall.Syscall(funcGetDesktopWindow, 0, 0, 0, 0) + return win.HWND(ret) +} + +func enumDisplayMonitors(hdc win.HDC, lprcClip *win.RECT, lpfnEnum uintptr, dwData uintptr) bool { + ret, _, _ := syscall.Syscall6(funcEnumDisplayMonitors, 4, + uintptr(hdc), + uintptr(unsafe.Pointer(lprcClip)), + lpfnEnum, + dwData, + 0, + 0) + return int(ret) != 0 +} + +func countupMonitorCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { + var count *int + count = (*int)(unsafe.Pointer(dwData)) + *count = *count + 1 + return uintptr(1) +} + +type getMonitorBoundsContext struct { + Index int + Rect win.RECT + Count int +} + +func getMonitorBoundsCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { + var ctx *getMonitorBoundsContext + ctx = (*getMonitorBoundsContext)(unsafe.Pointer(dwData)) + if ctx.Count == ctx.Index { + ctx.Rect = *lprcMonitor + return uintptr(0) + } else { + ctx.Count = ctx.Count + 1 + return uintptr(1) + } +}