// File: /workspace/github_c_makefile_star2_O1/yui0[P]img2vec/img2vec.c
// Function: color_quant
void color_quant(unsigned char *im, int w, int h, int n_colors, char *name, int flag)
{
	int i;
	unsigned char *pix = im;
	node_heap heap = { 0, 0, 0 };

	oct_node root = node_new(0, 0, 0);
	for (i=0; i < w * h; i++, pix += 3) {
		heap_add(&heap, node_insert(root, pix));
	}

	while (heap.n > n_colors + 1) {
		heap_add(&heap, node_fold(pop_heap(&heap)));
	}

	for (i=1; i < heap.n; i++) {
		oct_node got = heap.buf[i];
		double c = got->count;
		got->r = got->r / c + .5;
		got->g = got->g / c + .5;
		got->b = got->b / c + .5;
		printf("%2d | %3lu %3lu %3lu (%d pixels)\n",
		       i, got->r, got->g, got->b, got->count);
	}

	for (i=0, pix = im; i < w * h; i++, pix += 3) {
		color_replace(root, pix);
	}

	// output
	if (flag&1) stbi_write_jpg("posterized.jpg", w, h, 3, im, 0);
	FILE *fp = fopen(name, "w");
	if (!(flag&32)) fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
	if (!(flag&32)) fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", w, h);
	if (flag&32) fprintf(fp, "<svg id=\"illust\" xmlns=\"http://www.w3.org/2000/svg\" width=\"%dpx\" height=\"%dpx\" viewBox=\"0 0 %d %d\">\n", w, h, w, h);
	if (flag&32) fprintf(fp, "<!-- Generator: img2vec by Yuichiro Nakada -->");
	uint8_t *img = malloc(w * h *3*2);
	for (i=1; i < heap.n; i++) {
		memset(img, 0, w * h *3);
		oct_node got = heap.buf[i];
		int n;
		for (n=0, pix = im; n < w * h; n++, pix += 3) {
			if (got->r==pix[0] && got->g==pix[1] && got->b==pix[2]) {
				img[n*3] = got->r;
				img[n*3+1] = got->g;
				img[n*3+2] = got->b;
				/*img[n*3] = 255;
				img[n*3+1] = 255;
				img[n*3+2] = 255;*/
			}
		}
		if (flag&1) { // debug
			char str[256];
			snprintf(str, sizeof(str), "original_d%02d.bmp", i);
			stbi_write_bmp(str, w, h, 3, img);
		}
		if (flag&4) { // alpha
			if (got->r==255 && got->g==255 && got->b==255) continue;
		}
		if (flag&2) { // dilate
			imgp_dilate(img, w, h, 3, img+w * h *3);
			img2vec(fp, img+w * h *3, w, h, got->r, got->g, got->b, flag);
		} else {
			img2vec(fp, img, w, h, got->r, got->g, got->b, flag);
		}
	}
	free(img);
	if (!(flag&32)) fprintf(fp, "%%EOF\n");
	if (flag&32) fprintf(fp, "</svg>\n");
	fclose(fp);

	node_free();
	free(heap.buf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r9, 0x8(%rsp)
movq %r8, 0x18(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movl %esi, %r15d
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7855
movq %rax, %r13
movl %r14d, 0x4(%rsp)
movl %r14d, %ecx
movl %r15d, (%rsp)
imull %r15d, %ecx
movq %rcx, 0x10(%rsp)
testl %ecx, %ecx
jle 0xcb1a
leaq 0x20(%rsp), %r14
movq 0x10(%rsp), %rax
movl %eax, %r12d
movq %rbx, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x78f7
movq %r14, %rdi
movq %rax, %rsi
callq 0x7766
addq $0x3, %r15
decl %r12d
jne 0xcafb
incl %ebp
movl 0x24(%rsp), %r15d
cmpl %ebp, %r15d
jle 0xcb50
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x7807
movq %rax, %rdi
callq 0x798b
movq %r14, %rdi
movq %rax, %rsi
callq 0x7766
movl 0x24(%rsp), %r15d
cmpl %ebp, %r15d
jg 0xcb2b
cmpl $0x2, %r15d
jl 0xcc70
movq 0x28(%rsp), %r12
movl %r15d, %ebp
movl $0x1, %r14d
movq (%r12,%r14,8), %rax
movl 0x18(%rax), %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movsd (%rax), %xmm1
movsd 0x1057c(%rip), %xmm3    # 0x1d100
unpcklps %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x10581(%rip), %xmm4    # 0x1d110
subpd %xmm4, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movsd 0x12e85(%rip), %xmm5    # 0x1fa30
addsd %xmm5, %xmm2
cvttsd2si %xmm2, %rcx
movq %rcx, %rsi
movsd 0x12f19(%rip), %xmm6    # 0x1fad8
subsd %xmm6, %xmm2
cvttsd2si %xmm2, %rdx
sarq $0x3f, %rsi
andq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, (%rax)
movsd 0x8(%rax), %xmm1
unpcklps %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
addsd %xmm5, %xmm2
cvttsd2si %xmm2, %rsi
movq %rsi, %rdi
sarq $0x3f, %rdi
subsd %xmm6, %xmm2
cvttsd2si %xmm2, %rcx
andq %rdi, %rcx
orq %rsi, %rcx
movq %rcx, 0x8(%rax)
movsd 0x10(%rax), %xmm1
unpcklps %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
addsd %xmm5, %xmm2
cvttsd2si %xmm2, %rsi
movq %rsi, %rdi
subsd %xmm6, %xmm2
cvttsd2si %xmm2, %r8
sarq $0x3f, %rdi
andq %rdi, %r8
orq %rsi, %r8
movq %r8, 0x10(%rax)
leaq 0x1310b(%rip), %rdi     # 0x1fd65
movl %r14d, %esi
xorl %eax, %eax
callq 0x10c0
incq %r14
cmpq %r14, %rbp
jne 0xcb68
cmpl $0x0, 0x10(%rsp)
jle 0xcc94
movq 0x10(%rsp), %rax
movl %eax, %ebp
movq %rbx, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x79c8
addq $0x3, %r14
decl %ebp
jne 0xcc81
testb $0x1, 0x8(%rsp)
je 0xccb9
leaq 0x130e5(%rip), %rdi     # 0x1fd87
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x5901
leaq 0x130d6(%rip), %rsi     # 0x1fd96
movq 0x18(%rsp), %rdi
callq 0x11e0
movq %rax, %r14
testb $0x20, 0x8(%rsp)
movq %rax, 0x18(%rsp)
jne 0xcd04
leaq 0x130b8(%rip), %rsi     # 0x1fd98
movq %r14, %rdi
xorl %eax, %eax
callq 0x1140
leaq 0x130c1(%rip), %rsi     # 0x1fdb2
movq %r14, %rdi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x1140
jmp 0xcd3b
leaq 0x130c3(%rip), %rsi     # 0x1fdce
movq %r14, %rdi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
movl %edx, %r8d
movl %ecx, %r9d
xorl %eax, %eax
callq 0x1140
leaq 0x1310a(%rip), %rdi     # 0x1fe33
movl $0x2e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x1220
movq 0x10(%rsp), %rax
addl %eax, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, %r13
cmpl $0x2, %r15d
jl 0xceb2
movq 0x10(%rsp), %rcx
leal (%rcx,%rcx,2), %eax
movslq %eax, %rdx
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r13, %rax
movq %rdx, 0x40(%rsp)
addq %rdx, %rax
movq %rax, 0x30(%rsp)
movl %r15d, %eax
movq %rax, 0x38(%rsp)
movl %ecx, %eax
leaq (%rax,%rax,2), %r14
movl $0x1, %r12d
movq %r13, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x10f0
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %r15
cmpl $0x0, 0x10(%rsp)
jle 0xcdf6
xorl %eax, %eax
movq (%r15), %rcx
movzbl (%rbx,%rax), %edx
cmpq %rdx, %rcx
jne 0xcded
movzbl 0x1(%rbx,%rax), %edx
cmpq %rdx, 0x8(%r15)
jne 0xcded
movzbl 0x2(%rbx,%rax), %edx
cmpq %rdx, 0x10(%r15)
jne 0xcded
movb %cl, (%r13,%rax)
movb 0x8(%r15), %cl
movb %cl, 0x1(%r13,%rax)
movb 0x10(%r15), %cl
movb %cl, 0x2(%r13,%rax)
addq $0x3, %rax
cmpq %rax, %r14
jne 0xcdb4
testb $0x1, 0x8(%rsp)
je 0xce32
movl $0x100, %esi            # imm = 0x100
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
leaq 0x13051(%rip), %rdx     # 0x1fe62
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10d0
movq %rbp, %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq %r13, %r8
callq 0x3244
testb $0x4, 0x8(%rsp)
je 0xce56
cmpq $0xff, (%r15)
jne 0xce56
cmpq $0xff, 0x8(%r15)
jne 0xce56
cmpq $0xff, 0x10(%r15)
je 0xcea4
movq %r13, %rsi
testb $0x2, 0x8(%rsp)
je 0xce7f
movq %r13, %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq 0x30(%rsp), %rbp
movq %rbp, %r8
callq 0x70ee
movq %rbp, %rsi
movl (%r15), %r8d
movl 0x8(%r15), %r9d
movl 0x10(%r15), %eax
movq 0x18(%rsp), %rdi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
pushq 0x8(%rsp)
pushq %rax
callq 0xc535
addq $0x10, %rsp
incq %r12
cmpq 0x38(%rsp), %r12
jne 0xcd93
movq %r13, %rdi
callq 0x1030
testb $0x20, 0x8(%rsp)
jne 0xced9
leaq 0x12fad(%rip), %rsi     # 0x1fe75
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1140
jmp 0xcef7
leaq 0x12f9c(%rip), %rdi     # 0x1fe7c
movl $0x7, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x1220
movq %rbx, %rdi
callq 0x10b0
movq 0x1a33a(%rip), %rdi     # 0x27240
testq %rdi, %rdi
je 0xcf23
movq 0x60(%rdi), %rbx
callq 0x1030
movq %rbx, 0x1a325(%rip)     # 0x27240
movq %rbx, %rdi
testq %rbx, %rbx
jne 0xcf0b
movq 0x28(%rsp), %rdi
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030