// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %rdi, %rbx
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7940
movq %rax, %r13
movl %r14d, 0x4(%rsp)
movl %r14d, %ecx
movl %r12d, (%rsp)
imull %r12d, %ecx
movq %rcx, 0x10(%rsp)
testl %ecx, %ecx
jle 0xca61
leaq 0x20(%rsp), %r14
movq 0x10(%rsp), %rax
movl %eax, %r12d
movq %rbx, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x79df
movq %r14, %rdi
movq %rax, %rsi
callq 0x7856
addq $0x3, %r15
decl %r12d
jne 0xca3d
movl 0x24(%rsp), %r15d
incl %ebp
cmpl %ebp, %r15d
jle 0xca92
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x78f9
movq %rax, %rdi
callq 0x7a6a
movq %r14, %rdi
movq %rax, %rsi
callq 0x7856
movl 0x24(%rsp), %r15d
cmpl %ebp, %r15d
jg 0xca6d
cmpl $0x2, %r15d
jl 0xcbc9
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
movdqu (%rax), %xmm2
movdqa %xmm2, %xmm1
pand 0x116b6(%rip), %xmm1    # 0x1e180
por 0x116be(%rip), %xmm1    # 0x1e190
psrlq $0x20, %xmm2
por 0x116c1(%rip), %xmm2    # 0x1e1a0
subpd 0x116c9(%rip), %xmm2    # 0x1e1b0
addpd %xmm1, %xmm2
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
divpd %xmm1, %xmm2
addpd 0x11661(%rip), %xmm2    # 0x1e160
cvttsd2si %xmm2, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
movapd %xmm2, %xmm1
movsd 0x14101(%rip), %xmm4    # 0x20c18
subsd %xmm4, %xmm1
cvttsd2si %xmm1, %rdx
andq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
cvttsd2si %xmm2, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
subsd %xmm4, %xmm2
cvttsd2si %xmm2, %rdi
andq %rsi, %rdi
orq %rcx, %rdi
movq %rdi, %xmm2
punpcklqdq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0]
movdqu %xmm1, (%rax)
movsd 0x10(%rax), %xmm2
unpcklps 0x1165d(%rip), %xmm2    # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x11665(%rip), %xmm2    # 0x1e1d0
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
divsd %xmm0, %xmm3
addsd 0x1401d(%rip), %xmm3    # 0x20ba0
cvttsd2si %xmm3, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
subsd %xmm4, %xmm3
cvttsd2si %xmm3, %r8
andq %rsi, %r8
orq %rcx, %r8
movq %r8, 0x10(%rax)
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
movq %xmm0, %rcx
leaq 0x142f2(%rip), %rdi     # 0x20ea5
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
incq %r14
cmpq %r14, %rbp
jne 0xcaaa
cmpl $0x0, 0x10(%rsp)
jle 0xcbed
movq 0x10(%rsp), %rax
movl %eax, %ebp
movq %rbx, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x7aa9
addq $0x3, %r14
decl %ebp
jne 0xcbda
testb $0x1, 0x8(%rsp)
je 0xcc12
leaq 0x142cc(%rip), %rdi     # 0x20ec7
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x5a28
leaq 0x142bd(%rip), %rsi     # 0x20ed6
movq 0x18(%rsp), %rdi
callq 0x11d0
movq %rax, %r14
testb $0x20, 0x8(%rsp)
movq %rax, 0x18(%rsp)
jne 0xcc5d
leaq 0x1429f(%rip), %rsi     # 0x20ed8
movq %r14, %rdi
xorl %eax, %eax
callq 0x1130
leaq 0x142a8(%rip), %rsi     # 0x20ef2
movq %r14, %rdi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x1130
jmp 0xcc94
leaq 0x142aa(%rip), %rsi     # 0x20f0e
movq %r14, %rdi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
movl %edx, %r8d
movl %ecx, %r9d
xorl %eax, %eax
callq 0x1130
leaq 0x142f1(%rip), %rdi     # 0x20f73
movl $0x2e, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x1210
movq 0x10(%rsp), %rax
addl %eax, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, %r13
cmpl $0x2, %r15d
jl 0xce0b
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 0x10e0
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %r15
cmpl $0x0, 0x10(%rsp)
jle 0xcd4f
xorl %eax, %eax
movq (%r15), %rcx
movzbl (%rbx,%rax), %edx
cmpq %rdx, %rcx
jne 0xcd46
movzbl 0x1(%rbx,%rax), %edx
cmpq %rdx, 0x8(%r15)
jne 0xcd46
movzbl 0x2(%rbx,%rax), %edx
cmpq %rdx, 0x10(%r15)
jne 0xcd46
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 0xcd0d
testb $0x1, 0x8(%rsp)
je 0xcd8b
movl $0x100, %esi            # imm = 0x100
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
leaq 0x14238(%rip), %rdx     # 0x20fa2
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq %r13, %r8
callq 0x33b7
testb $0x4, 0x8(%rsp)
je 0xcdaf
cmpq $0xff, (%r15)
jne 0xcdaf
cmpq $0xff, 0x8(%r15)
jne 0xcdaf
cmpq $0xff, 0x10(%r15)
je 0xcdfd
movq %r13, %rsi
testb $0x2, 0x8(%rsp)
je 0xcdd8
movq %r13, %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x3, %ecx
movq 0x30(%rsp), %rbp
movq %rbp, %r8
callq 0x7205
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 0xc438
addq $0x10, %rsp
incq %r12
cmpq 0x38(%rsp), %r12
jne 0xccec
movq %r13, %rdi
callq 0x1030
testb $0x20, 0x8(%rsp)
jne 0xce32
leaq 0x14194(%rip), %rsi     # 0x20fb5
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1130
jmp 0xce50
leaq 0x14183(%rip), %rdi     # 0x20fbc
movl $0x7, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x1210
movq %rbx, %rdi
callq 0x10a0
movq 0x1b3c1(%rip), %rdi     # 0x28220
testq %rdi, %rdi
je 0xce7c
movq 0x60(%rdi), %rbx
callq 0x1030
movq %rbx, 0x1b3ac(%rip)     # 0x28220
movq %rbx, %rdi
testq %rbx, %rbx
jne 0xce64
movq 0x28(%rsp), %rdi
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030