// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
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 0xc28b
cmpl $0x2, %ecx
jne 0xc2d5
movabsq $0x200000001, %rdx      # imm = 0x200000001
movq %rdx, (%rax)
leaq (%rax,%r10,4), %rdx
addq $0x4, %rdx
movl $0x3, -0x4(%rdx)
jmp 0xc2cf
movaps 0x52ee(%rip), %xmm0     # 0x11580
movaps %xmm0, (%rax)
movaps 0x52f4(%rip), %xmm0     # 0x11590
movups %xmm0, (%rax,%r10,4)
movapd 0x52f7(%rip), %xmm0     # 0x115a0
movapd %xmm0, 0x20(%rax)
leaq (%r10,%r10,2), %rdx
movabsq $0x30000000f, %r8       # imm = 0x30000000F
leaq (%rax,%rdx,4), %rdx
addq $0xc, %rdx
movq %r8, -0xc(%rdx)
movl $0xc, -0x4(%rdx)
movl $0x0, (%rdx)
testl %ecx, %ecx
jle 0xc32d
movl %ecx, %edx
imull %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leaq (,%r10,4), %rdx
xorl %r8d, %r8d
movsd 0x4e30(%rip), %xmm1     # 0x11128
movq %r11, %r9
xorl %ebx, %ebx
xorps %xmm2, %xmm2
cvtsi2sdl (%rax,%rbx,4), %xmm2
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm2, (%r9,%rbx,4)
incq %rbx
cmpq %rbx, %r10
jne 0xc2fd
incq %r8
addq %rdx, %r9
addq %rdx, %rax
cmpq %r10, %r8
jne 0xc2fb
cmpl $0x0, 0xc(%rsi)
jle 0xc3e1
xorl %r8d, %r8d
movsd 0x51ce(%rip), %xmm0     # 0x11510
cmpl $0x0, 0x8(%rsi)
jle 0xc3d4
xorl %r9d, %r9d
movl 0x10(%rsi), %ebx
testl %ebx, %ebx
jle 0xc3c7
movl %r8d, %eax
cltd
idivl %ecx
imulq %r10, %rdx
leaq (%r11,%rdx,4), %r14
movl %r9d, %eax
cltd
idivl %ecx
movss (%r14,%rdx,4), %xmm1
xorl %eax, %eax
movl 0x8(%rdi), %edx
imull %r8d, %edx
addl %r9d, %edx
imull 0x10(%rdi), %edx
movslq %edx, %rdx
addq 0x18(%rdi), %rdx
movzbl (%rax,%rdx), %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
ucomiss %xmm2, %xmm1
movl $0x0, %edx
sbbl %edx, %edx
movl 0x8(%rsi), %r14d
imull %r8d, %r14d
addl %r9d, %r14d
imull %ebx, %r14d
movslq %r14d, %rbx
addq 0x18(%rsi), %rbx
movb %dl, (%rax,%rbx)
movl 0x10(%rsi), %ebx
incq %rax
cmpl %ebx, %eax
jl 0xc372
incl %r9d
cmpl 0x8(%rsi), %r9d
jl 0xc34f
incl %r8d
cmpl 0xc(%rsi), %r8d
jl 0xc342
leaq -0x10(%rbp), %rsp
popq %rbx
popq %r14
popq %rbp
retq