// File: /workspace/github_cpp_makefile_star2_O3/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 0x1172f
movq %r8, %r15
movq %rcx, %r12
movl 0x2c(%rcx), %ebp
cmpl %ebp, 0x2c(%r8)
jne 0x1172f
movq %rdi, %r13
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1c346
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 0x1d788
movq 0x25ce0(%rip), %r13     # 0x36fd0
leaq 0x19f0e(%rip), %rsi     # 0x2b205
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 0x1d788
movl $0x1, %edx
movq %r13, %rdi
leaq 0x19ec1(%rip), %rsi     # 0x2b205
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 0x1ead4
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1ead4
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 0x1e4de
movq %r15, %rdi
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r15
callq 0x1c346
subq %r12, %r15
sarq $0x3, %r15
incl %eax
cltq
cmpq %rax, %r15
jne 0x1174e
movslq 0x14(%rsp), %r15
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movq %r15, %rsi
callq 0x120c0
movq 0xc0(%rsp), %r12
leaq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %r15, %rsi
callq 0x120c0
movl 0x14(%rsp), %r8d
testl %r8d, %r8d
jle 0x1149c
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 0x18c0f(%rip), %xmm0    # 0x2a058
movsd 0x18bf7(%rip), %xmm1    # 0x2a048
movabsq $-0x4010000000000000, %r10 # imm = 0xBFF0000000000000
testq %r9, %r9
je 0x1146a
movq %r10, (%rax,%r9,8)
movq %r10, (%rcx,%r9,8)
jmp 0x11494
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 0x1145b
leaq 0xe8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1214e
movq 0xb8(%rsp), %r15
leaq 0xe8(%rsp), %rdx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x1ef54
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x114e8
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
leaq 0xd0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1214e
leaq 0xd0(%rsp), %rdx
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x1ef54
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x1152c
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x38(%r12), %rsi
movq %r14, %rdi
callq 0x1e372
movq 0x25a90(%rip), %rdi     # 0x36fd0
leaq 0x19cbe(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x25a78(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x38(%r15), %rsi
movq %r13, %rdi
callq 0x1e372
movq 0x25a60(%rip), %rdi     # 0x36fd0
leaq 0x19c8e(%rip), %rsi     # 0x2b205
movl $0x1, %edx
callq 0x3360
movq 0x25a48(%rip), %rdi     # 0x36fd0
callq 0x31f0
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
xorl %edi, %edi
callq 0x231fe
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
jge 0x11607
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 0x115fa
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 0x115dd
addl $0x2, %eax
addl %esi, %r9d
addl %r8d, %r10d
cmpl %ebp, %eax
jl 0x115d6
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl $0x2, %edi
callq 0x231fe
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
jge 0x11682
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 0x11675
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 0x11658
addl $0x2, %eax
addl %esi, %r9d
addl %r8d, %r10d
cmpl %ebp, %eax
jl 0x11651
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x11699
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x116b3
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x116d0
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x116e7
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x116fe
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1171b
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 0x1a17b(%rip), %rdi     # 0x2b8b1
leaq 0x19d73(%rip), %rsi     # 0x2b4b0
leaq 0x1a198(%rip), %rcx     # 0x2b8dc
movl $0x2be, %edx            # imm = 0x2BE
callq 0x31c0
leaq 0x1a224(%rip), %rdi     # 0x2b979
leaq 0x19d54(%rip), %rsi     # 0x2b4b0
leaq 0x1a179(%rip), %rcx     # 0x2b8dc
movl $0x2dc, %edx            # imm = 0x2DC
callq 0x31c0
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x117bf
movq 0xe0(%rsp), %rsi
jmp 0x1179f
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x117bf
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
jmp 0x117bf
movq %rax, %rbx
jmp 0x117d6
jmp 0x117b2
jmp 0x117bc
movq %rax, %rbx
jmp 0x117f0
movq %rax, %rbx
jmp 0x11824
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x117d6
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x117f0
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x1180d
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x11824
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1183b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x11858
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500