// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdx, %r14
movl %esi, %r12d
movq %rdi, %rbx
movq 0x1fea8(%rip), %r15     # 0x36fd0
leaq 0x1581e(%rip), %rsi     # 0x2c94d
movl $0x29, %edx
movq %r15, %rdi
callq 0x3360
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl 0x30(%rbx), %eax
movq %rbx, 0x8(%rsp)
movq 0x38(%rbx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
xorpd %xmm1, %xmm1
movsd %xmm1, (%rsp)
movsd 0x12ed2(%rip), %xmm2    # 0x2a058
testl %eax, %eax
jle 0x171c4
movq (%rcx,%rdx,8), %rsi
movq 0x38(%rsi), %rsi
xorl %edi, %edi
movsd (%rsp), %xmm1
movsd (%rsi,%rdi,8), %xmm3
ucomisd %xmm0, %xmm3
jb 0x1798a
ucomisd %xmm3, %xmm2
jb 0x1798a
maxsd %xmm1, %xmm3
incq %rdi
movapd %xmm3, %xmm1
cmpq %rdi, %rax
jne 0x17199
jmp 0x171c9
movsd (%rsp), %xmm3
movsd %xmm3, (%rsp)
incq %rdx
cmpq $0x3, %rdx
jne 0x17186
movsd (%rsp), %xmm0
ucomisd 0x12e74(%rip), %xmm0    # 0x2a058
movq 0x1fde5(%rip), %r15     # 0x36fd0
jne 0x171ef
jnp 0x1722a
leaq 0x154c1(%rip), %rsi     # 0x2c6b7
movl $0x38, %edx
movq %r15, %rdi
callq 0x3360
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
leaq 0x154bf(%rip), %rsi     # 0x2c6f0
movl $0x52, %edx
movq %r15, %rdi
callq 0x3360
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movsd 0x1496b(%rip), %xmm0    # 0x2bbd8
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x13a81
movsd %xmm0, 0x10(%rsp)
leaq 0x154bc(%rip), %rsi     # 0x2c743
movl $0x3b, %edx
movq %r15, %rdi
callq 0x3360
movq %r15, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3480
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0xe04c
movsd 0xa8(%r14), %xmm2
movsd 0x20(%r14), %xmm0
movsd 0x48(%r14), %xmm1
unpcklps 0x12cfe(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x12d06(%rip), %xmm1    # 0x2a030
movsd 0x40(%r14), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x13bb6
movsd 0x48(%r14), %xmm1
unpcklps 0x12cd2(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x12cda(%rip), %xmm1    # 0x2a030
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 0x12cc8(%rip), %xmm2    # 0x2a048
movapd %xmm2, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movapd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movsd 0x148e5(%rip), %xmm0    # 0x2bc88
movsd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
addsd 0x14835(%rip), %xmm1    # 0x2bbf0
movsd %xmm1, 0x10(%rsp)
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xf8(%rsp), %r13
movzbl %r12b, %r12d
movq 0x1fbef(%rip), %rbx     # 0x36fd0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movl $0x50, %edx
movq %rax, %rdi
leaq 0x15365(%rip), %rsi     # 0x2c77f
callq 0x3360
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %r15, 0x58(%rsp)
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rax, %rdx
je 0x1749c
movsd 0xa0(%r14), %xmm0
cmpq $0x1, %rcx
movq %rcx, %rdx
adcq $0x0, %rdx
movq %rax, %rsi
xorl %r15d, %r15d
movsd (%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1748b
jnp 0x1749f
incq %r15
addq $0x10, %rsi
cmpq %r15, %rdx
jne 0x1747f
movq %rdx, %r15
jmp 0x1749f
xorl %r15d, %r15d
cmpq %r15, %rcx
jne 0x1762c
movq 0x8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xe140
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0x28, 0x10(%rbp,%rcx)
movq -0x18(%rax), %rax
movl 0x18(%rbp,%rax), %ecx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl %edx, %ecx
orl $0x20, %ecx
movl %ecx, 0x18(%rbp,%rax)
movl $0x17, %edx
movq %rbp, %rdi
leaq 0x152b7(%rip), %rsi     # 0x2c7d0
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq %rbp, %rdi
callq 0x3480
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx,8), %rbp
movq %r13, %rdi
movq %r14, %rsi
callq 0xa486
movq %rbp, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x140d6
movq %r13, %rdi
callq 0x9f80
incq %rbx
cmpq $0x3, %rbx
jne 0x1755b
movq 0x8(%rsp), %rdi
movsd 0x14639(%rip), %xmm0    # 0x2bbd8
callq 0x13a81
movsd %xmm0, 0x18(%rsp)
movq 0x1fa1f(%rip), %rbx     # 0x36fd0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x151fe(%rip), %rsi     # 0x2c7e8
callq 0x3360
movq %rbp, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x3480
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
jmp 0x176b1
movq %r15, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, (%rsp)
movl $0x19, %edx
movq %rbx, %rdi
leaq 0x151b8(%rip), %rsi     # 0x2c805
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq %rbx, %rdi
callq 0x3480
movq %rax, %rbp
movl $0xc, %edx
movq %rax, %rdi
leaq 0x151aa(%rip), %rsi     # 0x2c81f
callq 0x3360
movq %rbp, %rdi
movsd (%rsp), %xmm0
callq 0x3480
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movsd 0x18(%rsp), %xmm1
ucomisd 0x60(%rsp), %xmm1
jb 0x176cf
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x177da
movsd 0x14519(%rip), %xmm0    # 0x2bbf0
ucomisd 0x40(%rsp), %xmm0
ja 0x177ff
movsd 0xa0(%r14), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd (%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x1771e
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
movq 0x58(%rsp), %rax
jmp 0x17732
leaq 0x20(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x18c34
movq 0x58(%rsp), %rax
movsd 0x60(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
movapd 0x40(%rsp), %xmm0
mulsd 0x128fc(%rip), %xmm0    # 0x2a048
movsd 0xa0(%r14), %xmm1
jbe 0x1777f
cmpl $-0x1, %eax
je 0x17762
movapd 0x40(%rsp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%r14)
movl $0x1, %r15d
movsd 0x128db(%rip), %xmm2    # 0x2a058
jmp 0x177ab
cmpl $0x1, %eax
je 0x1778a
movapd 0x40(%rsp), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%r14)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movsd 0x128b3(%rip), %xmm2    # 0x2a058
ucomisd %xmm1, %xmm2
ja 0x1780d
ucomisd %xmm2, %xmm1
movapd %xmm0, 0x40(%rsp)
jae 0x173e1
leaq 0x150dd(%rip), %rdi     # 0x2c89f
leaq 0x144f8(%rip), %rsi     # 0x2bcc1
leaq 0x151a7(%rip), %rcx     # 0x2c977
movl $0x4ee, %edx            # imm = 0x4EE
callq 0x31c0
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x4, %rax
cmpq %r15, %rax
jne 0x179a9
movl $0x1c, %edx
leaq 0x15053(%rip), %rsi     # 0x2c850
jmp 0x1782a
movl $0x31, %edx
leaq 0x15062(%rip), %rsi     # 0x2c86d
jmp 0x1782a
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xa0(%r14)
movl $0x38, %edx
leaq 0x15190(%rip), %rsi     # 0x2c9ba
movq 0x1f79f(%rip), %r15     # 0x36fd0
movq %r15, %rdi
callq 0x3360
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x1f779(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq 0x1f765(%rip), %rdi     # 0x36fd0
leaq 0x15047(%rip), %rsi     # 0x2c8b9
movl $0xb, %edx
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq 0x1f744(%rip), %rdi     # 0x36fd0
callq 0x3480
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
leaq 0x78(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x14ced(%rip), %rsi     # 0x2c5b8
leaq 0x14ce9(%rip), %rdx     # 0x2c5bb
leaq 0x68(%rsp), %rdi
callq 0xa9c2
leaq 0x68(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0xce5a
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x17902
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x98(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0xa92c
leaq 0x88(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0xce5a
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x17954
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1796b
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
callq 0xe2a6
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14f51(%rip), %rdi     # 0x2c8e2
leaq 0x14329(%rip), %rsi     # 0x2bcc1
leaq 0x14fd8(%rip), %rcx     # 0x2c977
movl $0x48d, %edx            # imm = 0x48D
callq 0x31c0
leaq 0x14e7c(%rip), %rdi     # 0x2c82c
leaq 0x1430a(%rip), %rsi     # 0x2bcc1
leaq 0x14fb9(%rip), %rcx     # 0x2c977
movl $0x4c6, %edx            # imm = 0x4C6
callq 0x31c0
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x17a22
movq 0x98(%rsp), %rsi
jmp 0x179f4
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x17a22
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x17a22
movq %rax, %rbx
jmp 0x17a39
jmp 0x17a1f
jmp 0x17a1f
jmp 0x17a1f
jmp 0x17a1f
jmp 0x17a1f
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x9f80
jmp 0x17a22
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17a39
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
callq 0xe2a6
movq %rbx, %rdi
callq 0x3500