// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %r9, 0x10(%rsp)
movq %r8, 0x18(%rsp)
movl %ecx, %ebp
movl %edx, 0x8(%rsp)
movl %esi, %r12d
movq %rdi, %r15
pxor %xmm0, %xmm0
leaq 0x40(%rsp), %r14
movdqa %xmm0, (%r14)
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x72d1
movq %rax, %r13
movl 0x8(%rsp), %eax
movl %r12d, 0xc(%rsp)
imull %r12d, %eax
testl %eax, %eax
movq %rax, 0x28(%rsp)
cmovgl %eax, %ebx
movq %rbx, 0x20(%rsp)
movq %r15, %r12
subl $0x1, %ebx
jb 0xb987
movq %r13, %rdi
movq %r12, %rsi
callq 0x736b
movq %r14, %rdi
movq %rax, %rsi
callq 0x71fa
addq $0x3, %r12
jmp 0xb966
incl %ebp
leaq 0x40(%rsp), %r14
movslq 0x44(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl %ebp, %eax
jle 0xb9b9
movq %r14, %rdi
callq 0x728d
movq %rax, %rdi
callq 0x73fc
movq %r14, %rdi
movq %rax, %rsi
callq 0x71fa
jmp 0xb98e
pushq $0x1
popq %r14
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x103cf(%rip), %r12     # 0x1bd9d
cmpq 0x38(%rsp), %r14
jge 0xbaf2
movq 0x30(%rsp), %rax
movq (%rax,%r14,8), %rax
movl 0x18(%rax), %r9d
cvtsi2sd %r9d, %xmm0
movdqu (%rax), %xmm2
movdqa %xmm2, %xmm1
pand 0xd715(%rip), %xmm1     # 0x19110
por 0xd71d(%rip), %xmm1     # 0x19120
psrlq $0x20, %xmm2
por 0xd720(%rip), %xmm2     # 0x19130
subpd 0xd728(%rip), %xmm2     # 0x19140
addpd %xmm1, %xmm2
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
divpd %xmm1, %xmm2
addpd 0xd6d0(%rip), %xmm2     # 0x19100
cvttsd2si %xmm2, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
movapd %xmm2, %xmm1
movsd 0x100c0(%rip), %xmm4    # 0x1bb08
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 0xd6bc(%rip), %xmm2     # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xd6c4(%rip), %xmm2     # 0x19160
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
divsd %xmm0, %xmm3
addsd 0xffdc(%rip), %xmm3     # 0x1ba90
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
movq %r12, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
incq %r14
jmp 0xb9ce
movq 0x20(%rsp), %r12
movl %r12d, %ebp
movq %r15, %r14
subl $0x1, %ebp
jb 0xbb13
movq %r13, %rdi
movq %r14, %rsi
callq 0x7438
addq $0x3, %r14
jmp 0xbafd
testb $0x1, 0x10(%rsp)
je 0xbb37
leaq 0x1029e(%rip), %rdi     # 0x1bdbf
pushq $0x3
popq %rcx
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x5728
leaq 0x10290(%rip), %rsi     # 0x1bdce
movq 0x18(%rsp), %rdi
callq 0x11d0
movq %rax, %r14
testb $0x20, 0x10(%rsp)
movq %rax, 0x18(%rsp)
jne 0xbb83
leaq 0x10272(%rip), %rsi     # 0x1bdd0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1130
leaq 0x1027b(%rip), %rsi     # 0x1bdea
movq %r14, %rdi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
xorl %eax, %eax
callq 0x1130
jmp 0xbbb7
leaq 0x1027c(%rip), %rsi     # 0x1be06
movq %r14, %rdi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %edx, %r8d
movl %ecx, %r9d
xorl %eax, %eax
callq 0x1130
leaq 0x102c2(%rip), %rdi     # 0x1be6b
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1210
movq 0x28(%rsp), %rax
leal (%rax,%rax,2), %ebp
imull $0x6, %eax, %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, %r13
movslq %ebp, %rax
movq %r13, %rcx
movq %rax, 0x28(%rsp)
addq %rax, %rcx
movq %rcx, 0x58(%rsp)
movl %r12d, %eax
leaq (%rax,%rax,2), %r14
pushq $0x1
popq %r12
cmpq 0x38(%rsp), %r12
jge 0xbd14
movq %r13, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x10e0
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rbp
xorl %eax, %eax
cmpq %rax, %r14
je 0xbc56
movq (%rbp), %rcx
movzbl (%r15,%rax), %edx
cmpq %rdx, %rcx
jne 0xbc50
movzbl 0x1(%r15,%rax), %edx
cmpq %rdx, 0x8(%rbp)
jne 0xbc50
movzbl 0x2(%r15,%rax), %edx
cmpq %rdx, 0x10(%rbp)
jne 0xbc50
movb %cl, (%r13,%rax)
movb 0x8(%rbp), %cl
movb %cl, 0x1(%r13,%rax)
movb 0x10(%rbp), %cl
movb %cl, 0x2(%r13,%rax)
addq $0x3, %rax
jmp 0xbc10
testb $0x1, 0x10(%rsp)
je 0xbc91
movl $0x100, %esi            # imm = 0x100
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x10229(%rip), %rdx     # 0x1be9a
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
pushq $0x3
popq %rcx
movq %r13, %r8
callq 0x3246
testb $0x4, 0x10(%rsp)
je 0xbcb6
cmpq $0xff, (%rbp)
jne 0xbcb6
cmpq $0xff, 0x8(%rbp)
jne 0xbcb6
cmpq $0xff, 0x10(%rbp)
je 0xbd0c
movq %r13, %rsi
testb $0x2, 0x10(%rsp)
je 0xbce8
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
pushq $0x3
popq %rcx
movq %r12, 0x20(%rsp)
movq 0x58(%rsp), %rbx
movq %rbx, %r8
callq 0x6c0d
movq %rbx, %rsi
movq 0x20(%rsp), %r12
movl (%rbp), %r8d
movl 0x8(%rbp), %r9d
movl 0x10(%rbp), %eax
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
pushq 0x10(%rsp)
pushq %rax
callq 0xb3b4
popq %rcx
popq %rdx
incq %r12
jmp 0xbbeb
movq %r13, %rdi
callq 0x1030
testb $0x20, 0x10(%rsp)
jne 0xbd3b
leaq 0x10183(%rip), %rsi     # 0x1bead
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1130
jmp 0xbd55
leaq 0x10172(%rip), %rdi     # 0x1beb4
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x1210
movq %rbx, %rdi
callq 0x10a0
callq 0x7347
movq 0x30(%rsp), %rdi
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030