def test_invalid_encoding(self): width = 32 height = 32 bpp = 4 pixel_format = "BGRA" pixel_data = bytes(b"0" * bpp * width * height) Bpp = bpp // 8 image = ImageWrapper(0, 0, width, height, pixel_data, pixel_format, 32, width * Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True, palette=None) for encoding in (None, "", True, "hello", 1): try: encode(encoding, image) except Exception: pass else: raise Exception("'%s' is an invalid encoding" % encoding)
def test_invalid_pixel_format(self): width = 32 height = 32 bpp = 4 pixel_format = "invalid" pixel_data = bytes(b"0" * bpp * width * height) Bpp = bpp // 8 image = ImageWrapper(0, 0, width, height, pixel_data, pixel_format, 32, width * Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True, palette=None) try: encode("png", image) except Exception: pass else: raise Exception("should not be able to process '%s'" % pixel_format)
def do_test_pixel_format(self, pixel_format, bpp, encoding): maxsize = 1024 * 1024 * 4 buf = bytearray(maxsize) palette = [(10, 255, 127), (0, 0, 0), (255, 255, 255)] for transparency in (True, False): for quality in (0, 1, 50, 99, 100): for speed in (0, 1, 50, 99, 100): for width in (1, 10, 256): for height in (1, 10, 256): size = width * height * bpp pixel_data = bytes(buf[:size]) Bpp = bpp // 8 image = ImageWrapper(0, 0, width, height, pixel_data, pixel_format, 32, width * Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True, palette=palette) comp = encode( encoding, image, { "quality": quality, "speed": speed, "alpha": transparency, }) assert comp
def do_test_encode(rgb_data, w, h, encodings=("png", "png/P", "png/L", "jpeg", "webp"), N=5, Q=(0, 50, 100), S=(0, 1, 50, 90, 100), has_alpha=False): from xpra.codecs.pillow.encoder import encode from xpra.codecs.image_wrapper import ImageWrapper image = ImageWrapper(0, 0, w, h, rgb_data, "BGRA", 32, w*4, planes=ImageWrapper.PACKED, thread_safe=True) #buf = "\0" * (w*h*4) #buf = get_source_data(w*h*4) for encoding in encodings: Q_options = Q if encoding in ("png", "png/P", "png/L"): Q_options = [-1] S_options = S if encoding=="webp": S_options = [-1] if encoding=="jpeg": S_options = [0, -1] for q in Q_options: for s in S_options: #print("test_encode() quality=%s, speed=%s" % (q, s)) coding = encoding start = time.time() for _ in range(N): data = encode(coding, image, q, s, True) #def compress(pixels, width, height, quality=50, speed=50): end = time.time() mps = w*h*N/(end-start)/1024/1024 cdata = data[1] ratio = 100.0 * len(cdata) / len(rgb_data) print("%s : %s MPixels/s %s%% compression ratio : %sx%s to: %s KBytes (quality=%s, speed=%s) %s times in %sms average" % \ (coding.ljust(5), ("%.1f" % mps).rjust(5), ("%.1f" % ratio).rjust(5), str(w).rjust(4), str(h).ljust(4), str(len(cdata)/1024).rjust(6), str(q).rjust(3), str(s).rjust(3), N, str(int((end-start)*1000.0/N)).rjust(5)))
def test_grayscale(self): W = 128 H = 128 pixel_data = b"0"*128*128*4 Bpp = 4 image = ImageWrapper(0, 0, W, H, pixel_data, "BGRA", 32, W*Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True) comp = encode("png", image, 50, 50, True, grayscale=True) assert comp
def test_resize(self): W = 128 H = 128 for speed in (10, 30, 90, 100): pixel_data = b"0"*128*128*4 Bpp = 4 image = ImageWrapper(0, 0, W, H, pixel_data, "BGRX", 32, W*Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True) comp = encode("png", image, 50, speed, False, resize=(64, 64)) assert comp
def test_resize(self): W = 128 H = 128 for speed in (10, 30, 90, 100): pixel_data = b"0" * 128 * 128 * 4 Bpp = 4 image = ImageWrapper(0, 0, W, H, pixel_data, "BGRX", 32, W * Bpp, Bpp, planes=ImageWrapper.PACKED, thread_safe=True) comp = encode("png", image, { "speed": speed, "scaled-width": 64, "scaled-height": 64, }) assert comp
def pillow_encode(encoding, img): return encode(encoding, img, 90, 100, False, False, None)[1].data