- 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

@@ -15,29 +15,8 @@ class RfbBitmap():
self.red_shift = None
self.green_shift = None
self.blue_shift = None
self.bigendian = 0
def __quantizetopalette(self, silf, palette, dither=False):
"""Converts an RGB or L mode image to use a given P image's palette."""
silf.load()
# use palette from reference image
palette.load()
if palette.mode != "P":
raise ValueError("bad mode for palette image")
if silf.mode != "RGB" and silf.mode != "L":
raise ValueError(
"only RGB or L mode images can be quantized to a palette"
)
im = silf.im.convert("P", 1 if dither else 0, palette.im)
# the 0 above means turn OFF dithering
# Later versions of Pillow (4.x) rename _makeself to _new
try:
return silf._new(im)
except AttributeError:
return silf._makeself(im)
def get_bitmap(self, rectangle):
if self.bpp == 32:
redBits = 8
@@ -55,53 +34,42 @@ class RfbBitmap():
a[..., 2] = ( a[..., 2] ) & blueMask >> self.blue_shift
image = Image.fromarray(a)
if image.mode == "RGBA":
(r, g, b, a) = image.split()
image = Image.merge("RGB", (r, g, b))
del r, g, b, a
if self.primaryOrder == "rgb":
(b, g, r) = image.split()
image = Image.merge("RGB", (r, g, b))
del b,g,r
del b, g, r
image = image.convert("RGBX")
return image
elif self.bpp == 16: #BGR565
greenBits = 5
blueBits = 6
redBits = 5
image = rectangle
if self.primaryOrder == "bgr": # FIXME: does not work
(b, g, r) = image.split()
image = Image.merge("RGB", (r, g, b))
if self.depth == 16:
image = image.convert('BGR;16')
if self.depth == 15:
image = image.convert('BGR;15')
elif self.bpp == 16:
# BGR565
a = np.array(rectangle)
r = (a[..., 0] >> 3) & 0x1F
g = (a[..., 1] >> 2) & 0x3F
b = (a[..., 2] >> 3) & 0x1F
bgr565 = (r << 11) | (g << 5) | b
bgr565 = bgr565.astype('uint16')
if self.bigendian == 0:
bgr565 = bgr565.byteswap().newbyteorder()
bgr565_bytes = bgr565.tobytes()
image = Image.frombytes('RGB', rectangle.size, bgr565_bytes, 'raw', 'BGR;16')
return image
elif self.bpp == 8: #bgr233
redBits = 3
greenBits = 3
blueBits = 2
image = rectangle
palette = bgr233_palette.palette
if self.primaryOrder == "rgb":
#(b, g, r) = image.split()
#image = Image.merge("RGB", (r, g, b))
palette = np.reshape(palette, (-3,3))
palette[:,[0, 2]] = palette[:,[2, 0]]
palette = palette.flatten()
palette = list(palette)
p = Image.new('P',(16,16))
p.putpalette(palette)
image = self.__quantizetopalette(image, p, dither=self.dither)
#image = image.convert('RGB', colors=4).quantize(palette=p)
#log.debug(image)
elif self.bpp == 8:
# BGR233
image = rectangle.convert('RGB')
a = np.array(image)
r = (a[..., 0] >> 6) & 0x03
g = (a[..., 1] >> 5) & 0x07
b = (a[..., 2] >> 6) & 0x03
bgr233 = (b << 6) | (g << 3) | r
image = Image.fromarray(bgr233.astype('uint8'), 'P')
image.putpalette(bgr233_palette.palette)
return image
else: