// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/ImageProcs.cpp
// Function: addSensorNoiseWithAutoISO(Image3ChnlReal&, bool, NoiseParam)
void addSensorNoiseWithAutoISO(Image3ChnlReal &rgbImage, bool applyBayer, NoiseParam noiseParams)
{
	cout << "Adding color sensor noise with autoISO..." << endl;
	
	//Check to see if the image is normalized
	int totalPixels = rgbImage.totalPixels();
	Float_t maxVal = 0;
	for (int c = 0; c < 3; c++)
		for (int i = 0; i < totalPixels; i++)
		{
			assert((*rgbImage[c])[i] >= 0 && (*rgbImage[c])[i] <= 1.0);
			if ((*rgbImage[c])[i] > maxVal)
				maxVal = (*rgbImage[c])[i];
		}
	if (maxVal != 1.0)
		cout << "  ***  NOTE  ***  This input image is not normalized yet" << endl;
	
	cout << "Performing auto-ISO search for proper amplifier gain to achieve good saturation..." << endl;
	
	//Perform "auto-ISO" by altering the amplifier gain until image saturation is achieved which matches the input image.
	//This is not how a digital camera would perform auto-ISO (there is no "input image").
	//A real camera would aim for some rational indication of good saturation,
	//e.g., a mean pixel value of X some fraction of 1.0, or perhaps some ratio Y of clipped pixels to total pixels, something like that.
	//Using the input image as the target saturation ensures that all output images have the same saturation and that they correspond to the input image,
	//which then enables subsequent comparisons across a set of images (PSNR, Lab Delta E, etc.).
	Float_t nonOutlierMeanPixelValOri = calcNonoutlierMeanPixelValue(rgbImage, 3);
	cout << "  Input image mean intensity (auto-gain target intensity): " << nonOutlierMeanPixelValOri << endl << endl;
	Image3ChnlReal rgbImageOri = rgbImage;
	Float_t maxPPFD = luxToPPFD(noiseParams.lux_, 0, noiseParams.focalRatio_, noiseParams.pixelPitchMicrons_, noiseParams.fullWellCapacity_);
	noiseParams.ampGain_ = noiseParams.fullWellCapacity_ / (maxPPFD * noiseParams.exposureSecs_);
	Float_t stepSize = noiseParams.ampGain_ * .5;	//Beginning of binary search (well, approximately a binary search at any rate)
	vector<pair<Float_t, Float_t> > ampGainHistory;
	Float_t nonOutlierMeanPixelVal = 0;
	int lastStepDir = 0;
	while (true)
	{
		cout << endl << "--------------------------------------------------------------------------------" << endl;
		
		int historyIdx = 0;
		for (historyIdx = 0; historyIdx < ampGainHistory.size(); historyIdx++)
			if (ampGainHistory[historyIdx].first == noiseParams.ampGain_)
				break;
		
		int numSaturatedPixels = totalPixels;
		if (historyIdx == ampGainHistory.size())
		{
			//Restore the original image
			rgbImage = rgbImageOri;
			
			//Noisify the image
			cout << endl << setw(40) << left << "  Noisifying with ags: " << noiseParams.ampGain_ << endl;
			for (int c = 0; c < 3; c++)
				addSensorNoise(*rgbImage[c], applyBayer, noiseParams);
			
			//Determine the saturation
			nonOutlierMeanPixelVal = calcNonoutlierMeanPixelValue(rgbImage, 3);
			cout << endl << "    nonOutlierMeanPixelVal: " << nonOutlierMeanPixelVal << endl;
		}
		else	//This amp gain has already been tried
		{
			maxVal = ampGainHistory[historyIdx].second;
			cout << "  Already tried amp gain " << noiseParams.ampGain_ << ".  maxVal = " << maxVal << endl;
		}
		
		if (nonOutlierMeanPixelVal >= nonOutlierMeanPixelValOri - kAutoGainConvergence
			&& nonOutlierMeanPixelVal <= nonOutlierMeanPixelValOri + kAutoGainConvergence)	//Acceptable saturation
		{
			assert(historyIdx == ampGainHistory.size());
			cout << "    Good saturation achieved" << endl;
			break;
		}
		
		if (stepSize < .001)
		{
			cout << "Bailing on binary search, step size is too small." << endl;
			break;
		}
		
		ampGainHistory.push_back(pair<Float_t, Float_t>(noiseParams.ampGain_, maxVal));
		
		//cout << "    lastStepDir:" << setw(21) << lastStepDir << endl;
		if (nonOutlierMeanPixelVal < nonOutlierMeanPixelValOri - kAutoGainConvergence)	//Increase gain
		{
			if (lastStepDir == -1)
				stepSize *= .5;
			//cout << "    Increasing ags from " << setw(13) << ampGainScalar;
			noiseParams.ampGain_ += stepSize;
			lastStepDir = 1;
			//cout << " to " << setw(10) << ampGainScalar << " with ss " << setw(10) << stepSize << endl;
		}
		else	//Decrease gain
		{
			if (lastStepDir == 1)
				stepSize *= .5;
			//cout << "    Decreasing ags from " << setw(13) << ampGainScalar;
			noiseParams.ampGain_ -= stepSize;
			if (noiseParams.ampGain_ < 1)
			{
				noiseParams.ampGain_ = 1;
				cout << "Bailing on binary search, auto-gain descended below 1.0." << endl;
				break;
			}
			lastStepDir = -1;
			//cout << " to " << setw(10) << ampGainScalar << " with ss " << setw(10) << stepSize << endl;
		}
		//cout << "    lastStepDir:" << setw(21) << lastStepDir << endl;
		
		assert(noiseParams.ampGain_ >= 1);//0);
	}
	
	cout << "Final ags: " << noiseParams.ampGain_ << endl;
	
	rgbImage.appendFilename("_n-");
	rgbImage.appendFilename(noiseParams.label_);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
movq 0x1d864(%rip), %rdi     # 0x2ffd0
leaq 0x121da(%rip), %rsi     # 0x2494d
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movl 0x30(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rbx, (%rsp)
movq 0x38(%rbx), %rdx
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
movsd 0xf8b6(%rip), %xmm2     # 0x22058
cmpq $0x3, %rcx
je 0x127e2
xorl %esi, %esi
cmpq %rsi, %rax
je 0x127dd
movq (%rdx,%rcx,8), %rdi
movq 0x38(%rdi), %rdi
movsd (%rdi,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x12ce6
ucomisd %xmm1, %xmm2
jb 0x12ce6
maxsd %xmm3, %xmm1
incq %rsi
movapd %xmm1, %xmm3
jmp 0x127aa
incq %rcx
jmp 0x127a2
movsd %xmm3, 0x18(%rsp)
ucomisd 0xf868(%rip), %xmm3     # 0x22058
jne 0x127f4
jnp 0x1280f
movq 0x1d7d5(%rip), %rdi     # 0x2ffd0
leaq 0x11eb5(%rip), %rsi     # 0x246b7
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x1d7ba(%rip), %r15     # 0x2ffd0
leaq 0x11ed3(%rip), %rsi     # 0x246f0
movq %r15, %rdi
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movsd 0x11393(%rip), %xmm0    # 0x23bc8
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x100e6
movsd %xmm0, 0x8(%rsp)
leaq 0x11ef5(%rip), %rsi     # 0x24743
movq %r15, %rdi
callq 0x32c0
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x3120
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0xb45c
movsd 0xa8(%r14), %xmm2
movsd 0x20(%r14), %xmm0
movsd 0x48(%r14), %xmm1
unpcklps 0xf780(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xf788(%rip), %xmm1     # 0x22030
movsd 0x40(%r14), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x1021e
movsd 0x48(%r14), %xmm1
unpcklps 0xf754(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xf75c(%rip), %xmm1     # 0x22030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd 0x98(%r14), %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0xa0(%r14)
mulsd 0xf74a(%rip), %xmm2     # 0x22048
movapd %xmm2, 0x20(%rsp)
xorpd %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movsd 0x11352(%rip), %xmm0    # 0x23c70
movsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
addsd 0x112aa(%rip), %xmm1    # 0x23be0
movsd %xmm1, 0x8(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x1d681(%rip), %rbx     # 0x2ffd0
leaq 0xe8(%rsp), %r12
movzbl %bpl, %ebp
movq %rax, 0x60(%rsp)
movq %rbx, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x11e0d(%rip), %rsi     # 0x2477f
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
movq %rcx, %rdx
negq %rdx
pushq $-0x1
popq %r13
leaq (%rdx,%r13), %rsi
cmpq $-0x1, %rsi
je 0x12a16
movsd (%rax), %xmm0
incq %r13
addq $0x10, %rax
ucomisd 0xa0(%r14), %xmm0
jne 0x1299a
jp 0x1299a
cmpq %rcx, %r13
je 0x12a19
movsd -0x8(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
leaq 0x11e2f(%rip), %rsi     # 0x24805
callq 0x32c0
movsd 0xa0(%r14), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
leaq 0x11e29(%rip), %rsi     # 0x2481f
callq 0x32c0
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
jmp 0x12b04
movq %rcx, %r13
movq (%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xb538
movq %rbx, %rdi
callq 0x3120
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x28, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
movl $0xffffff4f, %esi       # imm = 0xFFFFFF4F
andl %esi, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
leaq 0x11d6e(%rip), %rsi     # 0x247d0
callq 0x32c0
movsd 0xa0(%r14), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
callq 0x3120
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x12ab9
movq (%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx,8), %r15
movq %r12, %rdi
movq %r14, %rsi
callq 0x8544
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x105a0
movq %r12, %rdi
callq 0x805c
incq %rbx
jmp 0x12a82
movq (%rsp), %rdi
movsd 0x11103(%rip), %xmm0    # 0x23bc8
callq 0x100e6
movsd %xmm0, 0x10(%rsp)
movq 0x1d4f9(%rip), %rbx     # 0x2ffd0
movq %rbx, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x11cff(%rip), %rsi     # 0x247e8
callq 0x32c0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
movsd 0x10(%rsp), %xmm1
ucomisd 0x38(%rsp), %xmm1
jb 0x12b22
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x12c07
movsd 0x110b6(%rip), %xmm0    # 0x23be0
ucomisd 0x20(%rsp), %xmm0
ja 0x12c27
movsd 0xa0(%r14), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x13960
movsd 0x38(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
movapd 0x20(%rsp), %xmm0
mulsd 0xf4ce(%rip), %xmm0     # 0x22048
movsd 0xa0(%r14), %xmm1
movq 0x60(%rsp), %rax
jbe 0x12baf
cmpl $-0x1, %eax
je 0x12b95
movapd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%r14)
pushq $0x1
popq %rax
movsd 0xf4ab(%rip), %xmm2     # 0x22058
jmp 0x12bd8
cmpl $0x1, %eax
je 0x12bba
movapd 0x20(%rsp), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%r14)
movsd 0xf489(%rip), %xmm2     # 0x22058
ucomisd %xmm1, %xmm2
pushq $-0x1
popq %rax
ja 0x12c30
ucomisd %xmm2, %xmm1
movapd %xmm0, 0x20(%rsp)
jae 0x1295b
leaq 0x11cb0(%rip), %rdi     # 0x2489f
leaq 0x110cb(%rip), %rsi     # 0x23cc1
leaq 0x11d7a(%rip), %rcx     # 0x24977
movl $0x4ee, %edx            # imm = 0x4EE
callq 0x31a0
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
sarq $0x4, %rax
cmpq %r13, %rax
jne 0x12d05
leaq 0x11c2b(%rip), %rsi     # 0x24850
jmp 0x12c48
leaq 0x11c3f(%rip), %rsi     # 0x2486d
jmp 0x12c48
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xa0(%r14)
leaq 0x11d72(%rip), %rsi     # 0x249ba
movq 0x1d381(%rip), %rdi     # 0x2ffd0
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x1d36d(%rip), %rdi     # 0x2ffd0
leaq 0x11c4f(%rip), %rsi     # 0x248b9
callq 0x32c0
movsd 0xa0(%r14), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
callq 0x3120
leaq 0x11929(%rip), %rsi     # 0x245b8
movq (%rsp), %rdi
callq 0x8686
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x33b0
leaq 0x78(%rsp), %rsi
movq (%rsp), %rdi
callq 0xa508
leaq 0x78(%rsp), %rdi
callq 0x3548
leaq 0x40(%rsp), %rdi
callq 0x1392c
leaq 0x98(%rsp), %rdi
callq 0xb686
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11bf5(%rip), %rdi     # 0x248e2
leaq 0x10fcd(%rip), %rsi     # 0x23cc1
leaq 0x11c7c(%rip), %rcx     # 0x24977
movl $0x48d, %edx            # imm = 0x48D
callq 0x31a0
leaq 0x11b20(%rip), %rdi     # 0x2482c
leaq 0x10fae(%rip), %rsi     # 0x23cc1
leaq 0x11c5d(%rip), %rcx     # 0x24977
movl $0x4c6, %edx            # imm = 0x4C6
callq 0x31a0
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3548
jmp 0x12d57
movq %rax, %rbx
jmp 0x12d61
jmp 0x12d42
jmp 0x12d42
jmp 0x12d42
jmp 0x12d42
jmp 0x12d42
movq %rax, %rbx
jmp 0x12d57
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x805c
leaq 0x40(%rsp), %rdi
callq 0x1392c
leaq 0x98(%rsp), %rdi
callq 0xb686
movq %rbx, %rdi
callq 0x34d0