// File: kallaballa[P]PixelStencil/PixelStencil.cpp
// Function: main
int main(int argc, char** argv) {
	using namespace kallaballa;

	if(argc != 5) {
		std::cerr << "Usage: PixelStencil <img file> <pixel width mm> <pixel margin mm> <board margin mm>" << std::endl;
		return 1;
	}

	NamedColorTable ral;
	ral.readFromCSV("colors.txt");
	PixelPlanes pp(argv[1]);
	size_t pixelWidthMM = atoi(argv[2]);
	size_t pixelMarginMM = atoi(argv[3]);
	size_t boardMarginMM = atoi(argv[4]);

	int i = 0;
	for(auto it = pp.begin(); it != pp.end(); ++it) {
		const Color& color = (*it).first;
    const PixelList& pl = (*it).second;

    NamedColor sample;
    sample.rgb = color;
    auto nearest = ral.find_nearest(sample);
		std::cerr << std::dec << int(nearest.second) << "\t#" << std::setfill('0') << std::setw(6) << std::hex << (*nearest.first).rgb << '\t' << color << '\t' << (*nearest.first).name << std::endl;

		SVGStencil stencil((string(argv[1]) + std::to_string(i) + ".svg").c_str(),
		    *nearest.first,
		    pp.getWidth(),
				pp.getHeight(),
				pixelWidthMM,
				pixelMarginMM,
				boardMarginMM);

		for(auto it_l = pl.begin(); it_l != pl.end(); ++it_l) {
			stencil.writePixel((*it_l).first, (*it_l).second);
		}
		i++;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x555d
movq 0x4aaba(%rip), %rdi     # 0x4fff0
leaq 0x3cf21(%rip), %rsi     # 0x4245e
callq 0x4350
movq %rax, %rdi
movq 0x4aa64(%rip), %rsi     # 0x4ffb0
callq 0x43b0
movl $0x1, -0x4(%rbp)
jmp 0x5c48
leaq -0x50(%rbp), %rdi
callq 0x6030
leaq -0x71(%rbp), %rdi
movq %rdi, -0x498(%rbp)
callq 0x4630
movq -0x498(%rbp), %rdx
leaq 0x3cf2e(%rip), %rsi     # 0x424b2
leaq -0x70(%rbp), %rdi
callq 0x60b0
jmp 0x558f
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x3fb30
jmp 0x559e
leaq -0x70(%rbp), %rdi
callq 0x41f0
leaq -0x71(%rbp), %rdi
callq 0x43d0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x4870
jmp 0x55c6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4560
cltq
movq %rax, -0xf8(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x4560
cltq
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x4560
cltq
movq %rax, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x6160
movq %rax, -0x4a0(%rbp)
jmp 0x5627
movq -0x4a0(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x61c0
movq %rax, -0x4a8(%rbp)
jmp 0x564a
movq -0x4a8(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x6190
testb $0x1, %al
jne 0x5674
jmp 0x5c33
leaq -0x118(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x61f0
movq -0x4b8(%rbp), %rdi
movq %rax, -0x128(%rbp)
callq 0x61f0
addq $0x8, %rax
movq %rax, -0x130(%rbp)
leaq -0x158(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x6210
movq -0x4b0(%rbp), %rdx
movq -0x128(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x158(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x6230
jmp 0x56e0
movq 0x4a909(%rip), %rdi     # 0x4fff0
leaq 0x912(%rip), %rsi       # 0x6000
callq 0x4100
movq %rax, -0x4c0(%rbp)
jmp 0x56fc
movq -0x4c0(%rbp), %rdi
cvttsd2si -0x160(%rbp), %esi
callq 0x4610
movq %rax, -0x4c8(%rbp)
jmp 0x5719
movq -0x4c8(%rbp), %rdi
leaq 0x3cd96(%rip), %rsi     # 0x424bd
callq 0x4350
movq %rax, -0x4d0(%rbp)
jmp 0x5735
movl $0x30, %edi
callq 0x5f90
movb %al, -0x4d1(%rbp)
jmp 0x5747
movq -0x4d0(%rbp), %rdi
movb -0x4d1(%rbp), %al
movb %al, -0x169(%rbp)
movzbl -0x169(%rbp), %esi
callq 0x4580
movq %rax, -0x4e0(%rbp)
jmp 0x576f
movl $0x6, %edi
callq 0x5fb0
movl %eax, -0x4e4(%rbp)
jmp 0x5781
movq -0x4e0(%rbp), %rdi
movl -0x4e4(%rbp), %eax
movl %eax, -0x170(%rbp)
movl -0x170(%rbp), %esi
callq 0x4420
movq %rax, -0x4f0(%rbp)
jmp 0x57a8
movq -0x4f0(%rbp), %rdi
leaq 0x81a(%rip), %rsi       # 0x5fd0
callq 0x4100
movq %rax, -0x4f8(%rbp)
jmp 0x57c4
leaq -0x168(%rbp), %rdi
callq 0x6380
movq %rax, -0x500(%rbp)
jmp 0x57d9
movq -0x4f8(%rbp), %rdi
movq -0x500(%rbp), %rax
movl (%rax), %esi
callq 0x44a0
movq %rax, -0x508(%rbp)
jmp 0x57f7
movq -0x508(%rbp), %rdi
movl $0x9, %esi
callq 0x4440
movq %rax, -0x510(%rbp)
jmp 0x5811
movq -0x510(%rbp), %rdi
movq -0x128(%rbp), %rax
movl (%rax), %esi
callq 0x44a0
movq %rax, -0x518(%rbp)
jmp 0x582f
movq -0x518(%rbp), %rdi
movl $0x9, %esi
callq 0x4440
movq %rax, -0x520(%rbp)
jmp 0x5849
leaq -0x168(%rbp), %rdi
callq 0x6380
movq %rax, -0x528(%rbp)
jmp 0x585e
movq -0x520(%rbp), %rdi
movq -0x528(%rbp), %rsi
addq $0x8, %rsi
callq 0x4310
movq %rax, -0x530(%rbp)
jmp 0x587e
movq -0x530(%rbp), %rdi
movq 0x4a724(%rip), %rsi     # 0x4ffb0
callq 0x43b0
jmp 0x5893
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x540(%rbp)
leaq -0x431(%rbp), %rdi
movq %rdi, -0x538(%rbp)
callq 0x4630
movq -0x540(%rbp), %rsi
movq -0x538(%rbp), %rdx
leaq -0x430(%rbp), %rdi
callq 0x60b0
jmp 0x58d1
movl -0x10c(%rbp), %esi
leaq -0x458(%rbp), %rdi
callq 0x64c0
jmp 0x58e5
leaq -0x410(%rbp), %rdi
leaq -0x430(%rbp), %rsi
leaq -0x458(%rbp), %rdx
callq 0x63f0
jmp 0x5901
leaq 0x3cbb8(%rip), %rdx     # 0x424c0
leaq -0x3f0(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x63a0
jmp 0x591d
leaq -0x3f0(%rbp), %rdi
callq 0x4140
movq %rax, -0x550(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x6380
movq %rax, -0x548(%rbp)
jmp 0x5945
movq -0x548(%rbp), %rsi
leaq -0x480(%rbp), %rdi
callq 0x5f20
jmp 0x595a
leaq -0xf0(%rbp), %rdi
callq 0x65e0
movq %rax, -0x558(%rbp)
jmp 0x596f
leaq -0xf0(%rbp), %rdi
callq 0x6600
movq %rax, -0x560(%rbp)
jmp 0x5984
movq -0x560(%rbp), %r8
movq -0x558(%rbp), %rcx
movq -0x550(%rbp), %rsi
movq -0xf8(%rbp), %r9
movq -0x100(%rbp), %rdx
movq -0x108(%rbp), %rdi
movq %rsp, %rax
movq %rdi, 0x8(%rax)
movq %rdx, (%rax)
leaq -0x3d0(%rbp), %rdi
leaq -0x480(%rbp), %rdx
callq 0x4b20
jmp 0x59cd
leaq -0x480(%rbp), %rdi
callq 0x5f60
leaq -0x3f0(%rbp), %rdi
callq 0x41f0
leaq -0x410(%rbp), %rdi
callq 0x41f0
leaq -0x458(%rbp), %rdi
callq 0x41f0
leaq -0x430(%rbp), %rdi
callq 0x41f0
leaq -0x431(%rbp), %rdi
callq 0x43d0
movq -0x130(%rbp), %rdi
callq 0x6620
movq %rax, -0x488(%rbp)
movq -0x130(%rbp), %rdi
callq 0x6690
movq %rax, -0x490(%rbp)
leaq -0x488(%rbp), %rdi
leaq -0x490(%rbp), %rsi
callq 0x6650
testb $0x1, %al
jne 0x5a57
jmp 0x5bed
leaq -0x488(%rbp), %rdi
movq %rdi, -0x570(%rbp)
callq 0x66c0
movq -0x570(%rbp), %rdi
movslq (%rax), %rax
movq %rax, -0x568(%rbp)
callq 0x66c0
movq -0x568(%rbp), %rsi
movslq 0x4(%rax), %rdx
leaq -0x3d0(%rbp), %rdi
callq 0x5300
jmp 0x5a99
jmp 0x5a9b
leaq -0x488(%rbp), %rdi
callq 0x66e0
jmp 0x5a28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5ad5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x41f0
leaq -0x71(%rbp), %rdi
callq 0x43d0
jmp 0x5c60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5c60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5c54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5c25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5bc2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5bb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5baa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5b9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5b92
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x5f60
leaq -0x3f0(%rbp), %rdi
callq 0x41f0
leaq -0x410(%rbp), %rdi
callq 0x41f0
leaq -0x458(%rbp), %rdi
callq 0x41f0
leaq -0x430(%rbp), %rdi
callq 0x41f0
leaq -0x431(%rbp), %rdi
callq 0x43d0
jmp 0x5c25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x3d0(%rbp), %rdi
callq 0x5230
jmp 0x5c25
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
leaq -0x3d0(%rbp), %rdi
callq 0x5230
leaq -0x158(%rbp), %rdi
callq 0x5f60
leaq -0x118(%rbp), %rdi
callq 0x6700
jmp 0x5635
leaq -0x158(%rbp), %rdi
callq 0x5f60
jmp 0x5c54
leaq -0xf0(%rbp), %rdi
callq 0x4ab0
leaq -0x50(%rbp), %rdi
callq 0x6730
movl -0x4(%rbp), %eax
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
leaq -0xf0(%rbp), %rdi
callq 0x4ab0
leaq -0x50(%rbp), %rdi
callq 0x6730
movq -0x80(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)
nopl (%rax)