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

	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_count_ridge_length.setvalue(i,j,k,255);


	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_shrinked.setvalue(i,j,k,img_binary.getvalue(i,j,k));

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

	

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

		for(int i = 0+1; i < img_shrinked.rows-1; i++)
			for(int j = 0+1; j < img_shrinked.cols-1; j++)
				for(int k = 0; k < img_shrinked.channels; k++)
				{
					if(img_shrinked.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_shrinked.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 < SHRINK_FIRST_PATTERNS; t++)
						{
							std::string testpattern(shrink_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_shrinked.setvalue(i,j,k,1*255);
							}
							else
								img_shrinked.setvalue(i,j,k,img_shrinked.getvalue(i,j,k));
						}


					}

		pixel_count = 0;
		for(int i = 0+1; i < img_shrinked.rows-1; i++)
			for(int j = 0+1; j < img_shrinked.cols-1; j++)
				for(int k = 0; k < img_shrinked.channels; k++)
					if(img_shrinked.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;


	}
	std::cout << "Total number of ridges: " << black_pixel_count << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %edx
leaq 0x3e1b(%rip), %r15      # 0x12253
leaq 0xd8(%rsp), %rdi
movq %r15, %rsi
callq 0x4c56
movl 0x8(%r14), %ecx
movl 0xc(%r14), %edx
leaq 0xf8(%rsp), %rdi
movq %r15, %rsi
callq 0x4c56
movl 0xc(%r14), %edi
testl %edi, %edi
movq %rbx, 0x10(%rsp)
jle 0xe56c
movq 0x110(%rsp), %rax
movl 0x108(%rsp), %ecx
movl 0x8(%r14), %r9d
movl 0x100(%rsp), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %r8d, %r8d
testl %r9d, %r9d
jle 0xe4d9
movl 0x10(%r14), %r11d
movl %esi, %edi
xorl %r10d, %r10d
testl %r11d, %r11d
jle 0xe4c3
movslq %edi, %r9
addq %rax, %r9
xorl %ebx, %ebx
movb $-0x1, (%r9,%rbx)
incq %rbx
movslq 0x10(%r14), %r11
cmpq %r11, %rbx
jl 0xe4ae
movl 0x8(%r14), %r9d
incq %r10
movslq %r9d, %rbx
addl %ecx, %edi
cmpq %rbx, %r10
jl 0xe4a1
movl 0xc(%r14), %edi
movq 0x10(%rsp), %rbx
incl %r8d
addl %edx, %esi
cmpl %edi, %r8d
jl 0xe493
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movq %rax, 0x80(%rsp)
movb $0x0, (%rax)
testl %edi, %edi
jle 0xe588
movl 0x8(%r14), %edx
xorl %eax, %eax
testl %edx, %edx
jle 0xe564
movl 0x10(%r14), %esi
xorl %ecx, %ecx
testl %esi, %esi
jle 0xe55a
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 0xe51d
movl 0x8(%r14), %edx
incl %ecx
cmpl %edx, %ecx
jl 0xe517
movl 0xc(%r14), %edi
incl %eax
cmpl %edi, %eax
jl 0xe50d
jmp 0xe588
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movq %rax, 0x80(%rsp)
movb $0x0, (%rax)
movl 0xc(%rbx), %eax
xorl %ecx, %ecx
cmpl $0x3, %eax
jl 0xe5ff
leal -0x1(%rax), %edx
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %esi
movl 0x10(%rcx), %edi
leal -0x1(%rsi), %r8d
movq 0x18(%rcx), %r9
leal 0x1(%rsi), %r10d
imull %edi, %r10d
movl %esi, %r11d
imull %edi, %r11d
xorl %ecx, %ecx
movl $0x1, %r14d
cmpl $0x3, %esi
jl 0xe5f4
movl $0x1, %r15d
movl %r10d, %ebp
testl %edi, %edi
jle 0xe5ea
movslq %ebp, %rbx
addq %r9, %rbx
xorl %r12d, %r12d
cmpb $0x1, (%rbx,%r12)
adcl $0x0, %ecx
incq %r12
cmpq %r12, %rdi
jne 0xe5da
incq %r15
addl %edi, %ebp
cmpq %r8, %r15
jne 0xe5cd
incq %r14
addl %r11d, %r10d
cmpq %rdx, %r14
jne 0xe5bf
movq 0xf0(%rsp), %rdx
movq %rdx, 0x38(%rsp)
movl 0xe0(%rsp), %edx
movl 0xe4(%rsp), %esi
movl 0xe8(%rsp), %r13d
movq %rsi, 0xb8(%rsp)
decl %esi
movq %rsi, 0xc0(%rsp)
leal -0x2(%rdx), %esi
movq %rsi, 0xd0(%rsp)
movl %r13d, %esi
movq %rdx, 0x88(%rsp)
imull %edx, %esi
movl %esi, 0x24(%rsp)
movl %ecx, 0xac(%rsp)
testl %eax, %eax
jle 0xe931
movq 0x10(%rsp), %rbx
movl 0x8(%rbx), %esi
xorl %ecx, %ecx
xorl %edx, %edx
testl %esi, %esi
jle 0xe6ad
movl 0x10(%rbx), %r8d
movl %ecx, %eax
xorl %edi, %edi
testl %r8d, %r8d
jle 0xe69c
movslq %eax, %rsi
addq 0x38(%rsp), %rsi
xorl %r9d, %r9d
movb $-0x1, (%rsi,%r9)
incq %r9
movslq 0x10(%rbx), %r8
cmpq %r8, %r9
jl 0xe688
movl 0x8(%rbx), %esi
incq %rdi
movslq %esi, %r9
addl %r13d, %eax
cmpq %r9, %rdi
jl 0xe678
movl 0xc(%rbx), %eax
incl %edx
addl 0x24(%rsp), %ecx
cmpl %eax, %edx
jl 0xe66c
cmpl $0x3, %eax
jl 0xe936
movl 0x8(%rbx), %ecx
movl $0x1, %r14d
movl $0x3, 0xc(%rsp)
cmpl $0x3, %ecx
jl 0xe91c
leal -0x1(%r14), %eax
movl %eax, 0x20(%rsp)
movq 0x88(%rsp), %rax
imull %r14d, %eax
movq %rax, 0x28(%rsp)
movl 0x10(%rbx), %eax
movl $0x1, %esi
xorl %r15d, %r15d
testl %eax, %eax
jle 0xe904
movq 0x28(%rsp), %rcx
addl %esi, %ecx
imull %r13d, %ecx
movslq %ecx, %rcx
addq 0x38(%rsp), %rcx
movq %rcx, 0x90(%rsp)
xorl %edx, %edx
movq %rsi, 0x30(%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 0xe8f1
movq %rdx, 0x98(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x20(%rsp), %ebp
xorl %r12d, %r12d
cmpl %r14d, %ebp
sete %al
cmpq $0x1, %r12
sete %cl
testb %cl, %al
jne 0xe7ae
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
addl %r12d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x32ce(%rip), %rsi      # 0x11a69
je 0xe7a4
leaq 0x360f(%rip), %rsi      # 0x11db3
leaq 0x60(%rsp), %rdi
callq 0x22e0
incq %r12
cmpl $0x3, %r12d
jne 0xe768
incl %ebp
cmpl 0xc(%rsp), %ebp
jne 0xe765
xorl %ebp, %ebp
movq %rbp, %rax
shlq $0x5, %rax
leaq 0x50(%rsp), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x79e7(%rip), %rcx      # 0x161c0
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x10b98
movq %r12, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xe83c
movq 0x40(%rsp), %r12
xorl %eax, %eax
movq 0x60(%rsp), %rcx
xorl %edx, %edx
movb (%r12,%rax), %sil
cmpb $0x44, %sil
je 0xe82e
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xe8b7
incq %rax
cmpq $0x8, %rax
jne 0xe815
cmpl $0x1, %edx
jmp 0xe882
movq 0x68(%rsp), %rax
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xe87f
movq 0x60(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0xa0(%rsp)
callq 0x2190
testl %eax, %eax
movq 0xa0(%rsp), %rax
je 0xe87f
xorl %ebx, %ebx
jmp 0xe885
cmpq %rbx, %rax
sete %bl
leaq 0x50(%rsp), %rax
cmpq %rax, %r12
je 0xe89f
movq 0x50(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
testb %bl, %bl
movq 0x10(%rsp), %rbx
jne 0xe8d7
incq %rbp
cmpq $0x3e, %rbp
jne 0xe7c1
jmp 0xe8d3
leaq 0x50(%rsp), %rax
cmpq %rax, %r12
je 0xe8a8
movq 0x50(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
jmp 0xe8a8
movb $-0x1, %al
jmp 0xe8d9
xorl %eax, %eax
movq 0x90(%rsp), %rcx
movq 0x98(%rsp), %rdx
movb %al, (%rcx,%rdx)
movq 0x30(%rsp), %rsi
incq %rdx
movslq 0x10(%rbx), %rax
cmpq %rax, %rdx
jl 0xe728
movl 0x8(%rbx), %ecx
incq %rsi
leal -0x1(%rcx), %edx
movslq %edx, %rdx
incq %r15
cmpq %rdx, %rsi
jl 0xe6fe
movl 0xc(%rbx), %eax
incl %r14d
leal -0x1(%rax), %edx
incl 0xc(%rsp)
cmpl %edx, %r14d
jl 0xe6d1
jmp 0xe936
movq 0x10(%rsp), %rbx
cmpl $0x3, 0xb8(%rsp)
jl 0xebd7
movl $0x3, 0xc(%rsp)
movl $0x1, %r15d
movl $0x0, 0x1c(%rsp)
cmpl $0x3, 0x88(%rsp)
jl 0xebb3
movq %r15, %rax
imulq 0x88(%rsp), %rax
movq %rax, 0xc8(%rsp)
leal -0x1(%r15), %eax
movl %eax, 0x20(%rsp)
movl $0x1, %eax
movq %rax, 0x28(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq $0x0, 0xb0(%rsp)
testl %r13d, %r13d
jle 0xeb88
movq 0xc8(%rsp), %rax
movq 0x28(%rsp), %rcx
addl %ecx, %eax
imull %r13d, %eax
movslq %eax, %rdx
addq 0x38(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x90(%rsp)
cmpb $0x0, (%rdx,%rsi)
jne 0xeb7c
movq %rsi, 0x98(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x30(%rsp), %ebx
movl 0x20(%rsp), %eax
movl %eax, %r12d
movl %r12d, %r12d
movq $-0x1, %r14
movl %ebx, %ebp
movq %r15, %rax
xorq %r12, %rax
orq %r14, %rax
je 0xea3b
movslq %ebp, %rax
movq 0x38(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x3043(%rip), %rsi      # 0x11a6b
je 0xea31
leaq 0x3382(%rip), %rsi      # 0x11db3
leaq 0x60(%rsp), %rdi
callq 0x22e0
incq %r14
addl %r13d, %ebp
cmpq $0x2, %r14
jne 0xea0a
incl %r12d
addl 0x24(%rsp), %ebx
cmpl 0xc(%rsp), %r12d
jne 0xe9fe
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
movq %rbp, %rax
shlq $0x5, %rax
leaq 0x50(%rsp), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x89cc(%rip), %rcx      # 0x17440
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x10b98
movq %r14, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xeaca
movq 0x40(%rsp), %r12
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
movb (%r12,%rcx), %dl
cmpb $0x44, %dl
je 0xeabc
cmpb %dl, (%rax,%rcx)
jne 0xeb1a
incq %rcx
cmpq $0x8, %rcx
jne 0xeaae
xorl %r14d, %r14d
jmp 0xeb1d
movq 0x68(%rsp), %rax
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xeb0c
movq 0x60(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0xa0(%rsp)
callq 0x2190
movb $0x1, %r14b
testl %eax, %eax
movq 0xa0(%rsp), %rax
jne 0xeb13
cmpq %rbx, %rax
setne %r14b
movq 0x10(%rsp), %rbx
jmp 0xeb1d
movb $0x1, %r14b
leaq 0x50(%rsp), %rax
cmpq %rax, %r12
je 0xeb37
movq 0x50(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
cmpq $0x30, %rbp
leaq 0x1(%rbp), %rbp
setb %al
testb %al, %r14b
jne 0xea5c
testb %r14b, %r14b
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rsi
je 0xeb7c
movl 0x8(%rbx), %eax
imull %r15d, %eax
addl 0x28(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
cmpq %r13, %rsi
jne 0xe9d0
incq 0x28(%rsp)
movq 0xb0(%rsp), %rcx
incq %rcx
addl %r13d, 0x30(%rsp)
movq %rcx, 0xb0(%rsp)
cmpq 0xd0(%rsp), %rcx
jne 0xe9a2
incq %r15
incl 0xc(%rsp)
movl 0x1c(%rsp), %eax
addl 0x24(%rsp), %eax
movl %eax, 0x1c(%rsp)
cmpq 0xc0(%rsp), %r15
jne 0xe95a
movl 0xc(%rbx), %eax
xorl %ecx, %ecx
cmpl $0x3, %eax
jl 0xec4b
leal -0x1(%rax), %edx
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %esi
movl 0x10(%rcx), %edi
leal -0x1(%rsi), %r8d
movq 0x18(%rcx), %r9
leal 0x1(%rsi), %r10d
imull %edi, %r10d
movl %esi, %r11d
imull %edi, %r11d
xorl %ecx, %ecx
movl $0x1, %r14d
cmpl $0x3, %esi
jl 0xec40
movl $0x1, %r15d
movl %r10d, %ebp
testl %edi, %edi
jle 0xec36
movslq %ebp, %rbx
addq %r9, %rbx
xorl %r12d, %r12d
cmpb $0x1, (%rbx,%r12)
adcl $0x0, %ecx
incq %r12
cmpq %r12, %rdi
jne 0xec26
incq %r15
addl %edi, %ebp
cmpq %r8, %r15
jne 0xec19
incq %r14
addl %r11d, %r10d
cmpq %rdx, %r14
jne 0xec0b
movl 0xac(%rsp), %ebx
cmpl %ebx, %ecx
jne 0xe651
movq 0x736f(%rip), %rdi      # 0x15fd0
leaq 0x2e05(%rip), %rsi      # 0x11a6d
movl $0x18, %edx
callq 0x21a0
movq 0x7357(%rip), %rdi      # 0x15fd0
movl %ebx, %esi
callq 0x2280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movq 0x60(%rsp), %rdi
cmpq 0x80(%rsp), %rdi
je 0xecc6
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2180
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xece0
jmp 0xece0
jmp 0xece0
jmp 0xece0
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq 0x80(%rsp), %rdi
je 0xecff
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x22a0
nop