// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::thin(Image&, Image&)
void ImageProc::thin(Image &img_binary, Image &img_thinned)
{
	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_thinned.setvalue(i,j,k,img_binary.getvalue(i,j,k));

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

		

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

			for(int i = 0+1; i < img_thinned.rows-1; i++)
				for(int j = 0+1; j < img_thinned.cols-1; j++)
					for(int k = 0; k < img_thinned.channels; k++)
					{
						if(img_thinned.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_thinned.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 < THIN_FIRST_PATTERNS; t++)
							{
								std::string testpattern(thin_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 < SHRINK_THIN_SECOND_PATTERNS; t++)
								{
									std::string testpattern(shrink__thin_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_thinned.setvalue(i,j,k,1*255);
								}
								else
									img_thinned.setvalue(i,j,k,img_thinned.getvalue(i,j,k));
							}


						}

			pixel_count = 0;
			for(int i = 0+1; i < img_thinned.rows-1; i++)
				for(int j = 0+1; j < img_thinned.cols-1; j++)
					for(int k = 0; k < img_thinned.channels; k++)
						if(img_thinned.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 0x4308(%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 0xd41f
xorl %ecx, %ecx
cmpl 0x8(%r14), %ecx
jge 0xd41b
xorl %edx, %edx
movl 0x10(%r14), %esi
cmpl %esi, %edx
jge 0xd417
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 0xd3dc
incl %ecx
jmp 0xd3d4
incl %eax
jmp 0xd3cc
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 0xd4a7
movl %r10d, %r15d
pushq $0x1
popq %r12
cmpq %rdi, %r12
jge 0xd4a0
movslq %r15d, %r13
addq %r8, %r13
xorl %r14d, %r14d
cmpq %r14, %r9
je 0xd498
cmpb $0x1, (%r13,%r14)
movq 0x28(%rsp), %rdx
adcl $0x0, %edx
movq %rdx, 0x28(%rsp)
incq %r14
jmp 0xd47b
incq %r12
addl %eax, %r15d
jmp 0xd46d
incl %ebp
addl %r11d, %r10d
jmp 0xd462
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 0xda28
xorl %esi, %esi
xorl %edi, %edi
cmpl %r8d, %edi
jge 0xd56c
movl %esi, %edx
xorl %r8d, %r8d
movslq %ecx, %r9
cmpq %r9, %r8
jge 0xd560
movslq %edx, %rcx
addq 0x30(%rsp), %rcx
xorl %r9d, %r9d
movslq %eax, %r10
cmpq %r10, %r9
jge 0xd554
movb $-0x1, (%rcx,%r9)
incq %r9
movl 0x10(%rbx), %eax
jmp 0xd53f
incq %r8
movl 0x8(%rbx), %ecx
addl 0xc(%rsp), %edx
jmp 0xd52c
incl %edi
movl 0xc(%rbx), %r8d
addl 0x1c(%rsp), %esi
jmp 0xd522
pushq $0x3
popq %rdx
movl %edx, 0x10(%rsp)
pushq $0x1
popq %rdx
movl %edx, %r13d
decl %r8d
cmpl %r8d, %r13d
jge 0xd767
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 0xd757
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 0xd749
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 0xd73e
movq %rdx, 0x48(%rsp)
andq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %ebp
cmpl 0x10(%rsp), %ebp
je 0xd672
xorl %r14d, %r14d
cmpl $0x3, %r14d
je 0xd66e
cmpl %r13d, %ebp
sete %al
cmpq $0x1, %r14
sete %cl
testb %cl, %al
jne 0xd669
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 0x32b8(%rip), %rsi      # 0x10910
je 0xd661
leaq 0x329d(%rip), %rsi      # 0x108fe
movq %r15, %rdi
callq 0x22d0
incq %r14
jmp 0xd61f
incl %ebp
jmp 0xd616
xorl %ebp, %ebp
leaq 0xa8(%rsp), %r14
cmpq $0x2e, %rbp
je 0xd726
movq %rbp, %rsi
shlq $0x5, %rsi
leaq 0x82dc(%rip), %rax      # 0x15970
addq %rax, %rsi
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0xd6e6
movq 0xa8(%rsp), %rax
xorl %ecx, %ecx
movq 0x60(%rsp), %rdx
xorl %esi, %esi
cmpq $0x8, %rcx
je 0xd6e1
movb (%rax,%rcx), %dil
cmpb $0x44, %dil
je 0xd6dc
pushq $0x1
popq %rsi
cmpb %dil, (%rdx,%rcx)
jne 0xd716
incq %rcx
jmp 0xd6c3
cmpl $0x1, %esi
jmp 0xd6f3
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 0xd71e
jmp 0xd72a
movq %r14, %rdi
callq 0x20b0
incq %rbp
jmp 0xd67c
movb $-0x1, %al
jmp 0xd72c
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 0xd5d3
incq %rsi
movl 0x8(%rbx), %ecx
incq %r12
jmp 0xd5a4
incl %r13d
incl 0x10(%rsp)
movl 0xc(%rbx), %r8d
jmp 0xd579
xorl %ecx, %ecx
pushq $0x1
popq %rax
movl %eax, %edx
cmpl 0x58(%rsp), %eax
jge 0xd998
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 0xd987
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 0xd973
cmpb $0x0, (%rdx,%rsi)
jne 0xd96b
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 0xd882
xorl %r15d, %r15d
movl %r12d, 0x48(%rsp)
leaq (%r15,%r13), %rax
cmpq %rbp, %rax
ja 0xd873
cmpl 0x10(%rsp), %r14d
sete %al
cmpq $0x1, %r15
sete %cl
testb %cl, %al
jne 0xd869
movslq %r12d, %rax
movq 0x30(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x32c3(%rip), %rsi      # 0x10b19
je 0xd85f
leaq 0x309f(%rip), %rsi      # 0x108fe
leaq 0x60(%rsp), %rdi
callq 0x22d0
addl 0xc(%rsp), %r12d
incq %r15
jmp 0xd827
incl %r14d
movl 0x48(%rsp), %r12d
addl 0x1c(%rsp), %r12d
jmp 0xd818
xorl %esi, %esi
xorl %r14d, %r14d
cmpq $0x31, %rsi
je 0xd937
movq %rsi, %r12
shlq $0x5, %rsi
leaq 0x8b91(%rip), %rax      # 0x16430
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 0xd8fe
movq 0xa8(%rsp), %rax
movq 0x60(%rsp), %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
setne %r15b
je 0xd8f8
movb (%rax,%rdx), %sil
cmpb $0x44, %sil
je 0xd8f3
cmpb %sil, (%rcx,%rdx)
jne 0xd918
incq %rdx
jmp 0xd8d9
pushq $0x1
popq %r14
jmp 0xd91b
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
xorl %r14d, %r14d
testl %eax, %eax
setne %r15b
sete %r14b
jmp 0xd91b
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rdi
callq 0x20b0
movq %r12, %rsi
incq %rsi
testb %r15b, %r15b
jne 0xd887
testl %r14d, %r14d
leaq 0x60(%rsp), %r15
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rsi
jne 0xd96b
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 0xd7e3
incq %r13
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0xd7ac
movl 0x54(%rsp), %ecx
addl 0x1c(%rsp), %ecx
movl 0x20(%rsp), %eax
jmp 0xd76c
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 0xda14
movl %r11d, %r14d
pushq $0x1
popq %r15
cmpq %r8, %r15
jge 0xda07
movslq %r14d, %r13
addq %r9, %r13
xorl %edi, %edi
cmpq %rdi, %r10
je 0xd9ff
cmpb $0x1, (%r13,%rdi)
adcl $0x0, %esi
incq %rdi
jmp 0xd9ec
incq %r15
addl %eax, %r14d
jmp 0xd9df
incl %r12d
addl %ebp, %r11d
leaq 0x60(%rsp), %r15
jmp 0xd9d3
cmpl 0x28(%rsp), %esi
sete %dil
movl %esi, %edx
movq %rdx, 0x28(%rsp)
jmp 0xd50f
leaq 0x60(%rsp), %rdi
callq 0x20b0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xda48
jmp 0xda5e
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x20b0
jmp 0xda61
jmp 0xda5e
jmp 0xda5e
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2290
nop