// File: /workspace/github_c_makefile_star2_O2/halcy[P]quasi_crystals_bot/Engine/Downsample.c
// Function: DownsampleBitmap
void DownsampleBitmap(Bitmap *dest,const Bitmap *src,int factor)
{
	float gammatable[256];
	for(int i=0;i<256;i++) gammatable[i]=powf((float)i/255.0,Gamma);

	for(int y=0;y<dest->height;y++)
	for(int x=0;x<dest->width;x++)
	{
		float r=0,g=0,b=0;
		for(int dy=0;dy<factor;dy++)
		for(int dx=0;dx<factor;dx++)
		{
			Pixel p=ReadPixelNoClip(src,x*factor+dx,y*factor+dy);
			r+=gammatable[ExtractRed(p)];
			g+=gammatable[ExtractGreen(p)];
			b+=gammatable[ExtractBlue(p)];
		}

		DrawPixelNoClip(dest,x,y,RGB(
			powf(r/(float)(factor*factor),1.0/Gamma)*255,
			powf(g/(float)(factor*factor),1.0/Gamma)*255,
			powf(b/(float)(factor*factor),1.0/Gamma)*255
		));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x448(%rsp), %rsp
movq %rsi, %r14
xorl %ebx, %ebx
movl %edx, 0x8(%rsp)
movq %rdi, 0x30(%rsp)
cmpq $0x100, %rbx            # imm = 0x100
je 0x1aed
cvtsi2sd %ebx, %xmm0
movss 0xa5ff(%rip), %xmm1     # 0xc0d0
mulsd 0xa5ef(%rip), %xmm0     # 0xc0c8
cvtsd2ss %xmm0, %xmm0
callq 0x1190
movss %xmm0, 0x40(%rsp,%rbx,4)
incq %rbx
jmp 0x1abc
movl 0x8(%rsp), %ecx
movss 0xa50f(%rip), %xmm1     # 0xc008
movl $0xff, %r12d
movq 0x30(%rsp), %rbx
movl %ecx, %eax
imull %ecx, %eax
testl %ecx, %ecx
cvtsi2ss %eax, %xmm0
movl $0x0, %eax
cmovgl %ecx, %eax
xorl %ecx, %ecx
movl %eax, 0x28(%rsp)
divss %xmm0, %xmm1
movss 0x40(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss %xmm1, 0xc(%rsp)
movswl 0x2(%rbx), %eax
cmpl %eax, %ecx
jge 0x1d5c
movl %ecx, %eax
xorl %ebp, %ebp
movl %ecx, 0x1c(%rsp)
movl $0x0, 0x10(%rsp)
imull 0x8(%rsp), %eax
movq %rax, 0x38(%rsp)
movswl (%rbx), %eax
cmpl %eax, %ebp
jge 0x1d51
xorps %xmm0, %xmm0
xorl %ebx, %ebx
xorps %xmm2, %xmm2
movl %ebp, 0x24(%rsp)
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x18(%rsp)
cmpl 0x28(%rsp), %ebx
je 0x1c50
movq 0x38(%rsp), %rax
movl 0x10(%rsp), %ebp
movl 0x8(%rsp), %r15d
leal (%rbx,%rax), %r13d
subl $0x1, %r15d
jb 0x1c49
movq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
movss %xmm2, 0x20(%rsp)
callq 0x60f4
movl %eax, %esi
andl $0xff, %esi
je 0x1c0b
movl %eax, %ecx
xorl %edx, %edx
movzbl %ch, %eax
imull $0xff, %eax, %eax
divw %si
movl %ecx, %edx
shrl $0x18, %ecx
shrl $0x10, %edx
imull $0xff, %ecx, %ecx
andl %r12d, %edx
imull $0xff, %edx, %edx
movzwl %ax, %eax
movss 0x40(%rsp,%rax,4), %xmm0
movl %edx, %eax
xorl %edx, %edx
divw %si
xorl %edx, %edx
movzwl %ax, %eax
movss 0x40(%rsp,%rax,4), %xmm1
movl %ecx, %eax
divw %si
movzwl %ax, %eax
jmp 0x1c16
movss 0x2c(%rsp), %xmm0
xorl %eax, %eax
movaps %xmm0, %xmm1
movss 0x14(%rsp), %xmm3
movss 0x20(%rsp), %xmm2
incl %ebp
addss %xmm1, %xmm3
movss 0x18(%rsp), %xmm1
addss %xmm0, %xmm2
addss 0x40(%rsp,%rax,4), %xmm1
movss %xmm3, 0x14(%rsp)
movss %xmm1, 0x18(%rsp)
jmp 0x1b98
incl %ebx
jmp 0x1b7c
mulss 0xc(%rsp), %xmm2
movss 0xa476(%rip), %xmm1     # 0xc0d4
movaps %xmm2, %xmm0
callq 0x1190
movss 0xa39e(%rip), %xmm1     # 0xc00c
movl $0xff, %ebp
mulss %xmm1, %xmm0
ucomiss 0xa39e(%rip), %xmm0     # 0xc01c
jb 0x1c97
ucomiss %xmm1, %xmm0
movl $0xffff, %ebp           # imm = 0xFFFF
ja 0x1c97
cvttss2si %xmm0, %ebp
shll $0x8, %ebp
orl $0xff, %ebp
movss 0x14(%rsp), %xmm0
movss 0xa42f(%rip), %xmm1     # 0xc0d4
mulss 0xc(%rsp), %xmm0
callq 0x1190
movss 0xa354(%rip), %xmm1     # 0xc00c
xorl %ebx, %ebx
movl $0x0, %r15d
mulss %xmm1, %xmm0
ucomiss 0xa351(%rip), %xmm0     # 0xc01c
jb 0x1ce1
ucomiss %xmm1, %xmm0
movl $0xff0000, %r15d        # imm = 0xFF0000
ja 0x1ce1
cvttss2si %xmm0, %r15d
shll $0x10, %r15d
movss 0x18(%rsp), %xmm0
movss 0xa3e5(%rip), %xmm1     # 0xc0d4
mulss 0xc(%rsp), %xmm0
callq 0x1190
movss 0xa30a(%rip), %xmm1     # 0xc00c
mulss %xmm1, %xmm0
ucomiss 0xa30f(%rip), %xmm0     # 0xc01c
jb 0x1d20
ucomiss %xmm1, %xmm0
movl $0xff000000, %ebx       # imm = 0xFF000000
ja 0x1d20
cvttss2si %xmm0, %ebx
shll $0x18, %ebx
orl %r15d, %ebp
movl 0x24(%rsp), %esi
movl 0x1c(%rsp), %edx
orl %ebx, %ebp
movq 0x30(%rsp), %rbx
movl %ebp, %ecx
movq %rbx, %rdi
movl %esi, %ebp
callq 0x608b
movl 0x10(%rsp), %eax
incl %ebp
addl 0x8(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x1b59
movl 0x1c(%rsp), %ecx
incl %ecx
jmp 0x1b33
leaq 0x448(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop