// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl -0x24(%rbp), %eax
imull -0x28(%rbp), %eax
imull 0x10(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x30(%rbp)
leaq 0x1ed77(%rip), %rax     # 0x28748
cmpl $0x0, (%rax)
je 0x99ed
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movl 0x10(%rbp), %ecx
leaq 0x194db(%rip), %rdi     # 0x22ec1
movb $0x0, %al
callq 0x10d0
movl -0x30(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x11d0
movq %rax, -0x38(%rbp)
movl -0x24(%rbp), %eax
imull -0x28(%rbp), %eax
movslq %eax, %rdi
callq 0x11d0
movq %rax, -0x40(%rbp)
movb $0x0, %al
callq 0x13a0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
imull -0x28(%rbp), %edx
callq 0x14c0
movq -0x48(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x1c10
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %edx
callq 0x2b60
movq -0x48(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x3620
movq -0x48(%rbp), %rdi
callq 0x1450
cmpl $0x4, 0x10(%rbp)
jne 0x9ac7
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x9ac5
movq -0x40(%rbp), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
movq -0x40(%rbp), %rcx
movl -0x4c(%rbp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x9a7a
jmp 0x9ad8
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x11b0
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x9bd3
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl $0x8, %ecx
cltd
idivl %ecx
andl $0x1f, %eax
movb %al, -0x51(%rbp)
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl $0x8, %ecx
cltd
idivl %ecx
andl $0x1f, %eax
movb %al, -0x52(%rbp)
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl $0x8, %ecx
cltd
idivl %ecx
andl $0x1f, %eax
movb %al, -0x53(%rbp)
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovgl %ecx, %eax
movb %al, -0x54(%rbp)
movzbl -0x51(%rbp), %eax
shll $0x3, %eax
movzbl -0x52(%rbp), %ecx
sarl $0x2, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movzbl -0x52(%rbp), %eax
andl $0x3, %eax
shll $0x6, %eax
movzbl -0x53(%rbp), %ecx
shll %ecx
orl %ecx, %eax
movzbl -0x54(%rbp), %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x9adf
movq -0x38(%rbp), %rdi
callq 0x1040
movq -0x40(%rbp), %rdi
callq 0x1040
movl -0x2c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop