// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
cmpl $0x2, %edi
jl 0x14a3
movq 0x8(%rsi), %rbx
cmpb $0x0, (%rbx)
je 0x14a3
movq %rbx, -0xc0(%rbp)
leaq 0xcf0(%rip), %rsi       # 0x2010
movq %rbx, %rdi
callq 0x1150
testq %rax, %rax
je 0x176c
movq %rax, %r13
leaq 0xcf9(%rip), %rsi       # 0x2034
leaq -0xc0(%rbp), %rdi
callq 0x1120
testq %rax, %rax
je 0x1782
movq %rax, %rbx
movq %rax, %rdi
callq 0x1090
movl $0x7270732e, (%rbx,%rax) # imm = 0x7270732E
movb $0x0, 0x4(%rbx,%rax)
leaq 0xccd(%rip), %rsi       # 0x203b
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
callq 0x1150
movq %rax, -0xb0(%rbp)
leaq 0xcb6(%rip), %rdi       # 0x203e
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x11a0
movq %rax, %r12
movq %rax, %rbx
movq %rax, %rdi
callq 0x10f0
movq %rax, %r15
movq %rax, %r14
movq 0x2c32(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
movq %r12, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x10d0
movq %r12, %rdi
movq %r13, -0xb8(%rbp)
movq %r13, %rsi
callq 0x1130
movq %r12, %rdi
xorl %esi, %esi
callq 0x1160
movq %r12, %rdi
movq %r15, %rsi
callq 0x1110
movq %r12, %rdi
movq %r15, %rsi
callq 0x1140
movl %eax, -0x50(%rbp)
movl %eax, -0x4c(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1190
movl %eax, %r13d
movq %r12, %rdi
movq %r15, %rsi
callq 0x1060
movq %r12, %rdi
movq %r15, %rsi
callq 0x1040
movq %r12, %rdi
callq 0x10e0
movq %r12, %rdi
movq %r15, %rsi
callq 0x1180
movq %r12, %rdi
movq 0x2ba4(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10a0
movq %rax, %rdi
callq 0x10d0
movl %r13d, -0x48(%rbp)
movslq %r13d, %r15
leaq (,%r15,8), %rdi
callq 0x1100
movq %rax, %r12
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movl %ecx, %eax
movq -0x78(%rbp), %r15
movq %rax, %r13
cmpq %r15, %r13
je 0x14c4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10c0
movq %rax, %rdi
callq 0x1100
movq %rax, (%r12,%r15,8)
incq %r15
jmp 0x1482
leaq 0xc33(%rip), %rdi       # 0x20dd
callq 0x1070
pushq $0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1050
pcmpeqd %xmm0, %xmm0
leaq -0xf0(%rbp), %rax
movdqa %xmm0, 0x20(%rax)
movdqa %xmm0, 0x10(%rax)
movdqa %xmm0, (%rax)
pushq $0x4
popq %rcx
movl -0x4c(%rbp), %eax
cltd
idivl %ecx
imull -0x48(%rbp), %eax
movslq %eax, %rdi
movq %rdi, -0x98(%rbp)
callq 0x1100
movq %rax, -0x58(%rbp)
xorl %r15d, %r15d
movl -0x50(%rbp), %eax
testl %eax, %eax
cmovlel %r15d, %eax
movq %rax, -0x90(%rbp)
pushq $0x3
popq %rdi
movl %r15d, %eax
movq %rax, %rbx
movl %r15d, %r14d
movl %edi, %ecx
movq %r12, -0xa8(%rbp)
movq %r13, -0xa0(%rbp)
cmpq %r13, %rax
je 0x1651
movq %rax, -0x88(%rbp)
movq (%r12,%rax,8), %rax
movq %rax, -0x80(%rbp)
xorl %eax, %eax
movl %eax, %r13d
movl %ecx, %r12d
cmpq %r13, -0x90(%rbp)
je 0x1631
movq -0x80(%rbp), %rax
movzbl (%rax,%r13,4), %ecx
movl %ecx, -0x70(%rbp)
movzbl 0x1(%rax,%r13,4), %edx
movl %edx, -0x6c(%rbp)
movzbl 0x2(%rax,%r13,4), %esi
movl %esi, -0x68(%rbp)
movzbl 0x3(%rax,%r13,4), %eax
movl %eax, -0x64(%rbp)
testl %eax, %eax
je 0x15b7
movl %esi, -0x3c(%rbp)
movl %edx, -0x40(%rbp)
movl %ecx, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x12ac
cmpl $-0x1, %eax
je 0x15c1
movl %eax, %ecx
movl %r14d, %edx
movl %r15d, %eax
jmp 0x15f0
movl %r14d, %edx
pushq $0x1
popq %rax
movl %edi, %ecx
jmp 0x15f3
movslq %r14d, %rax
imulq $0xc, %rax, %rcx
movl -0x44(%rbp), %edx
movl %edx, -0xf0(%rbp,%rcx)
movl -0x40(%rbp), %edx
movl %edx, -0xec(%rbp,%rcx)
movl -0x3c(%rbp), %edx
movl %edx, -0xe8(%rbp,%rcx)
incl %eax
movl %eax, %edx
movl %r15d, %eax
movl %r14d, %ecx
pushq $0x3
popq %rdi
movl %ecx, %esi
movl %eax, %r15d
movl %r12d, %ecx
addl %ecx, %ecx
shll %cl, %esi
movl %esi, %ecx
movq -0x58(%rbp), %rsi
orb %cl, (%rsi,%rbx)
movl %edx, %r14d
movl %r12d, %ecx
decl %ecx
xorl %edx, %edx
testl %r12d, %r12d
setle %sil
movb %sil, %dl
movl %ecx, %r12d
cmovlel %edi, %r12d
addq %rbx, %rdx
incq %r13
movq %rdx, %rbx
jmp 0x1557
movq -0x88(%rbp), %rax
incq %rax
movl %r12d, %ecx
movq -0xa8(%rbp), %r12
movq -0xa0(%rbp), %r13
jmp 0x1537
pushq $0x1
popq %rsi
movl %r15d, -0x2c(%rbp)
movq -0x58(%rbp), %r15
movq %r15, %rdi
movq -0x98(%rbp), %rdx
movq -0xb0(%rbp), %r14
movq %r14, %rcx
callq 0x1170
leaq 0x9c9(%rip), %rdi       # 0x2045
xorl %ebx, %ebx
movq %rax, %rsi
movq -0x38(%rbp), %rdx
movl %ebx, %eax
callq 0x10b0
movq %r14, %rdi
callq 0x1080
movq %r15, %rdi
movl -0x2c(%rbp), %r15d
callq 0x1030
cmpq %rbx, %r13
je 0x16b3
movq (%r12,%rbx,8), %rdi
callq 0x1030
incq %rbx
jmp 0x16a0
movq %r12, %rdi
callq 0x1030
movq -0xb8(%rbp), %rdi
callq 0x1080
leaq 0x98e(%rip), %rdi       # 0x205c
xorl %r14d, %r14d
movl %r14d, %ebx
movq -0x38(%rbp), %rsi
movl %r14d, %eax
callq 0x10b0
pushq $0x4
popq %rax
subl %r15d, %eax
movl %eax, %r15d
leaq -0xe8(%rbp), %r13
pushq $0x24
popq %r12
cmpq %r14, %r15
je 0x174d
movl -0x8(%r13), %eax
movl -0x4(%r13), %ecx
cltd
idivl %r12d
shll $0x5, %eax
cvtsi2sd %eax, %xmm0
movl %ecx, %eax
cltd
idivl %r12d
shll $0x2, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl (%r13), %xmm2
addsd %xmm0, %xmm1
divsd 0x8dd(%rip), %xmm2      # 0x2008
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %edx
movl %r14d, %esi
leaq 0x934(%rip), %rdi       # 0x2071
movl %ebx, %eax
callq 0x10b0
incq %r14
addq $0xc, %r13
jmp 0x16f4
cmpl $0x0, -0x2c(%rbp)
je 0x1765
leaq 0x936(%rip), %rdi       # 0x2090
xorl %eax, %eax
movq -0x38(%rbp), %rsi
callq 0x10b0
xorl %eax, %eax
jmp 0x14b2
leaq 0x8a0(%rip), %rdi       # 0x2013
xorl %eax, %eax
movq %rbx, %rsi
callq 0x10b0
jmp 0x14af
leaq 0x928(%rip), %rdi       # 0x20b1
jmp 0x14aa