// File: /workspace/github_cpp_makefile_star2_O3/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::error_diffusion(Image&, Image&)
void ImageProc::error_diffusion(Image &img_orig, Image &img_modified)
{
	float diffusionError;
	int direction;
	float img_orig_float[img_orig.rows][img_orig.cols][img_orig.channels];

	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_float[i][j][k] = float(img_orig.getvalue(i,j,k)/255.0);
			}


	for(int i = 0; i < img_modified.rows-1; i++)
	{
		if(i%2 == 0)
		{
			direction = LEFT2RIGHT;
			for(int j = 0; j < img_modified.cols; j++)
				for(int k = 0; k < img_modified.channels; k++)
				{

					if(img_orig_float[i][j][k] <= 0.5)
					{
						diffusionError = img_orig_float[i][j][k] - 0.0;
						img_modified.setvalue(i,j,k,0);
					}
					else
					{
						diffusionError = img_orig_float[i][j][k] - 1.0;
						img_modified.setvalue(i,j,k,255);
					}

					img_orig_float[i][j+1][k] = img_orig_float[i][j+1][k]+(diffusionError*7.0/16);
					img_orig_float[i+1][j+1][k] = img_orig_float[i+1][j+1][k]+(diffusionError*1.0/16);
					img_orig_float[i+1][j][k] = img_orig_float[i+1][j][k]+(diffusionError*5.0/16);
					img_orig_float[i+1][j-1][k] = img_orig_float[i+1][j-1][k]+(diffusionError*3.0/16);

				}
		}

		else
		{
			direction = RIGHT2LEFT;
			for(int j = img_modified.cols - 1; j >= 0; j--)
				for(int k = 0; k < img_modified.channels; k++)
				{
					if(img_orig_float[i][j][k] <= 0.5)
					{
						diffusionError = img_orig_float[i][j][k] - 0.0;
						img_modified.setvalue(i,j,k,0);
					}
					else
					{
						diffusionError = img_orig_float[i][j][k] - 1.0;
						img_modified.setvalue(i,j,k,255);
					}

					img_orig_float[i][j-1][k] = img_orig_float[i][j-1][k]+(diffusionError*7.0/16);
					img_orig_float[i+1][j-1][k] = img_orig_float[i+1][j-1][k]+(diffusionError*1.0/16);
					img_orig_float[i+1][j][k] = img_orig_float[i+1][j][k]+(diffusionError*5.0/16);
					img_orig_float[i+1][j+1][k] = img_orig_float[i+1][j+1][k]+(diffusionError*3.0/16);
				}

		}


	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0x8(%rdi), %eax
movl 0xc(%rdi), %r8d
movl 0x10(%rdi), %ecx
movq %rcx, %rdx
movq %rax, %r9
imulq %rax, %rdx
movq %rdx, %rax
movq %rdx, -0x38(%rbp)
imulq %r8, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, -0x30(%rbp)
movq %rdx, %rsp
movq %r8, -0x50(%rbp)
testl %r8d, %r8d
jle 0xc61b
movq 0x18(%rdi), %rdi
movl %r9d, %eax
imull %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
leaq (,%rax,4), %rax
movq %rax, -0x40(%rbp)
leaq (,%rcx,4), %rax
xorl %r11d, %r11d
movsd 0x4ef8(%rip), %xmm0     # 0x114b0
movq -0x30(%rbp), %rbx
xorl %r14d, %r14d
testl %r9d, %r9d
jle 0xc60a
movq %rbx, %r15
movl %r11d, %r12d
xorl %r13d, %r13d
testl %ecx, %ecx
jle 0xc5fc
movslq %r12d, %r8
addq %rdi, %r8
xorl %edx, %edx
movzbl (%r8,%rdx), %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0xc5d9
incq %r13
addl %ecx, %r12d
addq %rax, %r15
cmpq %r9, %r13
jne 0xc5cd
incq %r14
addl -0x48(%rbp), %r11d
addq -0x40(%rbp), %rbx
cmpq -0x50(%rbp), %r14
jne 0xc5bf
cmpl $0x2, 0xc(%rsi)
jl 0xc991
leaq (,%rcx,4), %rax
movq %rax, -0x40(%rbp)
negq %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rdx
leaq (%rdx,%rax,4), %rdx
movq %rdx, -0x58(%rbp)
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
leaq -0x4(,%r9,4), %rax
movq %rax, -0x60(%rbp)
shlq $0x2, %r9
movq %r9, -0x48(%rbp)
xorl %ebx, %ebx
movss 0x5042(%rip), %xmm0     # 0x116b0
movss 0x503e(%rip), %xmm1     # 0x116b4
movsd 0x4e5a(%rip), %xmm2     # 0x114d8
movsd 0x4e5a(%rip), %xmm3     # 0x114e0
movsd 0x4e5a(%rip), %xmm4     # 0x114e8
movsd 0x4bfa(%rip), %xmm5     # 0x11290
movl 0x8(%rsi), %r14d
testb $0x1, %bl
jne 0xc810
testl %r14d, %r14d
jle 0xc972
movl 0x10(%rsi), %r11d
xorl %edx, %edx
testl %r11d, %r11d
jle 0xc7f9
leaq 0x1(%rdx), %r10
movq -0x60(%rbp), %r9
leaq (%r9,%rdx,4), %rdi
imulq %rcx, %rdi
movq -0x30(%rbp), %r8
addq %r8, %rdi
movq -0x48(%rbp), %rax
leaq (%rax,%rdx,4), %r14
imulq %rcx, %r14
addq %r8, %r14
leaq (%r9,%rdx,4), %r15
addq $0x8, %r15
imulq %rcx, %r15
addq %r8, %r15
movq -0x40(%rbp), %rax
movq %rax, %r12
imulq %r10, %r12
addq %r8, %r12
movq %rdx, %r13
imulq %rax, %r13
addq %r8, %r13
xorl %eax, %eax
movss (%r13,%rax,4), %xmm7
ucomiss %xmm7, %xmm0
movl $0x0, %r8d
sbbl %r8d, %r8d
ucomiss %xmm7, %xmm0
movaps %xmm7, %xmm6
addss %xmm1, %xmm6
jb 0xc72c
movaps %xmm7, %xmm6
movl 0x8(%rsi), %r9d
imull %ebx, %r9d
addl %edx, %r9d
imull %r9d, %r11d
movslq %r11d, %r9
addq 0x18(%rsi), %r9
movb %r8b, (%rax,%r9)
xorps %xmm7, %xmm7
cvtss2sd (%r12,%rax,4), %xmm7
cvtss2sd %xmm6, %xmm6
movaps %xmm6, %xmm8
mulsd %xmm2, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r12,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%r15,%rax,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r15,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%r14,%rax,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm4, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r14,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%rdi,%rax,4), %xmm7
mulsd %xmm5, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm7, %xmm6
cvtsd2ss %xmm6, %xmm6
movss %xmm6, (%rdi,%rax,4)
incq %rax
movslq 0x10(%rsi), %r11
cmpq %r11, %rax
jl 0xc70a
movl 0x8(%rsi), %r14d
movq %r10, %rdx
jmp 0xc7ff
incq %rdx
movq %rdx, %r10
movslq %r14d, %rax
cmpq %rax, %r10
jl 0xc6b2
jmp 0xc972
testl %r14d, %r14d
jle 0xc972
movl 0x10(%rsi), %edi
movl %r14d, %r15d
decl %r15d
movq -0x48(%rbp), %rax
leaq (%rax,%r14,4), %r12
imulq %rcx, %r12
movq -0x30(%rbp), %rax
addq %rax, %r12
leaq -0x1(%r14), %r13
movq -0x40(%rbp), %r8
imulq %r8, %r13
movq -0x58(%rbp), %r9
leaq (%r9,%r13), %rdx
leaq -0x2(%r14), %r10
imulq %r8, %r10
leaq (%r9,%r10), %r11
addq %rax, %r10
addq %rax, %r13
testl %edi, %edi
jle 0xc94b
xorl %eax, %eax
movss (%r13,%rax,4), %xmm7
ucomiss %xmm7, %xmm0
movl $0x0, %r8d
sbbl %r8d, %r8d
ucomiss %xmm7, %xmm0
movaps %xmm7, %xmm6
addss %xmm1, %xmm6
jb 0xc887
movaps %xmm7, %xmm6
movl 0x8(%rsi), %r9d
imull %ebx, %r9d
addl %r15d, %r9d
imull %r9d, %edi
movslq %edi, %rdi
addq 0x18(%rsi), %rdi
movb %r8b, (%rax,%rdi)
xorps %xmm7, %xmm7
cvtss2sd (%r10,%rax,4), %xmm7
cvtss2sd %xmm6, %xmm6
movaps %xmm6, %xmm8
mulsd %xmm2, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r10,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%r11,%rax,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r11,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%rdx,%rax,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm4, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%rdx,%rax,4)
xorps %xmm7, %xmm7
cvtss2sd (%r12,%rax,4), %xmm7
mulsd %xmm5, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm7, %xmm6
cvtsd2ss %xmm6, %xmm6
movss %xmm6, (%r12,%rax,4)
incq %rax
movslq 0x10(%rsi), %rdi
cmpq %rdi, %rax
jl 0xc865
leaq -0x1(%r14), %rax
decl %r15d
movq -0x50(%rbp), %r8
addq %r8, %r12
addq %r8, %rdx
addq %r8, %r11
addq %r8, %r10
addq %r8, %r13
cmpq $0x2, %r14
movq %rax, %r14
jge 0xc85b
incq %rbx
movslq 0xc(%rsi), %rax
decq %rax
movq -0x38(%rbp), %rdx
addq %rdx, -0x30(%rbp)
addq %rdx, -0x58(%rbp)
cmpq %rax, %rbx
jl 0xc696
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq