// File: 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
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x94(%rbp)
leaq -0x100(%rbp), %rax
movq %rax, %rcx
addq $0x60, %rcx
movq %rcx, -0x240(%rbp)
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x4c70
movq -0x248(%rbp), %rax
movq -0x240(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0x238(%rbp)
jne 0x3e8f
leaq -0x100(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x110(%rbp)
leaq -0x100(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x118(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x3f50
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3310(%rip), %rsi      # 0x721b
callq 0x22d0
cmpl $0x0, %eax
jne 0x3f50
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3df0
jmp 0x3f23
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x128(%rbp)
jmp 0x460b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x466d
cmpl $0x2, -0x8(%rbp)
je 0x3f7a
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3df0
jmp 0x3f64
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x128(%rbp)
jmp 0x460b
jmp 0x3f7c
movq 0x7045(%rip), %rax      # 0xafc8
movq (%rax), %rdx
leaq -0x90(%rbp), %rdi
movl $0x7f, %esi
callq 0x2330
movq %rax, -0x250(%rbp)
jmp 0x3fa0
movq -0x250(%rbp), %rax
cmpq $0x0, %rax
je 0x4166
movl -0x94(%rbp), %eax
incl %eax
movl %eax, -0x94(%rbp)
leaq -0x171(%rbp), %rdi
movq %rdi, -0x258(%rbp)
callq 0x2380
movq -0x258(%rbp), %rdx
leaq -0x170(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x46d0
jmp 0x3fee
leaq -0x14c(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x35f0
movb %al, -0x259(%rbp)
jmp 0x4009
leaq -0x170(%rbp), %rdi
callq 0x23d8
leaq -0x171(%rbp), %rdi
callq 0x2230
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x402d
jmp 0x40aa
cmpl $0x0, -0x134(%rbp)
jl 0x40a5
cmpl $0x3, -0x134(%rbp)
jg 0x40a5
movslq -0x134(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0x100(%rbp,%rax,8), %rdi
leaq -0x14c(%rbp), %rsi
callq 0x4c90
jmp 0x4060
jmp 0x40a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x4094
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x23d8
leaq -0x171(%rbp), %rdi
callq 0x2230
jmp 0x466d
jmp 0x4161
cmpl $-0x1, -0x134(%rbp)
jne 0x415f
movq 0x6f32(%rip), %rdi      # 0xaff0
leaq 0x71b3(%rip), %rsi      # 0xb278
callq 0x21b0
movq %rax, -0x268(%rbp)
jmp 0x40d3
movq -0x268(%rbp), %rdi
leaq 0x313d(%rip), %rsi      # 0x721e
callq 0x21e0
jmp 0x40e8
movl -0x94(%rbp), %esi
movq 0x6efb(%rip), %rdi      # 0xaff0
callq 0x2360
movq %rax, -0x270(%rbp)
jmp 0x4103
movq -0x270(%rbp), %rdi
leaq 0x7187(%rip), %rsi      # 0xb298
callq 0x21b0
movq %rax, -0x278(%rbp)
jmp 0x411f
movq -0x278(%rbp), %rdi
movq 0x6e83(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x4134
movq 0x6eb5(%rip), %rdi      # 0xaff0
leaq -0x90(%rbp), %rsi
callq 0x21e0
jmp 0x4149
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x128(%rbp)
jmp 0x460b
jmp 0x4161
jmp 0x3f7c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x30d1(%rip), %rsi      # 0x7246
callq 0x2280
movq %rax, -0x280(%rbp)
jmp 0x4183
movq -0x280(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x0, -0x180(%rbp)
jne 0x41bf
leaq 0x30a6(%rip), %rdi      # 0x7248
callq 0x2180
jmp 0x41a9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x128(%rbp)
jmp 0x460b
movq -0x180(%rbp), %rsi
leaq -0x198(%rbp), %rdi
callq 0x2ae0
jmp 0x41d4
leaq -0x1a0(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x20f0
movq -0x118(%rbp), %rdi
callq 0x4cd0
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
movl -0x1a8(%rbp), %eax
sarl %eax
movl %eax, -0x1a8(%rbp)
movl -0x1a4(%rbp), %eax
sarl %eax
movl %eax, -0x1a4(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x2c50
testb $0x1, %al
jne 0x4290
movq 0x6dc0(%rip), %rdi      # 0xaff0
leaq 0x3019(%rip), %rsi      # 0x7250
callq 0x21e0
movq %rax, -0x288(%rbp)
jmp 0x4245
movq -0x288(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x21e0
movq %rax, -0x290(%rbp)
jmp 0x4262
movq -0x290(%rbp), %rdi
movq 0x6d40(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x4277
jmp 0x4290
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x4661
movq 0x6d39(%rip), %rdi      # 0xafd0
leaq 0x2fd1(%rip), %rsi      # 0x726f
callq 0x21e0
movq %rax, -0x298(%rbp)
jmp 0x42ac
movq -0x108(%rbp), %rdi
callq 0x4d00
movq -0x298(%rbp), %rdi
movq %rax, %rsi
callq 0x2070
movq %rax, -0x2a0(%rbp)
jmp 0x42d0
movq -0x2a0(%rbp), %rdi
movq 0x6cd2(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x42e5
movq 0x6ce4(%rip), %rdi      # 0xafd0
leaq 0x2f90(%rip), %rsi      # 0x7283
callq 0x21e0
movq %rax, -0x2a8(%rbp)
jmp 0x4301
movq -0x110(%rbp), %rdi
callq 0x4d00
movq -0x2a8(%rbp), %rdi
movq %rax, %rsi
callq 0x2070
movq %rax, -0x2b0(%rbp)
jmp 0x4325
movq -0x2b0(%rbp), %rdi
movq 0x6c7d(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x433a
movq 0x6c8f(%rip), %rdi      # 0xafd0
leaq 0x2f4f(%rip), %rsi      # 0x7297
callq 0x21e0
movq %rax, -0x2b8(%rbp)
jmp 0x4356
movq -0x118(%rbp), %rdi
callq 0x4d00
movq -0x2b8(%rbp), %rdi
movq %rax, %rsi
callq 0x2070
movq %rax, -0x2c0(%rbp)
jmp 0x437a
movq -0x2c0(%rbp), %rdi
movq 0x6c28(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x438f
movq 0x6c3a(%rip), %rdi      # 0xafd0
leaq 0x2f0e(%rip), %rsi      # 0x72ab
callq 0x21e0
movq %rax, -0x2c8(%rbp)
jmp 0x43ab
movq -0x2c8(%rbp), %rdi
movl -0x1a8(%rbp), %esi
callq 0x2360
movq %rax, -0x2d0(%rbp)
jmp 0x43c6
movq -0x2d0(%rbp), %rdi
movl $0x20, %esi
callq 0x2270
movq %rax, -0x2d8(%rbp)
jmp 0x43e0
movq -0x2d8(%rbp), %rdi
movl -0x1a4(%rbp), %esi
callq 0x2360
movq %rax, -0x2e0(%rbp)
jmp 0x43fb
movq -0x2e0(%rbp), %rdi
leaq 0x2eb3(%rip), %rsi      # 0x72bc
callq 0x21e0
movq %rax, -0x2e8(%rbp)
jmp 0x4417
movq -0x108(%rbp), %rdi
callq 0x4d00
movq %rax, -0x300(%rbp)
movq -0x110(%rbp), %rdi
callq 0x4d00
movq %rax, %rcx
movq -0x300(%rbp), %rax
imulq %rcx, %rax
movq %rax, -0x2f8(%rbp)
movq -0x118(%rbp), %rdi
callq 0x4d00
movq -0x2f8(%rbp), %rsi
movq -0x2e8(%rbp), %rdi
imulq %rax, %rsi
callq 0x2070
movq %rax, -0x2f0(%rbp)
jmp 0x4477
movq -0x2f0(%rbp), %rdi
movq 0x6b2b(%rip), %rsi      # 0xafb0
callq 0x2210
jmp 0x448c
movq -0x198(%rbp), %rax
movslq -0x1a8(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x1ac(%rbp)
movq -0x190(%rbp), %rax
movslq -0x1a4(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x1b0(%rbp)
movl $0x0, -0x1b4(%rbp)
movl -0x1b4(%rbp), %eax
cmpl -0x1b0(%rbp), %eax
jge 0x45f5
movl $0x0, -0x1b8(%rbp)
movl -0x1b8(%rbp), %eax
cmpl -0x1ac(%rbp), %eax
jge 0x45d3
leaq -0x210(%rbp), %rdi
leaq -0x198(%rbp), %rsi
leaq -0x1a8(%rbp), %rdx
leaq -0x1a0(%rbp), %rcx
callq 0x2d10
jmp 0x4519
movslq -0x1b4(%rbp), %rsi
movslq -0x1b8(%rbp), %rdx
leaq -0x210(%rbp), %rdi
callq 0x2e50
jmp 0x4535
movl -0x1dc(%rbp), %esi
movl -0x1d8(%rbp), %edx
movl -0x1d4(%rbp), %ecx
movq -0x108(%rbp), %r8
movq -0x110(%rbp), %r9
movq -0x118(%rbp), %rdi
movq %rsp, %rax
movq %rdi, (%rax)
leaq -0x230(%rbp), %rdi
callq 0x3850
jmp 0x4570
leaq -0x230(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
movq 0x6a1a(%rip), %rax      # 0xafa0
movq (%rax), %rsi
callq 0x2260
jmp 0x4590
leaq -0x230(%rbp), %rdi
callq 0x23d8
movl -0x1b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b8(%rbp)
jmp 0x44e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x23d8
jmp 0x4661
movl $0xa, %edi
callq 0x22c0
jmp 0x45df
jmp 0x45e1
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x44c8
leaq -0x198(%rbp), %rdi
callq 0x2ca0
movl $0x0, -0x128(%rbp)
leaq -0x100(%rbp), %rax
movq %rax, -0x310(%rbp)
addq $0x60, %rax
movq %rax, -0x308(%rbp)
movq -0x308(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x318(%rbp)
callq 0x4d30
movq -0x310(%rbp), %rcx
movq -0x318(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x308(%rbp)
jne 0x4624
movl -0x4(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
leaq -0x198(%rbp), %rdi
callq 0x2ca0
leaq -0x100(%rbp), %rax
movq %rax, -0x328(%rbp)
addq $0x60, %rax
movq %rax, -0x320(%rbp)
movq -0x320(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x330(%rbp)
callq 0x4d30
movq -0x328(%rbp), %rcx
movq -0x330(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x320(%rbp)
jne 0x4686
jmp 0x46b9
movq -0x120(%rbp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)
nop