// File: /workspace/github_cpp_makefile_star2_O2/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 0x70323
leaq 0x286c94(%rip), %rax    # 0x2f6f6b
cmpb $0x0, (%rax)
je 0x702ff
leaq 0x286c0d(%rip), %rax    # 0x2f6ef0
mulsd (%rax), %xmm0
leaq 0x286bfa(%rip), %rax    # 0x2f6ee8
mulsd (%rax), %xmm1
leaq 0x286be7(%rip), %rax    # 0x2f6ee0
mulsd (%rax), %xmm2
jmp 0x70323
leaq 0x286c66(%rip), %rax    # 0x2f6f6c
cmpb $0x0, (%rax)
je 0x70323
movapd %xmm0, %xmm3
addsd %xmm1, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
testl %ebx, %ebx
je 0x7040d
leaq 0x286bbe(%rip), %rax    # 0x2f6ef0
divsd (%rax), %xmm0
leaq 0x286bab(%rip), %rax    # 0x2f6ee8
divsd (%rax), %xmm1
leaq 0x286b98(%rip), %rax    # 0x2f6ee0
divsd (%rax), %xmm2
callq 0x70199
movsd 0x2eaa7f(%rip), %xmm1   # 0x35add8
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
jne 0x70369
jnp 0x70594
movsd 0x2eaa4f(%rip), %xmm4   # 0x35adc0
divsd %xmm1, %xmm4
movsd 0x2eaa4b(%rip), %xmm0   # 0x35adc8
divsd %xmm1, %xmm0
cmpl $0x2, %ebx
je 0x70475
cmpl $0x1, %ebx
jne 0x7052e
addsd 0x9806d(%rip), %xmm4    # 0x108408
addsd 0x9806d(%rip), %xmm0    # 0x108410
divsd %xmm0, %xmm4
movsd 0x98069(%rip), %xmm0    # 0x108418
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
movsd 0x98061(%rip), %xmm1    # 0x108420
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
mulsd 0x98051(%rip), %xmm4    # 0x108428
addsd %xmm0, %xmm4
addsd 0x9804d(%rip), %xmm4    # 0x108430
movsd 0x82cdd(%rip), %xmm0    # 0xf30c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x286b50(%rip), %rax    # 0x2f6f58
jmp 0x70528
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm2
jne 0x7041d
jnp 0x70594
divsd %xmm2, %xmm0
leaq 0x286b28(%rip), %rax    # 0x2f6f50
mulsd (%rax), %xmm0
leaq 0x194dbd(%rip), %rax    # 0x2051f0
leaq 0x194dbe(%rip), %rcx    # 0x2051f8
mulsd (%rax), %xmm0
addsd (%rcx), %xmm0
xorl %eax, %eax
movsd 0x82c24(%rip), %xmm2    # 0xf3070
leaq 0xe413d(%rip), %rcx     # 0x154590
cmpq $0x13, %rax
je 0x70538
movss (%rcx,%rax,8), %xmm1
cvtss2sd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
ja 0x7053e
incq %rax
jmp 0x70453
addsd 0x97f43(%rip), %xmm4    # 0x1083c0
addsd 0x97f43(%rip), %xmm0    # 0x1083c8
xorpd 0x82d23(%rip), %xmm4    # 0xf31b0
divsd %xmm0, %xmm4
movapd %xmm4, -0x20(%rbp)
divsd 0x97f32(%rip), %xmm4    # 0x1083d0
movapd %xmm4, %xmm0
callq 0x96f0
mulsd 0x97f29(%rip), %xmm0    # 0x1083d8
addsd 0x97f29(%rip), %xmm0    # 0x1083e0
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0x97f1f(%rip), %xmm0    # 0x1083e8
callq 0x96f0
mulsd 0x97f1a(%rip), %xmm0    # 0x1083f0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0x97f0b(%rip), %xmm0    # 0x1083f8
callq 0x96f0
movapd %xmm0, %xmm4
mulsd 0x97f02(%rip), %xmm4    # 0x108400
addsd -0x10(%rbp), %xmm4
movsd 0x82bbd(%rip), %xmm0    # 0xf30c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x286a38(%rip), %rax    # 0x2f6f60
divsd (%rax), %xmm4
jmp 0x70594
movsd 0x82b3a(%rip), %xmm4    # 0xf3070
jmp 0x70594
movapd %xmm2, %xmm4
jmp 0x70594
testq %rax, %rax
je 0x7058c
leal -0x1(%rax), %edx
movss (%rcx,%rdx,8), %xmm2
subss %xmm2, %xmm1
xorpd %xmm3, %xmm3
ucomiss %xmm3, %xmm1
jne 0x7055a
jnp 0x70594
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
subsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movss 0x4(%rcx,%rax,8), %xmm1
movss 0x4(%rcx,%rdx,8), %xmm2
subss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
cvtss2sd %xmm2, %xmm4
addsd %xmm1, %xmm4
jmp 0x70594
movsd 0x82b34(%rip), %xmm4    # 0xf30c8
movapd %xmm4, %xmm0
addq $0x18, %rsp
popq %rbx
popq %rbp
retq