// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %r12
cmpl $0x1, %edi
jle 0xd491
movl %edi, %r13d
movq 0x8(%r12), %rax
movq %rax, 0x10(%rsp)
movl $0x4, 0x1c(%rsp)
movl $0x1, %ebp
movss 0x100a9(%rip), %xmm0    # 0x1d034
movss %xmm0, 0x4(%rsp)
movl $0x0, (%rsp)
movl $0x20, 0x20(%rsp)
leaq 0x12fe2(%rip), %rax     # 0x1ff89
movq %rax, 0x28(%rsp)
leaq 0x12fe2(%rip), %r15     # 0x1ff95
movslq %ebp, %rbx
movq (%r12,%rbx,8), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1130
testl %eax, %eax
je 0xd0a2
movq %r14, %rdi
leaq 0x12fc1(%rip), %rsi     # 0x1ff98
callq 0x1130
testl %eax, %eax
je 0xd0b1
movq %r14, %rdi
leaq 0x12fad(%rip), %rsi     # 0x1ff9b
callq 0x1130
testl %eax, %eax
je 0xd0c4
movq %r14, %rdi
leaq 0x12f99(%rip), %rsi     # 0x1ff9e
callq 0x1130
testl %eax, %eax
je 0xd0ca
movq %r14, %rdi
leaq 0x12f85(%rip), %rsi     # 0x1ffa1
callq 0x1130
testl %eax, %eax
je 0xd0d0
movq %r14, %rdi
leaq 0x12f71(%rip), %rsi     # 0x1ffa4
callq 0x1130
testl %eax, %eax
je 0xd0d6
movq %r14, %rdi
leaq 0x12f5d(%rip), %rsi     # 0x1ffa7
callq 0x1130
testl %eax, %eax
je 0xd0f3
movq %r14, %rdi
leaq 0x12f4a(%rip), %rsi     # 0x1ffab
callq 0x1130
testl %eax, %eax
je 0xd10a
movq %r14, %rdi
leaq 0x12f38(%rip), %rsi     # 0x1ffb0
callq 0x1130
testl %eax, %eax
je 0xd11b
movq %r14, %rdi
leaq 0x12f24(%rip), %rsi     # 0x1ffb3
callq 0x1130
movq %r14, 0x10(%rsp)
testl %eax, %eax
jne 0xd10e
jmp 0xd49b
leal 0x1(%rbx), %ebp
movq 0x8(%r12,%rbx,8), %rax
movq %rax, 0x28(%rsp)
jmp 0xd10e
leal 0x1(%rbx), %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x1200
movl %eax, 0x20(%rsp)
jmp 0xd10e
orl $0x1, (%rsp)
jmp 0xd10e
orl $0x2, (%rsp)
jmp 0xd10e
orl $0x4, (%rsp)
jmp 0xd10e
orl $0x8, (%rsp)
leal 0x1(%rbx), %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x1090
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
jmp 0xd10e
orl $0x10, (%rsp)
leal 0x1(%rbx), %ebp
movq 0x8(%r12,%rbx,8), %rdi
callq 0x1200
movl %eax, 0x1c(%rsp)
jmp 0xd10e
orl $0x20, (%rsp)
incl %ebp
cmpl %r13d, %ebp
jl 0xcfb3
jmp 0xd121
orl $0x40, (%rsp)
jmp 0xd0da
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x44(%rsp), %rcx
movq 0x10(%rsp), %rdi
movl $0x3, %r8d
callq 0x1447
testq %rax, %rax
je 0xd4a5
movq %rax, %r12
movl (%rsp), %ebp
testb $0x8, %bpl
je 0xd2c9
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x4(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r13d
movl 0x8(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
movq %rbp, 0x38(%rsp)
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movq %rax, 0x30(%rsp)
imull %r13d, %eax
leal (%rax,%rax,2), %ebx
addl %eax, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, 0x10(%rsp)
movslq %ebx, %r14
addq %rax, %r14
movl $0x1, %r11d
xorl %r15d, %r15d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0x3, %eax
movss 0xfe39(%rip), %xmm2     # 0x1d008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r12, %rdi
movl 0x24(%rsp), %ebx
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r14, %r8
movl %r13d, %r9d
movaps %xmm2, %xmm3
pushq %r11
pushq %r11
pushq %r11
pushq %r15
pushq %r15
pushq %r15
pushq %r15
pushq %r10
pushq %rax
pushq %r15
pushq %r15
movq 0x88(%rsp), %rbp
pushq %rbp
callq 0x59c3
addq $0x60, %rsp
leaq 0x19f47(%rip), %r8      # 0x27160
movsd 0x1285f(%rip), %xmm0    # 0x1fa80
xorps %xmm1, %xmm1
movq %r14, %rdi
movl %r13d, %r14d
movl %r13d, %esi
movl %ebp, %edx
movq 0x10(%rsp), %r13
movq %r13, %rcx
movl $0x4, %r9d
callq 0x7390
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r13, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl (%rsp), %ebp
xorl %ecx, %ecx
movq %r12, %r8
movl %ebx, %r14d
movl %ebx, %r9d
movss 0xfda2(%rip), %xmm2     # 0x1d008
movaps %xmm2, %xmm3
movl $0x1, %eax
pushq %rax
pushq %rax
movl $0x1, %eax
pushq %rax
movq 0x50(%rsp), %rbx
pushq %r15
pushq %r15
pushq %r15
pushq %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
movl $0x3, %eax
pushq %rax
pushq %r15
pushq %r15
pushq %rbx
callq 0x59c3
addq $0x60, %rsp
testb $0x1, %bpl
je 0xd2bf
leaq 0x12d2d(%rip), %rdi     # 0x1ffd7
movl %r14d, %esi
movl %ebx, %edx
movl $0x3, %ecx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x5901
movq 0x10(%rsp), %rdi
callq 0x1030
testb $0x10, %bpl
je 0xd2f9
movl 0xc(%rsp), %eax
imull 0x8(%rsp), %eax
leal (%rax,%rax,2), %edx
testl %edx, %edx
jle 0xd2f9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x1c(%rsp), %ecx
shll %cl, %eax
movl %edx, %ecx
xorl %edx, %edx
andb %al, (%r12,%rdx)
incq %rdx
cmpl %edx, %ecx
jne 0xd2ee
testb $0x40, %bpl
je 0xd45c
movl 0xc(%rsp), %r15d
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss 0x4(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ebx
movl 0x8(%rsp), %r13d
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ebp
movl %ebx, %eax
imull %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %r14
movq %r14, %rdi
callq 0x1190
movq %rax, 0x10(%rsp)
movss 0xfcb5(%rip), %xmm0     # 0x1d008
ucomiss 0x4(%rsp), %xmm0
jbe 0xd3f0
movl %r15d, %eax
imull %r13d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, %r14
leaq 0x19de6(%rip), %r8      # 0x27160
movsd 0x126fe(%rip), %xmm0    # 0x1fa80
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %r15d, %esi
movl %r13d, %edx
movq %rax, %rcx
movl $0x4, %r9d
callq 0x7390
movl $0x1, %r10d
xorl %eax, %eax
movss 0xfc5c(%rip), %xmm2     # 0x1d008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
movq 0x10(%rsp), %r15
movq %r15, %r8
movl %ebx, %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 %rbp
callq 0x59c3
addq $0x60, %rsp
movq %r14, %rdi
callq 0x1030
jmp 0xd446
movq %r14, %rdi
callq 0x1190
movl $0x1, %r10d
xorl %r11d, %r11d
movss 0xfbff(%rip), %xmm2     # 0x1d008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r12, %rdi
movl %r15d, %esi
movq %rax, %r15
movl %r13d, %edx
xorl %ecx, %ecx
movq %rax, %r8
movl %ebx, %r9d
movaps %xmm2, %xmm3
pushq %r10
pushq %r10
pushq %r10
pushq %r11
pushq %r11
pushq %r11
pushq %r11
pushq $-0x1
pushq $0x3
pushq $0x0
pushq %r11
pushq %rbp
callq 0x59c3
addq $0x60, %rsp
movq %r12, %rdi
callq 0x1030
movl %ebx, 0xc(%rsp)
movl %ebp, 0x8(%rsp)
movq %r15, %r12
movl (%rsp), %ebp
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movq %r12, %rdi
movl 0x20(%rsp), %ecx
movq 0x28(%rsp), %r8
movl %ebp, %r9d
callq 0xca94
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x1312
jmp 0xd480
movq %r12, %rdi
callq 0x12f4
jmp 0xd480
leaq 0x12ff7(%rip), %rdi     # 0x204a3
leaq 0x12b03(%rip), %rsi     # 0x1ffb6
leaq 0x12b06(%rip), %rcx     # 0x1ffc0
movl $0x10e, %edx            # imm = 0x10E
callq 0x10e0