// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::halftone_dither_matrix(Image&, Image&, int)
void ImageProc::halftone_dither_matrix(Image &img_orig, Image &img_modified, const int matrix_size)
{
	int dither_matrix[matrix_size][matrix_size];
	float T[matrix_size][matrix_size];
	if(matrix_size == 2)
	{
		dither_matrix[0][0] = 1;	dither_matrix[0][1] = 2;
		dither_matrix[1][0] = 3;	dither_matrix[1][1] = 0;
	}
	if(matrix_size == 4)
	{
		dither_matrix[0][0] = 5;	dither_matrix[0][1] = 9;	dither_matrix[0][2] = 6;	dither_matrix[0][3] = 10;
		dither_matrix[1][0] = 13;	dither_matrix[1][1] = 1;	dither_matrix[1][2] = 14;	dither_matrix[1][3] = 2;
		dither_matrix[2][0] = 7;	dither_matrix[2][1] = 11;	dither_matrix[2][2] = 4;	dither_matrix[2][3] = 8;
		dither_matrix[3][0] = 15;	dither_matrix[3][1] = 3;	dither_matrix[3][2] = 12;	dither_matrix[3][3] = 0;
	}

	for(int i = 0; i < matrix_size; i++)
		for(int j = 0; j < matrix_size; j++)
			T[i][j] = ((1.0 * dither_matrix[i][j]) + 0.5) / (matrix_size*matrix_size);


	for(int i = 0; i < img_modified.rows; i++)
		for(int j = 0; j < img_modified.cols; j++)
			for(int k = 0; k < img_modified.channels; k++)
				if(float(img_orig.getvalue(i,j,k)/255.0) <= T[i%matrix_size][j%matrix_size])
					img_modified.setvalue(i,j,k,0  );
				else
					img_modified.setvalue(i,j,k,255);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edx, %ecx
movl %edx, %r10d
movq %r10, %rdx
imulq %r10, %rdx
movq %rsp, %rax
leaq 0xf(,%rdx,4), %rdx
andq $-0x10, %rdx
subq %rdx, %rax
movq %rax, %rsp
movq %rsp, %r11
subq %rdx, %r11
movq %r11, %rsp
cmpl $0x4, %ecx
je 0xa980
cmpl $0x2, %ecx
jne 0xa9c4
movabsq $0x200000001, %rdx      # imm = 0x200000001
movq %rdx, (%rax)
leaq (%rax,%r10,4), %rdx
addq $0x4, %rdx
movl $0x3, -0x4(%rdx)
jmp 0xa9c1
movaps 0x5c49(%rip), %xmm0     # 0x105d0
movaps %xmm0, (%rax)
movaps 0x5c4f(%rip), %xmm0     # 0x105e0
movups %xmm0, (%rax,%r10,4)
movaps 0x5c53(%rip), %xmm0     # 0x105f0
movaps %xmm0, 0x20(%rax)
imulq $0xc, %r10, %rdx
movabsq $0x30000000f, %r8       # imm = 0x30000000F
addq %rax, %rdx
addq $0xc, %rdx
movq %r8, -0xc(%rdx)
movl $0xc, -0x4(%rdx)
andl $0x0, (%rdx)
movl %ecx, %edx
imull %ecx, %edx
cvtsi2sd %edx, %xmm0
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
leaq (,%r10,4), %r9
movsd 0x573d(%rip), %xmm1     # 0x10128
movq %r11, %rbx
cmpq %r8, %rdx
je 0xaa23
xorl %r14d, %r14d
cmpq %r14, %r10
je 0xaa18
cvtsi2sdl (%rax,%r14,4), %xmm2
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm2, (%rbx,%r14,4)
incq %r14
jmp 0xa9f6
incq %rdx
addq %r9, %rbx
addq %r9, %rax
jmp 0xa9ee
xorl %r8d, %r8d
movsd 0x5a82(%rip), %xmm0     # 0x104b0
cmpl 0xc(%rsi), %r8d
jge 0xaabe
xorl %r9d, %r9d
cmpl 0x8(%rsi), %r9d
jge 0xaab6
xorl %ebx, %ebx
movl 0x10(%rsi), %r14d
cmpl %r14d, %ebx
jge 0xaab1
movl 0x8(%rdi), %eax
imull %r8d, %eax
addl %r9d, %eax
imull 0x10(%rdi), %eax
cltq
addq 0x18(%rdi), %rax
movzbl (%rbx,%rax), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movl %r8d, %eax
cltd
idivl %ecx
imulq %r10, %rdx
leaq (%r11,%rdx,4), %r15
movl %r9d, %eax
cltd
idivl %ecx
movl 0x8(%rsi), %eax
imull %r8d, %eax
addl %r9d, %eax
imull %eax, %r14d
movslq %r14d, %rax
addq 0x18(%rsi), %rax
movss (%r15,%rdx,4), %xmm2
ucomiss %xmm1, %xmm2
movl $0x0, %edx
sbbl %edx, %edx
movb %dl, (%rbx,%rax)
incq %rbx
jmp 0xaa43
incl %r9d
jmp 0xaa3b
incl %r8d
jmp 0xaa2e
leaq -0x18(%rbp), %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop