// File: /workspace/github_cpp_makefile_star2_O1/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 0x1f9e9(%rip), %r15     # 0x36fd0
leaq 0x16357(%rip), %rsi     # 0x2d945
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, 0x18(%rsp)
movq 0x38(%rbx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
xorpd %xmm1, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x13a12(%rip), %xmm2    # 0x2b058
testl %eax, %eax
jle 0x17685
movq (%rcx,%rdx,8), %rsi
movq 0x38(%rsi), %rsi
xorl %edi, %edi
movsd 0x8(%rsp), %xmm1
movsd (%rsi,%rdi,8), %xmm3
ucomisd %xmm0, %xmm3
jb 0x17e94
ucomisd %xmm3, %xmm2
jb 0x17e94
maxsd %xmm1, %xmm3
incq %rdi
movapd %xmm3, %xmm1
cmpq %rdi, %rax
jne 0x1765a
jmp 0x1768b
movsd 0x8(%rsp), %xmm3
movsd %xmm3, 0x8(%rsp)
incq %rdx
cmpq $0x3, %rdx
jne 0x17646
movsd 0x8(%rsp), %xmm0
ucomisd 0x139b0(%rip), %xmm0    # 0x2b058
movq 0x1f921(%rip), %r15     # 0x36fd0
jne 0x176b3
jnp 0x176ee
leaq 0x15ff5(%rip), %rsi     # 0x2d6af
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 0x15ff3(%rip), %rsi     # 0x2d6e8
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 0x154bf(%rip), %xmm0    # 0x2cbf0
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x13ace
movsd %xmm0, 0x20(%rsp)
leaq 0x15ff0(%rip), %rsi     # 0x2d73b
movl $0x3b, %edx
movq %r15, %rdi
callq 0x3360
movq %r15, %rdi
movsd 0x20(%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 0xe118
movsd 0xa8(%r14), %xmm2
movsd 0x20(%r14), %xmm0
movsd 0x48(%r14), %xmm1
unpcklps 0x1383a(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x13842(%rip), %xmm1    # 0x2b030
movsd 0x40(%r14), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x13c07
movsd 0x48(%r14), %xmm1
unpcklps 0x1380e(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x13816(%rip), %xmm1    # 0x2b030
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 0x13804(%rip), %xmm2    # 0x2b048
movapd %xmm2, 0x50(%rsp)
xorpd %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movapd %xmm0, (%rax)
movsd 0x15441(%rip), %xmm0    # 0x2cca0
movsd 0x20(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
addsd 0x15391(%rip), %xmm1    # 0x2cc08
movsd %xmm1, 0x20(%rsp)
movq $0x0, 0x10(%rax)
movq $0x0, 0x48(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0xf8(%rsp), %rbp
movzbl %r12b, %r12d
movq 0x1f725(%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, %r13
movl $0x50, %edx
movq %rax, %rdi
leaq 0x15e93(%rip), %rsi     # 0x2d777
callq 0x3360
movq (%r13), %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 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rax, %rdx
je 0x17961
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 0x17950
jnp 0x17964
incq %r15
addq $0x10, %rsi
cmpq %r15, %rdx
jne 0x17944
movq %rdx, %r15
jmp 0x17964
xorl %r15d, %r15d
cmpq %r15, %rcx
jne 0x17af3
movq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xe20c
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, %r13
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0x28, 0x10(%r13,%rcx)
movq -0x18(%rax), %rax
movl 0x18(%r13,%rax), %ecx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl %edx, %ecx
orl $0x20, %ecx
movl %ecx, 0x18(%r13,%rax)
movl $0x17, %edx
movq %r13, %rdi
leaq 0x15de8(%rip), %rsi     # 0x2d7c8
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq %r13, %rdi
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
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx,8), %r13
movq %rbp, %rdi
movq %r14, %rsi
callq 0xa4d4
movq %r13, %rdi
movl %r12d, %esi
movq %rbp, %rdx
callq 0x14127
movq %rbp, %rdi
callq 0x9fce
incq %rbx
cmpq $0x3, %rbx
jne 0x17a22
movq 0x18(%rsp), %rdi
movsd 0x1518a(%rip), %xmm0    # 0x2cbf0
callq 0x13ace
movsd %xmm0, 0x28(%rsp)
movq 0x1f558(%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, %r13
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x15d2f(%rip), %rsi     # 0x2d7e0
callq 0x3360
movq %r13, %rdi
movsd 0x28(%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
jmp 0x17b7a
movq %r15, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x19, %edx
movq %rbx, %rdi
leaq 0x15ce8(%rip), %rsi     # 0x2d7fd
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq %rbx, %rdi
callq 0x3480
movq %rax, %r13
movl $0xc, %edx
movq %rax, %rdi
leaq 0x15cda(%rip), %rsi     # 0x2d817
callq 0x3360
movq %r13, %rdi
movsd 0x8(%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
movsd 0x28(%rsp), %xmm1
ucomisd 0x60(%rsp), %xmm1
jb 0x17bb8
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x17bb8
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
sarq $0x4, %rax
movl $0x1c, %edx
leaq 0x15c9a(%rip), %rsi     # 0x2d848
cmpq %r15, %rax
je 0x17bd4
jmp 0x17eb3
movl $0x31, %edx
movsd 0x15043(%rip), %xmm0    # 0x2cc08
ucomisd 0x50(%rsp), %xmm0
leaq 0x15c93(%rip), %rsi     # 0x2d865
jbe 0x17c0a
movq %rbx, %rdi
callq 0x3360
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
xorl %eax, %eax
jmp 0x17d66
movsd 0xa0(%r14), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x17c41
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x38(%rsp)
jmp 0x17c50
leaq 0x30(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1915c
movsd 0x60(%rsp), %xmm0
ucomisd 0x28(%rsp), %xmm0
movapd 0x50(%rsp), %xmm0
mulsd 0x133de(%rip), %xmm0    # 0x2b048
movsd %xmm0, 0x10(%rsp)
movsd 0xa0(%r14), %xmm0
jbe 0x17cab
cmpl $-0x1, 0x48(%rsp)
je 0x17c8e
movapd 0x50(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%r14)
movl $0x1, %ecx
jmp 0x17d42
cmpl $0x1, 0x48(%rsp)
je 0x17cbe
movapd 0x50(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
subsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xa0(%r14)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movsd 0x1337e(%rip), %xmm1    # 0x2b058
ucomisd %xmm0, %xmm1
jbe 0x17d3c
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xa0(%r14)
movl $0x38, %edx
movq %rbx, %rdi
leaq 0x15cb2(%rip), %rsi     # 0x2d9b2
callq 0x3360
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
xorl %eax, %eax
movsd 0x10(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
jmp 0x17d66
movsd 0x10(%rsp), %xmm1
movsd 0xa0(%r14), %xmm0
movb $0x1, %al
ucomisd 0x13303(%rip), %xmm0    # 0x2b058
movapd %xmm1, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
jb 0x17ed2
testb %al, %al
jne 0x178ab
movq 0x1f25b(%rip), %rdi     # 0x36fd0
leaq 0x15b35(%rip), %rsi     # 0x2d8b1
movl $0xb, %edx
callq 0x3360
movsd 0xa0(%r14), %xmm0
movq 0x1f23a(%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 0x157db(%rip), %rsi     # 0x2d5b0
leaq 0x157d7(%rip), %rdx     # 0x2d5b3
leaq 0x68(%rsp), %rdi
callq 0xaa0e
leaq 0x68(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xcf1e
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x17e0c
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 0xa97e
leaq 0x88(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xcf1e
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x17e5e
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3310
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17e75
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
callq 0xe372
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15a3f(%rip), %rdi     # 0x2d8da
leaq 0x14e17(%rip), %rsi     # 0x2ccb9
leaq 0x15ac6(%rip), %rcx     # 0x2d96f
movl $0x48d, %edx            # imm = 0x48D
callq 0x31c0
leaq 0x1596a(%rip), %rdi     # 0x2d824
leaq 0x14df8(%rip), %rsi     # 0x2ccb9
leaq 0x15aa7(%rip), %rcx     # 0x2d96f
movl $0x4c6, %edx            # imm = 0x4C6
callq 0x31c0
leaq 0x159be(%rip), %rdi     # 0x2d897
leaq 0x14dd9(%rip), %rsi     # 0x2ccb9
leaq 0x15a88(%rip), %rcx     # 0x2d96f
movl $0x4ee, %edx            # imm = 0x4EE
callq 0x31c0
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x17f49
movq 0x98(%rsp), %rsi
jmp 0x17f1d
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x17f49
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x17f49
movq %rax, %rbx
jmp 0x17f60
jmp 0x17f34
jmp 0x17f34
jmp 0x17f34
jmp 0x17f34
movq %rax, %rbx
jmp 0x17f49
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x9fce
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17f60
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
callq 0xe372
movq %rbx, %rdi
callq 0x3500