// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %eax, %eax
leaq (%rsp,%rax), %rcx
addq $0x50, %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq $0x0, 0x10(%rcx)
addq $0x18, %rax
cmpq $0x60, %rax
jne 0x3314
leaq 0xb0(%rsp), %r14
cmpl $0x2, %edi
jne 0x3528
movq 0x8(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x335f
cmpb $0x68, 0x1(%rax)
jne 0x335f
cmpb $0x0, 0x2(%rax)
je 0x3528
movq %rsi, 0x10(%rsp)
movq 0x2c5d(%rip), %rax      # 0x5fc8
movq (%rax), %rdx
leaq 0x130(%rsp), %rdi
movl $0x7f, %esi
callq 0x2220
testq %rax, %rax
je 0x347a
movl $0x1, %ebx
leaq 0x130(%rsp), %rbp
leaq 0x30(%rsp), %r15
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %rbp, %rdi
callq 0x20a0
leaq (%rsp,%rax), %rdx
addq $0x130, %rdx            # imm = 0x130
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3a7c
leaq 0xd8(%rsp), %rdi
movq %r15, %rsi
callq 0x2d92
movl %eax, %r13d
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x33f2
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2170
movl 0xf0(%rsp), %r12d
testb %r13b, %r13b
je 0x344e
cmpl $0x3, %r12d
ja 0x3458
movl $0x38, %edi
callq 0x2160
leaq (%r12,%r12,2), %rcx
leaq (%rsp,%rcx,8), %r13
addq $0x50, %r13
movl 0xf8(%rsp), %ecx
movl %ecx, 0x30(%rax)
movups 0xd8(%rsp), %xmm0
movups 0xe8(%rsp), %xmm1
movups %xmm1, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2050
incq 0x10(%r13)
jmp 0x3458
cmpl $-0x1, %r12d
je 0x353a
movq 0x2b69(%rip), %rax      # 0x5fc8
movq (%rax), %rdx
movq %rbp, %rdi
movl $0x7f, %esi
callq 0x2220
incl %ebx
testq %rax, %rax
jne 0x339b
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rdi
leaq 0xdae(%rip), %rsi       # 0x4238
callq 0x21a0
testq %rax, %rax
je 0x3952
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x275a
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rbp
movq 0x28(%rsp), %r12
testq %r12, %r12
setne %al
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %r13
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
setne %cl
andb %al, %cl
testq %r13, %r13
setne %al
testb %al, %cl
jne 0x3624
movq 0x2b08(%rip), %rbx      # 0x5ff0
leaq 0xd53(%rip), %rsi       # 0x4242
movl $0x1e, %edx
movq %rbx, %rdi
callq 0x2190
movq 0x8(%r15), %r15
testq %r15, %r15
je 0x35e2
movq %r15, %rdi
callq 0x20a0
movq 0x2ad8(%rip), %rdi      # 0x5ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2190
jmp 0x35f9
movq (%rsi), %rdi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x3270
jmp 0x3912
movq 0x2c97(%rip), %rsi      # 0x61d8
movq 0x2c98(%rip), %rdx      # 0x61e0
movq 0x2aa1(%rip), %rdi      # 0x5ff0
callq 0x2190
leaq 0xcb5(%rip), %rsi       # 0x4210
movl $0x27, %edx
movq %rax, %rdi
callq 0x2190
movq 0x2a81(%rip), %rdi      # 0x5ff0
movl %ebx, %esi
callq 0x2260
movq 0x2c7b(%rip), %rsi      # 0x61f8
movq 0x2c7c(%rip), %rdx      # 0x6200
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
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
callq 0x20a0
movq 0x2a23(%rip), %rdi      # 0x5ff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2190
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3912
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq 0x29d9(%rip), %rdi      # 0x5ff0
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq 0x29a5(%rip), %rdi      # 0x5fd0
leaq 0xc2f(%rip), %rsi       # 0x4261
movl $0x13, %edx
callq 0x2190
movq 0x78(%rsp), %rsi
movq 0x2988(%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 0x2952(%rip), %rdi      # 0x5fd0
leaq 0xbf0(%rip), %rsi       # 0x4275
movl $0x13, %edx
callq 0x2190
movq 0x90(%rsp), %rsi
movq 0x2932(%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 0x28fc(%rip), %rdi      # 0x5fd0
leaq 0xbae(%rip), %rsi       # 0x4289
movl $0x13, %edx
callq 0x2190
movq 0xa8(%rsp), %rsi
movq 0x28dc(%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 0x28a6(%rip), %rdi      # 0x5fd0
leaq 0xb6c(%rip), %rsi       # 0x429d
movl $0x10, %edx
callq 0x2190
movl %ebp, %r15d
sarl %r15d
movq 0x2888(%rip), %rdi      # 0x5fd0
movl %r15d, %esi
callq 0x2260
leaq 0xd8(%rsp), %rsi
movb $0x20, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x2190
sarq $0x21, %rbp
movq %rax, %rdi
movl %ebp, %esi
callq 0x2260
movq %rax, %rbx
leaq 0xb2e(%rip), %rsi       # 0x42ae
movl $0x7, %edx
movq %rax, %rdi
callq 0x2190
movq 0x90(%rsp), %rsi
imulq 0x78(%rsp), %rsi
imulq 0xa8(%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
movslq %ebp, %rcx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rdi
testl %edi, %edi
jle 0x3903
movslq %r15d, %rcx
movl %r15d, %esi
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
xorl %edx, %edx
divq %rcx
shlq $0x20, %rbp
movl %esi, %r12d
orq %rbp, %r12
movq %rax, 0x10(%rsp)
movslq %eax, %r13
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
leaq 0xb8(%rsp), %rbp
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x38e3
xorl %ebx, %ebx
leaq 0x18(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq %r12, 0xe0(%rsp)
movq $0x0, 0x104(%rsp)
leaq 0xd8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x28f4
movl 0x10c(%rsp), %esi
movl 0x110(%rsp), %edx
movl 0x114(%rsp), %ecx
leaq 0x98(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x68(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x2eeb
movq 0xb8(%rsp), %rdi
movq 0x26f6(%rip), %rax      # 0x5fa0
movq (%rax), %rsi
callq 0x2180
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x38d7
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x2170
incq %rbx
cmpq %r13, %rbx
jl 0x3831
movl $0xa, %edi
callq 0x21c0
incq %r15
movq 0x8(%rsp), %rdi
cmpq %rdi, %r15
jb 0x3824
movq 0x28(%rsp), %r12
testq %r12, %r12
je 0x3910
movq %r12, %rdi
callq 0x21b0
xorl %ebx, %ebx
leaq 0x50(%rsp), %r15
movq -0x18(%r14), %rdi
addq $-0x18, %r14
cmpq %r14, %rdi
je 0x3939
movq (%rdi), %r12
movl $0x38, %esi
callq 0x2170
movq %r12, %rdi
cmpq %r14, %r12
jne 0x3924
cmpq %r15, %r14
jne 0x3917
movl %ebx, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8e1(%rip), %rdi       # 0x423a
callq 0x2120
jmp 0x35d8
jmp 0x396b
jmp 0x396b
jmp 0x396b
jmp 0x396b
movq %rax, %rbx
jmp 0x39ab
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x39ab
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x39ab
movq %rax, %rbx
jmp 0x399e
movq %rax, %rbx
movq 0x28(%rsp), %r12
testq %r12, %r12
je 0x39ab
movq %r12, %rdi
callq 0x21b0
leaq 0x50(%rsp), %r15
movq -0x18(%r14), %rdi
addq $-0x18, %r14
cmpq %r14, %rdi
je 0x39d2
movq (%rdi), %r12
movl $0x38, %esi
callq 0x2170
movq %r12, %rdi
cmpq %r14, %r12
jne 0x39bd
cmpq %r15, %r14
jne 0x39b0
movq %rbx, %rdi
callq 0x2270
nop