// File: /workspace/github_cpp_makefile_star2_O2/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 0x4a9e(%rip), %r15      # 0x116b4
leaq 0xc8(%rsp), %rdi
movq %r15, %rsi
callq 0x3fd6
movl 0x8(%r14), %ecx
movl 0xc(%r14), %edx
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x3fd6
movq 0x18(%r12), %rax
movl 0x10(%r12), %ecx
movl 0x8(%r12), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %edi, %edi
movl 0xc(%r14), %r8d
cmpl %r8d, %edi
jge 0xcc99
movl %esi, %r8d
xorl %r9d, %r9d
movslq 0x8(%r14), %r10
cmpq %r10, %r9
jge 0xcc93
movslq %r8d, %r10
addq %rax, %r10
xorl %r11d, %r11d
movslq 0x10(%r14), %r15
cmpq %r15, %r11
jge 0xcc8b
movb $-0x1, (%r10,%r11)
incq %r11
jmp 0xcc78
incq %r9
addl %ecx, %r8d
jmp 0xcc66
incl %edi
addl %edx, %esi
jmp 0xcc57
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
cmpl %r8d, %eax
jge 0xcd02
xorl %ecx, %ecx
cmpl 0x8(%r14), %ecx
jge 0xccfa
xorl %edx, %edx
movl 0x10(%r14), %esi
cmpl %esi, %edx
jge 0xccf6
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 0xccbb
incl %ecx
jmp 0xccb3
incl %eax
movl 0xc(%r14), %r8d
jmp 0xccac
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 0xcd8a
movl %r10d, %r15d
pushq $0x1
popq %r12
cmpq %rdi, %r12
jge 0xcd83
movslq %r15d, %r13
addq %r8, %r13
xorl %r14d, %r14d
cmpq %r14, %r9
je 0xcd7b
cmpb $0x1, (%r13,%r14)
movq 0x28(%rsp), %rdx
adcl $0x0, %edx
movq %rdx, 0x28(%rsp)
incq %r14
jmp 0xcd5e
incq %r12
addl %eax, %r15d
jmp 0xcd50
incl %ebp
addl %r11d, %r10d
jmp 0xcd45
movq 0xe0(%rsp), %rdx
movq %rdx, 0x38(%rsp)
movslq 0xd0(%rsp), %rdx
movl 0xd4(%rsp), %esi
movl 0xd8(%rsp), %r8d
decl %esi
movl %esi, 0x60(%rsp)
movq %rdx, %rsi
decq %rsi
movq %rsi, 0x98(%rsp)
xorl %edi, %edi
testl %r8d, %r8d
movl $0x0, %r11d
cmovgl %r8d, %r11d
movq %rdx, 0x88(%rsp)
movl %r8d, 0xc(%rsp)
imull %r8d, %edx
movl %edx, 0x1c(%rsp)
leaq 0x68(%rsp), %r12
movq %r11, 0x40(%rsp)
testb $0x1, %dil
movq 0x10(%rsp), %r8
jne 0xd312
xorl %esi, %esi
xorl %edi, %edi
cmpl %r8d, %edi
jge 0xce4d
movl %esi, %edx
xorl %r8d, %r8d
movslq %ecx, %r9
cmpq %r9, %r8
jge 0xce41
movslq %edx, %rcx
addq 0x38(%rsp), %rcx
xorl %r9d, %r9d
movslq %eax, %r10
cmpq %r10, %r9
jge 0xce35
movb $-0x1, (%rcx,%r9)
incq %r9
movl 0x10(%rbx), %eax
jmp 0xce20
incq %r8
movl 0x8(%rbx), %ecx
addl 0xc(%rsp), %edx
jmp 0xce0d
incl %edi
movl 0xc(%rbx), %r8d
addl 0x1c(%rsp), %esi
jmp 0xce03
pushq $0x3
popq %rdx
movl %edx, 0x10(%rsp)
pushq $0x1
popq %rdx
movl %edx, %r13d
decl %r8d
cmpl %r8d, %r13d
jge 0xd04d
leal -0x1(%r13), %edx
movl %edx, 0x8(%rsp)
movq 0x88(%rsp), %rdx
imull %r13d, %edx
movq %rdx, 0x48(%rsp)
xorl %r14d, %r14d
pushq $0x1
popq %rsi
leal -0x1(%rcx), %edx
movslq %edx, %rdx
cmpq %rdx, %rsi
jge 0xd038
movq 0x48(%rsp), %rcx
addl %esi, %ecx
imull 0xc(%rsp), %ecx
movslq %ecx, %rcx
addq 0x38(%rsp), %rcx
movq %rcx, 0x50(%rsp)
xorl %edx, %edx
movq %rsi, 0x30(%rsp)
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0xd02a
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 0xd01f
movq %rdx, 0x20(%rsp)
andq $0x0, 0x70(%rsp)
movq 0x68(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %ebp
cmpl 0x10(%rsp), %ebp
je 0xcf53
xorl %r15d, %r15d
cmpl $0x3, %r15d
je 0xcf4f
cmpl %r13d, %ebp
sete %al
cmpq $0x1, %r15
sete %cl
testb %cl, %al
jne 0xcf4a
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %r14d, %ecx
addl %r15d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x39d7(%rip), %rsi      # 0x10910
je 0xcf42
leaq 0x39bc(%rip), %rsi      # 0x108fe
movq %r12, %rdi
callq 0x22d0
incq %r15
jmp 0xcf00
incl %ebp
jmp 0xcef7
xorl %ebp, %ebp
leaq 0xa8(%rsp), %r15
cmpq $0x3e, %rbp
je 0xd007
movq %rbp, %rsi
shlq $0x5, %rsi
leaq 0x823b(%rip), %rax      # 0x151b0
addq %rax, %rsi
movq %r15, %rdi
callq 0x21d0
movq %r15, %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0xcfc7
movq 0xa8(%rsp), %rax
xorl %ecx, %ecx
movq 0x68(%rsp), %rdx
xorl %esi, %esi
cmpq $0x8, %rcx
je 0xcfc2
movb (%rax,%rcx), %dil
cmpb $0x44, %dil
je 0xcfbd
pushq $0x1
popq %rsi
cmpb %dil, (%rdx,%rcx)
jne 0xcff7
incq %rcx
jmp 0xcfa4
cmpl $0x1, %esi
jmp 0xcfd4
movq %r12, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
sete %r15b
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
callq 0x20b0
testb %r15b, %r15b
movq %r12, %r15
leaq 0x68(%rsp), %r12
je 0xcfff
jmp 0xd00b
movq %r15, %rdi
callq 0x20b0
incq %rbp
jmp 0xcf5d
movb $-0x1, %al
jmp 0xd00d
xorl %eax, %eax
movq 0x50(%rsp), %rcx
movq 0x20(%rsp), %rdx
movb %al, (%rcx,%rdx)
movq 0x30(%rsp), %rsi
incq %rdx
movl 0x10(%rbx), %eax
jmp 0xceb4
incq %rsi
movl 0x8(%rbx), %ecx
incq %r14
jmp 0xce85
incl %r13d
incl 0x10(%rsp)
movl 0xc(%rbx), %r8d
movq 0x40(%rsp), %r11
jmp 0xce5a
xorl %ecx, %ecx
pushq $0x1
popq %rax
movl %eax, %edx
cmpl 0x60(%rsp), %eax
jge 0xd27d
movq 0x88(%rsp), %rax
imull %edx, %eax
movq %rax, 0x90(%rsp)
leal -0x1(%rdx), %eax
movl %eax, 0x64(%rsp)
movq %rdx, 0x10(%rsp)
leal 0x1(%rdx), %eax
movl %eax, 0x30(%rsp)
movl %ecx, 0x5c(%rsp)
movl %ecx, 0x8(%rsp)
pushq $0x1
popq %rbp
xorl %r13d, %r13d
cmpq 0x98(%rsp), %rbp
jge 0xd26c
movq 0x90(%rsp), %rax
addl %ebp, %eax
imull 0xc(%rsp), %eax
movq %rbp, 0xa0(%rsp)
incq %rbp
movslq %eax, %rdx
addq 0x38(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x48(%rsp)
cmpq %r11, %rsi
je 0xd258
cmpb $0x0, (%rdx,%rsi)
jne 0xd250
movq %rsi, 0x50(%rsp)
andq $0x0, 0x70(%rsp)
movq 0x68(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %r14d
movl 0x64(%rsp), %r15d
cmpl 0x30(%rsp), %r15d
jg 0xd163
xorl %r12d, %r12d
movl %r14d, 0x20(%rsp)
leaq (%r12,%r13), %rax
cmpq %rbp, %rax
ja 0xd154
cmpl 0x10(%rsp), %r15d
sete %al
cmpq $0x1, %r12
sete %cl
testb %cl, %al
jne 0xd14a
movslq %r14d, %rax
movq 0x38(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x39e2(%rip), %rsi      # 0x10b19
je 0xd140
leaq 0x37be(%rip), %rsi      # 0x108fe
leaq 0x68(%rsp), %rdi
callq 0x22d0
addl 0xc(%rsp), %r14d
incq %r12
jmp 0xd108
incl %r15d
movl 0x20(%rsp), %r14d
addl 0x1c(%rsp), %r14d
jmp 0xd0f9
xorl %esi, %esi
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
cmpq $0x31, %rsi
je 0xd21c
movq %rsi, 0x20(%rsp)
shlq $0x5, %rsi
leaq 0x92a9(%rip), %rax      # 0x16430
addq %rax, %rsi
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0xd1e1
movq 0xa8(%rsp), %rax
movq 0x68(%rsp), %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
setne %r15b
je 0xd1db
movb (%rax,%rdx), %sil
cmpb $0x44, %sil
je 0xd1d6
cmpb %sil, (%rcx,%rdx)
jne 0xd1fb
incq %rdx
jmp 0xd1bc
pushq $0x1
popq %r14
jmp 0xd1fe
movq %r12, %rdi
movq %r14, %rsi
callq 0x2040
xorl %r14d, %r14d
testl %eax, %eax
setne %r15b
sete %r14b
jmp 0xd1fe
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rdi
callq 0x20b0
movq 0x20(%rsp), %rsi
incq %rsi
testb %r15b, %r15b
jne 0xd16d
testl %r14d, %r14d
movq 0x40(%rsp), %r11
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rsi
jne 0xd250
movl 0x8(%rbx), %eax
imull 0x10(%rsp), %eax
addl 0xa0(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
jmp 0xd0c9
incq %r13
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0xd092
movl 0x5c(%rsp), %ecx
addl 0x1c(%rsp), %ecx
movl 0x30(%rsp), %eax
jmp 0xd052
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 %r14
cmpl %edx, %r14d
jge 0xd2f9
movl %r11d, %r15d
pushq $0x1
popq %r12
cmpq %r8, %r12
jge 0xd2ec
movslq %r15d, %r13
addq %r9, %r13
xorl %edi, %edi
cmpq %rdi, %r10
je 0xd2e4
cmpb $0x1, (%r13,%rdi)
adcl $0x0, %esi
incq %rdi
jmp 0xd2d1
incq %r12
addl %eax, %r15d
jmp 0xd2c4
incl %r14d
addl %ebp, %r11d
leaq 0x68(%rsp), %r12
jmp 0xd2b8
cmpl 0x28(%rsp), %esi
sete %dil
movl %esi, %edx
movq %rdx, 0x28(%rsp)
movq 0x40(%rsp), %r11
jmp 0xcdf0
movq 0x7cb7(%rip), %rdi      # 0x14fd0
leaq 0x3bae(%rip), %rsi      # 0x10ece
callq 0x2170
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x2270
movq %rax, %rdi
callq 0x20c0
leaq 0x68(%rsp), %rdi
callq 0x20b0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xd372
jmp 0xd35c
jmp 0xd372
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x20b0
jmp 0xd375
jmp 0xd372
jmp 0xd372
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2290
nop