// File: /workspace/github_cpp_makefile_star2_O3/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 0xc3d3
cmpl $0x2, %ecx
jne 0xc41d
movabsq $0x200000001, %rdx      # imm = 0x200000001
movq %rdx, (%rax)
leaq (%rax,%r10,4), %rdx
addq $0x4, %rdx
movl $0x3, -0x4(%rdx)
jmp 0xc415
movaps 0x5236(%rip), %xmm0     # 0x11610
movaps %xmm0, (%rax)
movaps 0x523c(%rip), %xmm0     # 0x11620
movups %xmm0, (%rax,%r10,4)
movaps 0x5240(%rip), %xmm0     # 0x11630
movaps %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)
jmp 0xc421
testl %ecx, %ecx
jle 0xc475
movl %ecx, %edx
imull %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
leaq (,%r10,4), %rdx
xorl %r8d, %r8d
movsd 0x4ce8(%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 0xc445
incq %r8
addq %rdx, %r9
addq %rdx, %rax
cmpq %r10, %r8
jne 0xc443
movl 0xc(%rsi), %edx
testl %edx, %edx
jle 0xc529
movl 0x8(%rsi), %eax
xorl %r8d, %r8d
movsd 0x5022(%rip), %xmm0     # 0x114b0
testl %eax, %eax
jle 0xc51d
movl 0x10(%rsi), %ebx
xorl %r9d, %r9d
testl %ebx, %ebx
jle 0xc512
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
movl 0x8(%rsi), %edx
imull %r8d, %edx
addl %r9d, %edx
imull %ebx, %edx
movslq %edx, %rdx
addq 0x18(%rsi), %rdx
ucomiss %xmm2, %xmm1
movl $0x0, %ebx
sbbl %ebx, %ebx
movb %bl, (%rax,%rdx)
movl 0x10(%rsi), %ebx
incq %rax
cmpl %ebx, %eax
jl 0xc4bc
movl 0x8(%rsi), %eax
incl %r9d
cmpl %eax, %r9d
jl 0xc49c
movl 0xc(%rsi), %edx
incl %r8d
cmpl %edx, %r8d
jl 0xc48e
leaq -0x10(%rbp), %rsp
popq %rbx
popq %r14
popq %rbp
retq