// File: /workspace/github_c_makefile_star2_O3/halcy[P]quasi_crystals_bot/Engine/Quantize.c
// Function: QuantizeBitmap
int QuantizeBitmap(const Bitmap *bitmap, uint8_t *output, Palette palette, const Bitmap* orig) {
    uint32_t *colourarray=calloc(256*256*256*4,1);
    uint32_t *colourarrayorig=calloc(256*256*256*4,1);
    uint32_t *colourcount=calloc(256*256*256*sizeof(int),1);
    uint32_t *colourindices=calloc(256*256*256*sizeof(int),1);
    Pixel* colourlist=(Pixel*)calloc(256*256*256*sizeof(Pixel),1);
    int numcolours=0;
    
    Pixel brightest = RGB(0, 0, 0);
    Pixel darkest = RGB(255, 255, 255);

    int palcount = 0;
    for(int y=0;y<orig->height;y++)
    for(int x=0;x<orig->width;x++)
    {
        Pixel p=ReadPixelNoClip(orig,x,y);
        int index=PixelToIndex(p);
        if(colourarrayorig[index] == 0) {
            palette[palcount] = p;
            palcount += 1;
        }
        colourarrayorig[index]++;
    }

    int palInitialSize = palcount;
    if(palcount > 150) {
        palInitialSize = 0;
    }
    for(int y=0;y<bitmap->height;y++)
    for(int x=0;x<bitmap->width;x++)
    {
        Pixel p=ReadPixelNoClip(bitmap,x,y);
        int index=PixelToIndex(p);
        colourcount[index]++;
        if(colourarray[index]==0) {
            colourlist[numcolours] = p;
            colourindices[numcolours] = index + 1;
            if(palcount > 150) {            
                if(colourarrayorig[index] != 0) {
                    // Can't be more than 256 colours in the orig image, so this is fine
                    palette[palInitialSize] = p;
                    palInitialSize++;
                }
            }
            numcolours++;
        }
        colourarray[index]++;
    }
        
    qsort(colourlist, numcolours, sizeof(Pixel), &PixelComp);
    
    int bestCountBright = 0;
    int bestCountDark = 0;
    for(int i = 0; i < numcolours / 25; i++) {
        if(colourcount[PixelToIndex(colourlist[i])] > bestCountBright) {
            bestCountBright = colourcount[PixelToIndex(colourlist[i])];
            brightest = colourlist[i];
        }

        if(colourcount[PixelToIndex(colourlist[numcolours - i - 1])] > bestCountDark) {
            bestCountDark = colourcount[PixelToIndex(colourlist[numcolours - i - 1])];
            darkest = colourlist[numcolours - i - 1];
        }
    }

    if(numcolours<=256)
    {
        int n=0;
        /*for(int i=0;i<256*256*256;i++)
        {
            if(colourarray[i])
            {
                palette[n]=IndexToPixel(i);
                colourarray[i]=n;
                n++;
            }
        }*/
        while(colourindices[n] != 0) {
            int i = colourindices[n] - 1;
            palette[n]=IndexToPixel(i);
            colourarray[i]=n;
            n++;
        }
        
        
        uint8_t *ptr=output;
        for(int y=0;y<bitmap->height;y++)
        for(int x=0;x<bitmap->width;x++)
        {
            Pixel p=ReadPixelNoClip(bitmap,x,y);
            int index=PixelToIndex(p);
            *ptr++=colourarray[index];
        }
    }
    else
    {
        palette[palInitialSize] = brightest;
        if(palInitialSize < 255) {
            palette[palInitialSize + 1] = darkest;
        }
        
        int index = palInitialSize + 2;
        float bestDiff = -1;
        
        // Terminates if index too big anyways, no need for more checks
        float* colDiffs = calloc(sizeof(float) * numcolours, 1);
        int lastIndex = 0;
        while(index < 256) {
            for(int col = 0; col < numcolours; col++) {
                Pixel p = colourlist[col];
                
                int found = 0;
                for(int i = 0; i < index; i++) {
                    if(palette[i] == p) {
                        found = 1;
                        break;
                    }
                }
                if(found == 1) {
                    continue;
                }
                
                for(int i = lastIndex; i < index; i++) {
                    colDiffs[col] += PixelDist(palette[i], p);
                }
                lastIndex = index;
                if(colDiffs[col] > bestDiff) {
                    palette[index] = p;
                    bestDiff = colDiffs[col];
                }
            }
            index += 1;
        }
        free(colDiffs);
        /*for(int i=0;i<256*256*256;i++)
        {
            if(colourarray[i])
            {
                Pixel p=IndexToPixel(i);
                float bestDiff = PixelDist(palette[0], p);
                colourarray[i] = 0;
                for(int j = 1; j < 256; j++) {
                    float dist = PixelDist(palette[j], p);
                    if(dist < bestDiff) {
                        bestDiff = dist;
                        colourarray[i] = j;
                    }
                    
                }
            }
        }*/
        
        int n = 0;
        while(colourindices[n] != 0) {
            int i = colourindices[n] - 1;
            Pixel p=IndexToPixel(i);
            float bestDiff = PixelDist(palette[0], p);
            colourarray[i] = 0;
            for(int j = 1; j < 256; j++) {
                float dist = PixelDist(palette[j], p);
                if(dist < bestDiff) {
                    bestDiff = dist;
                    colourarray[i] = j;
                }
                
            }
            n++;
        }
        
        uint8_t *ptr=output;
        for(int y=0;y<bitmap->height;y++)
        for(int x=0;x<bitmap->width;x++)
        {
            Pixel p=ReadPixelNoClip(bitmap,x,y);
            int index=PixelToIndex(p);
            *ptr++=colourarray[index];
            //*ptr++=SearchNeuquantIndex(&quant,ExtractRed(p),ExtractGreen(p),ExtractBlue(p));
        }

        numcolours=256;
        
// 		Neuquant quant;
// 		InitializeNeuquantNetwork(&quant);
// 		NeuquantLearn(&quant,bitmap,PixelLookup,bitmap->width*bitmap->height,1);
// 		UnbiasNeuquantNetwork(&quant);
// 
// 		for(int i=0;i<256;i++)
// 		{
// 			int r,g,b;
// 			GetNeuquantPaletteEntry(&quant,i,&r,&g,&b);
// 			palette[i]=RGB(r,g,b);
// 		}
// 
// 		BuildNeuquantIndex(&quant);
// 
// 		for(int i=0;i<256*256*256;i++)
// 		{
// 			if(colourarray[i])
// 			{
// 				Pixel p=IndexToPixel(i);
// 				colourarray[i]=SearchNeuquantIndex(&quant,ExtractRed(p),ExtractGreen(p),ExtractBlue(p));
// 			}
// 		}
// 
// 		uint8_t *ptr=output;
// 		for(int y=0;y<bitmap->height;y++)
// 		for(int x=0;x<bitmap->width;x++)
// 		{
// 			Pixel p=ReadPixelNoClip(bitmap,x,y);
// 			int index=PixelToIndex(p);
// 			*ptr++=colourarray[index];
// 			//*ptr++=SearchNeuquantIndex(&quant,ExtractRed(p),ExtractGreen(p),ExtractBlue(p));
// 		}
// 
// 		numcolours=256;
        
        
    }

    free(colourindices);
    free(colourlist);
    free(colourcount);
    free(colourarrayorig);
    free(colourarray);
    return 256;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x78(%rsp), %rsp
movq %rsi, 0x40(%rsp)
movq %rdi, 0x8(%rsp)
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
movq %rax, %rbx
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
movq %rax, %r12
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
movq %rax, 0x48(%rsp)
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
movq %rax, 0x18(%rsp)
callq 0x1110
movzwl 0x2(%r14), %ecx
movq %rax, 0x38(%rsp)
testw %cx, %cx
jle 0x4ea1
movzwl (%r14), %eax
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
testw %ax, %ax
jle 0x4e94
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x8544
movl %eax, %ecx
shrl $0x8, %ecx
movl (%r12,%rcx,4), %edx
testl %edx, %edx
jne 0x4e78
movslq %ebp, %rbp
movq 0x10(%rsp), %rsi
movl %eax, (%rsi,%rbp,4)
incl %ebp
incl %edx
incl %r15d
movl %edx, (%r12,%rcx,4)
movswl (%r14), %eax
cmpl %eax, %r15d
jl 0x4e50
movzwl 0x2(%r14), %ecx
movq 0x8(%rsp), %rdi
incl %r13d
movswl %cx, %edx
cmpl %edx, %r13d
jl 0x4e48
jmp 0x4ea8
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
movzwl 0x2(%rdi), %ecx
movq %r12, 0x58(%rsp)
testw %cx, %cx
jle 0x5269
xorl %r15d, %r15d
movl %ebp, %eax
cmpl $0x97, %ebp
cmovgel %r15d, %eax
xorl %r14d, %r14d
movq %rax, 0x20(%rsp)
movzwl (%rdi), %eax
testw %ax, %ax
jle 0x4f77
xorl %r13d, %r13d
movl %r13d, %esi
movl %r14d, %edx
callq 0x8544
movl %eax, %ecx
movq 0x48(%rsp), %rsi
shrl $0x8, %ecx
leal (%ecx), %edx
leal (,%rdx,4), %edx
incl (%rsi,%rdx)
movl (%rbx,%rdx), %edx
testl %edx, %edx
je 0x4f10
movq 0x8(%rsp), %rdi
jmp 0x4f5f
cmpl $0x97, %ebp
leal 0x1(%rcx), %esi
movslq %r15d, %rdi
movq 0x38(%rsp), %r8
movq 0x18(%rsp), %r9
movl %eax, (%r8,%rdi,4)
movl %esi, (%r9,%rdi,4)
jl 0x4f57
cmpl $0x0, (%r12,%rcx,4)
movq 0x8(%rsp), %rdi
je 0x4f5c
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %rsi
movslq %r8d, %r8
movl %eax, (%rsi,%r8,4)
incl %r8d
movq %r8, 0x20(%rsp)
jmp 0x4f5c
movq 0x8(%rsp), %rdi
incl %r15d
incl %edx
incl %r13d
movl %edx, (%rbx,%rcx,4)
movswl (%rdi), %eax
cmpl %eax, %r13d
jl 0x4ee0
movzwl 0x2(%rdi), %ecx
incl %r14d
movswl %cx, %edx
cmpl %edx, %r14d
jl 0x4ed4
movslq %r15d, %r14
leaq -0x2bc(%rip), %rcx      # 0x4cd4
movl $0x4, %edx
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x1050
cmpl $0x19, %r14d
movq 0x40(%rsp), %r12
jl 0x5286
movl %r14d, %eax
movq 0x38(%rsp), %r8
xorl %edi, %edi
movl $0xff, %ecx
xorl %r9d, %r9d
movq 0x48(%rsp), %r13
movq %rbx, 0x30(%rsp)
imulq $0x51eb851f, %rax, %rdx # imm = 0x51EB851F
movq %rax, 0x50(%rsp)
leaq -0x4(%r8,%r14,4), %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shrq $0x23, %rdx
movl (%r8), %r10d
movl (%rsi), %ebx
movl %r10d, %r11d
shrl $0x8, %r11d
movl (%r13,%r11,4), %r11d
cmpl %r9d, %r11d
cmoval %r11d, %r9d
movl %ebx, %r11d
cmoval %r10d, %ecx
shrl $0x8, %r11d
movl (%r13,%r11,4), %r10d
cmpl %edi, %r10d
cmoval %r10d, %edi
cmoval %ebx, %eax
leaq 0x4(%r8), %r8
leaq -0x4(%rsi), %rsi
decq %rdx
jne 0x4fe7
cmpl $0x100, %r15d           # imm = 0x100
movq 0x30(%rsp), %rbx
jle 0x5286
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rdx
movslq %r15d, %rbx
cmpl $0xff, %ebx
movl %ecx, (%rdx,%rbx,4)
jge 0x5353
shlq $0x2, %r14
movl $0x1, %esi
movl %eax, 0x4(%rdx,%rbx,4)
movq %r14, %rdi
callq 0x1110
cmpl $0xfe, %ebx
je 0x5364
addl $0x2, %r15d
movss 0x95df(%rip), %xmm0     # 0xe660
xorl %r8d, %r8d
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %rax, 0x60(%rsp)
movslq %r15d, %r9
movl %r9d, %r11d
xorl %r14d, %r14d
movq %r11, 0x68(%rsp)
movq 0x38(%rsp), %rcx
testq %r9, %r9
movq %rsi, %rdx
movl (%rcx,%r14,4), %r10d
movq %r11, %rcx
jle 0x50cc
cmpl %r10d, (%rdx)
je 0x5248
addq $0x4, %rdx
decq %rcx
jne 0x50ba
movslq %r8d, %rbp
cmpq %rbp, %r9
jle 0x5230
movl %r10d, %edx
movl $0xff, %edi
movl %r10d, %ecx
movl %r10d, %r15d
movss (%rax,%r14,4), %xmm1
movq %r14, 0x70(%rsp)
shrl $0x8, %edx
shrl $0x10, %ecx
andl %edi, %r15d
andl %edi, %edx
andl %edi, %ecx
movl %edx, %r8d
shll $0x8, %r8d
subl %edx, %r8d
movl %r10d, %edx
movl %r8d, 0x20(%rsp)
movl %ecx, %r8d
shrl $0x18, %edx
shll $0x8, %r8d
subl %ecx, %r8d
movl %r8d, 0x2c(%rsp)
movl %edx, %r8d
shll $0x8, %r8d
subl %edx, %r8d
movl (%rsi,%rbp,4), %ebx
xorl %r13d, %r13d
movl $0x0, %edi
movl %ebx, %r11d
andl $0xff, %r11d
je 0x5156
movzbl %bh, %ecx
xorl %edx, %edx
movl %ecx, %eax
shll $0x8, %eax
subl %ecx, %eax
divw %r11w
movzwl %ax, %edi
testl %r15d, %r15d
je 0x5169
movl 0x20(%rsp), %eax
xorl %edx, %edx
divw %r15w
movzwl %ax, %r13d
xorl %r12d, %r12d
movl $0x0, %r14d
testl %r11d, %r11d
je 0x5190
movl %ebx, %eax
xorl %edx, %edx
shrl $0x10, %eax
movzbl %al, %ecx
movl %ecx, %eax
shll $0x8, %eax
subl %ecx, %eax
divw %r11w
movzwl %ax, %r14d
testl %r15d, %r15d
je 0x51a3
movl 0x2c(%rsp), %eax
xorl %edx, %edx
divw %r15w
movzwl %ax, %r12d
xorl %esi, %esi
movl $0x0, %ecx
testl %r11d, %r11d
je 0x51c2
shrl $0x18, %ebx
xorl %edx, %edx
movl %ebx, %eax
shll $0x8, %eax
subl %ebx, %eax
divw %r11w
movzwl %ax, %ecx
testl %r15d, %r15d
je 0x51d3
movl %r8d, %eax
xorl %edx, %edx
divw %r15w
movzwl %ax, %esi
subl %r12d, %r14d
subl %r13d, %edi
subl %esi, %ecx
incq %rbp
movq 0x10(%rsp), %rsi
imull %edi, %edi
imull %r14d, %r14d
imull %ecx, %ecx
addl %edi, %r14d
addl %r14d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
cmpq %r9, %rbp
sqrtsd %xmm2, %xmm2
cvtsd2ss %xmm2, %xmm2
addss %xmm2, %xmm1
jl 0x512c
movq 0x60(%rsp), %rax
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %rbx
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %r11
movss %xmm1, (%rax,%r14,4)
jmp 0x5236
movss (%rax,%r14,4), %xmm1
ucomiss %xmm0, %xmm1
movl %r9d, %r8d
jbe 0x5248
movaps %xmm1, %xmm0
movl %r9d, %r8d
movl %r10d, (%rsi,%r9,4)
incq %r14
cmpq %rdi, %r14
jne 0x50a6
incq %r9
cmpl $0x100, %r9d            # imm = 0x100
jne 0x509b
jmp 0x5369
leaq -0x59c(%rip), %rcx      # 0x4cd4
movl $0x4, %edx
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x1050
movq 0x40(%rsp), %r12
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl (%rdi), %eax
testl %eax, %eax
je 0x52b6
xorl %ecx, %ecx
decl %eax
movslq %eax, %rdx
shll $0x8, %eax
orl $0xff, %eax
movl %eax, (%rsi,%rcx,4)
movl %ecx, (%rbx,%rdx,4)
movl 0x4(%rdi,%rcx,4), %eax
incq %rcx
testl %eax, %eax
jne 0x5298
movq 0x8(%rsp), %rax
movzwl 0x2(%rax), %ecx
testw %cx, %cx
jle 0x5313
movzwl (%rax), %eax
xorl %r14d, %r14d
testw %ax, %ax
jle 0x5308
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x8544
movq 0x8(%rsp), %rcx
shrl $0x8, %eax
movb (%rbx,%rax,4), %al
movb %al, (%r12,%r15)
incq %r15
movswl (%rcx), %eax
cmpl %eax, %r15d
jl 0x52d2
movzwl 0x2(%rcx), %ecx
addq %r15, %r12
movq 0x18(%rsp), %rdi
incl %r14d
movswl %cx, %edx
cmpl %edx, %r14d
jl 0x52ca
callq 0x1040
movq 0x38(%rsp), %rdi
callq 0x1040
movq 0x48(%rsp), %rdi
callq 0x1040
movq 0x58(%rsp), %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x1040
movl $0x100, %eax            # imm = 0x100
leaq 0x78(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shlq $0x2, %r14
movl $0x1, %esi
movq %r14, %rdi
callq 0x1110
movq 0x30(%rsp), %rbx
movq %rax, %rdi
callq 0x1040
movq 0x18(%rsp), %rax
movl (%rax), %ecx
testl %ecx, %ecx
je 0x5517
movq 0x10(%rsp), %rax
movl $0xff, %r10d
xorl %r14d, %r14d
movl (%rax), %eax
movl %eax, %esi
movl %eax, %edi
movzbl %ah, %edx
movl %eax, %r11d
shrl $0x18, %eax
shrl $0x10, %esi
andl $0xff00, %edi           # imm = 0xFF00
andl %r10d, %r11d
subl %edx, %edi
andl %r10d, %esi
movl %esi, %edx
movl %edi, 0x20(%rsp)
movl %eax, %edi
shll $0x8, %edx
shll $0x8, %edi
subl %esi, %edx
subl %eax, %edi
movl %edx, 0x2c(%rsp)
testl %r11d, %r11d
movl $0x0, %edx
movl $0x0, %esi
movl $0x0, %eax
je 0x5405
movl 0x20(%rsp), %eax
xorl %edx, %edx
divw %r11w
xorl %edx, %edx
movl %eax, %r8d
movl 0x2c(%rsp), %eax
movzwl %r8w, %esi
divw %r11w
xorl %edx, %edx
movl %eax, %r9d
movl %edi, %eax
divw %r11w
movzwl %r9w, %edx
movzwl %ax, %eax
decl %ecx
movl %ecx, %ebp
movl %ecx, %r13d
movslq %ecx, %rcx
shll $0x8, %ebp
andl %r10d, %r13d
movl $0x0, (%rbx,%rcx,4)
movl %ebp, %r15d
shrl $0x18, %ebp
subl %r13d, %esi
shrl $0x10, %r15d
subl %ebp, %eax
imull %esi, %esi
andl %r10d, %r15d
imull %eax, %eax
subl %r15d, %edx
imull %edx, %edx
addl %esi, %eax
movl $0x1, %esi
addl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
sqrtsd %xmm0, %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x10(%rsp), %rax
movl (%rax,%rsi,4), %r12d
movl %r12d, %ebx
andl $0xff, %ebx
je 0x54b5
movl %r12d, %edx
shrl $0x8, %edx
andl %r10d, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divw %bx
movl %r12d, %edx
shrl $0x18, %r12d
shrl $0x10, %edx
andl %r10d, %edx
movl %eax, %r8d
movl %edx, %eax
shll $0x8, %eax
movzwl %r8w, %r8d
subl %edx, %eax
xorl %edx, %edx
divw %bx
xorl %edx, %edx
movl %eax, %r9d
movl %r12d, %eax
shll $0x8, %eax
subl %r12d, %eax
divw %bx
movzwl %r9w, %edx
movzwl %ax, %eax
jmp 0x54bc
xorl %edx, %edx
xorl %r8d, %r8d
xorl %eax, %eax
subl %r15d, %edx
subl %r13d, %r8d
subl %ebp, %eax
movq 0x30(%rsp), %rbx
imull %r8d, %r8d
imull %edx, %edx
imull %eax, %eax
addl %r8d, %edx
addl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
sqrtsd %xmm1, %xmm1
cvtsd2ss %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x54f2
movaps %xmm1, %xmm0
movl %esi, (%rbx,%rcx,4)
incq %rsi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5452
movq 0x18(%rsp), %rax
movl 0x4(%rax,%r14,4), %ecx
incq %r14
testl %ecx, %ecx
jne 0x53c4
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %r12
movq 0x18(%rsp), %rdi
movzwl 0x2(%rax), %ecx
testw %cx, %cx
jle 0x5313
movzwl (%rax), %eax
xorl %r14d, %r14d
testw %ax, %ax
jle 0x5577
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x8544
movq 0x8(%rsp), %rcx
shrl $0x8, %eax
movb (%rbx,%rax,4), %al
movb %al, (%r12,%r15)
incq %r15
movswl (%rcx), %eax
cmpl %eax, %r15d
jl 0x5541
movzwl 0x2(%rcx), %ecx
addq %r15, %r12
movq 0x18(%rsp), %rdi
incl %r14d
movswl %cx, %edx
cmpl %edx, %r14d
jl 0x5539
jmp 0x5313
nop