// File: Alcaro[P]bridges/font.cpp
// Function: font::init_from_image(image const&)
void font::init_from_image(const image& img)
{
	memset(characters, 0, sizeof(characters));
	memset(width, 0, sizeof(uint8_t)*32);
	
	int cw = img.width/16;
	int ch = img.height/6;
	
	for (int cy=0;cy<6;cy++)
	for (int cx=0;cx<16;cx++)
	{
		uint8_t charid = 32 + cy*16 + cx;
		
		uint8_t maxbits = 1;
		for (int y=0;y<ch;y++)
		{
			int ty = cy*ch + y;
			uint32_t* pixels = img.pixels32 + img.stride/sizeof(uint32_t)*ty;
			
			uint8_t bits = 0;
			for (int x=0;x<cw;x++)
			{
				int tx = cx*cw + x;
				bits |= !(pixels[tx]&1) << x;
			}
			characters[charid][y] = bits;
			maxbits |= bits;
		}
		
		width[charid] = ilog2(maxbits)+2; // +1 because maxbits=1 has width 1 but log2(1)=0, +1 because letter spacing
	}
	
	height = ch;
	scale = 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x50(%rbp)
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x4170
movq -0x50(%rbp), %rdi
addq $0x400, %rdi            # imm = 0x400
xorl %esi, %esi
movl $0x20, %edx
callq 0x4170
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x6, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x6, -0x1c(%rbp)
jge 0x4729f
movl $0x0, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jge 0x4728f
movl -0x1c(%rbp), %eax
shll $0x4, %eax
addl $0x20, %eax
addl -0x20(%rbp), %eax
movb %al, -0x21(%rbp)
movb $0x1, -0x22(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x4725f
movl -0x1c(%rbp), %eax
imull -0x18(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
shrq $0x2, %rcx
movslq -0x2c(%rbp), %rdx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4722b
movl -0x20(%rbp), %eax
imull -0x14(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl -0x40(%rbp), %ecx
shll %cl, %eax
movl %eax, %ecx
movzbl -0x39(%rbp), %eax
orl %ecx, %eax
movb %al, -0x39(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x471e0
movq -0x50(%rbp), %rax
movb -0x39(%rbp), %dl
movzbl -0x21(%rbp), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movb %dl, (%rax,%rcx)
movzbl -0x39(%rbp), %ecx
movzbl -0x22(%rbp), %eax
orl %ecx, %eax
movb %al, -0x22(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x47196
movzbl -0x22(%rbp), %edi
callq 0x472c0
movb %al, %cl
movq -0x50(%rbp), %rax
movzbl %cl, %ecx
addl $0x2, %ecx
movb %cl, %dl
movzbl -0x21(%rbp), %ecx
movb %dl, 0x400(%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x47172
jmp 0x47291
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x47161
movq -0x50(%rbp), %rax
movl -0x18(%rbp), %ecx
movb %cl, 0x480(%rax)
movb $0x1, 0x488(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)