// File: /workspace/github_c_makefile_star2_O2/yui0[P]img2vec/img2vec.c
// Function: main
int main(int argc, char* argv[])
{
	char *name = argv[1];
	char *outfile = "img2vec.eps";
	int color = 32;
	int flag = 0;
	float scale = 2;
	int bit = 4;

	if (argc <=1) {
		usage(stderr, argv);
		return 0;
	}
	for (int i=1; i<argc; i++) {
		if (!strcmp(argv[i], "-o")) {
			outfile = argv[++i];
		} else if (!strcmp(argv[i], "-c")) {
			color = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-d")) {
			flag |= 1; // debug
		} else if (!strcmp(argv[i], "-x")) {
			flag |= 2; // dilate
		} else if (!strcmp(argv[i], "-a")) {
			flag |= 4; // alpha
		} else if (!strcmp(argv[i], "-b")) {
			flag |= 8; // blur
			scale = atof(argv[++i]);
		} else if (!strcmp(argv[i], "-cx")) {
			flag |= 16;
			bit = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-svg")) {
			flag |= 32;
		} else if (!strcmp(argv[i], "-s")) {
			flag |= 64;
			scale = atof(argv[++i]);
		} else if (!strcmp(argv[i], "-h")) {
			usage(stderr, argv);
			return 0;
		} else {
			name = argv[i];
			//printf("%s\n", name);
		}
	}

	uint8_t *pixels;
	int w, h, bpp;
	pixels = stbi_load(name, &w, &h, &bpp, 3);
	assert(pixels);

	/*uint8_t *posterized = malloc(w*h*6);
	_imgp_dilate(pixels, w, h, 3, posterized);
	stbi_write_jpg("original_dilated.jpg", w, h, 3, posterized, 0);
	color_quant(&(image_t){w, h, posterized}, 64);
	stbi_write_bmp("original_d64.bmp", w, h, 3, posterized);
	free(posterized);*/
	/*uint8_t *posterized = malloc(w*h*6);
	memcpy(posterized, pixels, w*h*3);
	color_quant(&(image_t){w, h, posterized}, 64);
	stbi_write_bmp("original_d64.bmp", w, h, 3, posterized);
	_imgp_dilate(posterized, w, h, 3, posterized+w*h*3);
	stbi_write_jpg("original_dilated.jpg", w, h, 3, posterized+w*h*3, 0);
	free(posterized);*/
	if (flag&8) { // blur
		int sx = w*scale;	// 2000
		int sy = h*scale;	// 2800
		uint8_t *posterized = malloc(sx*sy *3 *2);
		stbir_resize_uint8_srgb(pixels, w, h, 0, posterized+sx*sy*3, sx, sy, 0, 3, -1, 0);
		imgp_filter(posterized+sx*sy*3, sx, sy, posterized, magic_kernel, 4, 1, 0);
		stbir_resize_uint8_srgb(posterized, sx, sy, 0, pixels, w, h, 0, 3, -1, 0);
		if (flag&1) stbi_write_jpg("magic.jpg", w, h, 3, pixels, 0);
		free(posterized);
	}
	if (flag&16) { // 24bit -> 12bit
		//imgp_cq24to15(pixels, w, h, 3, pixels, 1);
		uint8_t *p = pixels;
		for (int n=0; n<w*h*3; n++) {
//			*p = *p & 0b11110000;
			*p = ((*p)>>bit)<<bit;
			p++;
		}
	}
	if (flag&64) { // scale
		int sx = w*scale;
		int sy = h*scale;
		uint8_t *posterized = malloc(sx*sy *3);
		if (scale<1) {
			uint8_t *pix = malloc(w*h *3);
			imgp_filter(pixels, w, h, pix, magic_kernel, 4, 1, 0);
			stbir_resize_uint8_srgb(pix, w, h, 0, posterized, sx, sy, 0, 3, -1, 0);
			free(pix);
		} else {
			posterized = malloc(sx*sy *3);
			stbir_resize_uint8_srgb(pixels, w, h, 0, posterized, sx, sy, 0, 3, -1, 0);
		}
		free(pixels);
		pixels = posterized;
		w = sx;
		h = sy;
	}
	color_quant(pixels, w, h, color, outfile, flag);

/*	uint8_t *gray = malloc(w*h*4);
	uint8_t *dilated = gray+w*h;
	uint8_t *diff = gray+w*h*2;
	uint8_t *contour = gray+w*h*3;
	imgp_gray(pixels, w, h, w, gray, w);
	color_quant(pixels, w, h, color, outfile, flag);
	//stbi_write_bmp("original64.bmp", w, h, 3, pixels);
	//stbi_write_bmp("gray.bmp", w, h, 1, gray);
	imgp_dilate(gray, w, h, 1, dilated);
	//stbi_write_jpg("dilated.jpg", w, h, 1, dilated, 0);
	imgp_absdiff(gray, dilated, w, h, diff);
	//stbi_write_jpg("diff.jpg", w, h, 1, diff, 0);
	imgp_reverse(diff, w, h, contour);
	//stbi_write_jpg("contour.jpg", w, h, 1, contour, 0);
	//stbi_write_bmp("contour.bmp", w, h, 1, contour);
	free(gray);*/
	stbi_image_free(pixels);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r12
movl %edi, 0x30(%rsp)
cmpl $0x1, %edi
jg 0xbdc2
movq 0x17246(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
movq %r12, %rsi
callq 0xbd7d
jmp 0xc233
movq 0x8(%r12), %r14
leaq 0x101f3(%rip), %rax     # 0x1bfc1
movq %rax, 0x60(%rsp)
pushq $0x20
popq %rax
movl %eax, 0x34(%rsp)
movl $0x0, 0x2c(%rsp)
movss 0xd24a(%rip), %xmm0     # 0x19034
movaps %xmm0, 0x50(%rsp)
pushq $0x1
popq %rbp
pushq $0x4
popq %rax
movl %eax, 0x40(%rsp)
cmpl 0x30(%rsp), %ebp
jge 0xbf55
movslq %ebp, %rbx
movq (%r12,%rbx,8), %r15
movb (%r15), %r13b
cmpb $0x2d, %r13b
jne 0xbe7f
cmpb $0x6f, 0x1(%r15)
jne 0xbe25
cmpb $0x0, 0x2(%r15)
je 0xbef2
cmpb $0x63, 0x1(%r15)
jne 0xbe37
cmpb $0x0, 0x2(%r15)
je 0xbf00
cmpb $0x64, 0x1(%r15)
jne 0xbe49
cmpb $0x0, 0x2(%r15)
je 0xbf12
cmpb $0x78, 0x1(%r15)
jne 0xbe5b
cmpb $0x0, 0x2(%r15)
je 0xbf19
cmpb $0x61, 0x1(%r15)
jne 0xbe6d
cmpb $0x0, 0x2(%r15)
je 0xbf20
cmpb $0x62, 0x1(%r15)
jne 0xbe7f
cmpb $0x0, 0x2(%r15)
je 0xbf27
movq %r15, %rdi
leaq 0x10144(%rip), %rsi     # 0x1bfcd
callq 0x1120
testl %eax, %eax
je 0xbed4
movq %r15, %rdi
leaq 0x10135(%rip), %rsi     # 0x1bfd1
callq 0x1120
testl %eax, %eax
je 0xbeeb
cmpb $0x2d, %r13b
jne 0xbf4b
cmpb $0x73, 0x1(%r15)
jne 0xbebd
cmpb $0x0, 0x2(%r15)
je 0xbf2e
cmpb $0x68, 0x1(%r15)
jne 0xbf4b
cmpb $0x0, 0x2(%r15)
jne 0xbf4b
jmp 0xbdab
orl $0x10, 0x2c(%rsp)
incl %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x11f0
movl %eax, 0x40(%rsp)
jmp 0xbf48
orl $0x20, 0x2c(%rsp)
jmp 0xbf48
incl %ebp
movq 0x8(%r12,%rbx,8), %rax
movq %rax, 0x60(%rsp)
jmp 0xbf48
incl %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x11f0
movl %eax, 0x34(%rsp)
jmp 0xbf48
orl $0x1, 0x2c(%rsp)
jmp 0xbf48
orl $0x2, 0x2c(%rsp)
jmp 0xbf48
orl $0x4, 0x2c(%rsp)
jmp 0xbf48
orl $0x8, 0x2c(%rsp)
jmp 0xbf33
orl $0x40, 0x2c(%rsp)
incl %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x1080
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq %r14, %r15
incl %ebp
movq %r15, %r14
jmp 0xbdf9
leaq 0x3c(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x6c(%rsp), %rcx
pushq $0x3
popq %r15
movq %r14, %rdi
movl %r15d, %r8d
callq 0x1377
testq %rax, %rax
je 0xc247
movq %rax, %r14
movl 0x3c(%rsp), %ebp
movl 0x38(%rsp), %r12d
movl 0x2c(%rsp), %ebx
testb $0x8, %bl
je 0xc0af
movd %ebp, %xmm0
movd %r12d, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2ps %xmm1, %xmm0
movaps 0x50(%rsp), %xmm1
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm0, %xmm1
cvttps2dq %xmm1, %xmm0
movdqa %xmm0, 0x70(%rsp)
movd %xmm0, %eax
movl %eax, 0x4c(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movl %ebp, 0x30(%rsp)
movd %xmm0, %ebp
imull %ebp, %eax
leal (%rax,%rax,2), %ebx
imull $0x6, %eax, %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, %r13
movl %r15d, %eax
movl %r12d, %r15d
movslq %ebx, %r12
addq %r13, %r12
andl $0x0, 0x20(%rsp)
orl $-0x1, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, %ebx
movaps 0x70(%rsp), %xmm0
movss %xmm0, (%rsp)
movq %r14, %rdi
movl 0x30(%rsp), %esi
movl %r15d, %edx
xorl %ecx, %ecx
movq %r12, %r8
movl %ebp, %r9d
callq 0x67fb
leaq 0x17133(%rip), %r8      # 0x23160
pushq $0x4
popq %r9
movsd 0xfa6f(%rip), %xmm0     # 0x1baa8
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %r15d, %r12d
movl %ebp, %esi
movl 0x4c(%rsp), %r15d
movl %r15d, %edx
movq %r13, %rcx
callq 0x6e79
andl $0x0, 0x20(%rsp)
orl $-0x1, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r12d, (%rsp)
movq %r13, %rdi
movl %ebp, %esi
movl 0x30(%rsp), %ebp
movl 0x2c(%rsp), %ebx
movl %r15d, %edx
xorl %ecx, %ecx
movq %r14, %r8
movl %ebp, %r9d
callq 0x67fb
testb $0x1, %bl
je 0xc0a7
leaq 0xff63(%rip), %rdi      # 0x1bff7
pushq $0x3
popq %rcx
movl %ebp, %esi
movl %r12d, %edx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x5728
movq %r13, %rdi
callq 0x1030
testb $0x10, %bl
jne 0xc0c4
testb $0x40, %bl
jne 0xc0ea
movl %r12d, %r13d
movl %ebp, %r15d
jmp 0xc211
movl %ebp, %eax
imull %r12d, %eax
leal (%rax,%rax,2), %eax
pushq $-0x1
popq %rdx
movl 0x40(%rsp), %ecx
shll %cl, %edx
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpl %ecx, %eax
je 0xc0b4
andb %dl, (%r14,%rcx)
incq %rcx
jmp 0xc0dd
cvtsi2ss %ebp, %xmm0
movaps 0x50(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r15d
cvtsi2ss %r12d, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r13d
movl %r15d, %eax
imull %r13d, %eax
leal (%rax,%rax,2), %eax
movl %r12d, 0x30(%rsp)
movslq %eax, %r12
movq %r12, %rdi
callq 0x1180
movq %rax, 0x40(%rsp)
movss 0xced7(%rip), %xmm0     # 0x19008
ucomiss 0x50(%rsp), %xmm0
jbe 0xc1c0
movl %ebp, %eax
movl 0x30(%rsp), %r12d
imull %r12d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1180
movl %ebp, %ebx
movq %rax, %rbp
leaq 0x17002(%rip), %r8      # 0x23160
pushq $0x4
popq %r9
movsd 0xf93e(%rip), %xmm0     # 0x1baa8
xorps %xmm1, %xmm1
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
movq %rax, %rcx
callq 0x6e79
andl $0x0, 0x20(%rsp)
orl $-0x1, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movl %r13d, (%rsp)
movl $0x3, 0x10(%rsp)
movq %rbp, %rdi
movl %ebx, %esi
movl %r12d, %edx
xorl %ecx, %ecx
movq 0x40(%rsp), %r12
movq %r12, %r8
movl %r15d, %r9d
callq 0x67fb
movq %rbp, %rdi
movl 0x2c(%rsp), %ebx
callq 0x1030
jmp 0xc1fc
movq %r12, %rdi
callq 0x1180
movq %rax, %r12
andl $0x0, 0x20(%rsp)
orl $-0x1, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movl %r13d, (%rsp)
movl $0x3, 0x10(%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl 0x30(%rsp), %edx
xorl %ecx, %ecx
movq %rax, %r8
movl %r15d, %r9d
callq 0x67fb
movq %r14, %rdi
callq 0x1030
movl %r15d, 0x3c(%rsp)
movl %r13d, 0x38(%rsp)
movq %r12, %r14
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
movl 0x34(%rsp), %ecx
movq 0x60(%rsp), %r8
movl %ebx, %r9d
callq 0xb902
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10275(%rip), %rdi     # 0x1c4c3
leaq 0xfd81(%rip), %rsi      # 0x1bfd6
leaq 0xfd84(%rip), %rcx      # 0x1bfe0
movl $0x10e, %edx            # imm = 0x10E
callq 0x10d0