// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, %r12
cmpl $0x1, %edi
jle 0xd3e5
movl %edi, %r13d
movq 0x8(%r12), %rax
movq %rax, 0x8(%rsp)
movl $0x4, 0x20(%rsp)
movl $0x1, %ebp
movss 0x1114c(%rip), %xmm0    # 0x1e030
movaps %xmm0, 0x30(%rsp)
movl $0x0, (%rsp)
movl $0x20, 0x14(%rsp)
leaq 0x141ca(%rip), %rax     # 0x210c9
movq %rax, 0x40(%rsp)
movslq %ebp, %r14
movq (%r12,%r14,8), %r15
movb (%r15), %bl
cmpb $0x2d, %bl
jne 0xcf7f
cmpb $0x6f, 0x1(%r15)
jne 0xcf25
cmpb $0x0, 0x2(%r15)
je 0xcff9
cmpb $0x63, 0x1(%r15)
jne 0xcf37
cmpb $0x0, 0x2(%r15)
je 0xd009
cmpb $0x64, 0x1(%r15)
jne 0xcf49
cmpb $0x0, 0x2(%r15)
je 0xd01d
cmpb $0x78, 0x1(%r15)
jne 0xcf5b
cmpb $0x0, 0x2(%r15)
je 0xd023
cmpb $0x61, 0x1(%r15)
jne 0xcf6d
cmpb $0x0, 0x2(%r15)
je 0xd029
cmpb $0x62, 0x1(%r15)
jne 0xcf7f
cmpb $0x0, 0x2(%r15)
je 0xd02f
movq %r15, %rdi
leaq 0x1414c(%rip), %rsi     # 0x210d5
callq 0x1120
testl %eax, %eax
je 0xcfd4
movq %r15, %rdi
leaq 0x1413d(%rip), %rsi     # 0x210d9
callq 0x1120
testl %eax, %eax
je 0xcff3
cmpb $0x2d, %bl
jne 0xcfec
cmpb $0x73, 0x1(%r15)
jne 0xcfb8
cmpb $0x0, 0x2(%r15)
je 0xd035
cmpb $0x68, 0x1(%r15)
jne 0xcfec
cmpb $0x0, 0x2(%r15)
movq %r15, 0x8(%rsp)
jne 0xd050
jmp 0xd3ef
orl $0x10, (%rsp)
leal 0x1(%r14), %ebp
movq 0x8(%r12,%r14,8), %rdi
callq 0x11f0
movl %eax, 0x20(%rsp)
jmp 0xd050
movq %r15, 0x8(%rsp)
jmp 0xd050
orl $0x20, (%rsp)
jmp 0xd050
leal 0x1(%r14), %ebp
movq 0x8(%r12,%r14,8), %rax
movq %rax, 0x40(%rsp)
jmp 0xd050
leal 0x1(%r14), %ebp
movq 0x8(%r12,%r14,8), %rdi
callq 0x11f0
movl %eax, 0x14(%rsp)
jmp 0xd050
orl $0x1, (%rsp)
jmp 0xd050
orl $0x2, (%rsp)
jmp 0xd050
orl $0x4, (%rsp)
jmp 0xd050
orl $0x8, (%rsp)
jmp 0xd039
orl $0x40, (%rsp)
leal 0x1(%r14), %ebp
movq 0x8(%r12,%r14,8), %rdi
callq 0x1080
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
incl %ebp
cmpl %r13d, %ebp
jl 0xcf04
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x4c(%rsp), %rcx
movq 0x8(%rsp), %rdi
movl $0x3, %r8d
callq 0x1437
testq %rax, %rax
je 0xd3f9
movq %rax, %r12
movl 0x1c(%rsp), %ebp
movl 0x18(%rsp), %r15d
movl (%rsp), %ebx
testb $0x8, %bl
movq %r15, 0x8(%rsp)
je 0xd223
movd %ebp, %xmm0
movd %r15d, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2ps %xmm1, %xmm0
movaps 0x30(%rsp), %xmm1
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm0, %xmm1
cvttps2dq %xmm1, %xmm0
movdqa %xmm0, 0x50(%rsp)
movd %xmm0, %eax
movl %eax, 0x2c(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r14d
imull %r14d, %eax
leal (%rax,%rax,2), %ebx
addl %eax, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, %r15
movslq %ebx, %r13
addq %rax, %r13
subq $0x60, %rsp
movl $0x1, %eax
movl %eax, 0x58(%rsp)
movl %eax, 0x50(%rsp)
movl %eax, 0x48(%rsp)
movl %ebp, %ebx
xorl %ebp, %ebp
movl %ebp, 0x40(%rsp)
movl %ebp, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebp, 0x28(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x20(%rsp)
movl $0x3, %eax
movl %eax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movaps 0xb0(%rsp), %xmm0
movss %xmm0, (%rsp)
movss 0x10eba(%rip), %xmm2    # 0x1e008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rdx
xorl %ecx, %ecx
movq %r13, %r8
movl %r14d, %r9d
movaps %xmm2, %xmm3
callq 0x5aea
addq $0x60, %rsp
leaq 0x1afe7(%rip), %r8      # 0x28160
movsd 0x13a37(%rip), %xmm0    # 0x20bb8
xorps %xmm1, %xmm1
movq %r13, %rdi
movq %r15, %r13
movl %r14d, %esi
movl 0x2c(%rsp), %r15d
movl %r15d, %edx
movq %r13, %rcx
movl $0x4, %r9d
callq 0x7477
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r13, %rdi
movl %r14d, %esi
movl %r15d, %edx
movq 0x8(%rsp), %r15
xorl %ecx, %ecx
movq %r12, %r8
movl %ebx, %r9d
movss 0x10e41(%rip), %xmm2    # 0x1e008
movaps %xmm2, %xmm3
movl $0x1, %eax
pushq %rax
pushq %rax
movl $0x1, %eax
pushq %rax
pushq %rbp
pushq %rbp
pushq %rbp
pushq %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
movl $0x3, %eax
pushq %rax
pushq %rbp
pushq %rbp
movl %ebx, %ebp
movl 0x58(%rsp), %ebx
pushq %r15
callq 0x5aea
addq $0x60, %rsp
testb $0x1, %bl
je 0xd21b
leaq 0x13ef9(%rip), %rdi     # 0x210ff
movl %ebp, %esi
movl %r15d, %edx
movl $0x3, %ecx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x5a28
movq %r13, %rdi
callq 0x1030
testb $0x10, %bl
je 0xd24f
movl %ebp, %eax
imull %r15d, %eax
leal (%rax,%rax,2), %edx
testl %edx, %edx
jle 0xd24f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x20(%rsp), %ecx
shll %cl, %eax
movl %edx, %ecx
xorl %edx, %edx
andb %al, (%r12,%rdx)
incq %rdx
cmpl %edx, %ecx
jne 0xd244
testb $0x40, %bl
jne 0xd25c
movl %ebp, %r14d
jmp 0xd3b2
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
movaps 0x30(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r14d
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r15d
movl %r14d, %eax
imull %r15d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %r13
movq %r13, %rdi
callq 0x1180
movq %rax, (%rsp)
movss 0x10d65(%rip), %xmm0    # 0x1e008
ucomiss 0x30(%rsp), %xmm0
jbe 0xd34b
movl %ebp, %eax
movq 0x8(%rsp), %r13
imull %r13d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1180
leaq 0x1ae95(%rip), %r8      # 0x28160
movsd 0x138e5(%rip), %xmm0    # 0x20bb8
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %ebp, %esi
movl %r13d, %edx
movq %rax, %rcx
movq %rax, %r13
movq %rax, 0x20(%rsp)
movl $0x4, %r9d
callq 0x7477
movl $0x1, %r10d
xorl %eax, %eax
movss 0x10d04(%rip), %xmm2    # 0x1e008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r13, %rdi
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
movq (%rsp), %r13
movq %r13, %r8
movl %r14d, %r9d
movaps %xmm2, %xmm3
pushq %r10
pushq %r10
pushq %r10
pushq %rax
pushq %rax
pushq %rax
pushq %rax
pushq $-0x1
pushq $0x3
pushq $0x0
pushq %rax
pushq %r15
callq 0x5aea
addq $0x60, %rsp
movq 0x20(%rsp), %rdi
callq 0x1030
jmp 0xd39d
movq %r13, %rdi
callq 0x1180
movq %rax, %r13
movl $0x1, %r10d
xorl %eax, %eax
movss 0x10ca2(%rip), %xmm2    # 0x1e008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
movq %r13, %r8
movl %r14d, %r9d
movaps %xmm2, %xmm3
pushq %r10
pushq %r10
pushq %r10
pushq %rax
pushq %rax
pushq %rax
pushq %rax
pushq $-0x1
pushq $0x3
pushq $0x0
pushq %rax
pushq %r15
callq 0x5aea
addq $0x60, %rsp
movq %r12, %rdi
callq 0x1030
movl %r14d, 0x1c(%rsp)
movl %r15d, 0x18(%rsp)
movq %r13, %r12
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
movl 0x14(%rsp), %ecx
movq 0x40(%rsp), %r8
movl %ebx, %r9d
callq 0xc9d3
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x1302
jmp 0xd3d4
movq %r12, %rdi
callq 0x12e4
jmp 0xd3d4
leaq 0x141cb(%rip), %rdi     # 0x215cb
leaq 0x13cd7(%rip), %rsi     # 0x210de
leaq 0x13cda(%rip), %rcx     # 0x210e8
movl $0x10e, %edx            # imm = 0x10E
callq 0x10d0