// File: /workspace/github_cpp_makefile_star2_O3/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 0x2327e
addq $0x20, %rsp
leaq 0x1e8(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xcfba
leaq 0x278(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xcfba
leaq 0x1a8(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xcfba
leaq 0x168(%rsp), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xcfba
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1e8(%rsp), %rsi
movq %r12, %rdi
callq 0xd426
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x278(%rsp), %rsi
movq %r12, %rdi
callq 0xd426
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1a8(%rsp), %rsi
callq 0xd426
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x168(%rsp), %rsi
callq 0xd426
movl %r14d, %ebp
shrl $0x1f, %ebp
addl %r14d, %ebp
sarl %ebp
cmpl %ebp, 0x210(%rsp)
jne 0x110a9
movl %r15d, %r14d
shrl $0x1f, %r14d
addl %r15d, %r14d
sarl %r14d
cmpl %r14d, 0x214(%rsp)
jne 0x110a9
leaq 0x308(%rsp), %rdi
callq 0x1ab34
leaq 0x2b8(%rsp), %rdi
callq 0x1ab34
leaq 0x118(%rsp), %rdi
callq 0x1ab34
leaq 0x228(%rsp), %rdi
callq 0x1ab34
movq 0x220(%rsp), %rsi
leaq 0x308(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1d788
movq 0x263f9(%rip), %rdi     # 0x36fd0
leaq 0x1a627(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x263e1(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x2b0(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1d788
movq 0x263b5(%rip), %rdi     # 0x36fd0
leaq 0x1a5e3(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x2639d(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1e0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1d788
movq 0x26371(%rip), %rdi     # 0x36fd0
leaq 0x1a59f(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x26359(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1a0(%rsp), %rsi
leaq 0x228(%rsp), %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x1d788
movq 0x2632d(%rip), %rdi     # 0x36fd0
leaq 0x1a55b(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x26315(%rip), %rdi     # 0x36fd0
callq 0x31f0
leaq 0x118(%rsp), %rdi
callq 0x1c346
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 0x1ead4
leaq 0x228(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x1ead4
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 0x120c0
leaq 0x80(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
movq %r14, %rsi
callq 0x120c0
leaq 0x118(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1e4de
movl %ebp, %r14d
movabsq $-0x4010000000000000, %r15 # imm = 0xBFF0000000000000
testl %ebp, %ebp
jle 0x10de2
movq 0x98(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
movsd 0x192a6(%rip), %xmm0    # 0x2a058
movsd 0x1928e(%rip), %xmm1    # 0x2a048
testq %rsi, %rsi
je 0x10dc5
movq %r15, (%rax,%rsi,8)
jmp 0x10dda
movsd (%rcx), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rdx), %xmm2
movsd %xmm2, (%rax)
incq %rsi
cmpq %rsi, %r14
jne 0x10dba
leaq 0x228(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1e4de
testl %ebp, %ebp
jle 0x10e49
movq 0x80(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
movsd 0x1923f(%rip), %xmm0    # 0x2a058
movsd 0x19227(%rip), %xmm1    # 0x2a048
testq %rsi, %rsi
je 0x10e2c
movq %r15, (%rax,%rsi,8)
jmp 0x10e41
movsd (%rcx), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rdx), %xmm2
movsd %xmm2, (%rax)
incq %rsi
cmpq %rsi, %r14
jne 0x10e21
leaq 0xe8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x1214e
leaq 0x118(%rsp), %rdi
leaq 0x308(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x1ef54
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x10e98
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1214e
leaq 0x228(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x1ef54
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x10ee7
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x1e0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x1e372
movq 0x260cd(%rip), %rdi     # 0x36fd0
leaq 0x1a2fb(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x260b5(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x1a0(%rsp), %rsi
leaq 0x228(%rsp), %rdi
callq 0x1e372
movq 0x26094(%rip), %rdi     # 0x36fd0
leaq 0x1a2c2(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x2607c(%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 0xd4fa
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x168(%rsp), %rdi
callq 0xd4fa
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x10fb0
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x10fcd
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x10fe4
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x10ffb
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x11012
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x1102f
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0x228(%rsp), %rdi
callq 0x1aeae
leaq 0x118(%rsp), %rdi
callq 0x1aeae
leaq 0x2b8(%rsp), %rdi
callq 0x1aeae
leaq 0x308(%rsp), %rdi
callq 0x1aeae
leaq 0x168(%rsp), %rdi
callq 0xd038
leaq 0x1a8(%rsp), %rdi
callq 0xd038
leaq 0x278(%rsp), %rdi
callq 0xd038
leaq 0x1e8(%rsp), %rdi
callq 0xd038
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a780(%rip), %rdi     # 0x2b830
leaq 0x1a3f9(%rip), %rsi     # 0x2b4b0
leaq 0x1a7bb(%rip), %rcx     # 0x2b879
movl $0x27a, %edx            # imm = 0x27A
callq 0x31c0
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x1115c
movq 0xe0(%rsp), %rsi
jmp 0x110fe
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x1115c
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x1115c
movq %rax, %rbx
jmp 0x11179
movq %rax, %rbx
jmp 0x11196
jmp 0x111fa
movq %rax, %rbx
jmp 0x1120a
movq %rax, %rbx
jmp 0x11217
movq %rax, %rbx
jmp 0x11224
jmp 0x11151
movq %rax, %rbx
jmp 0x1123e
movq %rax, %rbx
jmp 0x1124b
movq %rax, %rbx
jmp 0x11258
movq %rax, %rbx
jmp 0x111c4
movq %rax, %rbx
jmp 0x11231
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x11179
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x11196
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x111ad
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x111c4
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x111db
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x111fd
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x111fd
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x1aeae
leaq 0x118(%rsp), %rdi
callq 0x1aeae
leaq 0x2b8(%rsp), %rdi
callq 0x1aeae
leaq 0x308(%rsp), %rdi
callq 0x1aeae
leaq 0x168(%rsp), %rdi
callq 0xd038
leaq 0x1a8(%rsp), %rdi
callq 0xd038
leaq 0x278(%rsp), %rdi
callq 0xd038
leaq 0x1e8(%rsp), %rdi
callq 0xd038
movq %rbx, %rdi
callq 0x3500