// File: /workspace/github_cpp_makefile_star2_O3/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::skeletonize(Image&, Image&)
void ImageProc::skeletonize(Image &img_binary, Image &img_skeletonized)
{
	Image img_after_first("bw", img_binary.rows, img_binary.cols);

		int index  = 0, superflag = 1, flag = 0, black_pixel_count = 0, pixel_count = 0;
		std::string target;
		//char *target_char_arr = new char[9];

		for(int i = 0; i < img_binary.rows; i++)
			for(int j = 0; j < img_binary.cols; j++)
				for(int k = 0; k < img_binary.channels; k++)
					img_skeletonized.setvalue(i,j,k,img_binary.getvalue(i,j,k));

		for(int i = 0+1; i < img_skeletonized.rows-1; i++)
			for(int j = 0+1; j < img_skeletonized.cols-1; j++)
				for(int k = 0; k < img_skeletonized.channels; k++)
					if(img_skeletonized.getvalue(i,j,k) == 0) pixel_count++;
		black_pixel_count = pixel_count;

		//std::cout << " 1 " << black_pixel_count << std::endl;

		while(superflag == 1)
		{
			for(int i = 0; i < img_skeletonized.rows; i++)
				for(int j = 0; j < img_skeletonized.cols; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
						img_after_first.setvalue(i,j,k,255);

			for(int i = 0+1; i < img_skeletonized.rows-1; i++)
				for(int j = 0+1; j < img_skeletonized.cols-1; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
					{
						if(img_skeletonized.getvalue(i,j,k) == 0)
						{
							index = -1; flag = 0;
							target.clear();
							for(int p = i-1; p <= i+1; p++)
								for(int q = j-1; q <= j+1; q++)
								{
									//std::cout << img_shrinked.getvalue(p,q,k)/255 << std::endl;
									if(!(p==i && q==j))
										if(img_skeletonized.getvalue(p,q,0) == 0)
											target.append("1");
										else
											target.append("0");
									else
										continue;
								}
							//target_char_arr[8] = NULL;
							//target(target_char_arr);
							//std::cout << "target 1:" << target << std::endl;

							//std::string target_str(target);
							for(int t = 0; t < SKELETON_FIRST_PATTERNS; t++)
							{
								std::string testpattern(skeleton_first[t]);
								flag = 0;
								if(testpattern.find('D') == std::string::npos)
								{
									if(target.compare(testpattern) == 0)
									{
										//std::cout << "pattern match" << std::endl;
										flag = 1;
									}
									else
									{
										//std::cout << "pattern nomatch" << std::endl;
										flag = 0;
									}
								}
								else
								{
									for(int j = 0; j < 8; j++)
									{
										if(testpattern[j] == 'D')
											continue;
										else if(target[j] != testpattern[j])
										{
											//std::cout << target << " hey " << testpattern << std::endl;
											flag = 0;
											break;
										}
										else
											flag = 1;
									}
								}

								if(flag == 1)
									break;
								else
									continue;

							}

							if(flag == 1)
								img_after_first.setvalue(i,j,k,0*255);
							else
								img_after_first.setvalue(i,j,k,1*255);

						}
					}

				for(int i = 0+1; i < img_after_first.rows-1; i++)
					for(int j = 0+1; j < img_after_first.cols-1; j++)
						for(int k = 0; k < img_after_first.channels; k++)
						{
							if(img_after_first.getvalue(i,j,k) == 0)
							{
								target.clear();
								index = -1; flag = 0;
								for(int p = i-1; p <= i+1; p++)
									for(int q = j-1; q <= j+1; q++)
									{
										if(!(p==i && q==j))
											if(img_after_first.getvalue(p,q,0) == 0)
												target.append("M");
											else
												target.append("0");
									}

								//std::cout << "target1: " << target;

								for(int t = 0; t < SKELETON_SECOND_PATTERNS; t++)
								{
									std::string testpattern(skeleton_second[t]);
									flag = 1;
									//std::cout << " " << testpattern << std::endl;
									if(testpattern.find('D') == std::string::npos)
									{

										if(target.compare(testpattern) == 0)
										{
											//std::cout << "hi" << std::endl;
											flag = 1;
											break;
										}
										else
											flag = 0;
									}
									else
									{
										for(int j = 0; j < 8; j++)
										{
											if(testpattern[j] == 'D')
												continue;
											else if(target[j] == testpattern[j])
											{
												flag = 1;
												//std::cout << "match" << std::endl;
											}
											else
											{
												//std::cout << "breakpoint" << std::endl;
												flag = 0;
												break;
											}
										}
									}

									if(flag == 0)
										continue;
									else
										break;
								}
								//std::cout << flag << std::endl;
								if(flag == 0)
								{
									//std::cout << "reducing" << std::endl;
									img_skeletonized.setvalue(i,j,k,1*255);
								}
								else
									img_skeletonized.setvalue(i,j,k,img_skeletonized.getvalue(i,j,k));
							}


						}

			pixel_count = 0;
			for(int i = 0+1; i < img_skeletonized.rows-1; i++)
				for(int j = 0+1; j < img_skeletonized.cols-1; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
						if(img_skeletonized.getvalue(i,j,k) == 0) pixel_count++;



			if(pixel_count == black_pixel_count) superflag = 0;
			else								 black_pixel_count = pixel_count;
			//std::cout << black_pixel_count << std::endl;


		}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %edx
leaq 0x2d45(%rip), %rsi      # 0x12253
leaq 0xc8(%rsp), %rdi
callq 0x4c56
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0xc(%r14), %ecx
testl %ecx, %ecx
jle 0xf59a
movl 0x8(%r14), %edx
xorl %eax, %eax
testl %edx, %edx
jle 0xf594
movl 0x10(%r14), %esi
xorl %ecx, %ecx
testl %esi, %esi
jle 0xf58a
xorl %edx, %edx
movl 0x8(%r14), %edi
imull %eax, %edi
addl %ecx, %edi
imull %esi, %edi
movslq %edi, %rsi
addq 0x18(%r14), %rsi
movl 0x8(%rbx), %edi
imull %eax, %edi
addl %ecx, %edi
imull 0x10(%rbx), %edi
movb (%rdx,%rsi), %sil
movslq %edi, %rdi
addq 0x18(%rbx), %rdi
movb %sil, (%rdx,%rdi)
movl 0x10(%r14), %esi
incq %rdx
cmpl %esi, %edx
jl 0xf54d
movl 0x8(%r14), %edx
incl %ecx
cmpl %edx, %ecx
jl 0xf547
movl 0xc(%r14), %ecx
incl %eax
cmpl %ecx, %eax
jl 0xf53d
movq %rbx, 0x8(%rsp)
movl 0xc(%rbx), %eax
xorl %r12d, %r12d
cmpl $0x3, %eax
jl 0xf61a
leal -0x1(%rax), %ecx
movq 0x8(%rsp), %r8
movl 0x8(%r8), %edx
movl 0x10(%r8), %esi
leal -0x1(%rdx), %edi
movq 0x18(%r8), %r8
leal 0x1(%rdx), %r9d
imull %esi, %r9d
movl %edx, %r10d
imull %esi, %r10d
xorl %r12d, %r12d
movl $0x1, %r11d
cmpl $0x3, %edx
jl 0xf60f
movl $0x1, %r14d
movl %r9d, %ebp
testl %esi, %esi
jle 0xf605
movslq %ebp, %rbx
addq %r8, %rbx
xorl %r15d, %r15d
cmpb $0x1, (%rbx,%r15)
adcl $0x0, %r12d
incq %r15
cmpq %r15, %rsi
jne 0xf5f4
incq %r14
addl %esi, %ebp
cmpq %rdi, %r14
jne 0xf5e7
incq %r11
addl %r10d, %r9d
cmpq %rcx, %r11
jne 0xf5d9
movq 0xe0(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movl 0xd0(%rsp), %ecx
movl 0xd4(%rsp), %edx
movl 0xd8(%rsp), %ebp
movq %rdx, 0xa0(%rsp)
decl %edx
movq %rdx, 0xb0(%rsp)
leal -0x2(%rcx), %edx
movq %rdx, 0xc0(%rsp)
movl %ebp, %edx
movq %rcx, 0x78(%rsp)
imull %ecx, %edx
movl %edx, 0x1c(%rsp)
movq %r12, 0xa8(%rsp)
testl %eax, %eax
jle 0xf948
movq 0x8(%rsp), %rbx
movl 0x8(%rbx), %esi
xorl %ecx, %ecx
xorl %edx, %edx
testl %esi, %esi
jle 0xf6c3
movl 0x10(%rbx), %r8d
movl %ecx, %eax
xorl %edi, %edi
testl %r8d, %r8d
jle 0xf6b3
movslq %eax, %rsi
addq 0x30(%rsp), %rsi
xorl %r9d, %r9d
movb $-0x1, (%rsi,%r9)
incq %r9
movslq 0x10(%rbx), %r8
cmpq %r8, %r9
jl 0xf69f
movl 0x8(%rbx), %esi
incq %rdi
movslq %esi, %r9
addl %ebp, %eax
cmpq %r9, %rdi
jl 0xf68f
movl 0xc(%rbx), %eax
incl %edx
addl 0x1c(%rsp), %ecx
cmpl %eax, %edx
jl 0xf683
cmpl $0x3, %eax
jl 0xf94d
movl 0x8(%rbx), %ecx
movl $0x1, %r14d
movl $0x3, 0x4(%rsp)
cmpl $0x3, %ecx
jl 0xf933
leal -0x1(%r14), %eax
movl %eax, 0x18(%rsp)
movq 0x78(%rsp), %rax
imull %r14d, %eax
movq %rax, 0x20(%rsp)
movl 0x10(%rbx), %eax
movl $0x1, %esi
xorl %r15d, %r15d
testl %eax, %eax
jle 0xf91b
movq 0x20(%rsp), %rcx
addl %esi, %ecx
imull %ebp, %ecx
movslq %ecx, %rcx
addq 0x30(%rsp), %rcx
movq %rcx, 0x80(%rsp)
xorl %edx, %edx
movq %rsi, 0x28(%rsp)
movl 0x8(%rbx), %ecx
imull %r14d, %ecx
addl %esi, %ecx
imull %eax, %ecx
movslq %ecx, %rax
movq 0x18(%rbx), %rcx
addq %rdx, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0xf908
movq %rdx, 0x88(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x38(%rsp), %rax
movb $0x0, (%rax)
movl 0x18(%rsp), %r12d
xorl %r13d, %r13d
cmpl %r14d, %r12d
sete %al
cmpq $0x1, %r13
sete %cl
testb %cl, %al
jne 0xf7c2
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %r12d, %ecx
addl %r15d, %ecx
addl %r13d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x22ba(%rip), %rsi      # 0x11a69
je 0xf7b8
leaq 0x25fb(%rip), %rsi      # 0x11db3
leaq 0x38(%rsp), %rdi
callq 0x22e0
incq %r13
cmpl $0x3, %r13d
jne 0xf77b
incl %r12d
cmpl 0x4(%rsp), %r12d
jne 0xf778
xorl %r13d, %r13d
movq %r13, %rax
shlq $0x5, %rax
leaq 0x68(%rsp), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x7750(%rip), %rcx      # 0x16f40
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x10b98
movq %r12, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xf853
movq 0x58(%rsp), %r12
xorl %eax, %eax
movq 0x38(%rsp), %rcx
xorl %edx, %edx
movb (%r12,%rax), %sil
cmpb $0x44, %sil
je 0xf845
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xf8ce
incq %rax
cmpq $0x8, %rax
jne 0xf82c
cmpl $0x1, %edx
jmp 0xf899
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xf896
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0x90(%rsp)
callq 0x2190
testl %eax, %eax
movq 0x90(%rsp), %rax
je 0xf896
xorl %ebx, %ebx
jmp 0xf89c
cmpq %rbx, %rax
sete %bl
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xf8b6
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
testb %bl, %bl
movq 0x8(%rsp), %rbx
jne 0xf8ee
incq %r13
cmpq $0x28, %r13
jne 0xf7d8
jmp 0xf8ea
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xf8bf
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
jmp 0xf8bf
movb $-0x1, %al
jmp 0xf8f0
xorl %eax, %eax
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rdx
movb %al, (%rcx,%rdx)
movq 0x28(%rsp), %rsi
incq %rdx
movslq 0x10(%rbx), %rax
cmpq %rax, %rdx
jl 0xf73a
movl 0x8(%rbx), %ecx
incq %rsi
leal -0x1(%rcx), %edx
movslq %edx, %rdx
incq %r15
cmpq %rdx, %rsi
jl 0xf711
movl 0xc(%rbx), %eax
incl %r14d
leal -0x1(%rax), %edx
incl 0x4(%rsp)
cmpl %edx, %r14d
jl 0xf6e7
jmp 0xf94d
movq 0x8(%rsp), %rbx
cmpl $0x3, 0xa0(%rsp)
jl 0xfbe7
movl $0x3, 0x4(%rsp)
movl $0x1, %r15d
movl $0x0, 0x14(%rsp)
cmpl $0x3, 0x78(%rsp)
jl 0xfbc3
movq %r15, %rax
imulq 0x78(%rsp), %rax
movq %rax, 0xb8(%rsp)
leal -0x1(%r15), %eax
movl %eax, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x28(%rsp)
movq $0x0, 0x98(%rsp)
testl %ebp, %ebp
jle 0xfb99
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
imull %ebp, %eax
movslq %eax, %rdx
addq 0x30(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x80(%rsp)
cmpb $0x0, (%rdx,%rsi)
jne 0xfb8d
movq %rsi, 0x88(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x38(%rsp), %rax
movb $0x0, (%rax)
movl 0x28(%rsp), %ebx
movl 0x18(%rsp), %eax
movl %eax, %r13d
movl %r13d, %r13d
movq $-0x1, %r14
movl %ebx, %r12d
movq %r15, %rax
xorq %r13, %rax
orq %r14, %rax
je 0xfa4b
movslq %r12d, %rax
movq 0x30(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x2033(%rip), %rsi      # 0x11a6b
je 0xfa41
leaq 0x2372(%rip), %rsi      # 0x11db3
leaq 0x38(%rsp), %rdi
callq 0x22e0
incq %r14
addl %ebp, %r12d
cmpq $0x2, %r14
jne 0xfa1a
incl %r13d
addl 0x1c(%rsp), %ebx
cmpl 0x4(%rsp), %r13d
jne 0xfa0d
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq %r13, %rax
shlq $0x5, %rax
leaq 0x68(%rsp), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x7fdb(%rip), %rcx      # 0x17a60
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x10b98
movq %r14, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xfadb
movq 0x58(%rsp), %r12
movq 0x38(%rsp), %rax
xorl %ecx, %ecx
movb (%r12,%rcx), %dl
cmpb $0x44, %dl
je 0xfacd
cmpb %dl, (%rax,%rcx)
jne 0xfb2b
incq %rcx
cmpq $0x8, %rcx
jne 0xfabf
xorl %r14d, %r14d
jmp 0xfb2e
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xfb1d
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0x90(%rsp)
callq 0x2190
movb $0x1, %r14b
testl %eax, %eax
movq 0x90(%rsp), %rax
jne 0xfb24
cmpq %rbx, %rax
setne %r14b
movq 0x8(%rsp), %rbx
jmp 0xfb2e
movb $0x1, %r14b
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xfb48
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
cmpq $0x23, %r13
leaq 0x1(%r13), %r13
setb %al
testb %al, %r14b
jne 0xfa6d
testb %r14b, %r14b
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rsi
je 0xfb8d
movl 0x8(%rbx), %eax
imull %r15d, %eax
addl 0x20(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
cmpq %rbp, %rsi
jne 0xf9df
incq 0x20(%rsp)
movq 0x98(%rsp), %rcx
incq %rcx
addl %ebp, 0x28(%rsp)
movq %rcx, 0x98(%rsp)
cmpq 0xc0(%rsp), %rcx
jne 0xf9b3
incq %r15
incl 0x4(%rsp)
movl 0x14(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpq 0xb0(%rsp), %r15
jne 0xf971
movl 0xc(%rbx), %eax
xorl %r9d, %r9d
cmpl $0x3, %eax
jl 0xfc60
leal -0x1(%rax), %ecx
movq 0x8(%rsp), %r8
movl 0x8(%r8), %edx
movl 0x10(%r8), %esi
leal -0x1(%rdx), %edi
movq 0x18(%r8), %r8
leal 0x1(%rdx), %r10d
imull %esi, %r10d
movl %edx, %r11d
imull %esi, %r11d
xorl %r9d, %r9d
movl $0x1, %r14d
cmpl $0x3, %edx
jl 0xfc55
movl $0x1, %r15d
movl %r10d, %r12d
testl %esi, %esi
jle 0xfc4a
movslq %r12d, %rbx
addq %r8, %rbx
xorl %r13d, %r13d
cmpb $0x1, (%rbx,%r13)
adcl $0x0, %r9d
incq %r13
cmpq %r13, %rsi
jne 0xfc39
incq %r15
addl %esi, %r12d
cmpq %rdi, %r15
jne 0xfc2c
incq %r14
addl %r11d, %r10d
cmpq %rcx, %r14
jne 0xfc1e
cmpl 0xa8(%rsp), %r9d
movl %r9d, %r12d
jne 0xf667
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xfc8d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2180
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xfca5
jmp 0xfca5
jmp 0xfca5
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xfcc4
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x22a0