// File: /workspace/github_cpp_makefile_star2_O1/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, %r12d
movq %rdi, %r14
movq 0x204e1(%rip), %r15     # 0x36fd0
leaq 0x16b24(%rip), %rsi     # 0x2d61a
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 0x16b74
movq 0x38(%r14), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsd 0x14513(%rip), %xmm1    # 0x2b058
xorpd %xmm2, %xmm2
movsd (%rcx,%rdx,8), %xmm3
ucomisd %xmm0, %xmm3
jb 0x17246
ucomisd %xmm3, %xmm1
jb 0x17246
maxsd %xmm2, %xmm3
incq %rdx
movapd %xmm3, %xmm2
cmpq %rdx, %rax
jne 0x16b49
jmp 0x16b78
xorpd %xmm3, %xmm3
movsd %xmm3, 0x50(%rsp)
ucomisd 0x144d2(%rip), %xmm3    # 0x2b058
jne 0x16b8a
jnp 0x16bc5
leaq 0x16b1e(%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 0x16b1c(%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 0x15fe8(%rip), %xmm0    # 0x2cbf0
movq %r14, %rdi
callq 0x139b9
movsd %xmm0, 0x10(%rsp)
leaq 0x16b1e(%rip), %rsi     # 0x2d73b
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 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0xcfee
movsd 0xa8(%rbx), %xmm2
movsd 0x20(%rbx), %xmm0
movsd 0x48(%rbx), %xmm1
unpcklps 0x1436e(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x14376(%rip), %xmm1    # 0x2b030
movsd 0x40(%rbx), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x13c07
movsd 0x48(%rbx), %xmm1
unpcklps 0x14344(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1434c(%rip), %xmm1    # 0x2b030
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 0x1433c(%rip), %xmm2    # 0x2b048
movapd %xmm2, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movapd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movsd 0x15f71(%rip), %xmm0    # 0x2cca0
movsd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
addsd 0x15ec1(%rip), %xmm1    # 0x2cc08
movsd %xmm1, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xb8(%rsp), %rbp
movzbl %r12b, %eax
movl %eax, 0x5c(%rsp)
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
movq %rax, %r13
movl $0x50, %edx
movq %rax, %rdi
leaq 0x169ce(%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 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rax, %rdx
je 0x16e25
movsd 0xa0(%rbx), %xmm0
cmpq $0x1, %rcx
movq %rcx, %rdx
adcq $0x0, %rdx
movq %rax, %rsi
xorl %r12d, %r12d
movsd (%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x16e14
jnp 0x16e28
incq %r12
addq $0x10, %rsi
cmpq %r12, %rdx
jne 0x16e08
movq %rdx, %r12
jmp 0x16e28
xorl %r12d, %r12d
cmpq %r12, %rcx
jne 0x16f91
movq %r14, %rdi
leaq 0x78(%rsp), %rsi
callq 0xd036
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
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 0x16929(%rip), %rsi     # 0x2d7c8
callq 0x3360
movsd 0xa0(%rbx), %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
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xa4d4
movq %r14, %rdi
movl 0x5c(%rsp), %esi
movq %rbp, %rdx
callq 0x14127
movq %rbp, %rdi
callq 0x9fce
movq %r14, %rdi
movsd 0x15ce5(%rip), %xmm0    # 0x2cbf0
callq 0x139b9
movsd %xmm0, 0x18(%rsp)
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
movq %rax, %r13
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x16891(%rip), %rsi     # 0x2d7e0
callq 0x3360
movq %r13, %rdi
movsd 0x18(%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 0x17017
movq %r12, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, 0x50(%rsp)
movl $0x19, %edx
movq %r15, %rdi
leaq 0x1684a(%rip), %rsi     # 0x2d7fd
callq 0x3360
movsd 0xa0(%rbx), %xmm0
movq %r15, %rdi
callq 0x3480
movq %rax, %r13
movl $0xc, %edx
movq %rax, %rdi
leaq 0x1683d(%rip), %rsi     # 0x2d817
callq 0x3360
movq %r13, %rdi
movsd 0x50(%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 0x18(%rsp), %xmm1
ucomisd 0x60(%rsp), %xmm1
jb 0x17055
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x17055
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x4, %rax
movl $0x1c, %edx
leaq 0x167fd(%rip), %rsi     # 0x2d848
cmpq %r12, %rax
je 0x17071
jmp 0x17265
movl $0x31, %edx
movsd 0x15ba6(%rip), %xmm0    # 0x2cc08
ucomisd 0x40(%rsp), %xmm0
leaq 0x167f6(%rip), %rsi     # 0x2d865
jbe 0x170a7
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
xorl %eax, %eax
jmp 0x171b5
movsd 0xa0(%rbx), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x170dd
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
jmp 0x170ec
leaq 0x20(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1915c
movsd 0x60(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
mulsd 0x13f42(%rip), %xmm1    # 0x2b048
movsd 0xa0(%rbx), %xmm0
jbe 0x1713b
cmpl $-0x1, 0x8(%rsp)
je 0x1711d
movapd 0x40(%rsp), %xmm1
addsd %xmm1, %xmm0
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movapd %xmm1, 0x40(%rsp)
movsd 0x13f1f(%rip), %xmm3    # 0x2b058
jmp 0x171a1
cmpl $0x1, 0x8(%rsp)
je 0x17148
movapd 0x40(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0x13f04(%rip), %xmm2    # 0x2b058
ucomisd %xmm0, %xmm2
movapd %xmm1, %xmm2
mulsd 0x13ee4(%rip), %xmm2    # 0x2b048
movapd %xmm2, 0x40(%rsp)
ja 0x17172
movapd %xmm1, 0x40(%rsp)
movsd 0x13ede(%rip), %xmm3    # 0x2b058
addsd %xmm3, %xmm2
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rsp)
movapd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rbx)
movb $0x1, %al
ucomisd %xmm3, %xmm0
jb 0x17284
testb %al, %al
jne 0x16d70
movq 0x1fe0c(%rip), %rdi     # 0x36fd0
leaq 0x166e6(%rip), %rsi     # 0x2d8b1
movl $0xb, %edx
callq 0x3360
movsd 0xa0(%rbx), %xmm0
movq 0x1fdec(%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 0x1722a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x78(%rsp), %rdi
callq 0xd0fc
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x163fb(%rip), %rdi     # 0x2d648
leaq 0x15a65(%rip), %rsi     # 0x2ccb9
leaq 0x16416(%rip), %rcx     # 0x2d671
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x31c0
leaq 0x165b8(%rip), %rdi     # 0x2d824
leaq 0x15a46(%rip), %rsi     # 0x2ccb9
leaq 0x163f7(%rip), %rcx     # 0x2d671
movl $0x431, %edx            # imm = 0x431
callq 0x31c0
leaq 0x1660c(%rip), %rdi     # 0x2d897
leaq 0x15a27(%rip), %rsi     # 0x2ccb9
leaq 0x163d8(%rip), %rcx     # 0x2d671
movl $0x45b, %edx            # imm = 0x45B
callq 0x31c0
movq %rax, %rbx
jmp 0x172da
jmp 0x172c0
jmp 0x172c0
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x9fce
jmp 0x172c3
jmp 0x172c0
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x172da
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x78(%rsp), %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0x3500