public static @Nullable BufferedImage captureScreen(@Nullable Window belowWindow, @NotNull Rectangle rect) {
    ID pool = Foundation.invoke("NSAutoreleasePool", "new");
    try {
      ID windowId = belowWindow != null ? MacUtil.findWindowFromJavaWindow(belowWindow) : null;
      CoreGraphics.CGRect cgRect = new CoreGraphics.CGRect(rect.x, rect.y, rect.width, rect.height);
      ID cgWindowId = windowId != null ? Foundation.invoke(windowId, "windowNumber") : ID.NIL;
      int windowListOptions = cgWindowId != ID.NIL
                              ? FoundationLibrary.kCGWindowListOptionOnScreenBelowWindow
                              : FoundationLibrary.kCGWindowListOptionOnScreenOnly;

      int windowImageOptions = FoundationLibrary.kCGWindowImageNominalResolution;
      ID cgImageRef = CoreGraphics.cgWindowListCreateImage(cgRect, windowListOptions, cgWindowId, windowImageOptions);
      ID bitmapRep = Foundation.invoke(Foundation.invoke("NSBitmapImageRep", "alloc"), "initWithCGImage:", cgImageRef);
      ID nsImage = Foundation.invoke(Foundation.invoke("NSImage", "alloc"), "init");
      Foundation.invoke(nsImage, "addRepresentation:", bitmapRep);
      ID data = Foundation.invoke(nsImage, "TIFFRepresentation");
      ID bytes = Foundation.invoke(data, "bytes");
      ID length = Foundation.invoke(data, "length");
      ByteBuffer byteBuffer = new Pointer(bytes.longValue()).getByteBuffer(0, length.longValue());
      Foundation.invoke(nsImage, "release");
      byte[] b = new byte[byteBuffer.remaining()];
      byteBuffer.get(b);

      BufferedImage result = ImageIO.read(new ByteArrayInputStream(b));
      if (result != null) {
        ColorSpace ics = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorConvertOp cco = new ColorConvertOp(ics, null);
        return cco.filter(result, null);
      }
      return null;
    }
    catch (Throwable t) {
      LOG.error(t);
      return null;
    }
    finally {
      Foundation.invoke(pool, "release");
    }
  }
public static @Nullable BufferedImage captureScreen(@Nullable Window belowWindow, @NotNull Rectangle rect)