- Improved efficiency and readability of the RfbBitmap configuration logic. Refactored redundant code blocks for different pixel format (bpp) configurations into a single, streamlined method. This change enhances maintainability and clarity of the bitmap configuration process.

- Added cursor pseudo encoding support.

- Added Windows support for cursor image capturing in `get_cursor_image` method. Implemented Windows-specific logic using the `win32gui`, `win32ui`, and related libraries to capture the current cursor image, enhancing the cross-platform capability of the application.

- Fixed issues in `get_bitmap` method for handling different bpp formats. Specifically addressed the processing logic for 16 bpp (BGR565) format, ensuring that the image conversion and rendering are handled correctly for VNC clients expecting this format.

- Added initial Tight encoding support.

- Updated the `send_image` method in the Tight encoding class to correctly handle JPEG and ZLIB compression. This includes proper signaling to the client about the type of compression used (JPEG or ZLIB) and ensuring that the data is formatted and sent according to the Tight encoding specifications.

- Added checks and conversions in `send_image` to handle different image modes (like RGBX and RGBA) and convert them to the appropriate format (RGB) before compression and transmission.

- Implemented a more robust and accurate method for determining when to use JPEG compression in Tight encoding based on the unique color count and image characteristics.

These updates significantly improve the functionality, stability, and compatibility of the VNC server, particularly in handling different pixel formats and encoding methods.
This commit is contained in:
Matias Fernandez
2023-12-06 09:21:26 -03:00
parent 76e309ef08
commit 2bc431f0a8
18 changed files with 650 additions and 433 deletions

View File

@@ -0,0 +1,82 @@
import ctypes
import ctypes.wintypes
from PIL import Image
import numpy as np
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [
("biSize", ctypes.wintypes.DWORD),
("biWidth", ctypes.wintypes.LONG),
("biHeight", ctypes.wintypes.LONG),
("biPlanes", ctypes.wintypes.WORD),
("biBitCount", ctypes.wintypes.WORD),
("biCompression", ctypes.wintypes.DWORD),
("biSizeImage", ctypes.wintypes.DWORD),
("biXPelsPerMeter", ctypes.wintypes.LONG),
("biYPelsPerMeter", ctypes.wintypes.LONG),
("biClrUsed", ctypes.wintypes.DWORD),
("biClrImportant", ctypes.wintypes.DWORD)
]
class RGBQUAD(ctypes.Structure):
_fields_ = [
("rgbBlue", ctypes.c_ubyte),
("rgbGreen", ctypes.c_ubyte),
("rgbRed", ctypes.c_ubyte),
("rgbReserved", ctypes.c_ubyte)
]
class BITMAPINFO(ctypes.Structure):
_fields_ = [("bmiHeader", BITMAPINFOHEADER), ("bmiColors", RGBQUAD * 1)]
class ICONINFO(ctypes.Structure):
_fields_ = [
("fIcon", ctypes.wintypes.BOOL),
("xHotspot", ctypes.wintypes.DWORD),
("yHotspot", ctypes.wintypes.DWORD),
("hbmMask", ctypes.wintypes.HBITMAP),
("hbmColor", ctypes.wintypes.HBITMAP),
]
class CURSORINFO(ctypes.Structure):
_fields_ = [
("cbSize", ctypes.wintypes.DWORD),
("flags", ctypes.wintypes.DWORD),
("hCursor", ctypes.wintypes.HANDLE),
("ptScreenPos", ctypes.wintypes.POINT),
]
def get_cursor_image():
ci = CURSORINFO()
ci.cbSize = ctypes.sizeof(CURSORINFO)
ctypes.windll.user32.GetCursorInfo(ctypes.byref(ci))
ii = ICONINFO()
ctypes.windll.user32.GetIconInfo(ci.hCursor, ctypes.byref(ii))
hdc = ctypes.windll.user32.GetDC(0) # Usar 0 en lugar de None
hbmp = ctypes.wintypes.HANDLE(ii.hbmColor) # Asegurarse de que hbmp es un HANDLE
bmpinfo = BITMAPINFO()
bmpinfo.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
ctypes.windll.gdi32.GetDIBits(hdc, hbmp, 0, 0, None, ctypes.byref(bmpinfo), 0)
width, height = bmpinfo.bmiHeader.biWidth, bmpinfo.bmiHeader.biHeight
bmpinfo.bmiHeader.biCompression = 0 # BI_RGB
buffer = ctypes.create_string_buffer(width * height * 4)
ctypes.windll.gdi32.GetDIBits(hdc, hbmp, 0, height, buffer, ctypes.byref(bmpinfo), 0)
img = np.frombuffer(buffer, dtype=np.uint8)
img = img.reshape((height, width, 4))
img = np.flip(img, axis=0) # Las imágenes de bitmap en Windows están al revés
img = Image.fromarray(img, 'RGBA')
# Free resources
try:
ctypes.windll.gdi32.DeleteObject(hbmp)
ctypes.windll.gdi32.DeleteObject(ii.hbmMask)
ctypes.windll.user32.ReleaseDC(None, hdc)
except:
pass
return img