// File: /workspace/github_c_makefile_star2_O0/minkcv[P]vm/convertsprite.c
// Function: main
int main (int argc, char** argv)
{
    /*
    fprintf(stderr, "   Compiled with libpng %s; using libpng %s.\n",
      PNG_LIBPNG_VER_STRING, png_libpng_ver);
    fprintf(stderr, "   Compiled with zlib %s; using zlib %s.\n",
      ZLIB_VERSION, zlib_version);
    */

    if (argc < 2 || !strlen(argv[1]))
    {
        printf("Usage: convertsprite sprite.png\n");
        return 1;
    }
    char* filename = argv[1];
    FILE* fp = fopen(filename, "rb");
    if (fp == NULL)
    {
        printf("Couldn't open file with name %s\n", filename);
        return 1;
    }
    char* filenameNoExtension = strsep(&filename, ".");
    if (filenameNoExtension == NULL)
    {
        printf("Filename should have \".png\" as an extension\n");
        return 1;
    }

    FILE* spriteFile = fopen(strcat(filenameNoExtension, ".spr"), "wb");
    int x, y;
    int width, height;
    png_byte colorType;
    png_byte bitDepth;
    png_structp pngPtr;
    png_infop infoPtr;
    int numberOfPasses;
    png_bytep* rowPointers;
    pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    infoPtr = png_create_info_struct(pngPtr);
    setjmp(png_jmpbuf(pngPtr));
    png_init_io(pngPtr, fp);
    png_set_sig_bytes(pngPtr, 0);
    png_read_info(pngPtr, infoPtr);
    width = png_get_image_width(pngPtr, infoPtr);
    height = png_get_image_height(pngPtr, infoPtr);
    colorType = png_get_color_type(pngPtr, infoPtr);
    bitDepth = png_get_bit_depth(pngPtr, infoPtr);
    //printf("Image width: %d\n", width);
    //printf("Image height: %d\n", height);
    //printf("Color type: %d\n", colorType);
    //printf("Bit depth: %d\n", bitDepth);
    numberOfPasses = png_set_interlace_handling(pngPtr);
    png_read_update_info(pngPtr, infoPtr);
    setjmp(png_jmpbuf(pngPtr));
    rowPointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
    for (y = 0; y < height; y++)
        rowPointers[y] = (png_byte*)malloc(png_get_rowbytes(pngPtr, infoPtr));
    png_read_image(pngPtr, rowPointers);

    int usedColors = 0;
    int colors[4][3];
    memset(colors, -1, sizeof(int) * 4 * 3);
    int rgba[4] = {};
    uint8_t* sprite = (uint8_t*)malloc((width / 4) * height);
    size_t spriteOffset = 0;
    int currentBits = 3; // 4 pairs of bits in a byte
    int useAlpha = 0;
    for (y = 0; y < height; y++)
    {
        png_byte* row = rowPointers[y];
        for (x = 0; x < width; x++)
        {
            png_byte* pixel = &(row[x * 4]); // RGBA
            rgba[0] = pixel[0];
            rgba[1] = pixel[1];
            rgba[2] = pixel[2];
            rgba[3] = pixel[3];
            int index;
            if (rgba[3] == 0) // Transparent
            {
                index = 3;
                useAlpha = 1;
            }
            else
            {
                index = getColorIndex(rgba, colors); // 2 bit index into colors[4]
                if (index == -1)
                {
                    //printf("%3d %3d %3d (%3d) at %2d %2d is a new color\n", rgba[0], rgba[1], rgba[2], rgba[3], x, y);
                    colors[usedColors][0] = rgba[0];
                    colors[usedColors][1] = rgba[1];
                    colors[usedColors][2] = rgba[2];
                    index = usedColors;
                    usedColors++;
                }
            }
            sprite[spriteOffset] |= index << (2 * currentBits);
            currentBits--;
            if (currentBits < 0)
            {
                currentBits = 3;
                spriteOffset++;
            }
        }
    }

    size_t written = fwrite(sprite, sizeof(uint8_t), (width  / 4) * height, spriteFile);
    printf("Wrote %zd bytes to %s\n", written, filenameNoExtension);
    fclose(spriteFile);
    free(sprite);
    for (y = 0; y < height; y++)
        free(rowPointers[y]);
    free(rowPointers);

    fclose(fp);

    printf("%s's color palette:\n", filenameNoExtension);
    int i;
    for (i = 0; i < 4 - useAlpha; i++)
    {
        // Look in the gimp palette to gain insight into this.
        int fullPaletteIndex = ceil((colors[i][0] / 36) * 32) + ((colors[i][1] / 36) * 4) + (colors[i][2] / 85.0);
        printf("Color %d has palette index %d\n", i, fullPaletteIndex);
    }
    if (useAlpha)
        printf("%s uses color 4 as transparency\n", filenameNoExtension);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x1392
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1080
cmpq $0x0, %rax
jne 0x13ac
leaq 0xc77(%rip), %rdi       # 0x2010
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1952
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xc6e(%rip), %rsi       # 0x2031
callq 0x1150
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x13f1
movq -0x18(%rbp), %rsi
leaq 0xc56(%rip), %rdi       # 0x2034
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1952
leaq -0x18(%rbp), %rdi
leaq 0xc59(%rip), %rsi       # 0x2055
callq 0x1120
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1426
leaq 0xc44(%rip), %rdi       # 0x2057
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1952
movq -0x28(%rbp), %rdi
leaq 0xc53(%rip), %rsi       # 0x2084
callq 0x1170
movq %rax, %rdi
leaq 0xc49(%rip), %rsi       # 0x2089
callq 0x1150
movq %rax, -0x30(%rbp)
leaq 0xc3c(%rip), %rdi       # 0x208c
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x11c0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x10f0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq 0x2b6d(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x1090
movq %rax, %rdi
callq 0x10d0
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x1130
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x1160
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1110
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1140
movl %eax, -0x3c(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x11a0
movl %eax, -0x40(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1060
movb %al, -0x41(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1040
movb %al, -0x42(%rbp)
movq -0x50(%rbp), %rdi
callq 0x10e0
movl %eax, -0x5c(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1190
movq -0x50(%rbp), %rdi
movq 0x2ad2(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x1090
movq %rax, %rdi
callq 0x10d0
movslq -0x40(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1100
movq %rax, -0x68(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x1577
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x10c0
movq %rax, %rdi
callq 0x1100
movq %rax, %rdx
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1540
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x1050
movl $0x0, -0x6c(%rbp)
leaq -0xa0(%rbp), %rdi
movl $0xff, %esi
movl $0x30, %edx
callq 0x10b0
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10b0
movl -0x3c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
imull -0x40(%rbp), %eax
movslq %eax, %rdi
callq 0x1100
movq %rax, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movl $0x3, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x17aa
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x179a
movq -0xd0(%rbp), %rax
movl -0x34(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, -0xac(%rbp)
movq -0xd8(%rbp), %rax
movzbl 0x2(%rax), %eax
movl %eax, -0xa8(%rbp)
movq -0xd8(%rbp), %rax
movzbl 0x3(%rax), %eax
movl %eax, -0xa4(%rbp)
cmpl $0x0, -0xa4(%rbp)
jne 0x16a9
movl $0x3, -0xdc(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0x172f
leaq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x12d0
movl %eax, -0xdc(%rbp)
cmpl $-0x1, -0xdc(%rbp)
jne 0x172d
movl -0xb0(%rbp), %ecx
movslq -0x6c(%rbp), %rdx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0xac(%rbp), %ecx
movslq -0x6c(%rbp), %rdx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0xa8(%rbp), %ecx
movslq -0x6c(%rbp), %rdx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x6c(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x172f
movl -0xdc(%rbp), %esi
movl -0xc4(%rbp), %ecx
shll %ecx
shll %cl, %esi
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0xc4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x178a
movl $0x3, -0xc4(%rbp)
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x178c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x161e
jmp 0x179c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x15f8
movq -0xb8(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
imull -0x40(%rbp), %eax
movslq %eax, %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %esi
callq 0x1180
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq 0x8a9(%rip), %rdi       # 0x2093
movb $0x0, %al
callq 0x10a0
movq -0x30(%rbp), %rdi
callq 0x1070
movq -0xb8(%rbp), %rdi
callq 0x1030
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x1831
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1030
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x180d
movq -0x68(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1070
movq -0x28(%rbp), %rsi
leaq 0x85c(%rip), %rdi       # 0x20aa
movb $0x0, %al
callq 0x10a0
movl $0x0, -0xec(%rbp)
movl -0xec(%rbp), %eax
movl $0x4, %ecx
subl -0xc8(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1930
movslq -0xec(%rbp), %rcx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl $0x24, %ecx
cltd
idivl %ecx
shll $0x5, %eax
cvtsi2sd %eax, %xmm0
callq 0x11b0
movslq -0xec(%rbp), %rcx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl $0x24, %ecx
cltd
idivl %ecx
shll $0x2, %eax
cvtsi2sd %eax, %xmm1
addsd %xmm1, %xmm0
movslq -0xec(%rbp), %rcx
leaq -0xa0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cvtsi2sdl 0x8(%rax), %xmm1
movsd 0x718(%rip), %xmm2      # 0x2008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xf0(%rbp)
movl -0xec(%rbp), %esi
movl -0xf0(%rbp), %edx
leaq 0x7aa(%rip), %rdi       # 0x20bf
movb $0x0, %al
callq 0x10a0
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x185f
cmpl $0x0, -0xc8(%rbp)
je 0x194b
movq -0x28(%rbp), %rsi
leaq 0x79a(%rip), %rdi       # 0x20de
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq