Esempio n. 1
0
 def test_decompress_by_name(self):
     for x in ("foo", "lz4XX", "zli"):
         try:
             compression.decompress_by_name(b"foo", x)
         except Exception:
             pass
         else:
             raise Exception("%s is not a valid compression" % x)
Esempio n. 2
0
 def process_delta(self, raw_data, width, height, rowstride, options):
     """
         Can be called from any thread, decompresses and xors the rgb raw_data,
         then stores it for later xoring if needed.
     """
     img_data = raw_data
     if options:
         #check for one of the compressors:
         comp = [x for x in compression.ALL_COMPRESSORS if options.intget(x, 0)]
         if comp:
             assert len(comp)==1, "more than one compressor specified: %s" % str(comp)
             img_data = compression.decompress_by_name(raw_data, algo=comp[0])
     if len(img_data)!=rowstride * height:
         log.error("invalid img data %s: %s", type(img_data), str(img_data)[:256])
         raise Exception("expected %s bytes for %sx%s with rowstride=%s but received %s (%s compressed)" %
                             (rowstride * height, width, height, rowstride, len(img_data), len(raw_data)))
     delta = options.intget("delta", -1)
     rgb_data = img_data
     if delta>=0:
         if not self._last_pixmap_data:
             raise Exception("delta region references pixmap data we do not have!")
         if xor_str is None:
             raise Exception("received a delta region but we do not support delta encoding!")
         lwidth, lheight, store, ldata = self._last_pixmap_data
         assert width==lwidth and height==lheight and delta==store
         rgb_data = xor_str(img_data, ldata)
     #store new pixels for next delta:
     store = options.intget("store", -1)
     if store>=0:
         self._last_pixmap_data =  width, height, store, rgb_data
     return rgb_data
Esempio n. 3
0
 def test_compressed_wrapper(self):
     r = compression.compressed_wrapper("test", b"a"*(compression.MIN_COMPRESS_SIZE+1))
     if not r.datatype.startswith("raw"):
         raise Exception("should not be able to use the wrapper without enabling a compressor, but got %s" % r)
     for x in ("lz4", "brotli", "zlib", "none"):
         if not compression.use(x):
             continue
         kwargs = {x : True}
         for level in (0, 1, 5, 10):
             for data in (
                 b"0"*1024,
                 b"0"*16,
                 b"\0",
                 memoryview(b"hello"),
                 bytearray(b"hello"),
                 b"1"*1024*1024*16,
                 ):
                 v = compression.compressed_wrapper("test", data, level=level, **kwargs)
                 assert v
                 assert repr(v)
                 assert compression.get_compression_type(v.level)
                 #and back:
                 try:
                     d = compression.decompress_by_name(v.data, v.algorithm)
                     assert d
                     if x!="none":
                         #we can't do none,
                         #because it would be mistaken for "zlib"
                         #(for historical reasons - 'zlib' uses level=0, and 'none' has no level)
                         d = compression.decompress(v.data, v.level)
                         assert d
                 except Exception:
                     print("error decompressing %s - generated with settings: %s" % (v, kwargs))
                     raise
Esempio n. 4
0
 def process_delta(self, raw_data, width, height, rowstride, options):
     """
         Can be called from any thread, decompresses and xors the rgb raw_data,
         then stores it for later xoring if needed.
     """
     img_data = raw_data
     if options:
         #check for one of the compressors:
         comp = [x for x in compression.ALL_COMPRESSORS if options.intget(x, 0)]
         if comp:
             assert len(comp)==1, "more than one compressor specified: %s" % str(comp)
             img_data = compression.decompress_by_name(raw_data, algo=comp[0])
     if len(img_data)!=rowstride * height:
         log.error("invalid img data %s: %s", type(img_data), str(img_data)[:256])
         raise Exception("expected %s bytes for %sx%s with rowstride=%s but received %s (%s compressed)" %
                             (rowstride * height, width, height, rowstride, len(img_data), len(raw_data)))
     delta = options.intget("delta", -1)
     bucket = options.intget("bucket", 0)
     rgb_data = img_data
     if delta>=0:
         assert bucket>=0 and bucket<DELTA_BUCKETS, "invalid delta bucket number: %s" % bucket
         if self._delta_pixel_data[bucket] is None:
             raise Exception("delta region bucket %s references pixmap data we do not have!" % bucket)
         lwidth, lheight, seq, ldata = self._delta_pixel_data[bucket]
         assert width==lwidth and height==lheight and delta==seq, "delta bucket %s data does not match: expected %s but got %s" % (bucket, (width, height, delta), (lwidth, lheight, seq))
         deltalog("delta: xoring with bucket %i", bucket)
         rgb_data = xor_str(img_data, ldata)
     #store new pixels for next delta:
     store = options.intget("store", -1)
     if store>=0:
         deltalog("delta: storing sequence %i in bucket %i", store, bucket)
         self._delta_pixel_data[bucket] =  width, height, store, rgb_data
     return rgb_data
Esempio n. 5
0
 def uncompress_data(self, data, metadata):
     if not data or not metadata:
         return data
     compress = metadata.get("compress")
     if not compress:
         return data
     assert compress in ("lz4", "lzo")
     v = decompress_by_name(data, compress)
     #log("decompressed %s data: %i bytes into %i bytes", compress, len(data), len(v))
     return v
Esempio n. 6
0
 def uncompress_data(self, data, metadata):
     if not data or not metadata:
         return data
     compress = metadata.get("compress")
     if not compress:
         return data
     assert compress in ("lz4", "lzo")
     v = decompress_by_name(data, compress)
     #log("decompressed %s data: %i bytes into %i bytes", compress, len(data), len(v))
     return v
Esempio n. 7
0
 def paint_rgb(self, rgb_format, raw_data, x, y, width, height, rowstride, options, callbacks):
     """ can be called from a non-UI thread """
     iwidth, iheight = options.intpair("scaled-size", (width, height))
     #was a compressor used?
     comp = tuple(x for x in compression.ALL_COMPRESSORS if options.intget(x, 0))
     if comp:
         assert len(comp)==1, "more than one compressor specified: %s" % str(comp)
         rgb_data = compression.decompress_by_name(raw_data, algo=comp[0])
     else:
         rgb_data = raw_data
     self.idle_add(self.do_paint_rgb, rgb_format, rgb_data,
                   x, y, iwidth, iheight, width, height, rowstride, options, callbacks)
Esempio n. 8
0
 def process_delta(self, raw_data, width, height, rowstride, options):
     """
         Can be called from any thread, decompresses and xors the rgb raw_data,
         then stores it for later xoring if needed.
     """
     img_data = raw_data
     if options:
         #check for one of the compressors:
         comp = [
             x for x in compression.ALL_COMPRESSORS if options.intget(x, 0)
         ]
         if comp:
             assert len(
                 comp
             ) == 1, "more than one compressor specified: %s" % str(comp)
             img_data = compression.decompress_by_name(raw_data,
                                                       algo=comp[0])
     if len(img_data) != rowstride * height:
         deltalog.error(
             "Error: invalid img data length: expected %s but got %s (%s: %s)",
             rowstride * height, len(img_data), type(img_data),
             repr_ellipsized(img_data))
         raise Exception(
             "expected %s bytes for %sx%s with rowstride=%s but received %s (%s compressed)"
             % (rowstride * height, width, height, rowstride, len(img_data),
                len(raw_data)))
     delta = options.intget(b"delta", -1)
     bucket = options.intget(b"bucket", 0)
     rgb_format = options.strget(b"rgb_format")
     rgb_data = img_data
     if delta >= 0:
         assert 0 <= bucket < DELTA_BUCKETS, "invalid delta bucket number: %s" % bucket
         if self._delta_pixel_data[bucket] is None:
             raise Exception(
                 "delta region bucket %s references pixmap data we do not have!"
                 % bucket)
         lwidth, lheight, lrgb_format, seq, ldata = self._delta_pixel_data[
             bucket]
         assert width==lwidth and height==lheight and delta==seq, \
             "delta bucket %s data does not match: expected %s but got %s" % (
                 bucket, (width, height, delta), (lwidth, lheight, seq))
         assert lrgb_format == rgb_format, "delta region uses %s format, was expecting %s" % (
             rgb_format, lrgb_format)
         deltalog("delta: xoring with bucket %i", bucket)
         rgb_data = xor_str(img_data, ldata)
     #store new pixels for next delta:
     store = options.intget("store", -1)
     if store >= 0:
         deltalog("delta: storing sequence %i in bucket %i", store, bucket)
         self._delta_pixel_data[
             bucket] = width, height, rgb_format, store, rgb_data
     return rgb_data