// File: 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
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0x78(%rbp)
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
movq %rax, %rcx
imulq %rcx, %rcx
leaq 0xf(,%rcx,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x28(%rbp)
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0x68(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
movq %rcx, -0x38(%rbp)
movq %rax, -0x40(%rbp)
cmpl $0x2, -0x14(%rbp)
jne 0x136bf
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
imulq $0x0, %rcx, %rdx
movl $0x1, (%rax,%rdx,4)
imulq $0x0, %rcx, %rdx
movl $0x2, 0x4(%rax,%rdx,4)
movq %rcx, %rdx
shlq $0x0, %rdx
movl $0x3, (%rax,%rdx,4)
shlq $0x0, %rcx
movl $0x0, 0x4(%rax,%rcx,4)
cmpl $0x4, -0x14(%rbp)
jne 0x137a1
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
imulq $0x0, %rcx, %rdx
movl $0x5, (%rax,%rdx,4)
imulq $0x0, %rcx, %rdx
movl $0x9, 0x4(%rax,%rdx,4)
imulq $0x0, %rcx, %rdx
movl $0x6, 0x8(%rax,%rdx,4)
imulq $0x0, %rcx, %rdx
movl $0xa, 0xc(%rax,%rdx,4)
movq %rcx, %rdx
shlq $0x0, %rdx
movl $0xd, (%rax,%rdx,4)
movq %rcx, %rdx
shlq $0x0, %rdx
movl $0x1, 0x4(%rax,%rdx,4)
movq %rcx, %rdx
shlq $0x0, %rdx
movl $0xe, 0x8(%rax,%rdx,4)
movq %rcx, %rdx
shlq $0x0, %rdx
movl $0x2, 0xc(%rax,%rdx,4)
movq %rcx, %rdx
shlq %rdx
movl $0x7, (%rax,%rdx,4)
movq %rcx, %rdx
shlq %rdx
movl $0xb, 0x4(%rax,%rdx,4)
movq %rcx, %rdx
shlq %rdx
movl $0x4, 0x8(%rax,%rdx,4)
movq %rcx, %rdx
shlq %rdx
movl $0x8, 0xc(%rax,%rdx,4)
imulq $0x3, %rcx, %rdx
movl $0xf, (%rax,%rdx,4)
imulq $0x3, %rcx, %rdx
movl $0x3, 0x4(%rax,%rdx,4)
imulq $0x3, %rcx, %rdx
movl $0xc, 0x8(%rax,%rdx,4)
imulq $0x3, %rcx, %rcx
movl $0x0, 0xc(%rax,%rcx,4)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1383d
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1382d
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %rdi
movslq -0x44(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x2, %rsi
addq %rsi, %rcx
movslq -0x48(%rbp), %rsi
cvtsi2sdl (%rcx,%rsi,4), %xmm0
movsd 0x8915(%rip), %xmm1     # 0x1c108
addsd %xmm1, %xmm0
movl -0x14(%rbp), %ecx
imull -0x14(%rbp), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0x44(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x48(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x137bb
jmp 0x1382f
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x137a8
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x13942
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x13932
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x13922
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl -0x50(%rbp), %edx
movl -0x54(%rbp), %ecx
callq 0x83b0
cvtsi2sd %eax, %xmm0
movsd 0x8c28(%rip), %xmm1     # 0x1c4c8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm1
movl -0x4c(%rbp), %eax
cltd
idivl -0x14(%rbp)
movq -0x60(%rbp), %rax
movl %edx, %ecx
movq -0x68(%rbp), %rdx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movl -0x50(%rbp), %eax
cltd
idivl -0x14(%rbp)
movq -0x80(%rbp), %rax
movslq %edx, %rcx
movss (%rax,%rcx,4), %xmm0
ucomiss %xmm1, %xmm0
jb 0x138fa
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl -0x50(%rbp), %edx
movl -0x54(%rbp), %ecx
xorl %r8d, %r8d
callq 0x83f0
jmp 0x13912
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl -0x50(%rbp), %edx
movl -0x54(%rbp), %ecx
movl $0xff, %r8d
callq 0x83f0
jmp 0x13914
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x13872
jmp 0x13924
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1385b
jmp 0x13934
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x13844
movq -0x20(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop