// File: /workspace/github_c_makefile_star2_O1/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 %rcx, %r14
movq %rdx, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x8(%rsp)
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
callq 0x1110
movq %rax, %r12
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
callq 0x1110
movq %rax, %rbp
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
callq 0x1110
movq %rax, 0x48(%rsp)
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
callq 0x1110
movq %rax, 0x20(%rsp)
movl $0x4000000, %edi        # imm = 0x4000000
movl $0x1, %esi
callq 0x1110
cmpw $0x0, 0x2(%r14)
movq %rax, 0x28(%rsp)
movl $0x0, %ebx
jle 0x4baf
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpw $0x0, (%r14)
jle 0x4ba2
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x7c24
movl %eax, %ecx
shrl $0x8, %ecx
movl (%rbp,%rcx,4), %edx
testl %edx, %edx
jne 0x4b90
movslq %ebx, %rbx
movq 0x18(%rsp), %rsi
movl %eax, (%rsi,%rbx,4)
incl %ebx
incl %edx
movl %edx, (%rbp,%rcx,4)
incl %r15d
movswl (%r14), %eax
cmpl %eax, %r15d
jl 0x4b68
incl %r13d
movswl 0x2(%r14), %eax
cmpl %eax, %r13d
jl 0x4b5e
cmpl $0x97, %ebx
movl %ebx, %ecx
movl $0x0, %eax
cmovgel %eax, %ecx
movq %rcx, 0x10(%rsp)
movq 0x8(%rsp), %rdi
cmpw $0x0, 0x2(%rdi)
jle 0x4c8e
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpw $0x0, (%rdi)
jle 0x4c7c
xorl %r13d, %r13d
movl %r13d, %esi
movl %r14d, %edx
callq 0x7c24
movl %eax, %ecx
shrl $0x8, %ecx
leal (%ecx), %edx
leal (,%rdx,4), %edx
movq 0x48(%rsp), %rsi
incl (%rsi,%rdx)
movl (%r12,%rdx), %edx
testl %edx, %edx
je 0x4c18
movq 0x8(%rsp), %rdi
jmp 0x4c67
leal 0x1(%rcx), %esi
cmpl $0x97, %ebx
movslq %r15d, %rdi
movq 0x28(%rsp), %r8
movl %eax, (%r8,%rdi,4)
movq 0x20(%rsp), %r8
movl %esi, (%r8,%rdi,4)
jl 0x4c5f
cmpl $0x0, (%rbp,%rcx,4)
movq 0x8(%rsp), %rdi
je 0x4c64
movq 0x10(%rsp), %r8
movslq %r8d, %r8
movq 0x18(%rsp), %rsi
movl %eax, (%rsi,%r8,4)
incl %r8d
movq %r8, 0x10(%rsp)
jmp 0x4c64
movq 0x8(%rsp), %rdi
incl %r15d
incl %edx
movl %edx, (%r12,%rcx,4)
incl %r13d
movswl (%rdi), %eax
cmpl %eax, %r13d
jl 0x4be7
incl %r14d
movswl 0x2(%rdi), %eax
cmpl %eax, %r14d
jl 0x4bda
jmp 0x4c91
xorl %r15d, %r15d
movl %r15d, %r14d
movslq %r15d, %r13
leaq -0x296(%rip), %rcx      # 0x4a08
movl $0x4, %edx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1050
cmpl $0x19, %r13d
movq %r13, 0x30(%rsp)
jl 0x4d2c
movl %r14d, %eax
imulq $0x51eb851f, %rax, %rdx # imm = 0x51EB851F
shrq $0x23, %rdx
leaq -0x4(%rbx,%r13,4), %rsi
xorl %edi, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xff, %ecx
movq %rbx, %r8
xorl %r9d, %r9d
movq 0x38(%rsp), %r13
movq 0x48(%rsp), %r14
movl (%r8), %r10d
movl %r10d, %r11d
shrl $0x8, %r11d
movl (%r14,%r11,4), %r11d
cmpl %r9d, %r11d
movl (%rsi), %ebx
cmoval %r11d, %r9d
movl %ebx, %r11d
cmoval %r10d, %ecx
shrl $0x8, %r11d
movl (%r14,%r11,4), %r10d
cmpl %edi, %r10d
cmoval %r10d, %edi
cmoval %ebx, %eax
leaq 0x4(%r8), %r8
addq $-0x4, %rsi
decq %rdx
jne 0x4ced
jmp 0x4d3b
movl $0xff, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x38(%rsp), %r13
cmpl $0x100, %r15d           # imm = 0x100
jg 0x4ddb
movq 0x20(%rsp), %rdi
movl (%rdi), %eax
testl %eax, %eax
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r8
je 0x4d7f
xorl %ecx, %ecx
decl %eax
movslq %eax, %rdx
shll $0x8, %eax
orl $0xff, %eax
movl %eax, (%r8,%rcx,4)
movl %ecx, (%r12,%rdx,4)
movl 0x4(%rdi,%rcx,4), %eax
incq %rcx
testl %eax, %eax
jne 0x4d5f
cmpw $0x0, 0x2(%rsi)
jle 0x5223
xorl %r14d, %r14d
cmpw $0x0, (%rsi)
jle 0x4dca
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x7c24
movq 0x8(%rsp), %rsi
shrl $0x8, %eax
movb (%r12,%rax,4), %al
movb %al, (%r13,%r15)
movswl (%rsi), %eax
incq %r15
cmpl %eax, %r15d
jl 0x4d96
addq %r15, %r13
movq 0x20(%rsp), %rdi
incl %r14d
movswl 0x2(%rsi), %eax
cmpl %eax, %r14d
jl 0x4d8d
jmp 0x5223
movq 0x10(%rsp), %r14
movslq %r14d, %rdx
cmpl $0xfe, %edx
movq 0x18(%rsp), %rbx
movl %ecx, (%rbx,%rdx,4)
jg 0x4df7
movl %eax, 0x4(%rbx,%rdx,4)
movq %r12, 0x40(%rsp)
movq 0x30(%rsp), %rdi
shlq $0x2, %rdi
movl $0x1, %esi
callq 0x1110
cmpl $0xfd, %r14d
movq %rbp, 0x50(%rsp)
movq 0x28(%rsp), %r8
jg 0x500b
addl $0x2, %r14d
cmpl $0x2, %r15d
movl $0x1, %r10d
cmovgel %r15d, %r10d
movslq %r14d, %rdi
movss 0x96cd(%rip), %xmm0     # 0xe510
xorl %r9d, %r9d
movq %rax, 0x60(%rsp)
movq %r10, 0x58(%rsp)
movl %edi, %r11d
xorl %r14d, %r14d
movq %r11, 0x68(%rsp)
testq %rdi, %rdi
movl (%r8,%r14,4), %esi
movq %r11, %rcx
movq %rbx, %rdx
jle 0x4e7b
cmpl %esi, (%rdx)
je 0x4ff0
addq $0x4, %rdx
decq %rcx
jne 0x4e6a
movslq %r9d, %rbp
cmpq %rbp, %rdi
jle 0x4fd4
movl %esi, %edx
shrl $0x8, %edx
movl %esi, %r12d
movl $0xff, %r9d
andl %r9d, %edx
movl %edx, %r8d
movl %esi, %ecx
shll $0x8, %r8d
shrl $0x10, %ecx
subl %edx, %r8d
movl %r8d, 0x10(%rsp)
andl %r9d, %ecx
movl %ecx, %r8d
movl %esi, %edx
shll $0x8, %r8d
shrl $0x18, %edx
subl %ecx, %r8d
movl %r8d, 0x30(%rsp)
movl %edx, %r8d
andl %r9d, %r12d
shll $0x8, %r8d
subl %edx, %r8d
movq %r14, 0x70(%rsp)
movss (%rax,%r14,4), %xmm1
movl (%rbx,%rbp,4), %ebx
xorl %r10d, %r10d
movl %ebx, %ecx
andl $0xff, %ecx
movl $0x0, %r9d
je 0x4f02
movzbl %bh, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r9d
testl %r12d, %r12d
je 0x4f13
movl 0x10(%rsp), %eax
xorl %edx, %edx
divl %r12d
movl %eax, %r10d
xorl %r14d, %r14d
testl %ecx, %ecx
movl $0x0, %r11d
je 0x4f36
movl %ebx, %eax
shrl $0x10, %eax
movzbl %al, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r11d
testl %r12d, %r12d
je 0x4f47
movl 0x30(%rsp), %eax
xorl %edx, %edx
divl %r12d
movl %eax, %r14d
xorl %r13d, %r13d
testl %ecx, %ecx
movl $0x0, %r15d
je 0x4f65
shrl $0x18, %ebx
movl %ebx, %eax
shll $0x8, %eax
subl %ebx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
testl %r12d, %r12d
je 0x4f75
movl %r8d, %eax
xorl %edx, %edx
divl %r12d
movl %eax, %r13d
subl %r13d, %r15d
subl %r14d, %r11d
subl %r10d, %r9d
imull %r9d, %r9d
imull %r11d, %r11d
imull %r15d, %r15d
addl %r9d, %r11d
addl %r11d, %r15d
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
sqrtsd %xmm2, %xmm2
cvtsd2ss %xmm2, %xmm2
addss %xmm2, %xmm1
incq %rbp
cmpq %rdi, %rbp
movq 0x18(%rsp), %rbx
jl 0x4edb
movq 0x60(%rsp), %rax
movq 0x70(%rsp), %r14
movss %xmm1, (%rax,%r14,4)
movq 0x28(%rsp), %r8
movq 0x58(%rsp), %r10
movq 0x68(%rsp), %r11
movss (%rax,%r14,4), %xmm1
ucomiss %xmm1, %xmm0
movl %edi, %r9d
movq 0x50(%rsp), %rbp
jae 0x4ff0
movl %esi, (%rbx,%rdi,4)
movaps %xmm1, %xmm0
movl %edi, %r9d
incq %r14
cmpq %r10, %r14
jne 0x4e5b
incq %rdi
cmpl $0x100, %edi            # imm = 0x100
jne 0x4e50
movq %rax, %rdi
callq 0x1040
movq 0x20(%rsp), %rdi
movl (%rdi), %r12d
testl %r12d, %r12d
je 0x51c2
movl (%rbx), %eax
movl $0xff, %ecx
movl %eax, %edx
movl %eax, %r11d
shrl $0x8, %edx
andl %ecx, %r11d
andl %ecx, %edx
movl %edx, %esi
movl %eax, %r8d
shll $0x8, %esi
shrl $0x10, %r8d
subl %edx, %esi
movl %esi, 0x10(%rsp)
andl %ecx, %r8d
movl %r8d, %ecx
shrl $0x18, %eax
shll $0x8, %ecx
movl %eax, %esi
subl %r8d, %ecx
movl %ecx, 0x30(%rsp)
shll $0x8, %esi
subl %eax, %esi
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
testl %r11d, %r11d
movl $0x0, %r8d
je 0x5083
movl 0x10(%rsp), %eax
xorl %edx, %edx
divl %r11d
movl %eax, %r8d
xorl %r9d, %r9d
testl %r11d, %r11d
movl $0x0, %r10d
je 0x509d
movl 0x30(%rsp), %eax
xorl %edx, %edx
divl %r11d
movl %eax, %r10d
testl %r11d, %r11d
je 0x50ac
movl %esi, %eax
xorl %edx, %edx
divl %r11d
movl %eax, %r9d
decl %r12d
movl %r12d, %ebp
shll $0x8, %ebp
movl %ebp, %eax
shrl $0x10, %eax
movzbl %r12b, %r14d
movzbl %al, %r15d
shrl $0x18, %ebp
subl %r15d, %r10d
subl %r14d, %r8d
subl %ebp, %r9d
imull %r8d, %r8d
imull %r9d, %r9d
imull %r10d, %r10d
addl %r8d, %r9d
addl %r10d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
sqrtsd %xmm0, %xmm0
cvtsd2ss %xmm0, %xmm0
movslq %r12d, %r10
movq 0x40(%rsp), %rax
movl $0x0, (%rax,%r10,4)
movl $0x1, %r12d
movl (%rbx,%r12,4), %ebx
xorl %r8d, %r8d
movl %ebx, %ecx
andl $0xff, %ecx
movl $0x0, %r9d
je 0x512e
movzbl %bh, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r9d
testl %ecx, %ecx
je 0x5148
movl %ebx, %eax
shrl $0x10, %eax
movzbl %al, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r8d
testl %ecx, %ecx
je 0x515c
shrl $0x18, %ebx
movl %ebx, %eax
shll $0x8, %eax
subl %ebx, %eax
xorl %edx, %edx
divl %ecx
jmp 0x515e
xorl %eax, %eax
movq 0x18(%rsp), %rbx
subl %r15d, %r8d
subl %r14d, %r9d
subl %ebp, %eax
imull %r9d, %r9d
imull %r8d, %r8d
imull %eax, %eax
addl %r9d, %r8d
addl %r8d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
sqrtsd %xmm1, %xmm1
cvtsd2ss %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x519c
movq 0x40(%rsp), %rax
movl %r12d, (%rax,%r10,4)
movaps %xmm1, %xmm0
incq %r12
cmpq $0x100, %r12            # imm = 0x100
jne 0x5106
movl 0x4(%rdi,%r13,4), %r12d
incq %r13
testl %r12d, %r12d
movq 0x50(%rsp), %rbp
jne 0x506c
movq 0x8(%rsp), %rcx
cmpw $0x0, 0x2(%rcx)
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %rbx
jle 0x5223
xorl %r14d, %r14d
cmpw $0x0, (%rcx)
jle 0x5217
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x7c24
movq 0x8(%rsp), %rcx
shrl $0x8, %eax
movb (%r12,%rax,4), %al
movb %al, (%rbx,%r15)
movswl (%rcx), %eax
incq %r15
cmpl %eax, %r15d
jl 0x51e4
addq %r15, %rbx
movq 0x20(%rsp), %rdi
incl %r14d
movswl 0x2(%rcx), %eax
cmpl %eax, %r14d
jl 0x51db
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1040
movq 0x48(%rsp), %rdi
callq 0x1040
movq %rbp, %rdi
callq 0x1040
movq %r12, %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
nopl (%rax)