Skip to content

Commit

Permalink
#4270 prefer BGRX with webp decoder
Browse files Browse the repository at this point in the history
and simplify output rgb format selection
  • Loading branch information
totaam committed Jul 1, 2024
1 parent 815f043 commit 398e510
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions xpra/codecs/webp/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ cdef inline int roundup(int n, int m):


def decompress_to_rgb(rgb_format: str, data: bytes, has_alpha: bool=True, rgb_formats: Sequence[str] = ()) -> ImageWrapper:
if rgb_format not in ("RGBX", "RGBA", "BGRA", "BGRX", "RGB", "BGR"):
raise ValueError(f"unsupported rgb format {rgb_format}")
cdef WebPDecoderConfig config
config.options.use_threads = 1
WebPInitDecoderConfig(&config)
Expand All @@ -187,28 +189,7 @@ def decompress_to_rgb(rgb_format: str, data: bytes, has_alpha: bool=True, rgb_fo
config.input.width, config.input.height, bool(config.input.has_alpha), rgb_format)

config.output.colorspace = MODE_BGRA
if has_alpha:
if len(rgb_format)!=4:
# use default if the format given is not valid:
out_format = "BGRA"
else:
out_format = rgb_format
else:
noalpha_format = (rgb_format or "").replace("A", "").replace("X", "")
# the webp encoder takes BGRA input,
# so we have to swap the colours in the output to match:
if noalpha_format == "RGB":
out_format = "BGR"
else:
out_format = "RGB"
if out_format in rgb_formats:
# we can use 3 bytes per pixel output:
config.output.colorspace = MODE_RGB
elif rgb_format in ("RGBX", "RGBA"):
out_format = "RGBX"
else:
out_format = "BGRX"
cdef int stride = len(out_format) * config.input.width
cdef int stride = 4 * config.input.width
cdef size_t size = stride * config.input.height
#allocate the buffer:
cdef uint8_t *buf = <uint8_t*> memalign(size + stride) #add one line of padding
Expand All @@ -229,10 +210,11 @@ def decompress_to_rgb(rgb_format: str, data: bytes, has_alpha: bool=True, rgb_fo
#we use external memory, so this is not needed:
#WebPFreeDecBuffer(&config.output)
may_save_image("webp", data)
cdef int alpha = 1 if (config.input.has_alpha and has_alpha) else 0
if alpha:
out_format = out_format.replace("X", "A")
else:
out_format = rgb_format
if len(rgb_format) == 3: # ie: "RGB", "BGR"
out_format = out_format + "X"
assert len(out_format) == 4
if not has_alpha or not config.input.has_alpha:
out_format = out_format.replace("A", "X")
pixels = PyMemoryView_FromMemory(<char *> buf, size, PyBUF_WRITE)
img = WebpImageWrapper(
Expand Down

0 comments on commit 398e510

Please sign in to comment.