// File: /workspace/github_cpp_makefile_star2_O1/aempirei[P]Chat-Art/pnmtoansi.cc
// Function: main
int main(int argc, char **argv) {

	char line[128];
	int line_num = 0;

	std::list<geometry> geometry_mode[4];

	std::list<geometry>& bgcolor = geometry_mode[geometry::BGCOLOR];
	std::list<geometry>& fgcolor = geometry_mode[geometry::FGCOLOR];
	std::list<geometry>& symbol = geometry_mode[geometry::SYMBOL];

	if(argc == 2 && strcmp(argv[1], "-h") == 0) {
		help(*argv);
		return -1;
	}

	if(argc != 2) {
		help(*argv);
		return -1;
	}

	while(fgets(line, sizeof(line) - 1, stdin) != NULL) {

		geometry g;

		line_num++;

		if(geometry_from_string(g, line)) {

			// good line

			if(g.mode >= geometry::MODE_MIN && g.mode <= geometry::MODE_MAX)
				geometry_mode[g.mode].push_back(g);

		} else if(g.mode == -1) {

			// bad line

			std::cerr << ansi::bold << "invalid calibration data on input line ";
			std::cerr << line_num << ansi::clear << std::endl;
			std::cerr << line;
			return -1;
		}
	}

	FILE *fp = fopen(argv[1],"r");

	if(fp == NULL) {
		perror("fopen()");
		return -1;
	}

	pnm f(fp);
	typedef std::vector<tile> tiles;

	const pos_t t0 = {0,0};
	pos_t t_sz;
	memcpy(t_sz, symbol.front().size, sizeof(t_sz));

	t_sz[0] >>= 1;
	t_sz[1] >>= 1;

	if(!f.isloaded()) {
		std::cerr << "failed to load pnm image from " << argv[1] << std::endl;
	}

	std::cout << "background colors: " << bgcolor.size() << std::endl;
	std::cout << "foreground colors: " << fgcolor.size() << std::endl;
	std::cout << "character symbols: " << symbol.size() << std::endl;
	std::cout << "max. pnmtoansi  " << t_sz[0] << ' ' << t_sz[1] << " size: " << (bgcolor.size() * fgcolor.size() * symbol.size()) << std::endl;

	int w = f.width / t_sz[0];
	int h = f.height / t_sz[1];

	for(int y = 0; y < h; y++) {
		for(int x = 0; x < w; x++) {
			tile base(&f,t_sz,t0);
			base.move(y,x);
			std::string s( ansi_match( base.rgb_mean[0],base.rgb_mean[1],base.rgb_mean[2], bgcolor,fgcolor,symbol ));
			fputs(s.c_str(), stdout);
		}
		putchar('\n');
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %rbx
xorl %eax, %eax
leaq (%rsp,%rax), %rcx
addq $0xb0, %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq $0x0, 0x10(%rcx)
addq $0x18, %rax
cmpq $0x60, %rax
jne 0x32e5
leaq 0x110(%rsp), %r14
cmpl $0x2, %edi
jne 0x34de
movq 0x8(%rbx), %rdi
leaq 0xeeb(%rip), %rsi       # 0x4210
callq 0x21d0
testl %eax, %eax
je 0x34de
movq %rbx, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x1, %r15d
leaq 0x48(%rsp), %rbp
leaq 0x130(%rsp), %r12
leaq 0x38(%rsp), %rbx
movq 0x2c6a(%rip), %rax      # 0x5fc8
movq (%rax), %rdx
movq %r12, %rdi
movl $0x7f, %esi
callq 0x2230
testq %rax, %rax
je 0x3533
movq %rbp, 0x38(%rsp)
movq %r12, %rdi
callq 0x20a0
leaq (%rsp,%rax), %rdx
addq $0x130, %rdx            # imm = 0x130
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a32
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x2d96
movl %eax, %ebx
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x33c0
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2170
testb %bl, %bl
je 0x341b
movl 0x70(%rsp), %ebx
movb $0x1, %r13b
cmpq $0x3, %rbx
ja 0x34cb
movl $0x38, %edi
callq 0x2160
leaq (%rbx,%rbx,2), %rcx
leaq (%rsp,%rcx,8), %rbx
addq $0xb0, %rbx
movl 0x78(%rsp), %ecx
movl %ecx, 0x30(%rax)
movups 0x58(%rsp), %xmm0
movups 0x68(%rsp), %xmm1
movups %xmm1, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2050
incq 0x10(%rbx)
jmp 0x34cb
movb $0x1, %r13b
cmpl $-0x1, 0x70(%rsp)
jne 0x34cb
movq 0x2db8(%rip), %rsi      # 0x61e8
movq 0x2db9(%rip), %rdx      # 0x61f0
movq 0x2bb2(%rip), %rdi      # 0x5ff0
callq 0x2190
movl $0x27, %edx
movq %rax, %rdi
leaq 0xdc1(%rip), %rsi       # 0x4213
callq 0x2190
movq 0x2b92(%rip), %rdi      # 0x5ff0
movl %r15d, %esi
callq 0x2270
movq 0x2d9b(%rip), %rsi      # 0x6208
movq 0x2d9c(%rip), %rdx      # 0x6210
movq %rax, %rdi
callq 0x2190
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq %r12, %rdi
callq 0x20a0
movq 0x2b3b(%rip), %rdi      # 0x5ff0
movq %r12, %rsi
movq %rax, %rdx
callq 0x2190
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r13d, %r13d
incl %r15d
testb %r13b, %r13b
leaq 0x38(%rsp), %rbx
jne 0x3357
jmp 0x34ee
movq (%rbx), %rdi
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
callq 0x323e
leaq 0xb0(%rsp), %rbx
movq -0x18(%r14), %rdi
addq $-0x18, %r14
cmpq %r14, %rdi
je 0x3518
movq (%rdi), %r15
movl $0x38, %esi
callq 0x2170
movq %r15, %rdi
cmpq %r14, %r15
jne 0x3503
cmpq %rbx, %r14
jne 0x34f6
movl 0xc(%rsp), %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rdi
leaq 0xcf8(%rip), %rsi       # 0x423b
callq 0x21a0
testq %rax, %rax
je 0x3901
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x276a
movq 0xf8(%rsp), %rax
movq 0x10(%rax), %r13
cmpq $0x0, 0x28(%rsp)
je 0x3586
cmpq $0x0, 0x18(%rsp)
je 0x3586
cmpq $0x0, 0x20(%rsp)
jne 0x3608
movq 0x2a63(%rip), %rbx      # 0x5ff0
leaq 0xcb1(%rip), %rsi       # 0x4245
movl $0x1e, %edx
movq %rbx, %rdi
callq 0x2190
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x35c6
movq %r12, %rdi
callq 0x20a0
movq 0x2a37(%rip), %rdi      # 0x5ff0
movq %r12, %rsi
movq %rax, %rdx
callq 0x2190
jmp 0x35dd
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2260
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq 0x29f5(%rip), %rdi      # 0x5ff0
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x29c1(%rip), %rdi      # 0x5fd0
leaq 0xc4e(%rip), %rsi       # 0x4264
movl $0x13, %edx
callq 0x2190
movq 0xd8(%rsp), %rsi
movq 0x29a1(%rip), %rdi      # 0x5fd0
callq 0x20b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x296b(%rip), %rdi      # 0x5fd0
leaq 0xc0c(%rip), %rsi       # 0x4278
movl $0x13, %edx
callq 0x2190
movq 0xf0(%rsp), %rsi
movq 0x294b(%rip), %rdi      # 0x5fd0
callq 0x20b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x2915(%rip), %rdi      # 0x5fd0
leaq 0xbca(%rip), %rsi       # 0x428c
movl $0x13, %edx
callq 0x2190
movq 0x108(%rsp), %rsi
movq 0x28f5(%rip), %rdi      # 0x5fd0
callq 0x20b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x28bf(%rip), %rdi      # 0x5fd0
leaq 0xb88(%rip), %rsi       # 0x42a0
movl $0x10, %edx
callq 0x2190
movl %r13d, %ebp
sarl %ebp
movq 0x28a2(%rip), %rdi      # 0x5fd0
movl %ebp, %esi
callq 0x2270
leaq 0x58(%rsp), %rsi
movb $0x20, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x2190
sarq $0x21, %r13
movq %rax, %rdi
movl %r13d, %esi
callq 0x2270
movq %rax, %rbx
leaq 0xb4e(%rip), %rsi       # 0x42b1
movl $0x7, %edx
movq %rax, %rdi
callq 0x2190
movq 0xf0(%rsp), %rsi
imulq 0xd8(%rsp), %rsi
imulq 0x108(%rsp), %rsi
movq %rbx, %rdi
callq 0x20b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x20(%rsp), %rax
movslq %r13d, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x10(%rsp)
testl %eax, %eax
jle 0x38e0
movq 0x18(%rsp), %rax
movslq %ebp, %rcx
xorl %r12d, %r12d
xorl %edx, %edx
divq %rcx
shlq $0x20, %r13
movl %ebp, %r15d
orq %r13, %r15
movq %rax, 0x30(%rsp)
movslq %eax, %r13
movq 0x10(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x10(%rsp)
leaq 0x110(%rsp), %rbp
cmpl $0x0, 0x30(%rsp)
jle 0x38c8
xorl %ebx, %ebx
leaq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq $0x0, 0x84(%rsp)
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2904
movl 0x8c(%rsp), %esi
movl 0x90(%rsp), %edx
movl 0x94(%rsp), %ecx
leaq 0xf8(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0xc8(%rsp), %r8
leaq 0xe0(%rsp), %r9
callq 0x2eef
movq 0x110(%rsp), %rdi
movq 0x2711(%rip), %rax      # 0x5fa0
movq (%rax), %rsi
callq 0x2180
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x38bc
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x2170
incq %rbx
cmpq %r13, %rbx
jl 0x381c
movl $0xa, %edi
callq 0x21c0
incq %r12
cmpq 0x10(%rsp), %r12
jb 0x380f
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x34ee
callq 0x21b0
movq $0x0, 0x28(%rsp)
jmp 0x34ee
leaq 0x935(%rip), %rdi       # 0x423d
callq 0x2120
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x34ee
jmp 0x393e
jmp 0x393e
jmp 0x3943
jmp 0x393e
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x395e
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x395e
movq %rax, %rbx
jmp 0x395e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x395e
callq 0x21b0
movq $0x0, 0x28(%rsp)
leaq 0xb0(%rsp), %r15
movq -0x18(%r14), %rdi
addq $-0x18, %r14
cmpq %r14, %rdi
je 0x3988
movq (%rdi), %r12
movl $0x38, %esi
callq 0x2170
movq %r12, %rdi
cmpq %r14, %r12
jne 0x3973
cmpq %r15, %r14
jne 0x3966
movq %rbx, %rdi
callq 0x2280
nop