// File: /workspace/github_cpp_makefile_star2_O2/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), %ecx
movl 0xc(%rdi), %r8d
movl 0x10(%rdi), %r9d
movq %r9, %r11
imulq %rcx, %r11
movq %r8, %rax
imulq %r11, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
xorl %eax, %eax
testl %r9d, %r9d
movl $0x0, %edx
cmovgl %r9d, %edx
testl %ecx, %ecx
movl $0x0, %r10d
cmovgl %ecx, %r10d
testl %r8d, %r8d
movl $0x0, %r14d
cmovgl %r8d, %r14d
movq %r14, -0x40(%rbp)
movq 0x18(%rdi), %rdi
movq %rcx, -0x38(%rbp)
imull %r9d, %ecx
movl %ecx, -0x30(%rbp)
movq %r11, -0x60(%rbp)
leaq (,%r11,4), %rcx
movq %rcx, -0x58(%rbp)
movq %r9, -0x48(%rbp)
leaq (,%r9,4), %r9
movsd 0x5949(%rip), %xmm0     # 0x104b0
movq %rbx, -0x50(%rbp)
movq %rbx, %r8
xorl %r14d, %r14d
cmpq -0x40(%rbp), %r14
je 0xabc7
movq %r8, %r15
movl %eax, %r12d
xorl %r13d, %r13d
cmpq %r10, %r13
je 0xabbb
movslq %r12d, %rcx
addq %rdi, %rcx
xorl %r11d, %r11d
cmpq %r11, %rdx
je 0xabaf
movzbl (%rcx,%r11), %ebx
cvtsi2sd %ebx, %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r15,%r11,4)
incq %r11
jmp 0xab8e
incq %r13
addl -0x48(%rbp), %r12d
addq %r9, %r15
jmp 0xab80
incq %r14
addl -0x30(%rbp), %eax
addq -0x58(%rbp), %r8
jmp 0xab71
shlq $0x2, -0x38(%rbp)
movq %r9, %rax
negq %rax
movq %rax, -0x40(%rbp)
xorl %r11d, %r11d
movss 0x5acf(%rip), %xmm0     # 0x106b0
movss 0x5acb(%rip), %xmm1     # 0x106b4
movsd 0x58e7(%rip), %xmm2     # 0x104d8
movsd 0x58e7(%rip), %xmm3     # 0x104e0
movsd 0x58e7(%rip), %xmm4     # 0x104e8
movsd 0x5687(%rip), %xmm5     # 0x10290
movslq 0xc(%rsi), %rax
decq %rax
cmpq %rax, %r11
jge 0xaefe
testb $0x1, %r11b
jne 0xad7f
movq -0x60(%rbp), %rax
imulq %r11, %rax
movq -0x50(%rbp), %rdi
leaq (%rdi,%rax,4), %r14
leaq 0x1(%r11), %rdx
movq -0x38(%rbp), %rax
movq %rax, %r15
imulq %rdx, %r15
leaq -0x4(%r15), %r12
movq -0x48(%rbp), %rcx
imulq %rcx, %r12
addq %rdi, %r12
movq -0x58(%rbp), %r13
movq %rdx, -0x30(%rbp)
imulq %rdx, %r13
addq %rdi, %r13
addq $0x4, %r15
imulq %rcx, %r15
addq %rdi, %r15
movq %r11, %rdx
imulq %rax, %rdx
addq $0x4, %rdx
imulq %rcx, %rdx
addq %rdi, %rdx
xorl %eax, %eax
movslq 0x8(%rsi), %rcx
cmpq %rcx, %rax
jge 0xaef5
movl %eax, %edi
incq %rax
xorl %r8d, %r8d
movslq 0x10(%rsi), %rbx
cmpq %rbx, %r8
jge 0xad6b
movss (%r14,%r8,4), %xmm7
ucomiss %xmm7, %xmm0
movl $0x0, %ecx
sbbl %ecx, %ecx
ucomiss %xmm7, %xmm0
movaps %xmm7, %xmm6
addss %xmm1, %xmm6
jb 0xacc0
movaps %xmm7, %xmm6
movl 0x8(%rsi), %r10d
imull %r11d, %r10d
addl %edi, %r10d
imull %ebx, %r10d
movslq %r10d, %r10
addq 0x18(%rsi), %r10
movb %cl, (%r8,%r10)
cvtss2sd (%rdx,%r8,4), %xmm7
cvtss2sd %xmm6, %xmm6
movaps %xmm6, %xmm8
mulsd %xmm2, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%rdx,%r8,4)
cvtss2sd (%r15,%r8,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r15,%r8,4)
cvtss2sd (%r13,%r8,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm4, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r13,%r8,4)
cvtss2sd (%r12,%r8,4), %xmm7
mulsd %xmm5, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm7, %xmm6
cvtsd2ss %xmm6, %xmm6
movss %xmm6, (%r12,%r8,4)
incq %r8
jmp 0xac94
addq %r9, %r12
addq %r9, %r13
addq %r9, %r15
addq %r9, %rdx
addq %r9, %r14
jmp 0xac7f
movslq 0x8(%rsi), %r12
leaq 0x1(%r11), %rdx
leal -0x1(%r12), %r14d
movq -0x38(%rbp), %rcx
movq %rcx, %rax
movq %rdx, -0x30(%rbp)
imulq %rdx, %rax
leaq (%rax,%r12,4), %r15
movq -0x48(%rbp), %r8
imulq %r8, %r15
movq -0x50(%rbp), %r10
addq %r10, %r15
leaq (%rax,%r12,4), %r13
addq $-0x4, %r13
imulq %r8, %r13
addq %r10, %r13
leaq (%rax,%r12,4), %rdx
addq $-0x8, %rdx
imulq %r8, %rdx
addq %r10, %rdx
movq %r11, %rax
imulq %rcx, %rax
leaq (%rax,%r12,4), %rdi
addq $-0x8, %rdi
imulq %r8, %rdi
addq %r10, %rdi
leaq (%rax,%r12,4), %rax
addq $-0x4, %rax
imulq %r8, %rax
addq %r10, %rax
movq %r12, %rcx
decq %r12
testq %rcx, %rcx
jle 0xaef5
xorl %r8d, %r8d
movslq 0x10(%rsi), %rbx
cmpq %rbx, %r8
jge 0xaeda
movss (%rax,%r8,4), %xmm7
ucomiss %xmm7, %xmm0
movl $0x0, %ecx
sbbl %ecx, %ecx
ucomiss %xmm7, %xmm0
movaps %xmm7, %xmm6
addss %xmm1, %xmm6
jb 0xae2f
movaps %xmm7, %xmm6
movl 0x8(%rsi), %r10d
imull %r11d, %r10d
addl %r14d, %r10d
imull %ebx, %r10d
movslq %r10d, %r10
addq 0x18(%rsi), %r10
movb %cl, (%r8,%r10)
cvtss2sd (%rdi,%r8,4), %xmm7
cvtss2sd %xmm6, %xmm6
movaps %xmm6, %xmm8
mulsd %xmm2, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%rdi,%r8,4)
cvtss2sd (%rdx,%r8,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%rdx,%r8,4)
cvtss2sd (%r13,%r8,4), %xmm7
movaps %xmm6, %xmm8
mulsd %xmm4, %xmm8
mulsd %xmm3, %xmm8
addsd %xmm7, %xmm8
cvtsd2ss %xmm8, %xmm7
movss %xmm7, (%r13,%r8,4)
cvtss2sd (%r15,%r8,4), %xmm7
mulsd %xmm5, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm7, %xmm6
cvtsd2ss %xmm6, %xmm6
movss %xmm6, (%r15,%r8,4)
incq %r8
jmp 0xae03
decl %r14d
movq -0x40(%rbp), %rcx
addq %rcx, %r15
addq %rcx, %r13
addq %rcx, %rdx
addq %rcx, %rdi
addq %rcx, %rax
jmp 0xadf1
movq -0x30(%rbp), %r11
jmp 0xac09
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop