// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c08, %rsp           # imm = 0x1C08
movq %rsi, %rbx
movq %rdi, %r14
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0xca2c
andl $0x0, (%rsp,%rax,4)
incq %rax
jmp 0xca1b
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
movq 0x18(%r14), %rdx
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movl 0x10(%r14), %r9d
movl %eax, %r10d
imull %r9d, %r10d
xorl %r11d, %r11d
movl %esi, %r15d
movq %rdi, %r12
cmpl %r8d, %r11d
je 0xca88
subq $0x1, %r12
jb 0xca80
movslq %r15d, %r15
movzbl (%rdx,%r15), %r13d
incl (%rsp,%r13,4)
addl %r9d, %r15d
jmp 0xca69
incl %r11d
addl %r10d, %esi
jmp 0xca5e
imull %ecx, %eax
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0xcab0
cvtsi2sdl (%rsp,%rax,4), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x1400(%rsp,%rax,8)
incq %rax
jmp 0xca91
andq $0x0, 0x400(%rsp)
movsd 0x1400(%rsp), %xmm0
movsd %xmm0, 0xc00(%rsp)
pushq $0x1
popq %rax
xorpd %xmm1, %xmm1
cmpq $0x100, %rax            # imm = 0x100
je 0xcb0a
movsd 0x1400(%rsp,%rax,8), %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, 0xc00(%rsp,%rax,8)
cvtsi2sd %eax, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, 0x400(%rsp,%rax,8)
incq %rax
jmp 0xcad2
xorpd %xmm2, %xmm2
movsd 0xbf8(%rsp), %xmm0
xorl %eax, %eax
movsd 0x3767(%rip), %xmm1     # 0x10288
cmpq $0xff, %rax
je 0xcb7f
movsd 0xc00(%rsp,%rax,8), %xmm4
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm4
jne 0xcb3e
jnp 0xcb6b
ucomisd %xmm1, %xmm4
jne 0xcb46
jnp 0xcb6b
movapd %xmm0, %xmm3
mulsd %xmm4, %xmm3
subsd 0x400(%rsp,%rax,8), %xmm3
mulsd %xmm3, %xmm3
movapd %xmm1, %xmm5
subsd %xmm4, %xmm5
mulsd %xmm4, %xmm5
divsd %xmm5, %xmm3
ucomisd %xmm2, %xmm3
cmoval %eax, %ebp
maxsd %xmm2, %xmm3
incq %rax
movapd %xmm3, %xmm2
jmp 0xcb21
leaq 0x4331(%rip), %rdi      # 0x10eb7
xorl %r15d, %r15d
movl %ebp, %esi
xorl %eax, %eax
callq 0x2050
cmpl 0xc(%rbx), %r15d
jge 0xcbdf
xorl %eax, %eax
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
jge 0xcbda
movq 0x18(%r14), %rdx
movl 0x8(%r14), %esi
imull %r15d, %esi
addl %eax, %esi
imull 0x10(%r14), %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
cmpl %edx, %ebp
setl %dl
movq 0x18(%rbx), %rsi
imull %r15d, %ecx
addl %eax, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
negb %dl
movb %dl, (%rsi,%rcx)
incl %eax
jmp 0xcb9a
incl %r15d
jmp 0xcb92
addq $0x1c08, %rsp           # imm = 0x1C08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop