// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0xbf13(%rip), %r12d     # 0x15b20
testl %r12d, %r12d
sete %al
movl 0xbf0b(%rip), %ebp      # 0x15b24
testl %ebp, %ebp
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x9ca0
movl $0x1028, %r14d          # imm = 0x1028
addq 0x83a6(%rip), %r14      # 0x11fd8
leaq 0x49c1(%rip), %rsi      # 0xe5fa
pushq $0xa
popq %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x33a0
movl %eax, %ebx
testl %eax, %eax
movl $0x640, %eax            # imm = 0x640
movl $0x320, %r13d           # imm = 0x320
cmovel %eax, %r13d
leaq 0x49a2(%rip), %rsi      # 0xe604
movq %r14, %rdi
movq %r15, %rdx
callq 0x33a0
testl %eax, %eax
movl $0xc80, %ecx            # imm = 0xC80
cmovnel %r13d, %ecx
testl %r12d, %r12d
jne 0x9c96
testl %ebx, %ebx
pushq $0x32
popq %rdx
pushq $0x64
popq %rsi
cmovel %edx, %esi
testl %eax, %eax
pushq $0x28
popq %rax
cmovnel %esi, %eax
movl %eax, 0xbe8a(%rip)      # 0x15b20
testl %ebp, %ebp
jne 0x9ca0
movl %ecx, 0xbe84(%rip)      # 0x15b24
cvtss2sd 0xbe80(%rip), %xmm0     # 0x15b28
movsd 0x4878(%rip), %xmm1     # 0xe528
ucomisd %xmm0, %xmm1
jb 0x9cc0
movl $0x38d1b717, 0xbe68(%rip) # imm = 0x38D1B717
movss 0xbe64(%rip), %xmm1     # 0x15b2c
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
cmpeqss %xmm1, %xmm0
movss 0x1705(%rip), %xmm2     # 0xb3e0
andps %xmm0, %xmm2
andnps %xmm1, %xmm0
orps %xmm2, %xmm0
cvtss2sd %xmm0, %xmm1
jne 0x9cec
jnp 0x9cfa
movsd 0x483c(%rip), %xmm2     # 0xe530
ucomisd %xmm1, %xmm2
jb 0x9d18
movsd 0x482e(%rip), %xmm2     # 0xe530
ucomisd %xmm1, %xmm2
jb 0x9d10
movss 0x48a0(%rip), %xmm0     # 0xe5b0
movss %xmm0, 0xbe14(%rip)     # 0x15b2c
movl 0xbe16(%rip), %eax      # 0x15b34
testl %eax, %eax
jne 0x9d31
movl $0x7d00, 0xbe08(%rip)   # imm = 0x7D00
movl $0x7d00, %eax           # imm = 0x7D00
cvtsi2sd %eax, %xmm0
divsd 0x859b(%rip), %xmm0     # 0x122d8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x9d4d
sqrtsd %xmm0, %xmm0
jmp 0x9d52
callq 0x34c0
movsd 0x29ae(%rip), %xmm1     # 0xc708
divsd %xmm0, %xmm1
cvtss2sd 0xbdc2(%rip), %xmm5     # 0x15b28
movaps %xmm5, %xmm0
maxsd %xmm1, %xmm0
cvtss2sd 0xbdb7(%rip), %xmm3     # 0x15b2c
movaps %xmm3, %xmm1
minsd %xmm0, %xmm1
movsd 0x8554(%rip), %xmm0     # 0x122d8
movapd %xmm0, %xmm4
divsd %xmm1, %xmm4
cvtsi2sdl 0xbd90(%rip), %xmm1 # 0x15b24
cvtsi2sdl 0xbd84(%rip), %xmm2 # 0x15b20
minsd %xmm4, %xmm1
maxsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
divsd %xmm2, %xmm0
xorl %eax, %eax
movb %al, 0xbd82(%rip)       # 0x15b38
movb %al, 0xbd80(%rip)       # 0x15b3c
ucomisd %xmm0, %xmm5
jbe 0x9df9
movb $0x1, 0xbd6f(%rip)      # 0x15b38
leaq 0x91f8(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x4834(%rip), %rdi      # 0xe60e
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
movsd %xmm5, 0x8(%rsp)
callq 0x34b0
cvtss2sd 0xbd39(%rip), %xmm3     # 0x15b2c
movsd 0x8(%rsp), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x9e2e
movb $0x1, 0xbd36(%rip)      # 0x15b3c
leaq 0x91bb(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x4816(%rip), %rdi      # 0xe62d
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
movsd %xmm3, 0x8(%rsp)
callq 0x34b0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x918d(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
cvttsd2si 0x10(%rsp), %ebx
movsd 0x848c(%rip), %xmm1     # 0x122d8
leaq 0x47f8(%rip), %rsi      # 0xe64b
movl %ebx, %edx
movb $0x2, %al
callq 0x32a0
movsd 0x8(%rsp), %xmm0
mulsd 0x46ce(%rip), %xmm0     # 0xe538
cvttsd2si %xmm0, %ecx
leaq 0xbccb(%rip), %r14      # 0x15b40
leaq 0x47fb(%rip), %rdx      # 0xe677
pushq $0x32
popq %rsi
movq %r14, %rdi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3190
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq