// File: /workspace/github_c_makefile_star2_O2/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 -0x58(%rsp), %rsp
movq %rcx, %r14
movq %rdx, %rbp
movq %rsi, 0x38(%rsp)
movq %rdi, 0x8(%rsp)
pushq $0x1
popq %r12
movl $0x4000000, %edi        # imm = 0x4000000
movq %r12, %rsi
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movq %r12, %rsi
movq %rax, %r15
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movq %r12, %rsi
movq %rax, 0x28(%rsp)
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movq %r12, %rsi
movq %rax, 0x30(%rsp)
callq 0x1110
movl $0x4000000, %edi        # imm = 0x4000000
movq %r12, %rsi
movq %rax, 0x20(%rsp)
callq 0x1110
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %rax, 0x18(%rsp)
movswl 0x2(%r14), %eax
cmpl %eax, %r13d
jge 0x458f
xorl %r12d, %r12d
movswl (%r14), %eax
cmpl %eax, %r12d
jge 0x458a
movq %r14, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x60f4
movl %eax, %ecx
movq 0x28(%rsp), %rdx
shrl $0x8, %ecx
movl (%rdx,%rcx,4), %edx
testl %edx, %edx
jne 0x457b
movslq %ebx, %rbx
movl %eax, (%rbp,%rbx,4)
incl %ebx
incl %edx
movq 0x28(%rsp), %rax
incl %r12d
movl %edx, (%rax,%rcx,4)
jmp 0x454a
incl %r13d
jmp 0x453d
xorl %r14d, %r14d
movl %ebx, %eax
movq 0x8(%rsp), %rdi
cmpl $0x97, %ebx
cmovgel %r14d, %eax
xorl %r12d, %r12d
movq %rax, 0x10(%rsp)
movswl 0x2(%rdi), %eax
cmpl %eax, %r14d
jge 0x465b
xorl %r13d, %r13d
movswl (%rdi), %eax
cmpl %eax, %r13d
jge 0x4653
movl %r13d, %esi
movl %r14d, %edx
callq 0x60f4
movl %eax, %ecx
movq 0x30(%rsp), %rsi
shrl $0x8, %ecx
leal (%ecx), %edx
leal (,%rdx,4), %edx
incl (%rsi,%rdx)
movl (%r15,%rdx), %edx
testl %edx, %edx
je 0x45f8
movq 0x8(%rsp), %rdi
jmp 0x4645
cmpl $0x97, %ebx
leal 0x1(%rcx), %esi
movslq %r12d, %rdi
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movl %eax, (%r8,%rdi,4)
movl %esi, (%r9,%rdi,4)
jl 0x463d
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdi
cmpl $0x0, (%rsi,%rcx,4)
je 0x4642
movq 0x10(%rsp), %rsi
movslq %esi, %rsi
movl %eax, (%rbp,%rsi,4)
incl %esi
movq %rsi, 0x10(%rsp)
jmp 0x4642
movq 0x8(%rsp), %rdi
incl %r12d
incl %edx
incl %r13d
movl %edx, (%r15,%rcx,4)
jmp 0x45bb
incl %r14d
jmp 0x45ab
movslq %r12d, %r14
leaq -0x271(%rip), %rcx      # 0x43f4
pushq $0x4
popq %rdx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x1050
pushq $0x19
popq %rcx
movl %r14d, %eax
xorl %esi, %esi
leal -0x1(%r14), %edi
cltd
idivl %ecx
testl %eax, %eax
movl $0xff, %edx
cmovlel %esi, %eax
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x30(%rsp), %r13
cmpq %rsi, %rax
je 0x46e4
movq 0x18(%rsp), %rbx
movslq %edi, %rdi
movl (%rbx,%rsi,4), %r10d
movl (%rbx,%rdi,4), %ebx
movl %r10d, %r11d
shrl $0x8, %r11d
movl (%r13,%r11,4), %r11d
cmpl %r8d, %r11d
cmoval %r11d, %r8d
movl %ebx, %r11d
cmoval %r10d, %edx
shrl $0x8, %r11d
movl (%r13,%r11,4), %r10d
cmpl %r9d, %r10d
cmoval %r10d, %r9d
cmoval %ebx, %ecx
incq %rsi
decl %edi
jmp 0x469c
cmpl $0x100, %r12d           # imm = 0x100
jg 0x4724
xorl %eax, %eax
movq 0x38(%rsp), %rbx
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
testl %ecx, %ecx
je 0x4811
decl %ecx
movslq %ecx, %rdx
shll $0x8, %ecx
orl $0xff, %ecx
movl %ecx, (%rbp,%rax,4)
movl %eax, (%r15,%rdx,4)
incq %rax
jmp 0x46f9
movq 0x10(%rsp), %rbx
movslq %ebx, %rax
cmpl $0xfe, %eax
movl %edx, (%rbp,%rax,4)
jg 0x473b
movl %ecx, 0x4(%rbp,%rax,4)
addl $0x2, %ebx
shlq $0x2, %r14
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x1110
movq %rax, %r14
movl %r12d, %esi
movss 0x7db5(%rip), %xmm0     # 0xc510
movslq %ebx, %rbx
xorl %eax, %eax
movq %r15, 0x40(%rsp)
movss %xmm0, 0x10(%rsp)
movq %rsi, 0x48(%rsp)
testl %ebx, %ebx
movl $0x0, %edi
cmovgl %ebx, %edi
cmpq $0xff, %rbx
jg 0x4859
xorl %r12d, %r12d
movq %rdi, 0x50(%rsp)
cmpq %rsi, %r12
je 0x4809
movq 0x18(%rsp), %rcx
movq %rdi, %rdx
movl (%rcx,%r12,4), %r13d
movq %rbp, %rcx
subq $0x1, %rdx
jb 0x47b4
cmpl %r13d, (%rcx)
leaq 0x4(%rcx), %rcx
jne 0x47a3
jmp 0x4804
movslq %eax, %r15
cmpq %rbx, %r15
jge 0x47da
movl (%rbp,%r15,4), %edi
movl %r13d, %esi
callq 0x496d
addss (%r14,%r12,4), %xmm0
incq %r15
movss %xmm0, (%r14,%r12,4)
jmp 0x47b7
movss (%r14,%r12,4), %xmm0
movss 0x10(%rsp), %xmm1
movl %ebx, %eax
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdi
ucomiss %xmm0, %xmm1
jae 0x4804
movl %ebx, %eax
movl %r13d, (%rbp,%rbx,4)
movss %xmm0, 0x10(%rsp)
incq %r12
jmp 0x478f
incq %rbx
jmp 0x4770
xorl %r14d, %r14d
movswl 0x2(%rsi), %eax
cmpl %eax, %r14d
jge 0x4928
xorl %r12d, %r12d
movswl (%rsi), %eax
cmpl %eax, %r12d
jge 0x4851
movq 0x8(%rsp), %rdi
movl %r12d, %esi
movl %r14d, %edx
callq 0x60f4
movq 0x8(%rsp), %rsi
shrl $0x8, %eax
movb (%r15,%rax,4), %al
movb %al, (%rbx,%r12)
incq %r12
jmp 0x4824
incl %r14d
addq %r12, %rbx
jmp 0x4814
movq %r14, %rdi
callq 0x1040
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x48d5
decl %r15d
movl (%rbp), %edi
movl %r15d, %r14d
shll $0x8, %r14d
orl $0xff, %r14d
movl %r14d, %esi
callq 0x496d
movslq %r15d, %r12
movq 0x40(%rsp), %r15
movss %xmm0, 0x10(%rsp)
andl $0x0, (%r15,%r12,4)
pushq $0x1
popq %r13
cmpq $0x100, %r13            # imm = 0x100
je 0x48d0
movl (%rbp,%r13,4), %edi
movl %r14d, %esi
callq 0x496d
ucomiss 0x10(%rsp), %xmm0
jae 0x48cb
movl %r13d, (%r15,%r12,4)
movss %xmm0, 0x10(%rsp)
incq %r13
jmp 0x48a4
incq %rbx
jmp 0x4863
xorl %r14d, %r14d
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %rbx
movq 0x8(%rsp), %rcx
movswl 0x2(%rcx), %eax
cmpl %eax, %r14d
jge 0x4928
xorl %r12d, %r12d
movswl (%rcx), %eax
cmpl %eax, %r12d
jge 0x4920
movq 0x8(%rsp), %rdi
movl %r12d, %esi
movl %r14d, %edx
callq 0x60f4
movq 0x8(%rsp), %rcx
shrl $0x8, %eax
movb (%r15,%rax,4), %al
movb %al, (%rbx,%r12)
incq %r12
jmp 0x48f3
incl %r14d
addq %r12, %rbx
jmp 0x48e7
movq 0x20(%rsp), %rdi
callq 0x1040
movq 0x18(%rsp), %rdi
callq 0x1040
movq 0x30(%rsp), %rdi
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
movl $0x100, %eax            # imm = 0x100
leaq 0x58(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq