// File: /workspace/github_cpp_makefile_star2_O1/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: altProj_repairRedBlue(SteerablePyramid&, SteerablePyramid const&, SteerablePyramid&, ImageReal&, ImageReal&, ImageReal const&, ImageReal const&)
bool altProj_repairRedBlue(SteerablePyramid &steerPyrR, const SteerablePyramid &steerPyrG, SteerablePyramid &steerPyrB,
							ImageReal &r, ImageReal &b,
							const ImageReal &rRealOri, const ImageReal &bRealOri)
{
	int width = r.width(), height = r.height();
	assert(b.width() == width && b.height() == height);
	
	int numLevels = steerPyrG.NumLevels();
	
	//Perform a wavelet analysis of red and blue
	steerPyrR.DoFullWaveletAnalysis(r.img(), width, height, true);
	cout << ".";	cout.flush();
	steerPyrB.DoFullWaveletAnalysis(b.img(), width, height, true);
	cout << ".";	cout.flush();
	
	//Compare the high frequency components of red and blue with green.
	//Use the similarity/correlation between high frequency red/green and blue/green
	//to derive thresholds to be used below.
	vector<Float_t> greenRedCorrs, greenBlueCorrs;	//One correlation per level
	steerPyrG.findCorrelations(steerPyrR, greenRedCorrs);
	steerPyrG.findCorrelations(steerPyrB, 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;
	*/
	int arrayOffset = 1;	//1 for Steerable Pyramid, 0 for Daubechies
	
	vector<Float_t> means, stdDevs;
	steerPyrG.CalcStats(means, stdDevs);
	assert(means.size() == steerPyrG.NumLevels() + arrayOffset);
	
	vector<Float_t> thresholdsR(numLevels), thresholdsB(numLevels);
	for (int i = 0; i < numLevels; i++)
	{
		if (i == 0)
		{
			thresholdsR[i] = (((greenRedCorrs[i] + 1) / 2) * means[i + 1]);
			thresholdsB[i] = (((greenBlueCorrs[i] + 1) / 2) * means[i + 1]);
		}
		else thresholdsR[i] = thresholdsB[i] = -1;
	}
	
	//for (int i = 0; i < numLevels; i++)
	//	cout << "Red/Blue thresholds: " << thresholdsR[i] << "    " << thresholdsB[i] << endl;
	
	//Push the high frequency coefficients of red and blue toward those of green
	steerPyrR.CopyCoefficients(steerPyrG, thresholdsR);
	steerPyrB.CopyCoefficients(steerPyrG, thresholdsB);
	
	//Reconstruct red and blue
	steerPyrR.DoFullWaveletSynthesis(r.img());
	cout << ".";	cout.flush();
	steerPyrB.DoFullWaveletSynthesis(b.img());
	cout << ".";	cout.flush();
	
	//Copy original red and blue pixels into the reconstruction
	int startX, startY;
	determineBlockCoord(R, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			r(x, y) = rRealOri(x, y);
	
	determineBlockCoord(B, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			b(x, y) = bRealOri(x, y);
	
	//Determine if convergence has been attained
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %r9, 0xb0(%rsp)
movq %rdx, 0xc8(%rsp)
movq %rsi, 0x18(%rsp)
movl 0x28(%rcx), %ebx
cmpl %ebx, 0x28(%r8)
jne 0x11798
movq %r8, %r15
movq %rcx, %r12
movl 0x2c(%rcx), %ebp
cmpl %ebp, 0x2c(%r8)
jne 0x11798
movq %rdi, %r13
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1c844
movl %eax, 0x14(%rsp)
movq %r12, 0xc0(%rsp)
movq 0x38(%r12), %rsi
movq %r13, %r12
movq %r13, %rdi
movl %ebx, %edx
movl %ebp, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x25c77(%rip), %r13     # 0x36fd0
leaq 0x1aecd(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
movq %r13, %rdi
callq 0x3360
movq %r13, %rdi
callq 0x31f0
movq %r15, 0xb8(%rsp)
movq 0x38(%r15), %rsi
movq %r14, %r15
movq 0xc8(%rsp), %r14
movq %r14, %rdi
movl %ebx, %edx
movl %ebp, %ecx
movl $0x1, %r8d
callq 0x1db64
movl $0x1, %edx
movq %r13, %rdi
leaq 0x1ae80(%rip), %rsi     # 0x2c22d
callq 0x3360
movq %r13, %rdi
movq %r14, %r13
movq %r12, %r14
callq 0x31f0
xorl %eax, %eax
leaq 0x90(%rsp), %rdx
movq %rax, 0x10(%rdx)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdx)
movq %rax, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x1ed6a
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1ed6a
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movapd %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0x100(%rsp), %rdx
movq %rax, 0x10(%rdx)
movapd %xmm0, (%rdx)
movq %r15, %rdi
callq 0x1e764
movq %r15, %rdi
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r15
callq 0x1c844
subq %r12, %r15
sarq $0x3, %r15
incl %eax
cltq
cmpq %rax, %r15
jne 0x117b7
movslq 0x14(%rsp), %r15
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movq %r15, %rsi
callq 0x12128
movq 0xc0(%rsp), %r12
leaq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %r15, %rsi
callq 0x12128
movl 0x14(%rsp), %r8d
testl %r8d, %r8d
jle 0x11505
movq 0x60(%rsp), %rax
movq 0x78(%rsp), %rcx
movq 0x90(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdi
movl %r8d, %r8d
xorl %r9d, %r9d
movsd 0x19ba6(%rip), %xmm0    # 0x2b058
movsd 0x19b8e(%rip), %xmm1    # 0x2b048
movabsq $-0x4010000000000000, %r10 # imm = 0xBFF0000000000000
testq %r9, %r9
je 0x114d3
movq %r10, (%rax,%r9,8)
movq %r10, (%rcx,%r9,8)
jmp 0x114fd
movsd (%rdx), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rsi), %xmm2
movsd %xmm2, (%rcx)
movsd (%rdi), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rsi), %xmm2
movsd %xmm2, (%rax)
incq %r9
cmpq %r9, %r8
jne 0x114c4
leaq 0xe8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x121b2
movq 0xb8(%rsp), %r15
leaq 0xe8(%rsp), %rdx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x1f1f4
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x11551
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xd0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x121b2
leaq 0xd0(%rsp), %rdx
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x1f1f4
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x11595
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x38(%r12), %rsi
movq %r14, %rdi
callq 0x1e5f8
movq 0x25a27(%rip), %rdi     # 0x36fd0
leaq 0x1ac7d(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x25a0f(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x38(%r15), %rsi
movq %r13, %rdi
callq 0x1e5f8
movq 0x259f7(%rip), %rdi     # 0x36fd0
leaq 0x1ac4d(%rip), %rsi     # 0x2c22d
movl $0x1, %edx
callq 0x3360
movq 0x259df(%rip), %rdi     # 0x36fd0
callq 0x31f0
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
xorl %edi, %edi
callq 0x2346a
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
jge 0x11670
movl 0xc(%rsp), %ecx
movq 0xb0(%rsp), %rsi
movq 0x38(%rsi), %rdx
movl 0x28(%rsi), %esi
movq 0x38(%r12), %rdi
movl 0x28(%r12), %r8d
movl %esi, %r9d
imull %eax, %r9d
addl %esi, %esi
movl %r8d, %r10d
imull %eax, %r10d
addl %r8d, %r8d
movl %ecx, %r11d
cmpl %ebx, %ecx
jge 0x11663
leal (%r9,%r11), %r14d
movsd (%rdx,%r14,8), %xmm0
leal (%r10,%r11), %r14d
movsd %xmm0, (%rdi,%r14,8)
addl $0x2, %r11d
cmpl %ebx, %r11d
jl 0x11646
addl $0x2, %eax
addl %esi, %r9d
addl %r8d, %r10d
cmpl %ebp, %eax
jl 0x1163f
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl $0x2, %edi
callq 0x2346a
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
jge 0x116eb
movq 0x150(%rsp), %rsi
movl 0xc(%rsp), %ecx
movq 0x38(%rsi), %rdx
movl 0x28(%rsi), %esi
movq 0x38(%r15), %rdi
movl 0x28(%r15), %r8d
movl %esi, %r9d
imull %eax, %r9d
addl %esi, %esi
movl %r8d, %r10d
imull %eax, %r10d
addl %r8d, %r8d
movl %ecx, %r11d
cmpl %ebx, %ecx
jge 0x116de
leal (%r9,%r11), %r14d
movsd (%rdx,%r14,8), %xmm0
leal (%r10,%r11), %r14d
movsd %xmm0, (%rdi,%r14,8)
addl $0x2, %r11d
cmpl %ebx, %r11d
jl 0x116c1
addl $0x2, %eax
addl %esi, %r9d
addl %r8d, %r10d
cmpl %ebp, %eax
jl 0x116ba
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x11702
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x1171c
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x11739
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x11750
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x11767
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x11784
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movb $0x1, %al
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b12a(%rip), %rdi     # 0x2c8c9
leaq 0x1ad22(%rip), %rsi     # 0x2c4c8
leaq 0x1b147(%rip), %rcx     # 0x2c8f4
movl $0x2be, %edx            # imm = 0x2BE
callq 0x31c0
leaq 0x1b1d3(%rip), %rdi     # 0x2c991
leaq 0x1ad03(%rip), %rsi     # 0x2c4c8
leaq 0x1b128(%rip), %rcx     # 0x2c8f4
movl $0x2dc, %edx            # imm = 0x2DC
callq 0x31c0
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x11828
movq 0xe0(%rsp), %rsi
jmp 0x11808
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x11828
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x11828
movq %rax, %rbx
jmp 0x1183f
jmp 0x1181b
jmp 0x11825
movq %rax, %rbx
jmp 0x11859
movq %rax, %rbx
jmp 0x1188d
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1183f
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x11859
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x11876
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1188d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x118a4
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x118c1
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500