def write_bytes(file, source):
    with open(source.path, mode="rb") as binary:
        buffer = binary.read()
        buffer = buffer[148:]  # skip .dds header

        file.write("0x{:02x}".format(buffer[0]))
        for byte in range(1, len(buffer)):
            file.write(",0x{:02x}".format(buffer[byte]))


def run(target, source, env):
    with open(str(target[0]), "w", encoding="utf-8", newline="\n") as file:
        file.write('/* WARNING, THIS FILE WAS GENERATED BY THE "{}" SCRIPT, DO NOT EDIT */ \n\n'.format(source[0].name))

        file.write(
            "// LTC Lookup table for BRDF fitting by Eric Heitz (https://eheitzresearch.wordpress.com/415-2/) \n"
        )
        file.write("static const int LTC_LUT_DIMENSIONS = 64; // 64x64\n")
        file.write("static const uint8_t LTC_LUT1[] = {")
        write_bytes(file, source[1])
        file.write("};\n\n")

        file.write("static const uint8_t LTC_LUT2[] = {")
        write_bytes(file, source[2])
        file.write("};\n\n")
