// File: /workspace/github_cpp_makefile_star2_O1/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: altProj_repairGreen(ImageReal&, Image3ChnlReal&)
void altProj_repairGreen(ImageReal &grayImage, Image3ChnlReal &rgbImage)
{
	int width = grayImage.width(), height = grayImage.height();
	int rx, ry, g1x, g2y, bx, by;
	int redArrayPos = 0, greenArrayPos = 0, blueArrayPos = 0, secondGreenUnusedPos = 0;
	determineBlockColorOrder(rx, ry, g1x, g2y, bx, by, redArrayPos, greenArrayPos, blueArrayPos, secondGreenUnusedPos);
	
	//Generate downsampled images of red, blue, and two greens, one that overlaps red and one that overlaps blue
	ImageReal downsampledR(width, height), downsampledB(width, height);
	ImageReal downsampledGR(width, height), downsampledGB(width, height);
	
	grayImage.downsample(downsampledR, rx, ry);
	grayImage.downsample(downsampledB, bx, by);
	rgbImage[1]->downsample(downsampledGR, rx, ry);
	rgbImage[1]->downsample(downsampledGB, bx, by);
	
	int halfWidth = width / 2, halfHeight = height / 2;
	assert(downsampledR.width() == halfWidth && downsampledR.height() == halfHeight);
	
	//Perform a wavelet analysis of red, blue, and each of the interpolated greens that overlapped red and blue
	SteerablePyramid waveletR, waveletB, waveletGR, waveletGB;		int arrayOffset = 1;
	//Daubechies waveletR, waveletB, waveletGR, waveletGB;		int arrayOffset = 0;
	
	waveletR.DoFullWaveletAnalysis(downsampledR.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletB.DoFullWaveletAnalysis(downsampledB.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletGR.DoFullWaveletAnalysis(downsampledGR.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletGB.DoFullWaveletAnalysis(downsampledGB.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	
	int numLevels = waveletGR.NumLevels();
	
	//Find the correlation between green and red and between green and blue on a per level basis
	vector<Float_t> greenRedCorrs, greenBlueCorrs;	//One correlation per level
	waveletGR.findCorrelations(waveletR, greenRedCorrs);
	waveletGB.findCorrelations(waveletB, greenBlueCorrs);
	/*
	cout << "Green/Red correlations: ";
	for (int i = 0; i < greenRedCorrs.size(); i++)
		cout << setw(15) << greenRedCorrs[i];
	cout << endl;
	cout << "Green/Blue correlations:";
	for (int i = 0; i < greenBlueCorrs.size(); i++)
		cout << setw(15) << greenBlueCorrs[i];
	cout << endl;
	*/
	vector<Float_t> means, stdDevs;
	vector<Float_t> thresholdsR(numLevels), thresholdsB(numLevels);
	
	waveletGR.CalcStats(means, stdDevs);
	for (int i = 0; i < numLevels; i++)
		if (i == 0)
			thresholdsR[i] = (((greenRedCorrs[i] + 1) / 2) * means[i + arrayOffset]);
		else thresholdsR[i] = -1;
	
	waveletGB.CalcStats(means, stdDevs);
	for (int i = 0; i < numLevels; i++)
		if (i == 0)
			thresholdsB[i] = (((greenBlueCorrs[i] + 1) / 2) * means[i + arrayOffset]);
		else thresholdsB[i] = -1;
	
	//for (int i = 0; i < numLevels; i++)
	//	cout << "Red/Blue thresholds: " << thresholdsR[i] << "    " << thresholdsB[i] << endl;
	
	//Replace the high frequency coefficients of green with those of red and blue
	waveletGR.CopyCoefficients(waveletR, thresholdsR);
	waveletGB.CopyCoefficients(waveletB, thresholdsB);
	
	//Reconstruct green.  Note that this will only affect the interpolated green pixels.
	waveletGR.DoFullWaveletSynthesis(downsampledGR.img());
	cout << ".";	cout.flush();
	waveletGB.DoFullWaveletSynthesis(downsampledGB.img());
	cout << ".";	cout.flush();
	
	downsampledGR.upsample(*rgbImage[1], rx, ry);
	downsampledGB.upsample(*rgbImage[1], bx, by);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rsi, %rbx
movq %rdi, %r12
movl 0x28(%rdi), %r14d
movl 0x2c(%rdi), %r15d
xorl %eax, %eax
leaq 0x10c(%rsp), %r10
movl %eax, (%r10)
leaq 0x108(%rsp), %r11
movl %eax, (%r11)
leaq 0x104(%rsp), %r13
movl %eax, (%r13)
leaq 0x100(%rsp), %rbp
movl %eax, (%rbp)
leaq 0x18(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x114(%rsp), %rdx
leaq 0x110(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
pushq %rbp
pushq %r13
pushq %r11
pushq %r10
callq 0x234ea
addq $0x20, %rsp
leaq 0x1e8(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xd07e
leaq 0x278(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xd07e
leaq 0x1a8(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xd07e
leaq 0x168(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xd07e
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1e8(%rsp), %rsi
movq %r12, %rdi
callq 0xd4f2
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x278(%rsp), %rsi
movq %r12, %rdi
callq 0xd4f2
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1a8(%rsp), %rsi
callq 0xd4f2
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x168(%rsp), %rsi
callq 0xd4f2
movl %r14d, %ebp
shrl $0x1f, %ebp
addl %r14d, %ebp
sarl %ebp
cmpl %ebp, 0x210(%rsp)
jne 0x11112
movl %r15d, %r14d
shrl $0x1f, %r14d
addl %r15d, %r14d
sarl %r14d
cmpl %r14d, 0x214(%rsp)
jne 0x11112
leaq 0x308(%rsp), %rdi
callq 0x1afc0
leaq 0x2b8(%rsp), %rdi
callq 0x1afc0
leaq 0x118(%rsp), %rdi
callq 0x1afc0
leaq 0x228(%rsp), %rdi
callq 0x1afc0
movq 0x220(%rsp), %rsi
leaq 0x308(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x26390(%rip), %rdi     # 0x36fd0
leaq 0x1b5e6(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x26378(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x2b0(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x2634c(%rip), %rdi     # 0x36fd0
leaq 0x1b5a2(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x26334(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1e0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x26308(%rip), %rdi     # 0x36fd0
leaq 0x1b55e(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x262f0(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1a0(%rsp), %rsi
leaq 0x228(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x262c4(%rip), %rdi     # 0x36fd0
leaq 0x1b51a(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x262ac(%rip), %rdi     # 0x36fd0
callq 0x31f0
leaq 0x118(%rsp), %rdi
callq 0x1c844
movl %eax, %ebp
xorpd %xmm0, %xmm0
leaq 0xb0(%rsp), %rdx
movapd %xmm0, (%rdx)
xorl %eax, %eax
movq %rax, 0x10(%rdx)
movq %rax, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x1ed6a
leaq 0x228(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x1ed6a
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movslq %ebp, %r14
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rdx
movq %r14, %rsi
callq 0x12128
leaq 0x80(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
movq %r14, %rsi
callq 0x12128
leaq 0x118(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1e764
movl %ebp, %r14d
movabsq $-0x4010000000000000, %r15 # imm = 0xBFF0000000000000
testl %ebp, %ebp
jle 0x10e4b
movq 0x98(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
movsd 0x1a23d(%rip), %xmm0    # 0x2b058
movsd 0x1a225(%rip), %xmm1    # 0x2b048
testq %rsi, %rsi
je 0x10e2e
movq %r15, (%rax,%rsi,8)
jmp 0x10e43
movsd (%rcx), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rdx), %xmm2
movsd %xmm2, (%rax)
incq %rsi
cmpq %rsi, %r14
jne 0x10e23
leaq 0x228(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1e764
testl %ebp, %ebp
jle 0x10eb2
movq 0x80(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
movsd 0x1a1d6(%rip), %xmm0    # 0x2b058
movsd 0x1a1be(%rip), %xmm1    # 0x2b048
testq %rsi, %rsi
je 0x10e95
movq %r15, (%rax,%rsi,8)
jmp 0x10eaa
movsd (%rcx), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rdx), %xmm2
movsd %xmm2, (%rax)
incq %rsi
cmpq %rsi, %r14
jne 0x10e8a
leaq 0xe8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x121b2
leaq 0x118(%rsp), %rdi
leaq 0x308(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x1f1f4
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x10f01
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x121b2
leaq 0x228(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x1f1f4
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x10f50
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x1e0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x1e5f8
movq 0x26064(%rip), %rdi     # 0x36fd0
leaq 0x1b2ba(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x2604c(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1a0(%rsp), %rsi
leaq 0x228(%rsp), %rdi
callq 0x1e5f8
movq 0x2602b(%rip), %rdi     # 0x36fd0
leaq 0x1b281(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x26013(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1a8(%rsp), %rdi
callq 0xd5bc
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x168(%rsp), %rdi
callq 0xd5bc
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x11019
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x11036
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1104d
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x11064
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1107b
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x11098
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x228(%rsp), %rdi
callq 0x1b33a
leaq 0x118(%rsp), %rdi
callq 0x1b33a
leaq 0x2b8(%rsp), %rdi
callq 0x1b33a
leaq 0x308(%rsp), %rdi
callq 0x1b33a
leaq 0x168(%rsp), %rdi
callq 0xd0fc
leaq 0x1a8(%rsp), %rdi
callq 0xd0fc
leaq 0x278(%rsp), %rdi
callq 0xd0fc
leaq 0x1e8(%rsp), %rdi
callq 0xd0fc
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b72f(%rip), %rdi     # 0x2c848
leaq 0x1b3a8(%rip), %rsi     # 0x2c4c8
leaq 0x1b76a(%rip), %rcx     # 0x2c891
movl $0x27a, %edx            # imm = 0x27A
callq 0x31c0
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x111c5
movq 0xe0(%rsp), %rsi
jmp 0x11167
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x111c5
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x111c5
movq %rax, %rbx
jmp 0x111e2
movq %rax, %rbx
jmp 0x111ff
jmp 0x11263
movq %rax, %rbx
jmp 0x11273
movq %rax, %rbx
jmp 0x11280
movq %rax, %rbx
jmp 0x1128d
jmp 0x111ba
movq %rax, %rbx
jmp 0x112a7
movq %rax, %rbx
jmp 0x112b4
movq %rax, %rbx
jmp 0x112c1
movq %rax, %rbx
jmp 0x1122d
movq %rax, %rbx
jmp 0x1129a
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x111e2
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x111ff
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x11216
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1122d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x11244
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x11266
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x11266
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x1b33a
leaq 0x118(%rsp), %rdi
callq 0x1b33a
leaq 0x2b8(%rsp), %rdi
callq 0x1b33a
leaq 0x308(%rsp), %rdi
callq 0x1b33a
leaq 0x168(%rsp), %rdi
callq 0xd0fc
leaq 0x1a8(%rsp), %rdi
callq 0xd0fc
leaq 0x278(%rsp), %rdi
callq 0xd0fc
leaq 0x1e8(%rsp), %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0x3500