// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/exposure.c
// Function: GetRaspistillExpParms
char * GetRaspistillExpParms()
{
    // if ISO min/max are not configured manually, set the according to camera module.
    if (ex.ISOmin == 0 || ex.ISOmax == 0){
        int min, max;
        if (memcmp(ImageInfo.CameraModel, "RP_ov5647",10) == 0){
            //V1 (5 mp) camera module
            min = 100; max = 800;
        }
        if (memcmp(ImageInfo.CameraModel, "RP_imx219",10) == 0){
            // V2 (8 mp) camera module.
            min = 50; max = 1600;
        }
        if (memcmp(ImageInfo.CameraModel, "RP_imx477",10) == 0){
            // HQ (12 mp) camera module.
            min = 40; max = 3200;
        }
        if (ex.ISOmin == 0) ex.ISOmin = min;
        if (ex.ISOmax == 0) ex.ISOmax = max;
    }
    // Set Tmin and max if not configured.
    if (ex.Tmin <= 0.0001)  ex.Tmin = 0.0001;
    if (ex.Tmax == 0) ex.Tmax = 0.25;
    if (ex.Tmax <= 0.001)  ex.Tmax = 0.001;
    if (ex.ISOoverExTime == 0) ex.ISOoverExTime = 32000;



    double ExTime = 1/sqrt(ex.ISOoverExTime/ISOtimesExp);

    if (ExTime < ex.Tmin) ExTime = ex.Tmin;
    if (ExTime > ex.Tmax) ExTime = ex.Tmax;
    double ISO = ISOtimesExp / ExTime;

    // Apply limits to ISO.
    if (ISO > ex.ISOmax) ISO = ex.ISOmax;
    if (ISO < ex.ISOmin) ISO = ex.ISOmin;

    // Re-compute exposure time, in case we hit ISO rails.
    ExTime = ISOtimesExp / ISO;

    // Re-apply limits to exposure time.
    ExMaxLimitHit = ExMinLimitHit = 0;
    if (ExTime < ex.Tmin) {
        ExTime = ex.Tmin;
        ExMinLimitHit = 1;// too much light.  Kind of unlikely.
        fprintf(Log,"Exposure at min ISO and time.\n");
    }
    if (ExTime > ex.Tmax) {
        ExTime = ex.Tmax;
        ExMaxLimitHit = 1;// Dark image, but further exposure increase not possible.
        fprintf(Log,"Exposure at max ISO and time\n");
    }

    fprintf(Log,"New exposure: t=%0.5f ISO=%d ISO*Exp=%5.0f\n",ExTime, (int)ISO,ISOtimesExp);

    static char RaspiParms[50];
    snprintf(RaspiParms, 50, " -ss %d -ISO %d",(int)(ExTime*1000000), (int)ISO);

    //printf("Raspiparms: '%s'\n",RaspiParms);
    return RaspiParms;
}

// Assembly:
movl 0xb5ce(%rip), %ecx      # 0x15b10
testl %ecx, %ecx
sete %dl
movl 0xb5c7(%rip), %eax      # 0x15b14
testl %eax, %eax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0xa5fb
leaq 0x8ef1(%rip), %rdx      # 0x13458
movq 0x1028(%rdx), %rsi
movabsq $0x3132786d695f5052, %rdi # imm = 0x3132786D695F5052
xorq %rsi, %rdi
movzwl 0x1030(%rdx), %r9d
movq %r9, %r8
xorq $0x39, %r8
xorl %edx, %edx
orq %rdi, %r8
setne %dil
movl $0x640, %r8d            # imm = 0x640
movl $0x320, %r10d           # imm = 0x320
cmovel %r8d, %r10d
movabsq $0x3734786d695f5052, %r11 # imm = 0x3734786D695F5052
xorq %rsi, %r11
xorq $0x37, %r9
xorl %r8d, %r8d
orq %r11, %r9
setne %r9b
movl $0xc80, %esi            # imm = 0xC80
cmovnel %r10d, %esi
testl %ecx, %ecx
jne 0xa5f1
movb %dil, %dl
movb %r9b, %r8b
testl %edx, %edx
movl $0x32, %ecx
movl $0x64, %edx
cmovel %ecx, %edx
testl %r8d, %r8d
movl $0x28, %ecx
cmovnel %edx, %ecx
movl %ecx, 0xb51f(%rip)      # 0x15b10
testl %eax, %eax
jne 0xa5fb
movl %esi, 0xb519(%rip)      # 0x15b14
cvtss2sd 0xb515(%rip), %xmm0     # 0x15b18
movsd 0x3f4d(%rip), %xmm1     # 0xe558
ucomisd %xmm0, %xmm1
jb 0xa61b
movl $0x38d1b717, 0xb4fd(%rip) # imm = 0x38D1B717
movss 0xb4f9(%rip), %xmm1     # 0x15b1c
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
cmpeqss %xmm1, %xmm0
movss 0xdaa(%rip), %xmm2      # 0xb3e0
andps %xmm0, %xmm2
andnps %xmm1, %xmm0
orps %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
jne 0xa64a
jnp 0xa658
movsd 0x3f0e(%rip), %xmm2     # 0xe560
ucomisd %xmm1, %xmm2
jb 0xa676
movsd 0x3f00(%rip), %xmm2     # 0xe560
ucomisd %xmm1, %xmm2
jb 0xa66e
movss 0x3f72(%rip), %xmm0     # 0xe5e0
movss %xmm0, 0xb4a6(%rip)     # 0x15b1c
movl 0xb4a8(%rip), %eax      # 0x15b24
testl %eax, %eax
jne 0xa68f
movl $0x7d00, 0xb49a(%rip)   # imm = 0x7D00
movl $0x7d00, %eax           # imm = 0x7D00
pushq %r14
pushq %rbx
subq $0x18, %rsp
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x7c2b(%rip), %xmm0     # 0x122d0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xa6b5
sqrtsd %xmm0, %xmm0
jmp 0xa6ba
callq 0x34b0
movsd 0x2046(%rip), %xmm1     # 0xc708
divsd %xmm0, %xmm1
cvtss2sd 0xb44a(%rip), %xmm5     # 0x15b18
movaps %xmm5, %xmm0
maxsd %xmm1, %xmm0
cvtss2sd 0xb43f(%rip), %xmm4     # 0x15b1c
movaps %xmm4, %xmm1
minsd %xmm0, %xmm1
movsd 0x7be4(%rip), %xmm0     # 0x122d0
movapd %xmm0, %xmm3
divsd %xmm1, %xmm3
xorps %xmm1, %xmm1
cvtsi2sdl 0xb415(%rip), %xmm1 # 0x15b14
xorps %xmm2, %xmm2
cvtsi2sdl 0xb406(%rip), %xmm2 # 0x15b10
minsd %xmm3, %xmm1
maxsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
divsd %xmm2, %xmm0
xorl %eax, %eax
movb %al, 0xb404(%rip)       # 0x15b28
movb %al, 0xb402(%rip)       # 0x15b2c
ucomisd %xmm0, %xmm5
jbe 0xa76e
movb $0x1, 0xb3f1(%rip)      # 0x15b28
leaq 0x887a(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x3ef6(%rip), %rdi      # 0xe63e
movl $0x1e, %esi
movl $0x1, %edx
movsd %xmm5, 0x8(%rsp)
callq 0x34a0
xorps %xmm4, %xmm4
cvtss2sd 0xb3b4(%rip), %xmm4     # 0x15b1c
movsd 0x8(%rsp), %xmm0
ucomisd %xmm4, %xmm0
jbe 0xa7a7
movb $0x1, 0xb3b1(%rip)      # 0x15b2c
leaq 0x8836(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x3ed1(%rip), %rdi      # 0xe65d
movl $0x1d, %esi
movl $0x1, %edx
movsd %xmm4, 0x8(%rsp)
callq 0x34a0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x8804(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
cvttsd2si 0x10(%rsp), %ebx
movsd 0x7b0b(%rip), %xmm1     # 0x122d0
leaq 0x3eaf(%rip), %rsi      # 0xe67b
movl %ebx, %edx
movb $0x2, %al
callq 0x32a0
movsd 0x8(%rsp), %xmm0
mulsd 0x3d85(%rip), %xmm0     # 0xe568
cvttsd2si %xmm0, %ecx
leaq 0xb342(%rip), %r14      # 0x15b30
leaq 0x3eb2(%rip), %rdx      # 0xe6a7
movl $0x32, %esi
movq %r14, %rdi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3190
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
retq