// File: /workspace/github_c_makefile_star2_O1/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 0xb5b6(%rip), %ecx      # 0x15b10
testl %ecx, %ecx
sete %dl
movl 0xb5af(%rip), %eax      # 0x15b14
testl %eax, %eax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0xa613
leaq 0x8ed9(%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 0xa609
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, 0xb507(%rip)      # 0x15b10
testl %eax, %eax
jne 0xa613
movl %esi, 0xb501(%rip)      # 0x15b14
cvtss2sd 0xb4fd(%rip), %xmm0     # 0x15b18
movsd 0x3ec5(%rip), %xmm1     # 0xe4e8
ucomisd %xmm0, %xmm1
jb 0xa633
movl $0x38d1b717, 0xb4e5(%rip) # imm = 0x38D1B717
movss 0xb4e1(%rip), %xmm0     # 0x15b1c
xorpd %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0xa650
jp 0xa650
movl $0x3e800000, 0xb4cc(%rip) # imm = 0x3E800000
xorps %xmm0, %xmm0
cvtss2sd 0xb4c1(%rip), %xmm0     # 0x15b1c
movsd 0x3e8d(%rip), %xmm1     # 0xe4f0
ucomisd %xmm0, %xmm1
jb 0xa673
movl $0x3a83126f, 0xb4a9(%rip) # imm = 0x3A83126F
cmpl $0x0, 0xb4aa(%rip)      # 0x15b24
jne 0xa686
movl $0x7d00, 0xb49e(%rip)   # imm = 0x7D00
pushq %r14
pushq %rbx
subq $0x18, %rsp
xorps %xmm0, %xmm0
cvtsi2sdl 0xb48c(%rip), %xmm0 # 0x15b24
divsd 0x7c28(%rip), %xmm0     # 0x122c8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xa6b0
sqrtsd %xmm0, %xmm0
jmp 0xa6b5
callq 0x34a0
movsd 0x203b(%rip), %xmm1     # 0xc6f8
divsd %xmm0, %xmm1
cvtss2sd 0xb44f(%rip), %xmm4     # 0x15b18
movaps %xmm4, %xmm0
maxsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0xb441(%rip), %xmm1     # 0x15b1c
minsd %xmm0, %xmm1
movsd 0x7be1(%rip), %xmm0     # 0x122c8
movapd %xmm0, %xmm3
divsd %xmm1, %xmm3
xorps %xmm1, %xmm1
cvtsi2sdl 0xb41a(%rip), %xmm1 # 0x15b14
cvtsi2sdl 0xb40e(%rip), %xmm2 # 0x15b10
minsd %xmm3, %xmm1
maxsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
divsd %xmm2, %xmm0
xorl %eax, %eax
movb %al, 0xb40c(%rip)       # 0x15b28
movb %al, 0xb40a(%rip)       # 0x15b2c
ucomisd %xmm0, %xmm4
jbe 0xa75b
movb $0x1, 0xb3f9(%rip)      # 0x15b28
leaq 0x8882(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x3e76(%rip), %rdi      # 0xe5b6
movl $0x1e, %esi
movl $0x1, %edx
movsd %xmm4, 0x8(%rsp)
callq 0x3490
movsd 0x8(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0xb3b6(%rip), %xmm1     # 0x15b1c
ucomisd %xmm1, %xmm0
jbe 0xa79f
movb $0x1, 0xb3b9(%rip)      # 0x15b2c
leaq 0x883e(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x3e51(%rip), %rdi      # 0xe5d5
movl $0x1d, %esi
movl $0x1, %edx
movsd %xmm1, 0x8(%rsp)
callq 0x3490
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x880c(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
cvttsd2si 0x10(%rsp), %ebx
movsd 0x7b0b(%rip), %xmm1     # 0x122c8
leaq 0x3e2f(%rip), %rsi      # 0xe5f3
movl %ebx, %edx
movb $0x2, %al
callq 0x3290
movsd 0x8(%rsp), %xmm0
mulsd 0x3d1d(%rip), %xmm0     # 0xe4f8
cvttsd2si %xmm0, %ecx
leaq 0xb34a(%rip), %r14      # 0x15b30
leaq 0x3e32(%rip), %rdx      # 0xe61f
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