// File: /workspace/github_c_makefile_star2_O1/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
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0xc39d(%rip), %xmm0     # 0xe0c8
cvtsd2ss %xmm0, %xmm0
movss 0xc399(%rip), %xmm1     # 0xe0d0
callq 0x1190
movss %xmm0, 0x40(%rsp,%rbx,4)
incq %rbx
cmpq $0x100, %rbx            # imm = 0x100
jne 0x1d1c
cmpw $0x0, 0x2(%r15)
jle 0x1fc4
movl %ebp, %eax
imull %ebp, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0xc29a(%rip), %xmm1     # 0xe008
divss %xmm0, %xmm1
movss %xmm1, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl %ebp, 0x14(%rsp)
movq %r15, 0x30(%rsp)
cmpw $0x0, (%r15)
jle 0x1fab
movq 0x20(%rsp), %rax
imull %ebp, %eax
movq %rax, 0x38(%rsp)
movl $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movl %r13d, 0x2c(%rsp)
testl %ebp, %ebp
jle 0x1e8d
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %r15d, %r15d
movss %xmm0, 0x8(%rsp)
xorps %xmm1, %xmm1
movq 0x38(%rsp), %rax
leal (%r15,%rax), %r13d
movl 0x18(%rsp), %r12d
movss %xmm1, 0x1c(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x7c24
movl %eax, %ebx
movl %eax, %ecx
andl $0xff, %ecx
je 0x1e0d
movzbl %bh, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
jmp 0x1e0f
xorl %eax, %eax
movss 0x1c(%rsp), %xmm1
addss 0x40(%rsp,%rax,4), %xmm1
testl %ecx, %ecx
je 0x1e34
movl %ebx, %eax
shrl $0x10, %eax
movzbl %al, %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divl %ecx
jmp 0x1e36
xorl %eax, %eax
movss 0x8(%rsp), %xmm0
addss 0x40(%rsp,%rax,4), %xmm0
movss %xmm0, 0x8(%rsp)
testl %ecx, %ecx
je 0x1e5c
shrl $0x18, %ebx
movl %ebx, %eax
shll $0x8, %eax
subl %ebx, %eax
xorl %edx, %edx
divl %ecx
jmp 0x1e5e
xorl %eax, %eax
movss 0xc(%rsp), %xmm0
addss 0x40(%rsp,%rax,4), %xmm0
movss %xmm0, 0xc(%rsp)
incl %r12d
decl %ebp
jne 0x1ddd
incl %r15d
movl 0x14(%rsp), %ebp
cmpl %ebp, %r15d
jne 0x1dcf
jmp 0x1e9f
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0xc(%rsp)
mulss 0x10(%rsp), %xmm1
movaps %xmm1, %xmm0
movss 0xc224(%rip), %xmm1     # 0xe0d4
callq 0x1190
movss 0xc14f(%rip), %xmm1     # 0xe00c
mulss %xmm1, %xmm0
ucomiss 0xc154(%rip), %xmm0     # 0xe01c
movl $0xff, %r12d
jb 0x1eeb
ucomiss %xmm1, %xmm0
movl $0xffff, %r12d          # imm = 0xFFFF
ja 0x1eeb
cvttss2si %xmm0, %r12d
shll $0x8, %r12d
orl $0xff, %r12d
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
movss 0xc1d5(%rip), %xmm1     # 0xe0d4
callq 0x1190
movss 0xc100(%rip), %xmm1     # 0xe00c
mulss %xmm1, %xmm0
xorl %ebx, %ebx
ucomiss 0xc103(%rip), %xmm0     # 0xe01c
movl $0x0, %ebp
movq 0x30(%rsp), %r15
movl 0x2c(%rsp), %r13d
jb 0x1f3b
ucomiss %xmm1, %xmm0
movl $0xff0000, %ebp         # imm = 0xFF0000
ja 0x1f3b
cvttss2si %xmm0, %ebp
shll $0x10, %ebp
movss 0xc(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
movss 0xc185(%rip), %xmm1     # 0xe0d4
callq 0x1190
movss 0xc0b0(%rip), %xmm1     # 0xe00c
mulss %xmm1, %xmm0
ucomiss 0xc0b5(%rip), %xmm0     # 0xe01c
jb 0x1f7a
ucomiss %xmm1, %xmm0
movl $0xff000000, %ebx       # imm = 0xFF000000
ja 0x1f7a
cvttss2si %xmm0, %ebx
shll $0x18, %ebx
orl %ebp, %r12d
orl %ebx, %r12d
movq %r15, %rdi
movl %r13d, %esi
movq 0x20(%rsp), %rdx
movl %r12d, %ecx
callq 0x7b87
incl %r13d
movswl (%r15), %eax
movl 0x14(%rsp), %ebp
addl %ebp, 0x18(%rsp)
cmpl %eax, %r13d
jl 0x1dad
movq 0x20(%rsp), %rcx
incl %ecx
movswl 0x2(%r15), %eax
movq %rcx, 0x20(%rsp)
cmpl %eax, %ecx
jl 0x1d8a
leaq 0x448(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop