def unpack_PVR(self, buff, name):
     if buff[:3] != b"PVR":
         return buff
     if self.dal_dec.keepPVR:
         base_name = os.path.splitext(self.dal_dec.name)[0]
         filepath = os.path.join(self.dal_dec.output_path,
                                 self.dal_dec.relpath,
                                 name + base_name + ".pvr")
         self.dal_dec.write(filepath, buff)
     pixel_format = struct.unpack('<I', buff[4 * 2:4 * 2 + 4])[0]
     if pixel_format == 6:
         mode = 0
     elif pixel_format == 2:
         mode = 11
     height = struct.unpack('<I', buff[4 * 6:4 * 6 + 4])[0]
     width = struct.unpack('<I', buff[4 * 7:4 * 7 + 4])[0]
     img = buff[67:]
     rgba_data = basisu_decompress(img, width, height, mode)
     im = Image.frombytes("RGBA", (width, height), rgba_data)
     with io.BytesIO() as result:
         im = im.convert(
             'RGB') if self.base_ext[1:].lower() == 'jpg' else im
         im_format = 'JPEG' if self.base_ext[1:].lower(
         ) == 'jpg' else self.base_ext[1:].upper()
         im.save(result, format=im_format)
         result = result.getvalue()
     return result
Пример #2
0
def pvrtc(imageData: bytes, width: int, height: int, fmt: int):
    if platform == "darwin" or PREFER_BASISU:
        imageData = tex2img.decompress_pvrtc(imageData, width, height, False)
    else:
        imageData = tex2img.basisu_decompress(imageData, width, height, fmt)

    return Image.frombytes("RGBA", (width, height), imageData, "raw", "RGBA")
Пример #3
0
def test_basisu():
    print()  #new line in pytest
    for name in zip.namelist():
        if not name.endswith("data"):
            continue
        base_name = name[:-5]
        if base_name not in BASISU_FORMAT:
            continue
        print(base_name, BASISU_FORMAT[base_name], end="")

        # 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"]
        dec = basisu_decompress(data, width, height, BASISU_FORMAT[base_name])

        # load raw image data
        dec_img = Image.frombytes("RGBA", (width, height), dec, 'raw',
                                  ("RGBA"))

        print(" - decoding successfull", end="")
        # compare images
        try:
            dec_channels = dec_img.split()
            ori_channels = ori_img.split()
            for i in range(len(ori_channels)):
                assert (ImageChops.difference(
                    ori_channels[i], dec_channels[i]).getbbox() is None)
            print()
        except (AssertionError, ValueError):
            print(" - assert failed")