// File: lunasorcery[P]brailify/main.cpp
// Function: getPixel(rgba8888_t*, int, int, int, int)
float getPixel(rgba8888_t* data, int w, int h, int x, int y)
{
	if (x < 0 || y < 0 || x >= w || y >= h) {
		return -1;
	}

	rgba8888_t const pixel = data[(y*w)+x];

	uint8_t const r8 = flagInvert ? (255-pixel.r) : pixel.r;
	uint8_t const g8 = flagInvert ? (255-pixel.g) : pixel.g;
	uint8_t const b8 = flagInvert ? (255-pixel.b) : pixel.b;

	float const a = pixel.a / 255.f;

	if (useThreshold) {
		float const luma = ((0.2126 * r8) + (0.7152 * g8) + (0.0722 * b8)) * a;
		return luma - threshold;
	} else {
		float const r = powf(r8 / 255.f, kgamma);
		float const g = powf(g8 / 255.f, kgamma);
		float const b = powf(b8 / 255.f, kgamma);
		float const luma = ((0.2126 * r) + (0.7152 * g) + (0.0722 * b)) * a;
		float const ditherThreshold = (ditherTable[(y % ditherHeight) * ditherWidth + (x % ditherWidth)] + .5f) * ditherScale;
		return luma - ditherThreshold;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x3465
cmpl $0x0, -0x20(%rbp)
jl 0x3465
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3465
movl -0x20(%rbp), %eax
cmpl -0x18(%rbp), %eax
jl 0x3477
movss 0x1cbab(%rip), %xmm0    # 0x20018
movss %xmm0, -0x4(%rbp)
jmp 0x36c5
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
imull -0x14(%rbp), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
testb $0x1, 0x234af(%rip)     # 0x26944
je 0x34a7
movzbl -0x24(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movl %eax, -0x48(%rbp)
jmp 0x34ae
movzbl -0x24(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movb %al, -0x25(%rbp)
testb $0x1, 0x23489(%rip)     # 0x26944
je 0x34cd
movzbl -0x23(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
jmp 0x34d4
movzbl -0x23(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movb %al, -0x26(%rbp)
testb $0x1, 0x23463(%rip)     # 0x26944
je 0x34f3
movzbl -0x22(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movl %eax, -0x50(%rbp)
jmp 0x34fa
movzbl -0x22(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movb %al, -0x27(%rbp)
movzbl -0x21(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss 0x1caf8(%rip), %xmm1    # 0x20008
divss %xmm1, %xmm0
movss %xmm0, -0x2c(%rbp)
testb $0x1, 0x23425(%rip)     # 0x26945
je 0x3597
movzbl -0x25(%rbp), %eax
cvtsi2sd %eax, %xmm1
movzbl -0x26(%rbp), %eax
cvtsi2sd %eax, %xmm2
movsd 0x1cb6e(%rip), %xmm0    # 0x200a8
mulsd %xmm2, %xmm0
movsd 0x1cb5a(%rip), %xmm2    # 0x200a0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl -0x27(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x1cb3a(%rip), %xmm2    # 0x20098
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movss -0x2c(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x30(%rbp), %xmm0
cvtsi2ssl 0x22b77(%rip), %xmm1 # 0x26100
subss %xmm1, %xmm0
movss %xmm0, -0x4(%rbp)
jmp 0x36c5
movzbl -0x25(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss 0x1ca61(%rip), %xmm1    # 0x20008
divss %xmm1, %xmm0
movss 0x1ca61(%rip), %xmm1    # 0x20014
callq 0x1180
movss %xmm0, -0x34(%rbp)
movzbl -0x26(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss 0x1ca3b(%rip), %xmm1    # 0x20008
divss %xmm1, %xmm0
movss 0x1ca3b(%rip), %xmm1    # 0x20014
callq 0x1180
movss %xmm0, -0x38(%rbp)
movzbl -0x27(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss 0x1ca15(%rip), %xmm1    # 0x20008
divss %xmm1, %xmm0
movss 0x1ca15(%rip), %xmm1    # 0x20014
callq 0x1180
movss %xmm0, -0x3c(%rbp)
movss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm2
movsd 0x1ca85(%rip), %xmm0    # 0x200a8
mulsd %xmm2, %xmm0
movsd 0x1ca71(%rip), %xmm2    # 0x200a0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movss -0x3c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1ca50(%rip), %xmm2    # 0x20098
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movss -0x2c(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x40(%rbp)
movl -0x20(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
shll $0x3, %edx
movl %edx, -0x54(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0x54(%rbp), %eax
addl %edx, %eax
movslq %eax, %rcx
leaq 0x1cc7f(%rip), %rax     # 0x20310
movsbl (%rax,%rcx), %eax
cvtsi2ss %eax, %xmm0
movss 0x1c96f(%rip), %xmm1    # 0x20010
addss %xmm1, %xmm0
movss 0x1c95f(%rip), %xmm1    # 0x2000c
mulss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x40(%rbp), %xmm0
subss -0x44(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x4(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq