// File: jpbalarini[P]otsus/./algo.cpp
// Function: computeOtsusSegmentation(CImage&, unsigned int*, CImage*, int)
void computeOtsusSegmentation(CImage &input, unsigned* hist, CImage *output, int overrided_threshold){
  // Compute number of pixels
  long int N = input.get_width() * input.get_height();
  int threshold = 0;

  if (overrided_threshold != 0){
    // If threshold was manually entered
    threshold = overrided_threshold;
  }else{
    // Compute threshold
    // Init variables
    float sum = 0;
    float sumB = 0;
    int q1 = 0;
    int q2 = 0;
    float varMax = 0;

    // Auxiliary value for computing m2
    for (int i = 0; i <= MAX_INTENSITY; i++){
      sum += i * ((int)hist[i]);
    }

    for (int i = 0 ; i <= MAX_INTENSITY ; i++) {
      // Update q1
      q1 += hist[i];
      if (q1 == 0)
        continue;
      // Update q2
      q2 = N - q1;

      if (q2 == 0)
        break;
      // Update m1 and m2
      sumB += (float) (i * ((int)hist[i]));
      float m1 = sumB / q1;
      float m2 = (sum - sumB) / q2;

      // Update the between class variance
      float varBetween = (float) q1 * (float) q2 * (m1 - m2) * (m1 - m2);

      // Update the threshold if necessary
      if (varBetween > varMax) {
        varMax = varBetween;
        threshold = i;
      }
    }
  }

  // Perform the segmentation
  segmentImage(input, output, threshold);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x82d0
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x82b0
movl %eax, %ecx
movl -0x58(%rbp), %eax
imull %ecx, %eax
cltq
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xaef2
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xb041
xorps %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x83f0(%rip), %eax      # 0x13318
jg 0xaf52
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x44(%rbp), %rdx
imull (%rcx,%rdx,4), %eax
cvtsi2ss %eax, %xmm0
addss -0x30(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xaf1f
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl 0x83b6(%rip), %eax      # 0x13318
jg 0xb03f
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0xaf84
jmp 0xb031
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0xaf9d
jmp 0xb03f
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x48(%rbp), %rdx
imull (%rcx,%rdx,4), %eax
cvtsi2ss %eax, %xmm0
addss -0x34(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x34(%rbp), %xmm0
cvtsi2ssl -0x38(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x30(%rbp), %xmm0
subss -0x34(%rbp), %xmm0
cvtsi2ssl -0x3c(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x50(%rbp)
cvtsi2ssl -0x38(%rbp), %xmm0
cvtsi2ssl -0x3c(%rbp), %xmm1
mulss %xmm1, %xmm0
movss -0x4c(%rbp), %xmm1
subss -0x50(%rbp), %xmm1
mulss %xmm1, %xmm0
movss -0x4c(%rbp), %xmm1
subss -0x50(%rbp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x54(%rbp)
movss -0x54(%rbp), %xmm0
ucomiss -0x40(%rbp), %xmm0
jbe 0xb02f
movss -0x54(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xb031
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xaf59
jmp 0xb041
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0xade0
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)