// File: /workspace/github_cpp_makefile_star2_O2/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 $0x408, %rsp            # imm = 0x408
cmpl $0x5, %edi
jne 0x4206
movq %rsi, %r15
leaq 0xc8(%rsp), %rax
andq $0x0, 0x18(%rax)
leaq 0x1cadd(%rip), %rcx     # 0x20a20
movq %rcx, 0x20(%rax)
movq %rax, 0x8(%rax)
movq %rax, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x8(%rax)
leaq 0x1e4e1(%rip), %rsi     # 0x2243e
leaq 0x1a8(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
callq 0x439c
leaq 0xb8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x20b84
leaq 0x1a8(%rsp), %rdi
callq 0x30f0
movq 0x8(%r15), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x36dc
movq 0x10(%r15), %rdi
callq 0x3440
cltq
movq %rax, 0x28(%rsp)
movq 0x18(%r15), %rdi
callq 0x3440
cltq
movq %rax, 0x20(%rsp)
movq 0x20(%r15), %rdi
callq 0x3440
cltq
movq %rax, 0x18(%rsp)
leaq 0x138(%rsp), %rax
movq 0x10(%rax), %r13
movl $0x0, 0x4(%rsp)
leaq 0x188(%rsp), %r12
leaq 0x1a8(%rsp), %r14
leaq 0x138(%rsp), %rax
cmpq %rax, %r13
je 0x4226
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
andq $0x0, 0x40(%rsp)
movb $0x0, 0x48(%rsp)
movl 0x20(%r13), %eax
movl %eax, 0x30(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x43d8
movq 0x2bfab(%rip), %rdi     # 0x2fff0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl 0x18(%rdi,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rdi,%rax)
cvttsd2si 0x10(%rsp), %esi
callq 0x34f0
movq %rax, %rdi
leaq 0x1e3da(%rip), %rsi     # 0x22449
callq 0x3290
movq %rax, %rdi
pushq $0x30
popq %rsi
callq 0x3460
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x6, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x8(%rsp), %rcx
movl 0x18(%rcx), %esi
movq %rax, %rdi
callq 0x31a0
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x3330
movl 0x20(%r13), %esi
movq %rax, %rdi
callq 0x31a0
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x3330
movq 0x8(%rsp), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3100
movq 0x8(%r15), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x439c
movq %r12, %rbx
movq %r12, %rdi
movl 0x4(%rsp), %esi
callq 0x4514
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x44ad
movq %r15, %rbx
leaq 0x78(%rsp), %rdi
movq %rbp, %rsi
leaq 0x1e314(%rip), %rdx     # 0x2244c
callq 0x448e
movq 0x78(%rsp), %r15
movq 0x8(%rsp), %rsi
addq $0x18, %rsi
leaq 0x160(%rsp), %rbp
movq %rbp, %rdi
callq 0x4380
movq 0x120(%rsp), %rcx
movq 0x128(%rsp), %r8
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %r9
pushq 0x18(%rsp)
pushq 0x28(%rsp)
callq 0x3854
popq %rax
popq %rcx
leaq 0x168(%rsp), %rdi
callq 0x30f0
leaq 0x78(%rsp), %rdi
callq 0x30f0
leaq 0x98(%rsp), %rdi
callq 0x30f0
movq %r12, %rdi
callq 0x30f0
leaq 0x58(%rsp), %rdi
callq 0x30f0
movq 0x28(%r13), %r15
cmpq 0x30(%r13), %r15
je 0x41dd
movslq (%r15), %rsi
movslq 0x4(%r15), %rdx
movq %r14, %rdi
callq 0x3d9a
addq $0x8, %r15
jmp 0x41c2
incl 0x4(%rsp)
movq %r14, %rdi
callq 0x3d20
leaq 0x38(%rsp), %rdi
callq 0x30f0
movq %r13, %rdi
callq 0x31b0
movq %rax, %r13
movq %rbx, %r15
jmp 0x3ff9
movq 0x2bde3(%rip), %rdi     # 0x2fff0
leaq 0x1e1d6(%rip), %rsi     # 0x223ea
callq 0x3290
movq %rax, %rdi
callq 0x3100
pushq $0x1
popq %rax
jmp 0x4242
leaq 0xf8(%rsp), %rdi
callq 0x381a
leaq 0xb8(%rsp), %rdi
callq 0x456e
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x426b
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x30f0
jmp 0x42f5
movq %rax, %rbx
jmp 0x42f5
movq %rax, %rbx
jmp 0x42a9
movq %rax, %rbx
jmp 0x429c
movq %rax, %rbx
jmp 0x4292
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x30f0
leaq 0x78(%rsp), %rdi
callq 0x30f0
leaq 0x98(%rsp), %rdi
callq 0x30f0
leaq 0x188(%rsp), %rdi
callq 0x30f0
jmp 0x42bd
jmp 0x42c9
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x30f0
jmp 0x42de
movq %rax, %rbx
jmp 0x42de
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x3d20
leaq 0x38(%rsp), %rdi
callq 0x30f0
leaq 0xf8(%rsp), %rdi
callq 0x381a
leaq 0xb8(%rsp), %rdi
callq 0x456e
movq %rbx, %rdi
callq 0x3500