// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/ImageProcs.cpp
// Function: addSensorNoiseWithAutoISO(ImageReal&, bool, NoiseParam)
void addSensorNoiseWithAutoISO(ImageReal &grayImage, bool applyBayer, NoiseParam noiseParams)
{
	cout << "Adding grayscale sensor noise with autoISO..." << endl;
	
	//Check to see if the image is normalized
	int totalPixels = grayImage.totalPixels();
	Float_t maxVal = 0;
	for (int i = 0; i < totalPixels; i++)
	{
		assert(grayImage[i] >= 0 && grayImage[i] <= 1.0);
		if (grayImage[i] > maxVal)
			maxVal = grayImage[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(grayImage, 3);
	cout << "  Input image mean intensity (auto-gain target intensity): " << nonOutlierMeanPixelValOri << endl << endl;
	ImageReal grayImageOri = grayImage;
	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
			grayImage = grayImageOri;
			
			//Noisify the image
			cout << endl << setw(40) << left << "  Noisifying with ags: " << noiseParams.ampGain_ << endl;
			addSensorNoise(grayImage, applyBayer, noiseParams);
			
			//Determine the saturation
			nonOutlierMeanPixelVal = calcNonoutlierMeanPixelValue(grayImage, 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;
				stepSize *= .5;
				//cout << "    Increasing ags from " << setw(13) << ampGainScalar;
				noiseParams.ampGain_ += stepSize;
				lastStepDir = 1;
			}
			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;
	/*
	ofstream ofs;
	ofs.open("noiseData.txt");
	for (int i = 0; i < 256; i++)
		ofs << i << '\t' << sMeanNoise[i] << '\t' << sMeanNoiseProporation[i] << '\t'
			<< sMeanOutputValue[i] << '\t' << sMeanError[i] << endl;
	ofs.close();
	*/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movq 0x209dc(%rip), %r15     # 0x36fd0
leaq 0x16027(%rip), %rsi     # 0x2c622
movl $0x2d, %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
movslq 0x30(%r14), %rax
testq %rax, %rax
jle 0x1668b
movq 0x38(%r14), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd 0x13a0e(%rip), %xmm1    # 0x2a058
xorpd %xmm2, %xmm2
movsd (%rcx,%rdx,8), %xmm3
ucomisd %xmm0, %xmm3
jb 0x16da4
ucomisd %xmm3, %xmm1
jb 0x16da4
maxsd %xmm2, %xmm3
incq %rdx
movapd %xmm3, %xmm2
cmpq %rdx, %rax
jne 0x1664e
movsd %xmm3, 0x10(%rsp)
ucomisd 0x139d3(%rip), %xmm3    # 0x2a058
jne 0x16695
jp 0x16695
jmp 0x166d0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x1601b(%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 0x16019(%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 0x154c5(%rip), %xmm0    # 0x2bbd8
movq %r14, %rdi
callq 0x13954
movsd %xmm0, 0x8(%rsp)
leaq 0x1601b(%rip), %rsi     # 0x2c743
movl $0x3b, %edx
movq %r15, %rdi
callq 0x3360
movq %r15, %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
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 0x78(%rsp), %rdi
movq %r14, 0x58(%rsp)
movq %r14, %rsi
callq 0xcf2a
movsd 0xa8(%rbx), %xmm2
movsd 0x20(%rbx), %xmm0
movsd 0x48(%rbx), %xmm1
unpcklps 0x1385e(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x13866(%rip), %xmm1    # 0x2a030
movsd 0x40(%rbx), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x13bb6
movsd 0x48(%rbx), %xmm1
unpcklps 0x13834(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1383c(%rip), %xmm1    # 0x2a030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd 0x98(%rbx), %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0xa0(%rbx)
mulsd 0x1382c(%rip), %xmm2    # 0x2a048
movapd %xmm2, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movapd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movsd 0x15449(%rip), %xmm0    # 0x2bc88
movsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
addsd 0x15399(%rip), %xmm1    # 0x2bbf0
movsd %xmm1, 0x8(%rsp)
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xb8(%rsp), %r14
movzbl %bpl, %eax
movl %eax, 0x54(%rsp)
movq 0x2074f(%rip), %r12     # 0x36fd0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movl $0x50, %edx
movq %rax, %rdi
leaq 0x15ec4(%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 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rax, %rdx
je 0x16937
movsd 0xa0(%rbx), %xmm0
cmpq $0x1, %rcx
movq %rcx, %rdx
adcq $0x0, %rdx
movq %rax, %rsi
xorl %r13d, %r13d
movsd (%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x16926
jnp 0x1693a
incq %r13
addq $0x10, %rsi
cmpq %r13, %rdx
jne 0x1691a
movq %rdx, %r13
jmp 0x1693a
xorl %r13d, %r13d
cmpq %r13, %rcx
jne 0x16ac1
movq 0x58(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xcf72
movq 0x20677(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x20659(%rip), %rdi     # 0x36fd0
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 0x15e14(%rip), %rsi     # 0x2c7d0
callq 0x3360
movsd 0xa0(%rbx), %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
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa486
movq 0x58(%rsp), %r12
movq %r12, %rdi
movl 0x54(%rsp), %esi
movq %r14, %rbp
movq %r14, %rdx
callq 0x140d6
movq %rbp, %rdi
callq 0x9f80
movq %r12, %rdi
movsd 0x151a8(%rip), %xmm0    # 0x2bbd8
callq 0x13954
movsd %xmm0, 0x18(%rsp)
movq 0x2058e(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x20570(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x15d69(%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 0x16b4f
movq %r13, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x19, %edx
movq 0x204f0(%rip), %rdi     # 0x36fd0
leaq 0x15d1e(%rip), %rsi     # 0x2c805
callq 0x3360
movsd 0xa0(%rbx), %xmm0
movq 0x204d5(%rip), %rdi     # 0x36fd0
callq 0x3480
movq %rax, %rbp
movl $0xc, %edx
movq %rax, %rdi
leaq 0x15d0d(%rip), %rsi     # 0x2c81f
callq 0x3360
movq %rbp, %rdi
movsd 0x10(%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 0x16b6d
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x16cb0
movsd 0x1507b(%rip), %xmm0    # 0x2bbf0
ucomisd 0x40(%rsp), %xmm0
ja 0x16cd5
movsd 0xa0(%rbx), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x16bbe
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
movq 0x20414(%rip), %r12     # 0x36fd0
jmp 0x16bd4
leaq 0x20(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x18c34
movq 0x203fc(%rip), %r12     # 0x36fd0
movsd 0x60(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
mulsd 0x1345a(%rip), %xmm1    # 0x2a048
movsd 0xa0(%rbx), %xmm0
jbe 0x16c1e
cmpl $-0x1, %r15d
je 0x16c04
movapd 0x40(%rsp), %xmm1
addsd %xmm1, %xmm0
movl $0x1, %r15d
movapd %xmm1, 0x40(%rsp)
movsd 0x1343c(%rip), %xmm3    # 0x2a058
jmp 0x16c7f
cmpl $0x1, %r15d
je 0x16c2a
movapd 0x40(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0x13422(%rip), %xmm2    # 0x2a058
ucomisd %xmm0, %xmm2
movapd %xmm1, %xmm2
mulsd 0x13402(%rip), %xmm2    # 0x2a048
movapd %xmm2, 0x40(%rsp)
ja 0x16c54
movapd %xmm1, 0x40(%rsp)
movsd 0x133fc(%rip), %xmm3    # 0x2a058
addsd %xmm3, %xmm2
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movapd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rbx)
ucomisd %xmm3, %xmm0
jae 0x16881
leaq 0x15c07(%rip), %rdi     # 0x2c89f
leaq 0x15022(%rip), %rsi     # 0x2bcc1
leaq 0x159d3(%rip), %rcx     # 0x2c679
movl $0x45b, %edx            # imm = 0x45B
callq 0x31c0
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x4, %rax
cmpq %r13, %rax
jne 0x16dc3
movl $0x1c, %edx
leaq 0x15b7d(%rip), %rsi     # 0x2c850
jmp 0x16ce1
movl $0x31, %edx
leaq 0x15b8c(%rip), %rsi     # 0x2c86d
movq 0x202e8(%rip), %r14     # 0x36fd0
movq %r14, %rdi
callq 0x3360
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x202c2(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq 0x202ae(%rip), %rdi     # 0x36fd0
leaq 0x15b90(%rip), %rsi     # 0x2c8b9
movl $0xb, %edx
callq 0x3360
movsd 0xa0(%rbx), %xmm0
movq 0x2028e(%rip), %rdi     # 0x36fd0
callq 0x3480
movq %rax, %rbx
movq (%rax), %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 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x16d88
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x78(%rsp), %rdi
callq 0xd038
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x158a5(%rip), %rdi     # 0x2c650
leaq 0x14f0f(%rip), %rsi     # 0x2bcc1
leaq 0x158c0(%rip), %rcx     # 0x2c679
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x31c0
leaq 0x15a62(%rip), %rdi     # 0x2c82c
leaq 0x14ef0(%rip), %rsi     # 0x2bcc1
leaq 0x158a1(%rip), %rcx     # 0x2c679
movl $0x431, %edx            # imm = 0x431
callq 0x31c0
movq %rax, %rbx
jmp 0x16e1b
jmp 0x16e01
jmp 0x16e01
jmp 0x16e01
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x9f80
jmp 0x16e04
jmp 0x16e01
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x16e1b
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x78(%rsp), %rdi
callq 0xd038
movq %rbx, %rdi
callq 0x3500