// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::apply_bilateral_filter(char const*, char const*, int, int, int, int, int, int)
void ImageProc::apply_bilateral_filter(const char *infile, const char *outfile, int width, int height, int window_size, int sigma, int sigma_simi, int iter)
{
	Image img_orig("color", width, height);
	Image img_modified("color", width, height);
	img_orig.read_image(infile, img_orig.cols, img_orig.rows);

	// Image img_red("bw", img_orig.cols, img_orig.rows);
	// Image img_blue("bw", img_orig.cols, img_orig.rows);
	// Image img_green("bw", img_orig.cols, img_orig.rows);
	// Image img_redOut("bw", img_orig.cols, img_orig.rows);
	// Image img_greenOut("bw", img_orig.cols, img_orig.rows);
	// Image img_blueOut("bw", 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_red.setvalue(i,j,0,img_orig.getvalue(i,j,0));
	// 		img_green.setvalue(i,j,0,img_orig.getvalue(i,j,1));
	// 		img_blue.setvalue(i,j,0,img_orig.getvalue(i,j,2));
	// 	}

	for(int i = 0; i < iter; i++)
	{
		// median_filter(img_orig, img_modified, window_size);
		// 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_modified.getvalue(i,j,k));

		bilateral_filter(img_orig, img_modified, window_size, sigma, 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_modified.getvalue(i,j,k));
			 	}
	}

	img_modified.write_image(outfile, img_modified.cols, img_modified.rows);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq 0xd0f4(%rip), %rsi      # 0x1c5e8
movq %rsi, -0x88(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x7eb0
movq -0x88(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq -0x60(%rbp), %rdi
callq 0x7eb0
jmp 0xf51c
movq -0x8(%rbp), %rsi
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
leaq -0x40(%rbp), %rdi
callq 0x7f70
jmp 0xf531
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0xf63d
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl 0x10(%rbp), %r8d
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xf680
jmp 0xf55d
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0xf62d
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0xf61d
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0xf60d
movl -0x74(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x74(%rbp), %esi
movl -0x78(%rbp), %edx
movl -0x7c(%rbp), %ecx
leaq -0x60(%rbp), %rdi
callq 0x83b0
movl -0x94(%rbp), %esi
movl -0x90(%rbp), %edx
movl -0x8c(%rbp), %ecx
movl %eax, %r8d
leaq -0x40(%rbp), %rdi
callq 0x83f0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xf58a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xf66d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x1b2c0
jmp 0xf66d
jmp 0xf60f
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xf577
jmp 0xf61f
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xf564
jmp 0xf62f
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xf538
movq -0x10(%rbp), %rsi
movl -0x58(%rbp), %edx
movl -0x54(%rbp), %ecx
leaq -0x60(%rbp), %rdi
callq 0x8150
jmp 0xf652
leaq -0x60(%rbp), %rdi
callq 0x1b2c0
leaq -0x40(%rbp), %rdi
callq 0x1b2c0
addq $0xa0, %rsp
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x1b2c0
movq -0x68(%rbp), %rdi
callq 0x2360
nop