// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %edx
leaq 0x3db7(%rip), %r15      # 0x121e7
leaq 0xc8(%rsp), %rdi
movq %r15, %rsi
callq 0x4c6e
movl 0x8(%r14), %ecx
movl 0xc(%r14), %edx
leaq 0xe8(%rsp), %rdi
movq %r15, %rsi
callq 0x4c6e
cmpl $0x0, 0xc(%r14)
jle 0xe4c3
movq 0x100(%rsp), %rax
movl 0xf8(%rsp), %ecx
movl 0xf0(%rsp), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x0, 0x8(%r14)
jle 0xe4b9
movl %esi, %r8d
xorl %r9d, %r9d
cmpl $0x0, 0x10(%r14)
jle 0xe4aa
movslq %r8d, %r10
addq %rax, %r10
xorl %r11d, %r11d
movb $-0x1, (%r10,%r11)
incq %r11
movslq 0x10(%r14), %r15
cmpq %r15, %r11
jl 0xe499
incq %r9
movslq 0x8(%r14), %r10
addl %ecx, %r8d
cmpq %r10, %r9
jl 0xe489
incl %edi
addl %edx, %esi
cmpl 0xc(%r14), %edi
jl 0xe47c
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x0, 0xc(%r14)
jle 0xe53c
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
jle 0xe534
xorl %ecx, %ecx
movl 0x10(%r14), %esi
testl %esi, %esi
jle 0xe52c
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 0xe4f3
incl %ecx
cmpl 0x8(%r14), %ecx
jl 0xe4e9
incl %eax
cmpl 0xc(%r14), %eax
jl 0xe4e0
movl 0xc(%rbx), %eax
xorl %r12d, %r12d
cmpl $0x3, %eax
jl 0xe5af
decl %eax
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %edx
leal -0x1(%rcx), %esi
movq 0x18(%rbx), %rdi
leal 0x1(%rcx), %r8d
imull %edx, %r8d
movl %ecx, %r9d
imull %edx, %r9d
xorl %r12d, %r12d
movl $0x1, %r10d
cmpl $0x3, %ecx
jl 0xe5a4
movl $0x1, %r11d
movl %r8d, %ebp
testl %edx, %edx
jle 0xe59a
movslq %ebp, %r14
addq %rdi, %r14
xorl %r15d, %r15d
cmpb $0x1, (%r14,%r15)
adcl $0x0, %r12d
incq %r15
cmpq %r15, %rdx
jne 0xe589
incq %r11
addl %edx, %ebp
cmpq %rsi, %r11
jne 0xe57c
incq %r10
addl %r9d, %r8d
cmpq %rax, %r10
jne 0xe56e
movq 0xe0(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0xd8(%rsp), %r10d
leaq 0x40(%rsp), %r13
movl 0xd0(%rsp), %eax
movl 0xd4(%rsp), %ecx
movq %rcx, 0xa8(%rsp)
decl %ecx
movq %rcx, 0xb0(%rsp)
leal -0x2(%rax), %ecx
movq %rcx, 0xc0(%rsp)
movq %rax, 0x80(%rsp)
imull %r10d, %eax
movl %eax, 0x1c(%rsp)
movq %r10, 0x28(%rsp)
cmpl $0x0, 0xc(%rbx)
jle 0xe659
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x0, 0x8(%rbx)
jle 0xe64e
movl %eax, %edx
xorl %esi, %esi
cmpl $0x0, 0x10(%rbx)
jle 0xe63f
movslq %edx, %rdi
addq 0x58(%rsp), %rdi
xorl %r8d, %r8d
movb $-0x1, (%rdi,%r8)
incq %r8
movslq 0x10(%rbx), %r9
cmpq %r9, %r8
jl 0xe62e
incq %rsi
movslq 0x8(%rbx), %rdi
addl %r10d, %edx
cmpq %rdi, %rsi
jl 0xe61d
incl %ecx
addl 0x1c(%rsp), %eax
cmpl 0xc(%rbx), %ecx
jl 0xe613
cmpl $0x3, 0xc(%rbx)
movq %r12, 0xa0(%rsp)
jl 0xe8bf
movl $0x1, %ebp
movl $0x3, 0x8(%rsp)
cmpl $0x3, 0x8(%rbx)
jl 0xe8ac
leal -0x1(%rbp), %eax
movl %eax, 0x18(%rsp)
movq 0x80(%rsp), %rax
imull %ebp, %eax
movq %rax, 0x20(%rsp)
movl $0x1, %edx
xorl %r14d, %r14d
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0xe896
movq 0x20(%rsp), %rcx
addl %edx, %ecx
movl %ecx, 0x88(%rsp)
xorl %esi, %esi
movq %rdx, 0x50(%rsp)
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %edx, %ecx
imull %eax, %ecx
movslq %ecx, %rax
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0xe886
movq %rsi, 0x90(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x18(%rsp), %r12d
xorl %r15d, %r15d
cmpl %ebp, %r12d
sete %al
cmpq $0x1, %r15
sete %cl
testb %cl, %al
jne 0xe748
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %r12d, %ecx
addl %r14d, %ecx
addl %r15d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x32c8(%rip), %rsi      # 0x119fd
je 0xe73e
leaq 0x3609(%rip), %rsi      # 0x11d47
leaq 0x60(%rsp), %rdi
callq 0x22f0
incq %r15
cmpl $0x3, %r15d
jne 0xe701
incl %r12d
cmpl 0x8(%rsp), %r12d
jne 0xe6fe
xorl %r12d, %r12d
movq %r12, %rax
shlq $0x5, %rax
movq %r13, 0x30(%rsp)
leaq 0x7a4f(%rip), %rcx      # 0x161c0
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x10adc
movq %r15, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21f0
cmpq $-0x1, %rax
je 0xe7d4
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movq 0x60(%rsp), %rcx
xorl %edx, %edx
movb (%rdi,%rax), %sil
cmpb $0x44, %sil
je 0xe7c2
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xe803
incq %rax
cmpq $0x8, %rax
jne 0xe7ad
cmpl $0x1, %edx
sete %r15b
jmp 0xe828
movq 0x68(%rsp), %r15
movq 0x38(%rsp), %r13
cmpq %r15, %r13
movq %r15, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0xe817
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %al
jmp 0xe819
cmpq %r13, %rdi
je 0xe844
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
jmp 0xe844
movb $0x1, %al
cmpq %r13, %r15
sete %r15b
andb %al, %r15b
leaq 0x40(%rsp), %r13
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xe83f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
testb %r15b, %r15b
jne 0xe855
incq %r12
cmpq $0x3e, %r12
jne 0xe75e
movb $-0x1, %al
jmp 0xe857
xorl %eax, %eax
movl 0x88(%rsp), %ecx
imull 0x28(%rsp), %ecx
movslq %ecx, %rcx
movq 0x58(%rsp), %rdx
movq 0x90(%rsp), %rsi
addq %rsi, %rdx
movb %al, (%rcx,%rdx)
movq 0xa0(%rsp), %r12
movq 0x50(%rsp), %rdx
incq %rsi
movslq 0x10(%rbx), %rax
cmpq %rax, %rsi
jl 0xe6c1
incq %rdx
movslq 0x8(%rbx), %rax
decq %rax
incq %r14
cmpq %rax, %rdx
jl 0xe6a1
incl %ebp
movl 0xc(%rbx), %eax
decl %eax
incl 0x8(%rsp)
cmpl %eax, %ebp
jl 0xe678
cmpl $0x3, 0xa8(%rsp)
movq 0x28(%rsp), %rdx
jl 0xeb6e
movl $0x3, 0x8(%rsp)
movl $0x1, %ebp
movl $0x0, 0x14(%rsp)
cmpl $0x3, 0x80(%rsp)
jl 0xeb4d
movq %rbp, %rax
imulq 0x80(%rsp), %rax
movq %rax, 0xb8(%rsp)
leal -0x1(%rbp), %eax
movl %eax, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x50(%rsp)
movq $0x0, 0x98(%rsp)
testl %edx, %edx
jle 0xeb23
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
imull %edx, %eax
movslq %eax, %rsi
addq 0x58(%rsp), %rsi
xorl %edi, %edi
movq %rsi, 0x88(%rsp)
cmpb $0x0, (%rsi,%rdi)
jne 0xeb17
movq %rdi, 0x90(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x60(%rsp), %rax
movb $0x0, (%rax)
movl 0x50(%rsp), %r13d
movl 0x18(%rsp), %eax
movl %eax, %r14d
movl %r14d, %r14d
movq $-0x1, %r15
movl %r13d, %r12d
movq %rbp, %rax
xorq %r14, %rax
orq %r15, %rax
je 0xe9c7
movslq %r12d, %rax
movq 0x58(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x304b(%rip), %rsi      # 0x119ff
je 0xe9bd
leaq 0x338a(%rip), %rsi      # 0x11d47
leaq 0x60(%rsp), %rdi
callq 0x22f0
incq %r15
addl 0x28(%rsp), %r12d
cmpq $0x2, %r15
jne 0xe996
incl %r14d
addl 0x1c(%rsp), %r13d
cmpl 0x8(%rsp), %r14d
jne 0xe989
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
leaq 0x30(%rsp), %r13
movq %r12, %rax
shlq $0x5, %rax
movq %r15, 0x30(%rsp)
leaq 0x8a3c(%rip), %rcx      # 0x17440
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x10adc
movq %r13, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21f0
cmpq $-0x1, %rax
je 0xea5f
movq 0x30(%rsp), %rax
movq $-0x1, %rcx
movq 0x60(%rsp), %rdx
movb 0x1(%rax,%rcx), %sil
cmpb $0x44, %sil
je 0xea50
cmpb %sil, 0x1(%rdx,%rcx)
jne 0xea8f
incq %rcx
cmpq $0x7, %rcx
jne 0xea3e
setb %r14b
jmp 0xeaae
movq 0x68(%rsp), %r15
movq 0x38(%rsp), %r13
cmpq %r15, %r13
movq %r15, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0xea94
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %r14b
jmp 0xea97
movb $0x1, %r14b
jmp 0xeaae
movb $0x1, %r14b
cmpq %r13, %r15
setne %al
xorb $0x1, %r14b
orb %al, %r14b
leaq 0x40(%rsp), %r15
leaq 0x30(%rsp), %r13
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0xeac5
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
cmpq $0x30, %r12
leaq 0x1(%r12), %r12
setb %al
testb %al, %r14b
jne 0xe9f1
testb %r14b, %r14b
movq 0xa0(%rsp), %r12
movq 0x28(%rsp), %rdx
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdi
je 0xeb17
movl 0x8(%rbx), %eax
imull %ebp, %eax
addl 0x20(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rdi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rdi
cmpq %rdx, %rdi
jne 0xe95a
incq 0x20(%rsp)
movq 0x98(%rsp), %rcx
incq %rcx
addl %edx, 0x50(%rsp)
movq %rcx, 0x98(%rsp)
cmpq 0xc0(%rsp), %rcx
jne 0xe92e
incq %rbp
incl 0x8(%rsp)
movl 0x14(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpq 0xb0(%rsp), %rbp
jne 0xe8e7
movl 0xc(%rbx), %ecx
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
cmpl $0x3, %ecx
leaq 0x40(%rsp), %r13
jl 0xebf4
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %edx
leal -0x1(%rcx), %esi
movq 0x18(%rbx), %rdi
leal 0x1(%rcx), %r8d
imull %edx, %r8d
movl %ecx, %r9d
imull %edx, %r9d
xorl %eax, %eax
movl $0x1, %r10d
cmpl $0x3, %ecx
jl 0xebe7
movl $0x1, %r11d
movl %r8d, %ebp
testl %edx, %edx
jle 0xebdd
movslq %ebp, %r14
addq %rdi, %r14
xorl %r15d, %r15d
cmpb $0x1, (%r14,%r15)
adcl $0x0, %eax
incq %r15
cmpq %r15, %rdx
jne 0xebcd
incq %r11
addl %edx, %ebp
cmpq %rsi, %r11
jne 0xebc0
incq %r10
addl %r9d, %r8d
cmpq 0x8(%rsp), %r10
jne 0xebb2
cmpl %r12d, %eax
movl %eax, %r12d
movq 0x28(%rsp), %r10
jne 0xe609
movl %eax, %ebp
movq 0x73c2(%rip), %rdi      # 0x15fd0
leaq 0x2dec(%rip), %rsi      # 0x11a01
movl $0x18, %edx
callq 0x21b0
movq 0x73aa(%rip), %rdi      # 0x15fd0
movl %ebp, %esi
callq 0x2290
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xec73
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2190
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xec8d
jmp 0xec8d
jmp 0xec8d
jmp 0xec8d
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xecac
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22b0