// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movq 0x1dfde(%rip), %rdi     # 0x2ffd0
leaq 0x12629(%rip), %rsi     # 0x24622
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movl 0x30(%r14), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq 0x38(%r14), %rdx
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
movsd 0x10033(%rip), %xmm2    # 0x22058
cmpq %rcx, %rax
je 0x12050
movsd (%rdx,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x124f7
ucomisd %xmm1, %xmm2
jb 0x124f7
maxsd %xmm3, %xmm1
incq %rcx
movapd %xmm1, %xmm3
jmp 0x12025
movsd %xmm3, 0x28(%rsp)
ucomisd 0xfffa(%rip), %xmm3     # 0x22058
jne 0x12062
jnp 0x1207d
movq 0x1df67(%rip), %rdi     # 0x2ffd0
leaq 0x12647(%rip), %rsi     # 0x246b7
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x1df4c(%rip), %r15     # 0x2ffd0
leaq 0x12665(%rip), %rsi     # 0x246f0
movq %r15, %rdi
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movsd 0x11b25(%rip), %xmm0    # 0x23bc8
movq %r14, %rdi
callq 0xffe9
movsd %xmm0, (%rsp)
leaq 0x1268c(%rip), %rsi     # 0x24743
movq %r15, %rdi
callq 0x32c0
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x3120
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0xa5a2
movsd 0xa8(%rbx), %xmm2
movsd 0x20(%rbx), %xmm0
movsd 0x48(%rbx), %xmm1
unpcklps 0xff1e(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xff26(%rip), %xmm1     # 0x22030
movsd 0x40(%rbx), %xmm3
movapd %xmm1, %xmm4
unpckhpd %xmm1, %xmm4            # xmm4 = xmm4[1],xmm1[1]
addsd %xmm1, %xmm4
callq 0x1021e
movsd 0x48(%rbx), %xmm1
unpcklps 0xfef4(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xfefc(%rip), %xmm1     # 0x22030
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 0xfeec(%rip), %xmm2     # 0x22048
movapd %xmm2, 0x10(%rsp)
xorpd %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movsd 0x11af4(%rip), %xmm0    # 0x23c70
movsd (%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
addsd 0x11a4d(%rip), %xmm1    # 0x23be0
movsd %xmm1, (%rsp)
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x1de24(%rip), %r13     # 0x2ffd0
leaq 0xa8(%rsp), %r12
movzbl %bpl, %eax
movl %eax, 0x34(%rsp)
movq %r13, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x125b1(%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 %rbp
leaq (%rdx,%rbp), %rsi
cmpq $-0x1, %rsi
je 0x1226f
movsd (%rax), %xmm0
incq %rbp
addq $0x10, %rax
ucomisd 0xa0(%rbx), %xmm0
jne 0x121f5
jp 0x121f5
cmpq %rcx, %rbp
je 0x12272
movsd -0x8(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r13, %rdi
leaq 0x125d5(%rip), %rsi     # 0x24805
callq 0x32c0
movsd 0xa0(%rbx), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
leaq 0x125d0(%rip), %rsi     # 0x2481f
callq 0x32c0
movq %rax, %rdi
movsd 0x28(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
jmp 0x12339
movq %rcx, %rbp
movq %r14, %rdi
leaq 0x68(%rsp), %rsi
callq 0xa5e6
movq %r13, %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 0x12519(%rip), %rsi     # 0x247d0
callq 0x32c0
movsd 0xa0(%rbx), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
callq 0x3120
movq %r12, %rdi
movq %rbx, %rsi
callq 0x8544
movq %r14, %rdi
movl 0x34(%rsp), %esi
movq %r12, %rdx
callq 0x105a0
movq %r12, %rdi
callq 0x805c
movq %r14, %rdi
movsd 0x118c7(%rip), %xmm0    # 0x23bc8
callq 0xffe9
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x124ca(%rip), %rsi     # 0x247e8
callq 0x32c0
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3460
movq %rax, %rdi
callq 0x3120
movsd 0x8(%rsp), %xmm1
ucomisd 0x38(%rsp), %xmm1
jb 0x12356
movsd (%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x1246b
movsd 0x11882(%rip), %xmm0    # 0x23be0
ucomisd 0x10(%rsp), %xmm0
ja 0x1248b
movsd 0xa0(%rbx), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x13960
movsd 0x38(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
movapd 0x10(%rsp), %xmm1
mulsd 0xfc9b(%rip), %xmm1     # 0x22048
movsd 0xa0(%rbx), %xmm0
jbe 0x123db
cmpl $-0x1, %r15d
je 0x123c3
movapd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
movapd %xmm1, 0x10(%rsp)
pushq $0x1
popq %r15
movsd 0xfc7f(%rip), %xmm3     # 0x22058
jmp 0x1243a
cmpl $0x1, %r15d
je 0x123e7
movapd 0x10(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xfc65(%rip), %xmm2     # 0x22058
ucomisd %xmm0, %xmm2
movapd %xmm1, %xmm2
mulsd 0xfc45(%rip), %xmm2     # 0x22048
movapd %xmm2, 0x10(%rsp)
ja 0x12411
movapd %xmm1, 0x10(%rsp)
movsd 0xfc3f(%rip), %xmm3     # 0x22058
addsd %xmm3, %xmm2
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd %xmm1, %xmm0
pushq $-0x1
popq %r15
movsd %xmm0, 0xa0(%rbx)
ucomisd %xmm3, %xmm0
jae 0x121bc
leaq 0x1244c(%rip), %rdi     # 0x2489f
leaq 0x11867(%rip), %rsi     # 0x23cc1
leaq 0x12218(%rip), %rcx     # 0x24679
movl $0x45b, %edx            # imm = 0x45B
callq 0x31a0
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
sarq $0x4, %rax
cmpq %rbp, %rax
jne 0x12516
leaq 0x123c7(%rip), %rsi     # 0x24850
jmp 0x12492
leaq 0x123db(%rip), %rsi     # 0x2486d
movq 0x1db37(%rip), %rdi     # 0x2ffd0
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x1db23(%rip), %rdi     # 0x2ffd0
leaq 0x12405(%rip), %rsi     # 0x248b9
callq 0x32c0
movsd 0xa0(%rbx), %xmm0
movq %rax, %rdi
callq 0x3460
movq %rax, %rdi
callq 0x3120
leaq 0x40(%rsp), %rdi
callq 0x1392c
leaq 0x68(%rsp), %rdi
callq 0xa6a6
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12152(%rip), %rdi     # 0x24650
leaq 0x117bc(%rip), %rsi     # 0x23cc1
leaq 0x1216d(%rip), %rcx     # 0x24679
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x31a0
leaq 0x1230f(%rip), %rdi     # 0x2482c
leaq 0x1179d(%rip), %rsi     # 0x23cc1
leaq 0x1214e(%rip), %rcx     # 0x24679
movl $0x431, %edx            # imm = 0x431
callq 0x31a0
movq %rax, %rbx
jmp 0x12561
jmp 0x12554
jmp 0x12554
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x805c
jmp 0x12557
jmp 0x12554
jmp 0x12554
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1392c
leaq 0x68(%rsp), %rdi
callq 0xa6a6
movq %rbx, %rdi
callq 0x34d0