// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::otsu_th(Image&, Image&)
void ImageProc::otsu_th(Image &img_orig, Image &img_binary)
{
  int levels = 256;
  int hist[levels];
  double prob[levels], omega[levels]; //prob of graylevels
  double myu[levels];   //mean value for separation
  double max_sigma, sigma[levels]; //inter-class variance
  int i, threshold;

  for (i = 0; i < levels; i++) hist[i] = 0;
  for (int i = 0; i < img_orig.rows; i++)
    for (int j = 0; j < img_orig.cols; j++)
    {
      hist[img_orig.getvalue(i,j,0)]++;
    }


  for ( i = 0; i < levels; i ++ ) 
  {
    prob[i] = (double)hist[i] / (img_orig.rows * img_orig.cols);
  }


  omega[0] = prob[0];
  myu[0] = 0.0;
  for (i = 1; i < levels; i++) {
    omega[i] = omega[i-1] + prob[i];
    myu[i] = myu[i-1] + i*prob[i];

  }


  //threshold = 0;
  max_sigma = 0.0;
  for (i = 0; i < levels-1; i++) {
    if (omega[i] != 0.0 && omega[i] != 1.0)
      sigma[i] = pow(myu[levels-1]*omega[i] - myu[i], 2) /
	(omega[i]*(1.0 - omega[i]));
    else
      sigma[i] = 0.0;
    if (sigma[i] > max_sigma) {
      max_sigma = sigma[i];
      threshold = i;
    }
  }

   printf("\nthreshold value = %d\n", threshold);


  for (int i = 0; i < img_binary.rows; i++)
    for (int j = 0; j < img_binary.cols; j++)
      if (img_orig.getvalue(i,j,0) > threshold)
		img_binary.setvalue(i,j,0,255);
      else
		img_binary.setvalue(i,j,0,0);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x100, -0x14(%rbp)     # imm = 0x100
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x17683
movq -0x90(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1765e
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x176e7
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x176da
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x60(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
movl %eax, %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1769d
jmp 0x176dc
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1768a
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x17738
movq -0x88(%rbp), %rax
movq -0x90(%rbp), %rcx
movslq -0x54(%rbp), %rdx
cvtsi2sdl (%rcx,%rdx,4), %xmm0
movq -0x8(%rbp), %rcx
movl 0xc(%rcx), %ecx
movq -0x8(%rbp), %rdx
imull 0x8(%rdx), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x176ee
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rdx
movsd (%rdx), %xmm0
movsd %xmm0, (%rcx)
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x177cc
movq -0x78(%rbp), %rax
movq -0x88(%rbp), %rcx
movq -0x80(%rbp), %rdx
movl -0x54(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
movsd (%rdx,%rsi,8), %xmm0
movslq -0x54(%rbp), %rsi
addsd (%rcx,%rsi,8), %xmm0
movslq -0x54(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movl -0x54(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm1
cvtsi2sdl -0x54(%rbp), %xmm0
movslq -0x54(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1775d
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x178fd
movq -0x80(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x17809
jp 0x17809
jmp 0x178b1
movq -0x80(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x4a4a(%rip), %xmm1     # 0x1c268
ucomisd %xmm1, %xmm0
jne 0x1782b
jp 0x1782b
jmp 0x178b1
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rax
movl -0x14(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm0
movslq -0x54(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x1b490
movq -0x80(%rbp), %rcx
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
movslq -0x54(%rbp), %rdx
movsd 0x49cf(%rip), %xmm2     # 0x1c268
subsd (%rcx,%rdx,8), %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x178c1
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x178ed
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x178ef
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x177db
movl -0x58(%rbp), %esi
leaq 0x54d0(%rip), %rdi      # 0x1cdd7
movb $0x0, %al
callq 0x2050
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x17992
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x17985
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cmpl -0x58(%rbp), %eax
jle 0x17963
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x83f0
jmp 0x17978
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x83f0
jmp 0x1797a
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x17928
jmp 0x17987
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x17915
movq -0x20(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop