def _test(func): print() #new line in pytest for name in zip.namelist(): if not name.startswith(func) or not name.endswith("data"): continue base_name = name[:-5] print(base_name) # load sample data data = zip.open(base_name + ".data", "r").read() details = json.loads(zip.open(base_name + ".json", "r").read()) ori_img = Image.open(zip.open(base_name + ".png", "r")) # decompress data width = details["m_Width"] height = details["m_Height"] mode = "RGBA" if "ATC" == base_name[:3]: dec = decompress_atc(data, width, height, 'RGBA' in base_name) mode = 'RGBA' if 'RGBA' in base_name else 'RGB' elif "ASTC" == base_name[:4]: block_width, block_height = map( int, base_name.rsplit("_", 1)[-1].split("x")) dec = decompress_astc(data, width, height, block_width, block_height, False) elif "ETC" == base_name[:3]: if "Crunched" in base_name: data = crunch_unpack_level(data, 0) etc_format = {"ETC_RGB4": 0, "ETC2_RGB": 1, "ETC2_RGBA8": 3} dec = decompress_etc(data, width, height, etc_format[base_name.rstrip("Crunched")]) mode = 'RGBA' if "RGBA" in base_name else "RGB" elif "PVRTC" == base_name[:5]: dec = decompress_pvrtc(data, width, height, 0) elif func == "crunch" and "Crunched" in base_name: data = crunch_unpack_level(data, 0) continue # load raw image data dec_img = Image.frombytes(mode, (width, height), dec, 'raw') # compare images if func != "PVRTC" and not (platform[:5] == "linux" and func == "ETC" ): # PVRTC2 is always different..... assert (ImageChops.difference(ori_img, dec_img).getbbox() is None)
def image(self): from PIL import Image from decrunch import File as CrunchFile if self.format not in IMPLEMENTED_FORMATS: raise NotImplementedError("Unimplemented format %r" % (self.format)) if self.format in (TextureFormat.DXT1, TextureFormat.DXT1Crunched): codec = "bcn" args = (1, ) elif self.format in (TextureFormat.DXT5, TextureFormat.DXT5Crunched): codec = "bcn" args = (3, ) elif self.format == TextureFormat.BC7: codec = "bcn" args = (7, ) elif self.format in (TextureFormat.ETC_RGB4, TextureFormat.ETC2_RGB, TextureFormat.ETC2_RGBA1, TextureFormat.ETC2_RGBA8): codec = "etc2" args = ( self.format, self.format.pixel_format, ) else: codec = "raw" args = (self.format.pixel_format, ) mode = "RGB" if self.format.pixel_format in ("RGB", "RGB;16") else "RGBA" size = (self.width, self.height) data = self.image_data if self.format in (TextureFormat.DXT1Crunched, TextureFormat.DXT5Crunched): data = CrunchFile(self.image_data).decode_level(0) if self.format in TextureFormat.ASTC_FORMATS(): block_sz = self.format.astc_block_size data = decompress_astc(data, self.width, self.height, block_sz, block_sz, False) # Pillow wants bytes, not bytearrays data = bytes(data) if not data and size == (0, 0): return None return Image.frombytes(mode, size, data, codec, args)
def astc(imageData: bytes, width: int, height: int, block_size: tuple) -> Image: imageData = tex2img.decompress_astc(imageData, width, height, *block_size, False) return Image.frombytes("RGBA", (width, height), imageData, "raw", "RGBA")