import struct
from PIL import Image
import io
import os
import lzma

imgName = "ceedee"
hdr = "#ifndef _LI_"+imgName.upper()+"_TEX_H\n"
hdr += "#define _LI_"+imgName.upper()+"_TEX_H 1\n"
hdr += "#include <cstdint>\n"
hdr += "#include \"diff.hpp\"\n"

def readImg(imgName):
    with Image.open(imgName+".png") as img:
        img = img.convert("RGBA")
        imgWidth, imgHeight = img.size
        with io.BytesIO(b"") as bio:
                for y in range(0, imgHeight):
                    for x in range(0, imgWidth):
                        r, g, b, a = img.getpixel((x, y))
                        
                        rgba = 0;
                        rgba |= a << 24
                        rgba |= b << 16
                        rgba |= g << 8
                        rgba |= r 
                        
                        bio.write(struct.pack("I", rgba))
                bio.seek(0, os.SEEK_SET)
                return (imgName, imgWidth, imgHeight, 0x4, bio.read())

def diffTexture(firstImg, otherImg):
    global hdr
    
    imgName, imgWidth, imgHeight, imgChannels, imgData = firstImg
    otherImgName, otherImgWidth, otherImgHeight, otherImgChannels, otherImgData = otherImg
    
    lst = []
    for i in range(0, len(otherImgData), imgChannels):
        c1 = struct.unpack("I", otherImgData[i:i+otherImgChannels])[0]
        c2 = struct.unpack("I", imgData[i:i+imgChannels])[0]
        if c1 == c2:
            continue
        else:
            lst.append("ImgDiff({ "+hex(int(i/4))+", "+hex(c1)+" })")
    hdr += "const uint32_t "+otherImgName+"DiffSz = "+hex(len(lst))+";\n"
    hdr += "static ImgDiff "+otherImgName+"Diff["+otherImgName+"DiffSz] { " + ", ".join(lst) +"};\n"
    
def readTexture(img):
    global hdr
    imgName, imgWidth, imgHeight, imgChannels, imgData = img
    
    hdr += "const uint32_t "+imgName+"ImgWidth = "+hex(imgWidth)+";\n"
    hdr += "const uint32_t "+imgName+"ImgHeight = "+hex(imgHeight)+";\n"
    hdr += "const uint32_t "+imgName+"ImgChannels = "+hex(imgChannels)+";\n"
    
    compressedRgbaValues = []
    compressedData = bytearray(lzma.compress(imgData, format=lzma.FORMAT_RAW, filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}]))
    
    for i in range(0, len(compressedData)):
        compressedRgbaValues.append(hex(compressedData[i]))
        
    hdr += "const uint32_t "+imgName+"ImgCompressedDataSz = "+hex(len(compressedRgbaValues))+";\n"
    hdr += "const uint32_t "+imgName+"ImgUncompressedSz = (("+imgName+"ImgWidth * "+imgName+"ImgHeight) * "+imgName+"ImgChannels);\n"
    hdr += "static uint8_t "+imgName+"ImgCompressedData["+imgName+"ImgCompressedDataSz] { "+", ".join(compressedRgbaValues)+" };\n"
    
hdr += "/* Texture */\n"

img = readImg(imgName)
imgCd = readImg(imgName+"Cd")
imgDvd = readImg(imgName+"Dvd")
imgHdDvd = readImg(imgName+"HdDvd")
imgBd = readImg(imgName+"BluRay")

readTexture(img)

hdr += "/* Diff */\n"
diffTexture(img, imgCd)
diffTexture(img, imgDvd)
diffTexture(img, imgHdDvd)
diffTexture(img, imgBd)
hdr += "#endif"
print(hdr)