// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::apply_artistic_effect(char const*, char const*, int, int, float, float, float, int, int, int, int)
void ImageProc::apply_artistic_effect(const char *infile, const char *outfile, int width, int height, float tau, float epsilon, float phi, int window_size, int sigma_dist, int sigma_simi, int iter)
{
	Image img_orig("color", width, height);
	Image img_orig_rgb("color", width, height);
	Image img_orig_bw("bw", width, height);
	Image img_orig_bw_bf("bw", width, height);
	Image img_modified("bw", width, height);
	Image img_bilateral_color("color", width, height);
	Image img_modified_color("color", width, height);
	img_orig.read_image(infile, img_orig.cols, img_orig.rows);

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			img_orig_bw.setvalue(i,j,0,((img_orig.getvalue(i,j,0)*0.21)\
								  	   +(img_orig.getvalue(i,j,1)*0.72)\
								  	   +(img_orig.getvalue(i,j,2)*0.07)));

	//rgb2cielab(img_orig, img_modified);

	//	//Bilateral Filter
	for(int i = 0; i < iter; i++)
	{
		bilateral_filter(img_orig, img_bilateral_color, window_size, sigma_dist, sigma_simi);
		for(int i = 0; i < img_orig.rows; i++)
		 	for(int j = 0; j < img_orig.cols; j++)
		 		for(int k = 0; k < img_orig.channels; k++)
			 	{
			 		img_orig.setvalue(i,j,k,img_bilateral_color.getvalue(i,j,k));
			 	}
	}

	img_bilateral_color.write_image("../output_images/bilateral_art_effect.raw", img_bilateral_color.cols, img_bilateral_color.rows);
	//cielab2rgb(img_modified_color2, img_orig_rgb);

	//img_modified_color.write_image(outfile, img_modified.cols, img_modified.rows);

	for(int i = 0; i < img_orig_bw.rows; i++)
		for(int j = 0; j < img_orig_bw.cols; j++)
			for(int k = 0; k < img_orig_bw.channels; k++)
				img_orig_bw.setvalue(i,j,k,((img_orig.getvalue(i,j,0)*0.21)\
									  +(img_orig.getvalue(i,j,1)*0.72)\
									  +(img_orig.getvalue(i,j,2)*0.07)));

	Image img_sigma_small("bw", width, height);
	Image img_sigma_large("bw", width, height);
	Image img_xDoG("bw", width, height);

	int sigma = 1;

	gaussian_filter(img_orig_bw, img_sigma_small, 9, sigma);
	gaussian_filter(img_orig_bw, img_sigma_large, 9, sigma*sqrt(1.6));

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
				img_xDoG.setvalue(i,j,k,(int)((float)img_sigma_small.getvalue(i,j,k) - (float)(1.0*tau*img_sigma_large.getvalue(i,j,k))));
				//img_xDoG.setvalue(i,j,k,(int)((float)img_sigma_small.getvalue(i,j,k) - (float)(1.0*img_sigma_large.getvalue(i,j,k))));
	img_xDoG.write_image("../output_images/xDoG_art_effect.raw", img_xDoG.cols, img_xDoG.rows);

	float xDoG_val, value;
	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
			{
				value = (float)img_xDoG.getvalue(i,j,k)/255.0;
				if(value > epsilon)
					img_modified.setvalue(i,j,k,0);
				else
				{
					value = 1.0 + tanh(phi*(((float)img_sigma_small.getvalue(i,j,k) - (float)(1.0*tau*img_sigma_large.getvalue(i,j,k))) - epsilon));
					img_modified.setvalue(i,j,k,(int)(value*255.0));
				}

			}

	img_modified.write_image("../output_images/contours_art_effect.raw", img_modified.cols, img_modified.rows);

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
				if(img_modified.getvalue(i,j,k) == 0)
					img_modified.setvalue(i,j,k,0);
				else
					img_modified.setvalue(i,j,k,1);

//	for(int i = 0; i < img_orig.rows; i++)
//		for(int j = 0; j < img_orig.cols; j++)
//			for(int k = 0; k < img_orig.channels; k++)
//				if(img_xDoG.getvalue(i,j,k) == 255)
//					img_modified.setvalue(i,j,k,0);
//				else
//					img_modified.setvalue(i,j,k,1);


	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
				img_modified_color.setvalue(i,j,k,(int)((img_bilateral_color.getvalue(i,j,k) * (img_modified.getvalue(i,j,0)))));
	img_modified_color.write_image(outfile, img_modified.cols, img_modified.rows);


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movl %r9d, 0x68(%rsp)
movl %r8d, 0x14(%rsp)
movss %xmm2, 0xa4(%rsp)
movss %xmm1, 0x1c(%rsp)
movss %xmm0, 0x60(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, 0xa8(%rsp)
movq %rdi, %r13
leaq 0x4915(%rip), %r12      # 0x106c8
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3fd6
leaq 0x1d8(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3fd6
leaq 0x58d2(%rip), %rbx      # 0x116b4
leaq 0x118(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3fd6
leaq 0x1b8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3fd6
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3fd6
leaq 0xf8(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3fd6
leaq 0xb8(%rsp), %rdi
movq %r12, %rsi
movl %r14d, 0x3c(%rsp)
movl %r14d, %edx
movl %ebp, 0x10(%rsp)
movl %ebp, %ecx
callq 0x3fd6
movl 0x8(%r15), %r12d
movl 0xc(%r15), %ebx
movq %r15, %rdi
movq %r13, %rsi
callq 0x4032
movq 0x18(%r15), %rbp
movl 0x10(%r15), %r14d
leaq 0x118(%rsp), %rdx
movq 0x18(%rdx), %rax
movq %rax, 0x78(%rsp)
movl 0x8(%rdx), %ecx
movl 0x10(%rdx), %edx
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
testl %ebx, %ebx
cmovlel %eax, %ebx
movl %ebx, 0x8(%rsp)
movq %rcx, 0x88(%rsp)
movl %edx, 0x28(%rsp)
imull %edx, %ecx
movl %ecx, 0xc(%rsp)
movl %r12d, 0x18(%rsp)
imull %r14d, %r12d
movl %r12d, 0x20(%rsp)
movsd 0x45d1(%rip), %xmm2     # 0x10498
movsd 0x45d1(%rip), %xmm3     # 0x104a0
movsd 0x45d1(%rip), %xmm4     # 0x104a8
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x78(%rsp), %r10
movl %eax, %esi
movl %ecx, %edi
movq %r13, %r8
cmpl 0x8(%rsp), %edx
je 0xbf4c
subq $0x1, %r8
jb 0xbf40
movslq %esi, %rsi
movzbl (%rbp,%rsi), %r9d
cvtsi2sd %r9d, %xmm0
movzbl 0x1(%rbp,%rsi), %r9d
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movzbl 0x2(%rbp,%rsi), %r9d
cvtsi2sd %r9d, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
movslq %edi, %rdi
movb %r9b, (%r10,%rdi)
addl 0x28(%rsp), %edi
addl %r14d, %esi
jmp 0xbeed
incl %edx
addl 0xc(%rsp), %ecx
addl 0x20(%rsp), %eax
jmp 0xbee0
leaq 0xf8(%rsp), %rcx
xorl %edx, %edx
testl %r14d, %r14d
movl $0x0, %r15d
cmovgl %r14d, %r15d
movq 0x18(%rcx), %r12
movl 0x238(%rsp), %eax
testl %eax, %eax
cmovlel %edx, %eax
movl %eax, 0x70(%rsp)
movl 0x10(%rcx), %eax
movl 0x8(%rcx), %ecx
movl %eax, 0x5c(%rsp)
imull %eax, %ecx
movl %ecx, 0x58(%rsp)
cmpl 0x70(%rsp), %edx
je 0xc019
movq %rdx, 0x40(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
movl 0x14(%rsp), %edx
movl 0x68(%rsp), %ecx
movl 0x230(%rsp), %r8d
callq 0x8644
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl 0x8(%rsp), %edx
je 0xc00d
movl %eax, %esi
movl %ecx, %edi
xorl %r8d, %r8d
cmpq %r13, %r8
je 0xc001
movslq %edi, %r9
addq %rbp, %r9
movslq %esi, %r10
addq %r12, %r10
xorl %r11d, %r11d
cmpq %r11, %r15
je 0xbff5
movb (%r10,%r11), %bl
movb %bl, (%r9,%r11)
incq %r11
jmp 0xbfe3
incq %r8
addl %r14d, %edi
addl 0x5c(%rsp), %esi
jmp 0xbfcf
incl %edx
addl 0x20(%rsp), %ecx
addl 0x58(%rsp), %eax
jmp 0xbfc2
movq 0x40(%rsp), %rdx
incl %edx
jmp 0xbf88
leaq 0x4d67(%rip), %rsi      # 0x10d87
leaq 0xf8(%rsp), %rdi
callq 0x411c
xorl %eax, %eax
movl 0x28(%rsp), %edx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl 0x124(%rsp), %esi
movq 0x88(%rsp), %rdx
testl %edx, %edx
cmovlel %eax, %edx
testl %esi, %esi
cmovlel %eax, %esi
movl %esi, 0x40(%rsp)
xorl %esi, %esi
movq %r12, 0x20(%rsp)
movsd 0x442f(%rip), %xmm2     # 0x10498
movsd 0x442f(%rip), %xmm3     # 0x104a0
movsd 0x442f(%rip), %xmm4     # 0x104a8
cmpl 0x40(%rsp), %esi
je 0xc10c
movl %esi, %edi
imull 0x18(%rsp), %edi
movl %eax, %r8d
xorl %r9d, %r9d
cmpq %rdx, %r9
je 0xc101
movslq %r8d, %r10
addq 0x78(%rsp), %r10
leal (%rdi,%r9), %r11d
imull %r14d, %r11d
movslq %r11d, %r11
xorl %ebx, %ebx
cmpq %rbx, %rcx
je 0xc0f2
movzbl (%rbp,%r11), %r12d
cvtsi2sd %r12d, %xmm0
movzbl 0x1(%rbp,%r11), %r12d
cvtsi2sd %r12d, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movzbl 0x2(%rbp,%r11), %r12d
cvtsi2sd %r12d, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r12d
movb %r12b, (%r10,%rbx)
incq %rbx
jmp 0xc0aa
incq %r9
addl 0x28(%rsp), %r8d
movq 0x20(%rsp), %r12
jmp 0xc090
incl %esi
addl 0xc(%rsp), %eax
jmp 0xc079
leaq 0x55a1(%rip), %r14      # 0x116b4
leaq 0x178(%rsp), %rdi
movq %r14, %rsi
movl 0x3c(%rsp), %ebp
movl %ebp, %edx
movl 0x10(%rsp), %ebx
movl %ebx, %ecx
callq 0x3fd6
leaq 0x158(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x3fd6
leaq 0x138(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x3fd6
leaq 0x118(%rsp), %rbx
pushq $0x9
popq %rbp
movss 0x4532(%rip), %xmm0     # 0x1069c
movq %rbx, %rdi
leaq 0x178(%rsp), %r14
movq %r14, %rsi
movl %ebp, %edx
callq 0x78ee
movss 0x4531(%rip), %xmm0     # 0x106b8
movq %rbx, %rdi
leaq 0x158(%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %edx
callq 0x78ee
movq 0x18(%r14), %rax
movq %rax, 0x70(%rsp)
movl 0x10(%r14), %eax
cvtss2sd 0x60(%rsp), %xmm2
movq 0x18(%rbx), %rcx
movq %rcx, 0x68(%rsp)
movl 0x10(%rbx), %ecx
leaq 0x138(%rsp), %rsi
movq 0x18(%rsi), %rdx
movq %rdx, 0x60(%rsp)
movl 0x10(%rsi), %edx
movl 0x8(%rsi), %esi
movl %edx, 0x10(%rsp)
imull %edx, %esi
movl %esi, 0x54(%rsp)
movl 0x8(%rbx), %edx
movl %ecx, 0x14(%rsp)
imull %ecx, %edx
movl %edx, 0x50(%rsp)
movl 0x8(%r14), %ecx
movl %eax, 0x18(%rsp)
imull %eax, %ecx
movl %ecx, 0x4c(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpl 0x8(%rsp), %esi
je 0xc292
movl %eax, %edi
movl %ecx, %r8d
movl %edx, %r9d
xorl %r10d, %r10d
cmpq %r13, %r10
je 0xc27f
movslq %r9d, %r11
addq 0x60(%rsp), %r11
movslq %r8d, %rbx
addq 0x68(%rsp), %rbx
movslq %edi, %r14
addq 0x70(%rsp), %r14
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xc267
movzbl (%r14,%r12), %ebp
cvtsi2ss %ebp, %xmm0
movzbl (%rbx,%r12), %ebp
cvtsi2sd %ebp, %xmm1
mulsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
subss %xmm1, %xmm0
cvttss2si %xmm0, %ebp
movb %bpl, (%r11,%r12)
incq %r12
jmp 0xc237
incq %r10
addl 0x10(%rsp), %r9d
addl 0x14(%rsp), %r8d
addl 0x18(%rsp), %edi
movq 0x20(%rsp), %r12
jmp 0xc217
incl %esi
addl 0x54(%rsp), %edx
addl 0x50(%rsp), %ecx
addl 0x4c(%rsp), %eax
jmp 0xc202
movsd %xmm2, 0x78(%rsp)
leaq 0x4b12(%rip), %rsi      # 0x10db1
leaq 0x138(%rsp), %rdi
callq 0x411c
movq 0xf0(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0xe0(%rsp), %eax
movl 0xe8(%rsp), %ecx
movl %eax, 0x94(%rsp)
movl %ecx, 0xc(%rsp)
imull %ecx, %eax
movl %eax, 0x48(%rsp)
movq $0x0, 0x80(%rsp)
movsd 0x41c3(%rip), %xmm1     # 0x104b0
movl $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
xorl %ecx, %ecx
movq %r13, 0x40(%rsp)
movss 0x1c(%rsp), %xmm2
cmpl 0x8(%rsp), %ecx
je 0xc48e
movq 0x80(%rsp), %rax
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edi
xorl %r8d, %r8d
movl %ecx, 0x98(%rsp)
cmpq %r13, %r8
je 0xc44f
movq %r8, 0xb0(%rsp)
movl %edi, 0x9c(%rsp)
movslq %edi, %rbp
addq 0x28(%rsp), %rbp
movl %esi, 0xa0(%rsp)
movslq %esi, %r12
addq 0x68(%rsp), %r12
movl %edx, 0x88(%rsp)
movslq %edx, %rbx
addq 0x70(%rsp), %rbx
movl %eax, 0x3c(%rsp)
movslq %eax, %r13
addq 0x60(%rsp), %r13
xorl %r14d, %r14d
cmpq %r14, %r15
je 0xc405
movzbl (%r13,%r14), %eax
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
ucomiss %xmm2, %xmm0
jbe 0xc3a7
xorl %eax, %eax
jmp 0xc3fb
movzbl (%rbx,%r14), %eax
cvtsi2ss %eax, %xmm0
movzbl (%r12,%r14), %eax
cvtsi2sd %eax, %xmm1
mulsd 0x78(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
subss %xmm1, %xmm0
subss %xmm2, %xmm0
mulss 0xa4(%rsp), %xmm0
callq 0x2230
movsd 0x40cf(%rip), %xmm1     # 0x104b0
movss 0x1c(%rsp), %xmm2
addss 0x42ad(%rip), %xmm0     # 0x1069c
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, (%rbp,%r14)
incq %r14
jmp 0xc387
movq 0xb0(%rsp), %r8
incq %r8
movl 0x9c(%rsp), %edi
addl 0xc(%rsp), %edi
movl 0xa0(%rsp), %esi
addl 0x14(%rsp), %esi
movl 0x88(%rsp), %edx
addl 0x18(%rsp), %edx
movl 0x3c(%rsp), %eax
addl 0x10(%rsp), %eax
movq 0x40(%rsp), %r13
movq 0x20(%rsp), %r12
movl 0x98(%rsp), %ecx
jmp 0xc33a
incl %ecx
movl 0x30(%rsp), %eax
addl 0x48(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x34(%rsp), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x38(%rsp), %eax
addl 0x4c(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x80(%rsp), %rax
addl 0x54(%rsp), %eax
movq %rax, 0x80(%rsp)
jmp 0xc312
leaq 0x4941(%rip), %rsi      # 0x10dd6
leaq 0xd8(%rsp), %rdi
callq 0x411c
xorl %eax, %eax
xorl %ecx, %ecx
cmpl 0x8(%rsp), %ecx
je 0xc4e5
movl %eax, %edx
xorl %esi, %esi
cmpq %r13, %rsi
je 0xc4dd
movslq %edx, %rdi
addq 0x28(%rsp), %rdi
xorl %r8d, %r8d
cmpq %r8, %r15
je 0xc4d4
cmpb $0x0, (%rdi,%r8)
setne (%rdi,%r8)
incq %r8
jmp 0xc4c0
incq %rsi
addl 0xc(%rsp), %edx
jmp 0xc4b0
incl %ecx
addl 0x48(%rsp), %eax
jmp 0xc4a6
movq 0xd0(%rsp), %rcx
movl 0xc8(%rsp), %edx
movl 0xc0(%rsp), %eax
imull %edx, %eax
movl %eax, 0x1c(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl 0x8(%rsp), %r9d
je 0xc587
movl %r9d, %r10d
imull 0x94(%rsp), %r10d
movl %edi, %r11d
movl %r8d, %ebx
xorl %r14d, %r14d
cmpq %r13, %r14
je 0xc579
movq %r12, %rax
movslq %ebx, %r12
addq %rcx, %r12
movslq %r11d, %r13
addq %rax, %r13
leal (%r10,%r14), %eax
imull 0xc(%rsp), %eax
movslq %eax, %rbp
xorl %esi, %esi
cmpq %rsi, %r15
je 0xc563
movq 0x28(%rsp), %rax
movb (%rax,%rbp), %al
mulb (%r13,%rsi)
movb %al, (%r12,%rsi)
incq %rsi
jmp 0xc548
incq %r14
addl %edx, %ebx
addl 0x5c(%rsp), %r11d
movq 0x40(%rsp), %r13
movq 0x20(%rsp), %r12
jmp 0xc526
incl %r9d
addl 0x1c(%rsp), %r8d
addl 0x58(%rsp), %edi
jmp 0xc50a
leaq 0xb8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x411c
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq