// File: /workspace/github_c_makefile_star2_O2/DavidSM64[P]n64graphics_ci/n64graphics_ci.c
// Function: rgba2rawci
int rgba2rawci(uint8_t *raw, uint8_t *out_palette, int *pal_len, const rgba *img, int width, int height, int depth)
{
    int size = width * height * depth / 8;
    int num_colors = *pal_len / 2;

    INFO("Converting RGBA %dx%d to raw CI%d\n", width, height, depth);

    uint8_t *rgba32_palette = malloc(num_colors * 4);
    uint8_t *ci8_raw = malloc(width * height);

    // Use ExoQuant to convert the RGBA32 data buffer to an CI8 output
    exq_data *pExq = exq_init();
    exq_feed(pExq, (uint8_t*)img, width * height);
    exq_quantize_hq(pExq, num_colors);
    exq_get_palette(pExq, rgba32_palette, num_colors);
    exq_map_image_ordered(pExq, width, height, (uint8_t*)img, ci8_raw);
    exq_free(pExq);

    if (depth == 4) {
        // Convert CI8 image to CI4 image
        for (int i = 0; i < size; i++) {
            raw[i] = (ci8_raw[i * 2 + 0] << 4) | ci8_raw[i * 2 + 1];
        }
    }
    else {
        memcpy(raw, ci8_raw, size);
    }

    // Convert RGBA32 palette to RGBA16
    for (int i = 0; i < num_colors; i++) {
        unsigned char red = (rgba32_palette[i * 4 + 0] / 8) & 0x1F;
        unsigned char green = (rgba32_palette[i * 4 + 1] / 8) & 0x1F;
        unsigned char blue = (rgba32_palette[i * 4 + 2] / 8) & 0x1F;
        unsigned char alpha = rgba32_palette[i * 4 + 3] > 0 ? 1 : 0; // 1 bit alpha

        out_palette[i * 2 + 0] = (red << 3) | (green >> 2);
        out_palette[i * 2 + 1] = ((green & 3) << 6) | (blue << 1) | alpha;
    }

    free(rgba32_palette);
    free(ci8_raw);
    return size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r13d
movq %rcx, -0x50(%rbp)
movq %rdx, %r9
movq %rsi, %rbx
movq %rdi, -0x48(%rbp)
movl 0x10(%rbp), %ecx
movl %r13d, %r14d
movl %r8d, -0x30(%rbp)
imull %r8d, %r14d
movl %r14d, %eax
imull %ecx, %eax
pushq $0x8
popq %rsi
cltd
idivl %esi
movl %eax, -0x2c(%rbp)
movl (%r9), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
movl %eax, %r15d
leaq 0x115d2(%rip), %rax     # 0x181e8
cmpl $0x0, (%rax)
je 0x6c2f
leaq 0xc886(%rip), %rdi      # 0x134a8
movl -0x30(%rbp), %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x10e0
leal (,%r15,4), %eax
movslq %eax, %rdi
callq 0x11d0
movq %rax, %r12
movslq %r14d, %rdi
movq %rdi, -0x58(%rbp)
callq 0x11d0
movl %r13d, -0x3c(%rbp)
movq %rax, %r13
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x13ac
movq %rax, -0x38(%rbp)
movq %rax, %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x1447
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x17bf
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1f0a
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %esi
movl -0x3c(%rbp), %edx
movq -0x50(%rbp), %rcx
movq %r13, %r8
callq 0x24c1
movq -0x38(%rbp), %rdi
callq 0x1400
cmpl $0x4, 0x10(%rbp)
jne 0x6cdf
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq -0x48(%rbp), %rdx
cmpq %r14, %rax
je 0x6cef
movb (%r13,%r14,2), %cl
shlb $0x4, %cl
orb 0x1(%r13,%r14,2), %cl
movb %cl, (%rdx,%r14)
incq %r14
jmp 0x6cc4
movslq -0x2c(%rbp), %rdx
movq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x11b0
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
cmpq %rax, %r15
je 0x6d44
movb (%r12,%rax,4), %cl
movb 0x1(%r12,%rax,4), %dl
andb $-0x8, %cl
movb 0x2(%r12,%rax,4), %sil
cmpb $0x0, 0x3(%r12,%rax,4)
setne %dil
movl %edx, %r8d
shrb $0x5, %r8b
orb %cl, %r8b
movb %r8b, (%rbx,%rax,2)
shlb $0x3, %dl
andb $-0x40, %dl
shrb $0x2, %sil
andb $0x3e, %sil
orb %dl, %sil
orb %dil, %sil
movb %sil, 0x1(%rbx,%rax,2)
incq %rax
jmp 0x6cf8
movq %r12, %rdi
callq 0x1040
movq %r13, %rdi
callq 0x1040
movl -0x2c(%rbp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq