// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
cmpl $0x5, %edi
jne 0x4eaf
movq %rsi, %r15
leaq 0x120(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
leaq 0x27730(%rip), %rcx     # 0x2c038
movq %rcx, 0x20(%rax)
movq %rax, 0x8(%rax)
movq %rax, 0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x1c8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x28ac3(%rip), %rsi     # 0x2d3ee
leaq 0x28ac6(%rip), %rdx     # 0x2d3f8
leaq 0x1b8(%rsp), %rdi
callq 0x2b888
leaq 0x110(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x2c22a
movq 0x1b8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4971
movq 0x1c8(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x8(%r15), %rsi
leaq 0x150(%rsp), %rdi
callq 0x3b1c
movq 0x10(%r15), %rdi
callq 0x3430
movl %eax, %ebx
movq 0x18(%r15), %rdi
callq 0x3430
movl %eax, %ebp
movq %r15, 0x108(%rsp)
movq 0x20(%r15), %rdi
callq 0x3430
leaq 0x190(%rsp), %rcx
movq 0x10(%rcx), %r14
cmpq %rcx, %r14
je 0x4e91
movslq %ebx, %rcx
movq %rcx, 0x100(%rsp)
movslq %ebp, %rcx
movq %rcx, 0xf8(%rsp)
cltq
movq %rax, 0xf0(%rsp)
xorl %r13d, %r13d
movq 0x35608(%rip), %r12     # 0x39ff0
leaq 0x1b8(%rsp), %rbp
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq $0x0, 0xb0(%rsp)
movb $0x0, 0xb8(%rsp)
movl 0x20(%r14), %eax
movl %eax, 0xa0(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x110(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x50fe
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r12,%rax)
cvttsd2si 0x98(%rsp), %esi
movq %r12, %rdi
callq 0x34c0
movq %rax, %r15
movl $0x2, %edx
movq %rax, %rdi
leaq 0x28982(%rip), %rsi     # 0x2d3f9
callq 0x3330
movq (%r15), %rax
movq -0x18(%rax), %rax
movq %r15, %rbx
addq %rax, %rbx
cmpb $0x0, 0xe1(%r15,%rax)
jne 0x4aae
movq %rbx, %rdi
movl $0x20, %esi
callq 0x3290
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x30, 0xe0(%rbx)
movq (%r15), %rax
movq -0x18(%rax), %rcx
movq $0x6, 0x10(%r15,%rcx)
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r15,%rax)
movq 0x90(%rsp), %rax
movl 0x18(%rax), %esi
movq %r15, %rdi
callq 0x31a0
movb $0x9, 0x1b8(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3330
movl 0x20(%r14), %esi
movq %rax, %rdi
callq 0x31a0
movb $0x9, 0x1b8(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3330
movq 0x90(%rsp), %rcx
movq 0x20(%rcx), %rsi
movq 0x28(%rcx), %rdx
movq %rax, %rdi
callq 0x3330
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r15, %rdi
callq 0x3050
movq %rax, %rdi
callq 0x31b0
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x50c0
movl $0x1, %r15d
cmpl $0xa, %r13d
jb 0x4bd8
movl $0x4, %r15d
movl %r13d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x4bcf
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x4bd5
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x4bd8
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r15d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x4b9f
addl $-0x3, %r15d
jmp 0x4bd8
addl $-0x2, %r15d
jmp 0x4bd8
decl %r15d
movl %r15d, %esi
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x2d, %edx
callq 0x3340
movq 0x30(%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x535a
movq 0x50(%rsp), %rcx
movl $0xf, %esi
leaq 0x60(%rsp), %rax
cmpq %rax, %rcx
je 0x4c1d
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %r8
movq 0x38(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0x4c4b
movl $0xf, %esi
leaq 0x40(%rsp), %rdi
cmpq %rdi, 0x30(%rsp)
je 0x4c46
movq 0x40(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x4c5c
movq 0x30(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x30b0
jmp 0x4c6a
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3320
leaq 0x20(%rsp), %rsi
movq %rsi, 0x10(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x4c8f
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0x4c95
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x10(%rsp), %rdi
leaq 0x2873e(%rip), %rsi     # 0x2d3fc
callq 0x3530
leaq 0x80(%rsp), %rsi
movq %rsi, 0x70(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x4cf1
movq %rdx, 0x70(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x80(%rsp)
jmp 0x4cf7
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x78(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x70(%rsp), %r15
movq 0x90(%rsp), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0xc8(%rsp)
leaq 0xe0(%rsp), %rcx
movq %rcx, 0xd0(%rsp)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x52ca
movq 0x178(%rsp), %rcx
movq 0x180(%rsp), %r8
movq %rbp, %rdi
movq %r15, %rsi
leaq 0xc8(%rsp), %rdx
movq 0x100(%rsp), %r9
pushq 0xf0(%rsp)
pushq 0x100(%rsp)
callq 0x3cc6
addq $0x10, %rsp
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x4db0
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4dd2
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4dee
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4e0a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x4e26
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x28(%r14), %rbx
cmpq 0x30(%r14), %rbx
je 0x4e45
movslq (%rbx), %rsi
movslq 0x4(%rbx), %rdx
movq %rbp, %rdi
callq 0x4628
addq $0x8, %rbx
jmp 0x4e2a
movq %rbp, %rdi
callq 0x454c
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4e72
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x32d0
incl %r13d
movq %r14, %rdi
callq 0x31c0
movq %rax, %r14
leaq 0x190(%rsp), %rax
cmpq %rax, %r14
jne 0x49f0
leaq 0x150(%rsp), %rdi
callq 0x3c80
leaq 0x110(%rsp), %rdi
callq 0x5226
xorl %eax, %eax
jmp 0x4ef6
movq 0x3513a(%rip), %rbx     # 0x39ff0
leaq 0x284dd(%rip), %rsi     # 0x2d39a
movl $0x53, %edx
movq %rbx, %rdi
callq 0x3330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3050
movq %rax, %rdi
callq 0x31b0
movl $0x1, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4f33
movq %rax, %rbx
movq 0x1b8(%rsp), %rdi
cmpq %r14, %rdi
je 0x503b
movq 0x1c8(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0x503b
movq %rax, %rbx
jmp 0x503b
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x4f7c
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0x4f7c
jmp 0x4ff4
movq %rax, %rbx
jmp 0x4fd6
movq %rax, %rbx
jmp 0x4fba
movq %rax, %rbx
jmp 0x4f9e
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4f9e
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4fba
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4fd6
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x5009
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0x5009
movq %rax, %rbx
jmp 0x5009
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x454c
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x502e
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x32d0
leaq 0x150(%rsp), %rdi
callq 0x3c80
leaq 0x110(%rsp), %rdi
callq 0x5226
movq %rbx, %rdi
callq 0x34d0