// File: lunasorcery[P]brailify/main.cpp
// Function: processFile(char const*)
void processFile(const char* filepath)
{
	int srcWidth, srcHeight, srcComp;
	rgba8888_t* srcData = (rgba8888_t*)stbi_load(filepath, &srcWidth, &srcHeight, &srcComp, 4);
	rgba8888_t* dstData = srcData;

	if (!srcData) {
		printf("Failed to load image %s.\n", filepath);
		return;
	}

	int dstWidth  = srcWidth;
	int dstHeight = srcHeight;

	if (resizeWidth != 0) {
		dstWidth = resizeWidth * 2;
		dstHeight = (srcHeight * dstWidth) / srcWidth;
		dstData = new rgba8888_t[dstWidth * dstHeight * 3];
		stbir_resize_uint8((uint8_t const*)srcData, srcWidth, srcHeight, 0, (uint8_t*)dstData, dstWidth, dstHeight, 0, 4);
	}

	char buffer[4];
	buffer[0] = 0xe2;
	buffer[3] = 0;
	for (int y = 0; y < dstHeight; y += 4) {
		for (int x = 0; x < dstWidth; x += 2) {
			const unsigned char masks[8] = { 0x01, 0x02, 0x04, 0x40, 0x08, 0x10, 0x20, 0x80 };
			
			uint8_t bits = 0;
			for (int i = 0; i < 8; ++i) {
				if (getPixel(dstData, dstWidth, dstHeight, x + i / 4, y + i % 4) >= .0f) {
					bits |= masks[i];
				}
			}

			buffer[1] = 0xa0 | (bits >> 6);
			buffer[2] = 0x80 | (bits & 0x3f);
			fputs(buffer, stdout);
		}
		fputs("\n", stdout);
	}

	if (dstData != srcData) {
		delete[] dstData;
	}
	stbi_image_free(srcData);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x10(%rbp), %rdx
leaq -0x14(%rbp), %rcx
movl $0x4, %r8d
callq 0x1310
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3801
movq -0x8(%rbp), %rsi
leaq 0x1ce23(%rip), %rdi     # 0x20618
movb $0x0, %al
callq 0x1050
jmp 0x39cc
movl -0xc(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, 0x23134(%rip)     # 0x26948
je 0x388f
movl 0x2312c(%rip), %eax     # 0x26948
shll %eax
movl %eax, -0x2c(%rbp)
movl -0x10(%rbp), %eax
imull -0x2c(%rbp), %eax
cltd
idivl -0xc(%rbp)
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
imull $0x3, %eax, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1040
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
movl -0x30(%rbp), %eax
xorl %ecx, %ecx
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x4, 0x10(%rsp)
callq 0x2840
movb $-0x1e, -0x34(%rbp)
movb $0x0, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x39a0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x397c
movq 0x1c804(%rip), %rax     # 0x200c8
movq %rax, -0x44(%rbp)
movb $0x0, -0x45(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x8, -0x4c(%rbp)
jge 0x393d
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x3c(%rbp), %ecx
movl -0x4c(%rbp), %eax
movl $0x4, %r8d
cltd
idivl %r8d
addl %eax, %ecx
movl -0x38(%rbp), %r8d
movl -0x4c(%rbp), %eax
movl $0x4, %r9d
cltd
idivl %r9d
movl %edx, %eax
movl -0x50(%rbp), %edx
addl %eax, %r8d
callq 0x3430
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x3930
movslq -0x4c(%rbp), %rax
movzbl -0x44(%rbp,%rax), %ecx
movzbl -0x45(%rbp), %eax
orl %ecx, %eax
movb %al, -0x45(%rbp)
jmp 0x3932
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x38d3
movzbl -0x45(%rbp), %eax
sarl $0x6, %eax
orl $0xa0, %eax
movb %al, -0x33(%rbp)
movzbl -0x45(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, -0x32(%rbp)
leaq -0x34(%rbp), %rdi
movq 0x2265a(%rip), %rax     # 0x25fc0
movq (%rax), %rsi
callq 0x10d0
movl -0x3c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x3c(%rbp)
jmp 0x38b1
movq 0x2263d(%rip), %rax     # 0x25fc0
movq (%rax), %rsi
leaq 0x1ccba(%rip), %rdi     # 0x20647
callq 0x10d0
movl -0x38(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x38(%rbp)
jmp 0x389e
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x39c3
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x39c1
movq -0x58(%rbp), %rdi
callq 0x1110
jmp 0x39c3
movq -0x20(%rbp), %rdi
callq 0x12d0
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)