// File: /workspace/github_cpp_makefile_star2_O2/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 0x3c1c(%rip), %rsi      # 0x116b4
leaq 0xc8(%rsp), %rdi
callq 0x3fd6
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
cmpl 0xc(%r14), %eax
jge 0xdb0b
xorl %ecx, %ecx
cmpl 0x8(%r14), %ecx
jge 0xdb07
xorl %edx, %edx
movl 0x10(%r14), %esi
cmpl %esi, %edx
jge 0xdb03
movl 0x8(%r14), %edi
imull %eax, %edi
addl %ecx, %edi
imull %edi, %esi
movslq %esi, %rsi
addq 0x18(%r14), %rsi
movb (%rdx,%rsi), %sil
movl 0x8(%rbx), %edi
imull %eax, %edi
addl %ecx, %edi
imull 0x10(%rbx), %edi
movslq %edi, %rdi
addq 0x18(%rbx), %rdi
movb %sil, (%rdx,%rdi)
incq %rdx
jmp 0xdac8
incl %ecx
jmp 0xdac0
incl %eax
jmp 0xdab8
movl 0xc(%rbx), %ecx
movl 0x10(%rbx), %eax
movq %rcx, 0x10(%rsp)
leal -0x1(%rcx), %esi
movslq 0x8(%rbx), %rcx
movq %rcx, %rdi
decq %rdi
movq 0x18(%rbx), %r8
movq $0x0, 0x28(%rsp)
testl %eax, %eax
movl $0x0, %r9d
cmovgl %eax, %r9d
leal 0x1(%rcx), %r10d
imull %eax, %r10d
movl %ecx, %r11d
imull %eax, %r11d
pushq $0x1
popq %rbp
cmpl %esi, %ebp
jge 0xdb93
movl %r10d, %r15d
pushq $0x1
popq %r12
cmpq %rdi, %r12
jge 0xdb8c
movslq %r15d, %r13
addq %r8, %r13
xorl %r14d, %r14d
cmpq %r14, %r9
je 0xdb84
cmpb $0x1, (%r13,%r14)
movq 0x28(%rsp), %rdx
adcl $0x0, %edx
movq %rdx, 0x28(%rsp)
incq %r14
jmp 0xdb67
incq %r12
addl %eax, %r15d
jmp 0xdb59
incl %ebp
addl %r11d, %r10d
jmp 0xdb4e
movq 0xe0(%rsp), %rdx
movq %rdx, 0x30(%rsp)
movslq 0xd0(%rsp), %rdx
movl 0xd4(%rsp), %esi
movl 0xd8(%rsp), %r8d
decl %esi
movl %esi, 0x58(%rsp)
movq %rdx, %rsi
decq %rsi
movq %rsi, 0x90(%rsp)
xorl %edi, %edi
testl %r8d, %r8d
movl $0x0, %esi
cmovgl %r8d, %esi
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x80(%rsp)
movl %r8d, 0xc(%rsp)
imull %r8d, %edx
movl %edx, 0x1c(%rsp)
leaq 0x60(%rsp), %r15
testb $0x1, %dil
movq 0x10(%rsp), %r8
jne 0xe114
xorl %esi, %esi
xorl %edi, %edi
cmpl %r8d, %edi
jge 0xdc58
movl %esi, %edx
xorl %r8d, %r8d
movslq %ecx, %r9
cmpq %r9, %r8
jge 0xdc4c
movslq %edx, %rcx
addq 0x30(%rsp), %rcx
xorl %r9d, %r9d
movslq %eax, %r10
cmpq %r10, %r9
jge 0xdc40
movb $-0x1, (%rcx,%r9)
incq %r9
movl 0x10(%rbx), %eax
jmp 0xdc2b
incq %r8
movl 0x8(%rbx), %ecx
addl 0xc(%rsp), %edx
jmp 0xdc18
incl %edi
movl 0xc(%rbx), %r8d
addl 0x1c(%rsp), %esi
jmp 0xdc0e
pushq $0x3
popq %rdx
movl %edx, 0x10(%rsp)
pushq $0x1
popq %rdx
movl %edx, %r13d
decl %r8d
cmpl %r8d, %r13d
jge 0xde53
leal -0x1(%r13), %edx
movl %edx, 0x8(%rsp)
movq 0x80(%rsp), %rdx
imull %r13d, %edx
movq %rdx, 0x38(%rsp)
xorl %r12d, %r12d
pushq $0x1
popq %rsi
leal -0x1(%rcx), %edx
movslq %edx, %rdx
cmpq %rdx, %rsi
jge 0xde43
movq 0x38(%rsp), %rcx
addl %esi, %ecx
imull 0xc(%rsp), %ecx
movslq %ecx, %rcx
addq 0x30(%rsp), %rcx
movq %rcx, 0x40(%rsp)
xorl %edx, %edx
movq %rsi, 0x20(%rsp)
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0xde35
movl 0x8(%rbx), %ecx
imull %r13d, %ecx
addl %esi, %ecx
imull %eax, %ecx
movslq %ecx, %rax
movq 0x18(%rbx), %rcx
addq %rdx, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0xde2a
movq %rdx, 0x48(%rsp)
andq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %ebp
cmpl 0x10(%rsp), %ebp
je 0xdd5e
xorl %r14d, %r14d
cmpl $0x3, %r14d
je 0xdd5a
cmpl %r13d, %ebp
sete %al
cmpq $0x1, %r14
sete %cl
testb %cl, %al
jne 0xdd55
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %r12d, %ecx
addl %r14d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x2bcc(%rip), %rsi      # 0x10910
je 0xdd4d
leaq 0x2bb1(%rip), %rsi      # 0x108fe
movq %r15, %rdi
callq 0x22d0
incq %r14
jmp 0xdd0b
incl %ebp
jmp 0xdd02
xorl %ebp, %ebp
leaq 0xa8(%rsp), %r14
cmpq $0x28, %rbp
je 0xde12
movq %rbp, %rsi
shlq $0x5, %rsi
leaq 0x81b0(%rip), %rax      # 0x15f30
addq %rax, %rsi
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0xddd2
movq 0xa8(%rsp), %rax
xorl %ecx, %ecx
movq 0x60(%rsp), %rdx
xorl %esi, %esi
cmpq $0x8, %rcx
je 0xddcd
movb (%rax,%rcx), %dil
cmpb $0x44, %dil
je 0xddc8
pushq $0x1
popq %rsi
cmpb %dil, (%rdx,%rcx)
jne 0xde02
incq %rcx
jmp 0xddaf
cmpl $0x1, %esi
jmp 0xdddf
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
testl %eax, %eax
sete %r14b
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x20b0
testb %r14b, %r14b
movq %r15, %r14
leaq 0x60(%rsp), %r15
je 0xde0a
jmp 0xde16
movq %r14, %rdi
callq 0x20b0
incq %rbp
jmp 0xdd68
movb $-0x1, %al
jmp 0xde18
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
movb %al, (%rcx,%rdx)
movq 0x20(%rsp), %rsi
incq %rdx
movl 0x10(%rbx), %eax
jmp 0xdcbf
incq %rsi
movl 0x8(%rbx), %ecx
incq %r12
jmp 0xdc90
incl %r13d
incl 0x10(%rsp)
movl 0xc(%rbx), %r8d
jmp 0xdc65
xorl %ecx, %ecx
pushq $0x1
popq %rax
movl %eax, %edx
cmpl 0x58(%rsp), %eax
jge 0xe084
movq 0x80(%rsp), %rax
imull %edx, %eax
movq %rax, 0x88(%rsp)
leal -0x1(%rdx), %eax
movl %eax, 0x5c(%rsp)
movq %rdx, 0x10(%rsp)
leal 0x1(%rdx), %eax
movl %eax, 0x20(%rsp)
movl %ecx, 0x54(%rsp)
movl %ecx, 0x8(%rsp)
pushq $0x1
popq %rbp
xorl %r13d, %r13d
cmpq 0x90(%rsp), %rbp
jge 0xe073
movq 0x88(%rsp), %rax
addl %ebp, %eax
imull 0xc(%rsp), %eax
movq %rbp, 0x98(%rsp)
incq %rbp
movslq %eax, %rdx
addq 0x30(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x38(%rsp)
cmpq 0xa0(%rsp), %rsi
je 0xe05f
cmpb $0x0, (%rdx,%rsi)
jne 0xe057
movq %rsi, 0x40(%rsp)
andq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %r12d
movl 0x5c(%rsp), %r14d
cmpl 0x20(%rsp), %r14d
jg 0xdf6e
xorl %r15d, %r15d
movl %r12d, 0x48(%rsp)
leaq (%r15,%r13), %rax
cmpq %rbp, %rax
ja 0xdf5f
cmpl 0x10(%rsp), %r14d
sete %al
cmpq $0x1, %r15
sete %cl
testb %cl, %al
jne 0xdf55
movslq %r12d, %rax
movq 0x30(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x2bd7(%rip), %rsi      # 0x10b19
je 0xdf4b
leaq 0x29b3(%rip), %rsi      # 0x108fe
leaq 0x60(%rsp), %rdi
callq 0x22d0
addl 0xc(%rsp), %r12d
incq %r15
jmp 0xdf13
incl %r14d
movl 0x48(%rsp), %r12d
addl 0x1c(%rsp), %r12d
jmp 0xdf04
xorl %esi, %esi
xorl %r14d, %r14d
cmpq $0x24, %rsi
je 0xe023
movq %rsi, %r12
shlq $0x5, %rsi
leaq 0x8ac5(%rip), %rax      # 0x16a50
addq %rax, %rsi
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x21d0
leaq 0x60(%rsp), %r15
movq %r14, %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0xdfea
movq 0xa8(%rsp), %rax
movq 0x60(%rsp), %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
setne %r15b
je 0xdfe4
movb (%rax,%rdx), %sil
cmpb $0x44, %sil
je 0xdfdf
cmpb %sil, (%rcx,%rdx)
jne 0xe004
incq %rdx
jmp 0xdfc5
pushq $0x1
popq %r14
jmp 0xe007
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
xorl %r14d, %r14d
testl %eax, %eax
setne %r15b
sete %r14b
jmp 0xe007
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rdi
callq 0x20b0
movq %r12, %rsi
incq %rsi
testb %r15b, %r15b
jne 0xdf73
testl %r14d, %r14d
leaq 0x60(%rsp), %r15
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rsi
jne 0xe057
movl 0x8(%rbx), %eax
imull 0x10(%rsp), %eax
addl 0x98(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
jmp 0xdecf
incq %r13
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0xde98
movl 0x54(%rsp), %ecx
addl 0x1c(%rsp), %ecx
movl 0x20(%rsp), %eax
jmp 0xde58
movl 0xc(%rbx), %ecx
movl 0x10(%rbx), %eax
movq %rcx, 0x10(%rsp)
leal -0x1(%rcx), %edx
movslq 0x8(%rbx), %rcx
movq %rcx, %r8
decq %r8
movq 0x18(%rbx), %r9
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %r10d
cmovgl %eax, %r10d
leal 0x1(%rcx), %r11d
imull %eax, %r11d
movl %ecx, %ebp
imull %eax, %ebp
pushq $0x1
popq %r12
cmpl %edx, %r12d
jge 0xe100
movl %r11d, %r14d
pushq $0x1
popq %r15
cmpq %r8, %r15
jge 0xe0f3
movslq %r14d, %r13
addq %r9, %r13
xorl %edi, %edi
cmpq %rdi, %r10
je 0xe0eb
cmpb $0x1, (%r13,%rdi)
adcl $0x0, %esi
incq %rdi
jmp 0xe0d8
incq %r15
addl %eax, %r14d
jmp 0xe0cb
incl %r12d
addl %ebp, %r11d
leaq 0x60(%rsp), %r15
jmp 0xe0bf
cmpl 0x28(%rsp), %esi
sete %dil
movl %esi, %edx
movq %rdx, 0x28(%rsp)
jmp 0xdbfb
leaq 0x60(%rsp), %rdi
callq 0x20b0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe134
jmp 0xe14a
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x20b0
jmp 0xe14d
jmp 0xe14a
jmp 0xe14a
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2290
nop