// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
xorl %eax, %eax
leaq (%rsp,%rax), %rcx
addq $0x30, %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
andq $0x0, 0x10(%rcx)
addq $0x18, %rax
cmpq $0x60, %rax
jne 0x3099
cmpl $0x2, %edi
jne 0x316e
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
cmpb $0x2d, (%rax)
jne 0x30de
cmpb $0x68, 0x1(%rax)
jne 0x30de
cmpb $0x0, 0x2(%rax)
je 0x316e
pushq $0x1
popq %rbp
movq 0x3ee0(%rip), %r14      # 0x6fc8
leaq 0x130(%rsp), %r15
leaq 0x110(%rsp), %r13
leaq 0x98(%rsp), %r12
movq (%r14), %rdx
movq %r15, %rdi
pushq $0x7f
popq %rsi
callq 0x2230
testq %rax, %rax
je 0x3180
movq %r13, %rdi
movq %r15, %rsi
leaq 0xf0(%rsp), %rdx
callq 0x34d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x2c41
movl %eax, %ebx
movq %r13, %rdi
callq 0x22a8
movl 0xb0(%rsp), %eax
testb %bl, %bl
je 0x3161
cmpl $0x3, %eax
ja 0x316a
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0x30, %rdi
movq %r12, %rsi
callq 0x357c
jmp 0x316a
cmpl $-0x1, %eax
je 0x33e4
incl %ebp
jmp 0x3100
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x3034
jmp 0x343f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x10ab(%rip), %rsi      # 0x423b
callq 0x21b0
testq %rax, %rax
je 0x3471
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x26ae
movq 0x78(%rsp), %rax
movq 0x10(%rax), %r12
cmpq $0x0, 0x28(%rsp)
setne %al
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rbx
testq %r14, %r14
setne %cl
andb %al, %cl
testq %rbx, %rbx
setne %al
testb %al, %cl
jne 0x3205
movq 0x3e10(%rip), %rdi      # 0x6ff0
leaq 0x105e(%rip), %rsi      # 0x4245
callq 0x2160
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x2090
movq 0x3dc4(%rip), %rdi      # 0x6fd0
leaq 0x1051(%rip), %rsi      # 0x4264
callq 0x2160
movq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2090
movq 0x3d9c(%rip), %rdi      # 0x6fd0
leaq 0x103d(%rip), %rsi      # 0x4278
callq 0x2160
movq 0x70(%rsp), %rsi
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2090
movq 0x3d74(%rip), %rdi      # 0x6fd0
leaq 0x1029(%rip), %rsi      # 0x428c
callq 0x2160
movq 0x88(%rsp), %rsi
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2090
movq 0x3d49(%rip), %rdi      # 0x6fd0
leaq 0x1012(%rip), %rsi      # 0x42a0
callq 0x2160
movl %r12d, %ebp
sarl %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x2260
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x21a0
sarq $0x21, %r12
movq %rax, %rdi
movl %r12d, %esi
callq 0x2260
leaq 0xfee(%rip), %rsi       # 0x42b1
movq %rax, %rdi
callq 0x2160
movq 0x70(%rsp), %rsi
imulq 0x58(%rsp), %rsi
imulq 0x88(%rsp), %rsi
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2090
movslq %ebp, %rcx
xorl %r13d, %r13d
movq %r14, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movslq %r12d, %rsi
movq %rbx, %rax
xorl %edx, %edx
divq %rsi
shlq $0x20, %r12
movl %ebp, %r14d
orq %r12, %r14
movslq %ecx, %r15
cltq
movq %rax, 0x10(%rsp)
leaq 0xf0(%rsp), %rbp
movq 0x3c7a(%rip), %r12      # 0x6fa8
cmpq 0x10(%rsp), %r13
jge 0x33d6
xorl %ebx, %ebx
cmpq %r15, %rbx
jge 0x33c6
leaq 0x18(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r14, 0xa0(%rsp)
andq $0x0, 0xc4(%rsp)
leaq 0x98(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x281c
movl 0xcc(%rsp), %esi
movl 0xd0(%rsp), %edx
movl 0xd4(%rsp), %ecx
leaq 0x78(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x48(%rsp), %r8
leaq 0x60(%rsp), %r9
callq 0x2d87
movq 0xf0(%rsp), %rdi
movq (%r12), %rsi
callq 0x2190
movq %rbp, %rdi
callq 0x22a8
incq %rbx
jmp 0x333b
pushq $0xa
popq %rdi
callq 0x21e0
incq %r13
jmp 0x332e
leaq 0x18(%rsp), %rdi
callq 0x2760
xorl %ebx, %ebx
jmp 0x3442
movq 0x3c05(%rip), %rdi      # 0x6ff0
leaq 0x3de6(%rip), %rsi      # 0x71d8
callq 0x2140
leaq 0xe15(%rip), %rsi       # 0x4213
movq %rax, %rdi
callq 0x2160
movq 0x3be3(%rip), %rdi      # 0x6ff0
movl %ebp, %esi
callq 0x2260
leaq 0x3ddd(%rip), %rsi      # 0x71f8
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x2090
movq 0x3bbe(%rip), %rdi      # 0x6ff0
leaq 0x130(%rsp), %rsi
callq 0x2160
pushq $-0x1
popq %rbx
pushq $0x48
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x30, %rdi
callq 0x35dc
addq $-0x18, %r14
cmpq $-0x18, %r14
jne 0x3446
movl %ebx, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdc5(%rip), %rdi       # 0x423d
callq 0x2120
jmp 0x343f
jmp 0x3489
jmp 0x3489
jmp 0x3489
jmp 0x3489
jmp 0x34a0
movq %rax, %rbx
jmp 0x34ad
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x22a8
jmp 0x34ad
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2760
pushq $0x48
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x30, %rdi
callq 0x35dc
addq $-0x18, %r14
cmpq $-0x18, %r14
jne 0x34b1
movq %rbx, %rdi
callq 0x2270