// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebx
testl %esi, %esi
je 0x7ad46
leaq 0x292271(%rip), %rax    # 0x30cf6b
cmpb $0x0, (%rax)
je 0x7ad22
leaq 0x2921ea(%rip), %rax    # 0x30cef0
mulsd (%rax), %xmm0
leaq 0x2921d7(%rip), %rax    # 0x30cee8
mulsd (%rax), %xmm1
leaq 0x2921c4(%rip), %rax    # 0x30cee0
mulsd (%rax), %xmm2
jmp 0x7ad46
leaq 0x292243(%rip), %rax    # 0x30cf6c
cmpb $0x0, (%rax)
je 0x7ad46
movapd %xmm0, %xmm3
addsd %xmm1, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
testl %ebx, %ebx
je 0x7ae30
leaq 0x29219b(%rip), %rax    # 0x30cef0
divsd (%rax), %xmm0
leaq 0x292188(%rip), %rax    # 0x30cee8
divsd (%rax), %xmm1
leaq 0x292175(%rip), %rax    # 0x30cee0
divsd (%rax), %xmm2
callq 0x7ab99
movsd 0x2f605c(%rip), %xmm1   # 0x370dd8
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
jne 0x7ad8c
jnp 0x7af5f
movsd 0x2f602c(%rip), %xmm4   # 0x370dc0
divsd %xmm1, %xmm4
movsd 0x2f6028(%rip), %xmm0   # 0x370dc8
divsd %xmm1, %xmm0
cmpl $0x2, %ebx
je 0x7ae9e
cmpl $0x1, %ebx
jne 0x7af57
addsd 0xa3532(%rip), %xmm4    # 0x11e2f0
addsd 0xa3532(%rip), %xmm0    # 0x11e2f8
divsd %xmm0, %xmm4
movsd 0xa352e(%rip), %xmm0    # 0x11e300
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
movsd 0xa3526(%rip), %xmm1    # 0x11e308
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
mulsd 0xa3516(%rip), %xmm4    # 0x11e310
addsd %xmm0, %xmm4
addsd 0xa3512(%rip), %xmm4    # 0x11e318
movsd 0x8e2ba(%rip), %xmm0    # 0x1090c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x29212d(%rip), %rax    # 0x30cf58
jmp 0x7af51
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm2
jne 0x7ae40
jnp 0x7af5f
divsd %xmm2, %xmm0
leaq 0x292105(%rip), %rax    # 0x30cf50
mulsd (%rax), %xmm0
leaq 0x1a039a(%rip), %rax    # 0x21b1f0
leaq 0x1a039b(%rip), %rcx    # 0x21b1f8
mulsd (%rax), %xmm0
addsd (%rcx), %xmm0
xorl %eax, %eax
leaq 0xef722(%rip), %rcx     # 0x16a590
movsd 0x8e1fa(%rip), %xmm2    # 0x109070
movss (%rcx,%rax,8), %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
ja 0x7af6a
incq %rax
cmpq $0x13, %rax
jne 0x7ae76
movapd %xmm2, %xmm4
jmp 0x7af5f
addsd 0xa3402(%rip), %xmm4    # 0x11e2a8
addsd 0xa3402(%rip), %xmm0    # 0x11e2b0
xorpd 0x8e2ea(%rip), %xmm4    # 0x1091a0
divsd %xmm0, %xmm4
movapd %xmm4, -0x20(%rbp)
divsd 0xa33f1(%rip), %xmm4    # 0x11e2b8
movapd %xmm4, %xmm0
callq 0x96f0
mulsd 0xa33e8(%rip), %xmm0    # 0x11e2c0
addsd 0xa33e8(%rip), %xmm0    # 0x11e2c8
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0xa33de(%rip), %xmm0    # 0x11e2d0
callq 0x96f0
mulsd 0xa33d9(%rip), %xmm0    # 0x11e2d8
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0xa33ca(%rip), %xmm0    # 0x11e2e0
callq 0x96f0
movapd %xmm0, %xmm4
mulsd 0xa33c1(%rip), %xmm4    # 0x11e2e8
addsd -0x10(%rbp), %xmm4
movsd 0x8e194(%rip), %xmm0    # 0x1090c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x29200f(%rip), %rax    # 0x30cf60
divsd (%rax), %xmm4
jmp 0x7af5f
movsd 0x8e111(%rip), %xmm4    # 0x109070
movapd %xmm4, %xmm0
addq $0x18, %rsp
popq %rbx
popq %rbp
retq
testq %rax, %rax
je 0x7afb9
movss -0x8(%rcx,%rax,8), %xmm2
subss %xmm2, %xmm1
xorpd %xmm3, %xmm3
ucomiss %xmm3, %xmm1
jne 0x7af84
jnp 0x7af5f
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
subsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movss -0x4(%rcx,%rax,8), %xmm1
movss 0x4(%rcx,%rax,8), %xmm2
subss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm0, %xmm2
xorps %xmm4, %xmm4
cvtss2sd %xmm1, %xmm4
addsd %xmm2, %xmm4
jmp 0x7af5f
movsd 0x8e107(%rip), %xmm4    # 0x1090c8
jmp 0x7af5f