// File: /workspace/github_c_makefile_star2_O3/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
movq %rcx, -0x48(%rbp)
movq %rsi, %r14
movq %rdi, -0x40(%rbp)
movl 0x10(%rbp), %ecx
movl %r9d, -0x34(%rbp)
movl %r9d, %r12d
movl %r8d, -0x30(%rbp)
imull %r8d, %r12d
movl %r12d, %eax
imull %ecx, %eax
leal 0x7(%rax), %r15d
testl %eax, %eax
movq %rax, -0x50(%rbp)
cmovnsl %eax, %r15d
movl (%rdx), %eax
movl %eax, %ebx
shrl $0x1f, %ebx
movl %eax, -0x38(%rbp)
addl %eax, %ebx
sarl %ebx
leaq 0x12063(%rip), %rax     # 0x191c8
cmpl $0x0, (%rax)
je 0x717e
leaq 0xd347(%rip), %rdi      # 0x144b8
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
xorl %eax, %eax
callq 0x10e0
sarl $0x3, %r15d
movl %r15d, -0x2c(%rbp)
leal (,%rbx,4), %eax
movslq %eax, %rdi
callq 0x11d0
movq %rax, %r15
movslq %r12d, %rdi
movq %rdi, -0x58(%rbp)
callq 0x11d0
movq %rax, %r12
xorl %eax, %eax
callq 0x14bc
movq %rax, %r13
movq %rax, %rdi
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x154e
movq %r13, %rdi
movl %ebx, %esi
callq 0x18ed
movq %r13, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x207f
movq %r13, %rdi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
movq -0x48(%rbp), %rcx
movq %r12, %r8
callq 0x267e
movq %r13, %rdi
callq 0x1506
cmpl $0x4, 0x10(%rbp)
jne 0x7227
cmpl $0x8, -0x50(%rbp)
movl -0x2c(%rbp), %r13d
movq -0x40(%rbp), %rsi
jl 0x723a
movl %r13d, %eax
xorl %ecx, %ecx
movb (%r12,%rcx,2), %dl
shlb $0x4, %dl
orb 0x1(%r12,%rcx,2), %dl
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x720e
jmp 0x723a
movl -0x2c(%rbp), %r13d
movslq %r13d, %rdx
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x11b0
cmpl $0x2, -0x38(%rbp)
jl 0x7290
movl %ebx, %eax
xorl %ecx, %ecx
movb (%r15,%rcx,4), %dl
movb 0x1(%r15,%rcx,4), %sil
andb $-0x8, %dl
movb 0x2(%r15,%rcx,4), %dil
cmpb $0x0, 0x3(%r15,%rcx,4)
setne %r8b
movl %esi, %r9d
shrb $0x5, %r9b
orb %dl, %r9b
movb %r9b, (%r14,%rcx,2)
shlb $0x3, %sil
andb $-0x40, %sil
shrb $0x2, %dil
andb $0x3e, %dil
orb %sil, %dil
orb %r8b, %dil
movb %dil, 0x1(%r14,%rcx,2)
incq %rcx
cmpq %rcx, %rax
jne 0x7244
movq %r15, %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1040
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq