def decodeDXT(width , height , data , dxt_type):
    import squish
    if dxt_type == "DXT1":squish_type = squish.DXT1
    if dxt_type == "DXT3":squish_type = squish.DXT3
    if dxt_type == "DXT5":squish_type = squish.DXT5
    img_raw = squish.decompressImage(data, width, height, squish_type)
    return img_raw
Example #2
0
def decodeDXT(width, height, data, dxt_type):
    import squish
    if dxt_type == "DXT1": dxt_type = squish.DXT1
    if dxt_type == "DXT3": dxt_type = squish.DXT3
    if dxt_type == "DXT5": dxt_type = squish.DXT5
    img_raw = squish.decompressImage(data, width, height, squish.DXT1)
    return img_raw.tostring()
Example #3
0
def read_compressed(tex: TexFile, mipmap_index: int) -> PIL.Image:
    assert(tex.pixel_format in (
        PixelFormat.DXT1,
        PixelFormat.DXT3,
        PixelFormat.DXT5,
        PixelFormat.RGBA)
    )
    pf_to_squish = {
        PixelFormat.DXT1: squish.DXT1,
        PixelFormat.DXT3: squish.DXT3,
        PixelFormat.DXT5: squish.DXT5
    }

    mipmap = tex.mipmaps[mipmap_index]
    if tex.pixel_format in pf_to_squish:
        with Timed('squish.decompressImage'):
            decompressed_data = memoryview(squish.decompressImage(
                mipmap.data.tobytes(),
                mipmap.width,
                mipmap.height,
                pf_to_squish[tex.pixel_format]
            ))
    elif tex.pixel_format == PixelFormat.RGBA:
        decompressed_data = mipmap.data


    with Timed('PIL.Image.frombuffer'):
        image = PIL.Image.frombuffer(
            'RGBA',
            (mipmap.width, mipmap.height),
            decompressed_data,
            'raw',
            'RGBA',
            0,
            -1 # Vertical flip (along x)
        )

    return image
Example #4
0
#!/usr/bin/env python
import sys
import pygame
import squish

if __name__ == "__main__":

    if len(sys.argv) != 4:
        print "Usage: python dxt1decompress.py <image.dxt1> <width> <height>"
        sys.exit(1)

    fn, width, height = sys.argv[1:]
    width, height = int(width), int(height)
    with open(fn, "rb") as fd:
        data = fd.read()

    # convert
    img_raw = squish.decompressImage(data, width, height, squish.DXT1)

    # write
    fn_png = fn.rsplit(".", 1)[0] + ".png"
    img = pygame.image.fromstring(img_raw, (width, height), "RGBA")
    pygame.image.save(img, fn_png)

    print "Written to", fn_png
Example #5
0
    def readImage(self, mipmapCount):
        self.data.seek(self.vtf_header.headerSize)
        print(VTF_DATA.VTF_FORMATS(self.vtf_header.highResImageFormat).name)
        print(self.flags)
        # if 'NOMIP' in self.flags:
        if self.vtf_header.mipmapCount <= 1:
            data = self.data.read()
        else:

            if VTF_DATA.VTF_FORMATS(
                    self.vtf_header.highResImageFormat).name == 'DXT5':
                off = self.calcImageSize(self.vtf_header.width,
                                         self.vtf_header.height,
                                         self.vtf_header.depth, 'DXT5')
                print(off)
                self.data.seek(-off, io.SEEK_END)
                data = self.data.read()

            elif VTF_DATA.VTF_FORMATS(
                    self.vtf_header.highResImageFormat).name == 'DXT1':
                off = self.calcImageSize(self.vtf_header.width,
                                         self.vtf_header.height,
                                         self.vtf_header.depth, 'DXT1')
                print(off)
                self.data.seek(-off, io.SEEK_END)
                data = self.data.read()
            else:
                print('kek')
                data = self.data.read()
                # print(data[:250])
        if VTF_DATA.VTF_FORMATS(
                self.vtf_header.highResImageFormat).name == 'DXT5':
            print('DECOMPRES DXT5')
            data = squish.decompressImage(data, self.vtf_header.width,
                                          self.vtf_header.height, squish.DXT5)
            mode = 'RGBA'
        elif VTF_DATA.VTF_FORMATS(
                self.vtf_header.highResImageFormat).name == 'DXT3':
            print('DECOMPRES DXT3')
            data = squish.decompressImage(data, self.vtf_header.width,
                                          self.vtf_header.height, squish.DXT3)
            mode = 'RGBA'
        elif VTF_DATA.VTF_FORMATS(
                self.vtf_header.highResImageFormat).name == 'DXT1':
            print('DECOMPRES DXT1')
            print(len(data))
            data = squish.decompressImage(data, self.vtf_header.width,
                                          self.vtf_header.height, squish.DXT1)
            mode = 'RGBA'
        elif VTF_DATA.VTF_FORMATS(
                self.vtf_header.highResImageFormat).name == 'RGBA8888':
            print('Reading RGBA8888')
            mode = 'RGBA'
            data = data
        # elif VTF_DATA.VTF_FORMATS(self.vtf_header.highResImageFormat).name == 'RGB888':
        #     print('Reading RGB888')
        #     mode = 'RGBA'
        #     print(len(data))
        #     a = time.time()
        #     data = converter.rgb2rgba(data)
        #     print((time.time()-a)*1000,'ms')
        #     data = data
        else:
            raise NotImplementedError(
                'Format {} currently is not supported'.format(
                    VTF_DATA.VTF_FORMATS(
                        self.vtf_header.highResImageFormat).name))
        # print('DONE DECOMPRESSING')
        self.IMAGE = data
        self.MODE = mode