def main(): from xpra.platform import init, clean try: init("Loader", "Encoding Info") verbose = "-v" in sys.argv or "--verbose" in sys.argv if verbose: log.enable_debug() load_codecs() print("codecs and csc modules found:") #print("codec_status=%s" % codecs) for name in ALL_CODECS: mod = codecs.get(name, "") f = mod if mod and hasattr(mod, "__file__"): f = mod.__file__ if f.startswith(os.getcwd()): f = f[len(os.getcwd()):] if f.startswith(os.path.sep): f = f[1:] print("* %s : %s" % (name.ljust(20), f)) if mod and verbose: try: if name=="PIL": #special case for PIL which can be used for both encoding and decoding: from xpra.codecs.codec_constants import get_PIL_encodings, get_PIL_decodings e = get_PIL_encodings(mod) print(" ENCODE: %s" % ", ".join(e)) d = get_PIL_decodings(mod) print(" DECODE: %s" % ", ".join(d)) elif name.find("csc")>=0: cs = list(mod.get_input_colorspaces()) for c in list(cs): cs += list(mod.get_output_colorspaces(c)) print(" %s" % ", ".join(list(set(cs)))) elif name.find("enc")>=0 or name.find("dec")>=0: encodings = mod.get_encodings() print(" %s" % ", ".join(encodings)) try: i = mod.get_info() print(" %s" % i) except: pass except Exception as e: print("error getting extra information on %s: %s" % (name, e)) print("") print("codecs versions:") def pver(v): if type(v)==tuple: return ".".join([str(x) for x in v]) elif type(v) in (str, unicode) and v.startswith("v"): return v[1:] return str(v) for name in sorted(codec_versions.keys()): version = codec_versions[name] print("* %s : %s" % (name.ljust(20), pver(version))) finally: #this will wait for input on win32: clean()
def webp_encode(coding, image, rgb_formats, supports_transparency, quality, speed, options): pixel_format = image.get_pixel_format() #log("rgb_encode%s pixel_format=%s, rgb_formats=%s", (coding, image, rgb_formats, supports_transparency, speed, rgb_zlib, rgb_lz4), pixel_format, rgb_formats) if pixel_format not in rgb_formats: if not rgb_reformat(image, rgb_formats, supports_transparency): raise Exception( "cannot find compatible rgb format to use for %s! (supported: %s)" % (pixel_format, rgb_formats)) #get the new format: pixel_format = image.get_pixel_format() stride = image.get_rowstride() enc_webp = get_codec("enc_webp") #log("webp_encode%s stride=%s, enc_webp=%s", (coding, image, rgb_formats, supports_transparency, quality, speed, options), stride, enc_webp) if enc_webp and stride > 0 and stride % 4 == 0 and image.get_pixel_format( ) in ("BGRA", "BGRX", "RGBA", "RGBX"): #prefer Cython module: alpha = supports_transparency and image.get_pixel_format().find( "A") >= 0 w = image.get_width() h = image.get_height() if quality == 100: #webp lossless is unbearibly slow for only marginal compression improvements, #so force max speed: speed = 100 else: #normalize speed for webp: avoid low speeds! speed = int(sqrt(speed) * 10) speed = max(0, min(100, speed)) cdata = enc_webp.compress(image.get_pixels(), w, h, stride=stride / 4, quality=quality, speed=speed, has_alpha=alpha) client_options = {"speed": speed, "rgb_format": pixel_format} if quality >= 0 and quality <= 100: client_options["quality"] = quality if alpha: client_options["has_alpha"] = True return "webp", compression.Compressed( "webp", cdata), client_options, image.get_width( ), image.get_height(), 0, 24 #fallback to PIL log("using PIL fallback for webp: enc_webp=%s, stride=%s, pixel format=%s", enc_webp, stride, image.get_pixel_format()) encs = get_PIL_encodings(PIL) for x in ("webp", "png"): if x in encs: return PIL_encode(x, image, quality, speed, supports_transparency) raise Exception( "BUG: cannot use 'webp' encoding and none of the PIL fallbacks are available!" )
def do_test_encode(rgb_data, w, h, N=10, Q=[50], S=[0, 1, 2, 3, 4, 5, 6], has_alpha=False): #buf = "\0" * (w*h*4) #buf = get_source_data(w*h*4) image = ImageWrapper(0, 0, w, h, rgb_data, "BGRA", 32, w*4, planes=ImageWrapper.PACKED, thread_safe=True) def webp(q, s): return compress(rgb_data, w, h, quality=q, speed=s, has_alpha=has_alpha) def webm(q, s): data = webm_encode(image, q) #data = ("webp", Compressed("webp", str(enc(handler, **kwargs).data)), client_options, image.get_width(), image.get_height(), 0, bpp) return data[1] def PIL_webp(q, s): data = PIL_encode("webp", image, quality, speed, has_alpha) return data[1] TESTS = {"webp" : webp, "webm" : webm} if "webp" in get_PIL_encodings(PIL): TESTS["PIL"] = PIL_webp for name,encode in TESTS.items(): for q in Q: S_options = S if name=="webm": S_options = [-1] for s in S_options: #print("test_encode() quality=%s, speed=%s" % (q, s)) start = time.time() quality = q speed = s if s>0: speed = s*16.7 for _ in range(N): data = encode(quality, speed) #def webp_encode(coding, image, quality): if N>1: sys.stdout.write(".") sys.stdout.flush() if N>1: print("") #def compress(pixels, width, height, quality=50, speed=50): end = time.time() mps = w*h*N/(end-start)/1024/1024 ratio = 100.0 * len(data) / len(rgb_data) print("%s compressed at %4.1f MPixels/s %4.1f%% compression ratio : %4ix%-4i to: %9i bytes (quality=%3i, speed=%3i) %2i times in %5ims average" % (name, mps, ratio, w, h, len(data), quality, speed, N, (end-start)*1000.0/N))
def webp_encode(coding, image, rgb_formats, supports_transparency, quality, speed, options): pixel_format = image.get_pixel_format() #log("rgb_encode%s pixel_format=%s, rgb_formats=%s", (coding, image, rgb_formats, supports_transparency, speed, rgb_zlib, rgb_lz4), pixel_format, rgb_formats) if pixel_format not in rgb_formats: if not rgb_reformat(image, rgb_formats, supports_transparency): raise Exception("cannot find compatible rgb format to use for %s! (supported: %s)" % (pixel_format, rgb_formats)) #get the new format: pixel_format = image.get_pixel_format() stride = image.get_rowstride() enc_webp = get_codec("enc_webp") #log("webp_encode%s stride=%s, enc_webp=%s", (coding, image, rgb_formats, supports_transparency, quality, speed, options), stride, enc_webp) if enc_webp and stride>0 and stride%4==0 and image.get_pixel_format() in ("BGRA", "BGRX", "RGBA", "RGBX"): #prefer Cython module: alpha = supports_transparency and image.get_pixel_format().find("A")>=0 w = image.get_width() h = image.get_height() if quality==100: #webp lossless is unbearibly slow for only marginal compression improvements, #so force max speed: speed = 100 else: #normalize speed for webp: avoid low speeds! speed = int(sqrt(speed) * 10) speed = max(0, min(100, speed)) cdata = enc_webp.compress(image.get_pixels(), w, h, stride=stride/4, quality=quality, speed=speed, has_alpha=alpha) client_options = {"speed" : speed, "rgb_format" : pixel_format} if quality>=0 and quality<=100: client_options["quality"] = quality if alpha: client_options["has_alpha"] = True return "webp", compression.Compressed("webp", cdata), client_options, image.get_width(), image.get_height(), 0, 24 #fallback to PIL log("using PIL fallback for webp: enc_webp=%s, stride=%s, pixel format=%s", enc_webp, stride, image.get_pixel_format()) encs = get_PIL_encodings(PIL) for x in ("webp", "png"): if x in encs: return PIL_encode(x, image, quality, speed, supports_transparency) raise Exception("BUG: cannot use 'webp' encoding and none of the PIL fallbacks are available!")
def main(): from xpra.platform import init, clean try: init("Loader", "Encoding Info") verbose = "-v" in sys.argv or "--verbose" in sys.argv if verbose: log.enable_debug() load_codecs() print("codecs and csc modules found:") #print("codec_status=%s" % codecs) for name in ALL_CODECS: mod = codecs.get(name, "") f = mod if mod and hasattr(mod, "__file__"): f = mod.__file__ if f.startswith(os.getcwd()): f = f[len(os.getcwd()):] if f.startswith(os.path.sep): f = f[1:] print("* %s : %s" % (name.ljust(20), f)) if mod and verbose: try: if name == "PIL": #special case for PIL which can be used for both encoding and decoding: from xpra.codecs.codec_constants import get_PIL_encodings, get_PIL_decodings e = get_PIL_encodings(mod) print(" ENCODE: %s" % ", ".join(e)) d = get_PIL_decodings(mod) print(" DECODE: %s" % ", ".join(d)) elif name.find("csc") >= 0: cs = list(mod.get_input_colorspaces()) for c in list(cs): cs += list(mod.get_output_colorspaces(c)) print(" %s" % ", ".join(list(set(cs)))) elif name.find("enc") >= 0 or name.find("dec") >= 0: encodings = mod.get_encodings() print(" %s" % ", ".join(encodings)) except: e = sys.exc_info()[1] print("error getting extra information on %s: %s" % (name, e)) print("") print("codecs versions:") def pver(v): if type(v) == tuple: return ".".join([str(x) for x in v]) elif type(v) in (str, unicode) and v.startswith("v"): return v[1:] return str(v) for name in sorted(codec_versions.keys()): version = codec_versions[name] print("* %s : %s" % (name.ljust(20), pver(version))) finally: #this will wait for input on win32: clean()