// File: jkingan[P]ladyheather/heathgps.cpp
// Function: calc_cct(int, int, double, double, double)
DATA_SIZE calc_cct(int type, int undo_scale, double red, double green, double blue)
{
int i;
DATA_SIZE val;
DATA_SIZE dif;
DATA_SIZE pct;
DATA_SIZE red_uw, green_uw, blue_uw;
DATA_SIZE ccx, ccy, ccz;
DATA_SIZE n;
DATA_SIZE cct;

   if(undo_scale) {        // red, green and blue came from get_plot_q() values
      if(show_color_uw) {  // we need to undo the scale factor that was applied to those values 
         red *= RED_SENS;     // blue
         green *= GREEN_SENS; // green
         blue *= BLUE_SENS;   // red
      }
      else if(show_color_pct) {
         // !!!!!!! we cant properly undo those values
         red_uw = (DATA_SIZE) (red * (red + green + blue));
         green_uw = (DATA_SIZE) (green * (red + green + blue));
         blue_uw = (DATA_SIZE) (blue * (red + green + blue));

         red = red_uw;
         green = green_uw;
         blue = blue_uw;
      }
   }
if(0 && cct_dbg) sprintf(plot_title, "ty=%d r=%f g=%f b=%f", type, red, green, blue);

   if(type == 0) {
      if(blue == 0.0) return 0.0;
      val = (DATA_SIZE) ((red / blue) * cct_cal);
val = rb_m * val + rb_b;

      for(i=0; i<(int) NUM_CCT; i++) {
         if(val < cctt[i].ratio) {
            if(i == 0) return 99999.999F;

            dif = cctt[i].ratio - cctt[i-1].ratio;
            if(dif == 0.0F) return 0.0F;
            pct = (val - cctt[i-1].ratio) / dif;

            dif = cctt[i].k - cctt[i-1].k;
// sprintf(plot_title, "red=%f  rob=%f  val=%f  hz=%f  i=%d  pct=%f  dif=%f", red, r_over_b, val, red_hz/blue_hz, i, pct, dif);
            dif *= pct;
            return cctt[i-1].k + dif;
         }
      }
   }
   else {
      red_uw = (DATA_SIZE) red / RED_SENS;
      green_uw = (DATA_SIZE) green / GREEN_SENS;
      blue_uw = (DATA_SIZE) blue /  BLUE_SENS;

      calc_cie(red_uw, green_uw, blue_uw);
      if(cie_sum) {
         ccx = cie_x / cie_sum;
         ccy = cie_y / cie_sum;
         ccz = cie_z / cie_sum;
         if(type == 1) {
            n = (ccx - 0.3320F) / (ccy - 0.1858F);
            cct = (-449.0F*n*n*n) + (3525.0F*n*n) - (6823.3F*n) + 5520.33F;
            if((cct < 0.0F) || (cct > 100000.0F)) cct = 0.0F;
            cct /= cct1_cal;  // apply bogus correction factor
            return cct;
         }
         else if(type == 2) {
            n = (ccx - 0.3366F) / (ccy - 0.1735F);
            cct = (DATA_SIZE) ((-949.86315F) + 6253.80338F*exp(-n/0.91259F) + 28.70599F*exp(-n/0.20039) + 0.00004F*exp(-n/0.07125F));
            if((cct < 0.0F) || (cct > 100000.0F)) cct = 0.0F;
            cct /= cct2_cal;  // apply bogus correction factor
            return cct;
         }
      }
      else return 0.0;
   }

   return 1.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movsd %xmm2, -0x28(%rbp)
cmpl $0x0, -0x10(%rbp)
je 0xa8e3f
leaq 0x2c33da(%rip), %rax    # 0x36c14b
cmpb $0x0, (%rax)
je 0xa8dba
leaq 0x2c3353(%rip), %rax    # 0x36c0d0
movsd (%rax), %xmm0
mulsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
leaq 0x2c3336(%rip), %rax    # 0x36c0c8
movsd (%rax), %xmm0
mulsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
leaq 0x2c3319(%rip), %rax    # 0x36c0c0
movsd (%rax), %xmm0
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0xa8e3d
leaq 0x2c338b(%rip), %rax    # 0x36c14c
cmpb $0x0, (%rax)
je 0xa8e3b
movsd -0x18(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
addsd -0x20(%rbp), %xmm1
addsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x20(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
addsd -0x20(%rbp), %xmm1
addsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
addsd -0x20(%rbp), %xmm1
addsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0xa8e3d
jmp 0xa8e3f
cmpl $0x0, -0xc(%rbp)
jne 0xa8fe8
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa8e66
jp 0xa8e66
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
movsd -0x18(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
leaq 0x2c32b9(%rip), %rax    # 0x36c130
mulsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
leaq 0x1d1549(%rip), %rax    # 0x27a3d0
movsd (%rax), %xmm0
movsd -0x38(%rbp), %xmm2
leaq 0x1d1541(%rip), %rax    # 0x27a3d8
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x13, -0x2c(%rbp)
jge 0xa8fe3
movsd -0x38(%rbp), %xmm1
movslq -0x2c(%rbp), %rcx
leaq 0x1206b7(%rip), %rax    # 0x1c9580
movss (%rax,%rcx,8), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa8fd3
cmpl $0x0, -0x2c(%rbp)
jne 0xa8ef4
movsd 0xc61fe(%rip), %xmm0    # 0x16f0e8
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
movslq -0x2c(%rbp), %rcx
leaq 0x120681(%rip), %rax    # 0x1c9580
movss (%rax,%rcx,8), %xmm0
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x12066c(%rip), %rax    # 0x1c9580
subss (%rax,%rcx,8), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa8f3f
jp 0xa8f3f
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
movsd -0x38(%rbp), %xmm0
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x12062c(%rip), %rax    # 0x1c9580
movss (%rax,%rcx,8), %xmm1
cvtss2sd %xmm1, %xmm1
subsd %xmm1, %xmm0
divsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movslq -0x2c(%rbp), %rcx
leaq 0x12060a(%rip), %rax    # 0x1c9580
movss 0x4(%rax,%rcx,8), %xmm0
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x1205f4(%rip), %rax    # 0x1c9580
subss 0x4(%rax,%rcx,8), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x48(%rbp), %xmm0
mulsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x1205c6(%rip), %rax    # 0x1c9580
movss 0x4(%rax,%rcx,8), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
jmp 0xa8fd5
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xa8eaf
jmp 0xa9308
movsd -0x18(%rbp), %xmm0
leaq 0x2c30dc(%rip), %rax    # 0x36c0d0
divsd (%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x20(%rbp), %xmm0
leaq 0x2c30bf(%rip), %rax    # 0x36c0c8
divsd (%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm0
leaq 0x2c30a2(%rip), %rax    # 0x36c0c0
divsd (%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x50(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movsd -0x60(%rbp), %xmm2
callq 0xa8b70
movsd 0x326f65(%rip), %xmm0   # 0x3cffa8
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa9053
jp 0xa9053
jmp 0xa92fc
movsd 0x326f35(%rip), %xmm0   # 0x3cff90
divsd 0x326f45(%rip), %xmm0   # 0x3cffa8
movsd %xmm0, -0x68(%rbp)
movsd 0x326f28(%rip), %xmm0   # 0x3cff98
divsd 0x326f30(%rip), %xmm0   # 0x3cffa8
movsd %xmm0, -0x70(%rbp)
movsd 0x326f1b(%rip), %xmm0   # 0x3cffa0
divsd 0x326f1b(%rip), %xmm0   # 0x3cffa8
movsd %xmm0, -0x78(%rbp)
cmpl $0x1, -0xc(%rbp)
jne 0xa9180
movsd -0x68(%rbp), %xmm0
movsd 0xd9cef(%rip), %xmm1    # 0x182d98
subsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm1
movsd 0xd9cd6(%rip), %xmm2    # 0x182d90
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd 0xd9cb9(%rip), %xmm1    # 0x182d88
mulsd -0x80(%rbp), %xmm1
mulsd -0x80(%rbp), %xmm1
movsd -0x80(%rbp), %xmm2
movsd 0xd9c9a(%rip), %xmm0    # 0x182d80
mulsd -0x80(%rbp), %xmm0
mulsd -0x80(%rbp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x80(%rbp), %xmm0
movsd 0xd9c73(%rip), %xmm2    # 0x182d78
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xd9c5b(%rip), %xmm1    # 0x182d70
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x88(%rbp), %xmm0
ja 0xa9144
movsd -0x88(%rbp), %xmm0
movsd 0xc5faa(%rip), %xmm1    # 0x16f0e8
ucomisd %xmm1, %xmm0
jbe 0xa914f
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
leaq 0x2c2fe2(%rip), %rax    # 0x36c138
movsd (%rax), %xmm1
movsd -0x88(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
cmpl $0x2, -0xc(%rbp)
jne 0xa92f8
movsd -0x68(%rbp), %xmm0
movsd 0xd9bd1(%rip), %xmm1    # 0x182d68
subsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm1
movsd 0xd9bb8(%rip), %xmm2    # 0x182d60
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0xd9b7f(%rip), %xmm1    # 0x182d58
divsd %xmm1, %xmm0
callq 0x96d0
movsd 0xd9b5e(%rip), %xmm1    # 0x182d48
mulsd %xmm1, %xmm0
movsd 0xd9b5a(%rip), %xmm1    # 0x182d50
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x80(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0xd9b1a(%rip), %xmm1    # 0x182d40
divsd %xmm1, %xmm0
callq 0x96d0
movsd -0x98(%rbp), %xmm1
movsd 0xd9af9(%rip), %xmm2    # 0x182d38
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x80(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0xd9abd(%rip), %xmm1    # 0x182d30
divsd %xmm1, %xmm0
callq 0x96d0
movsd -0x90(%rbp), %xmm1
movsd 0xd9a9c(%rip), %xmm2    # 0x182d28
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x88(%rbp), %xmm0
ja 0xa92bf
movsd -0x88(%rbp), %xmm0
movsd 0xc5e2f(%rip), %xmm1    # 0x16f0e8
ucomisd %xmm1, %xmm0
jbe 0xa92ca
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
leaq 0x2c2e6f(%rip), %rax    # 0x36c140
movsd (%rax), %xmm1
movsd -0x88(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
jmp 0xa92fa
jmp 0xa9306
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xa9315
jmp 0xa9308
movsd 0xc5d58(%rip), %xmm0    # 0x16f068
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)