// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: altProj_deBayer(ImageReal&, Image3ChnlReal&)
void altProj_deBayer(ImageReal &grayImage, Image3ChnlReal &rgbImage)
{
	cout << "DeBayer alternating projections (A-E)  ";	cout.flush();
	
	int width = grayImage.width(), height = grayImage.height();
	
	//Start by using bilinear interpolation to reconstruct all three incomplete channels
	cout << "a";	cout.flush();
	bilinear_deBayer(grayImage, rgbImage);
	cout << "A";	cout.flush();
	
	//Alternatively, we can use edge-sensitive interpolation to construct the incomplete green channel.
	//Doing this effectively replaces the interpolated green pixels from the bilinear interpolation above.
	//A more efficient approach would not perform both methods on the green channel.
	//In one experiment I got a more accurate green reconstruction using bilinear however.
	
	cout << "b";	cout.flush();
	int startX = (determineBayerColor(0, 0) == G) ? 1 : 0;
	for (int y = 0; y < height; y++)
		for (int x = (y % 2 == 0 ? startX : (startX + 1) % 2); x < width; x += 2)
			hue_constructGreenChannelEdgeSensitive(grayImage, rgbImage, x, y);
	cout << "B";	cout.flush();
	
	//Repair the interpolated green pixels
	cout << "c";	cout.flush();
	altProj_repairGreen(grayImage, rgbImage);
	cout << "C";	cout.flush();
	
	//Generate wavelet decompositions for all three channels and analyze green
	ImageReal rReal = *rgbImage[0], gReal = *rgbImage[1], bReal = *rgbImage[2];
	
	SteerablePyramid steerPyrR, steerPyrG, steerPyrB;
	cout << "d";	cout.flush();
	assert(rgbImage[1]->width() == width && rgbImage[1]->height() == height);
	steerPyrG.DoFullWaveletAnalysis(gReal.img(), width, height, true);
	cout << "D";	cout.flush();
	
	//Loop until a stopping criterion is satisifed
	int numIterations = 0;
	while (true)
	{
		cout << "e";	cout.flush();
		bool converged = altProj_repairRedBlue(steerPyrR, steerPyrG, steerPyrB,
												rReal, bReal,
												*rgbImage[0], *rgbImage[2]);
		
		//if (converged)
		//	break;
		if (++numIterations == 2)	//2 yields highest performance on unnoisy lena. 
			break;
	}
	cout << "E";	cout.flush();
	
	*rgbImage[0] = rReal;
	*rgbImage[2] = bReal;
	
	cout << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %rbx
movq %rdi, %r15
movq 0x2187d(%rip), %r12     # 0x2ffd0
leaq 0x15243(%rip), %rsi     # 0x2399d
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
movl 0x28(%r15), %ebp
movl 0x2c(%r15), %r14d
leaq 0x1524c(%rip), %rsi     # 0x239c5
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xcdc3
leaq 0x1522c(%rip), %rsi     # 0x239c7
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
leaq 0x13bdf(%rip), %rsi     # 0x22391
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
xorl %r12d, %r12d
xorl %edi, %edi
xorl %esi, %esi
callq 0x1c294
movl %eax, 0x14(%rsp)
testl %r14d, %r14d
movl $0x0, %r13d
movl %r14d, 0x10(%rsp)
cmovgl %r14d, %r13d
cmpl %r13d, %r12d
je 0xe81f
cmpl $0x1, 0x14(%rsp)
sete %al
movl %r12d, %ecx
xorb %al, %cl
movzbl %cl, %r14d
andl $0x1, %r14d
cmpl %ebp, %r14d
jge 0xe81a
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r12d, %ecx
callq 0xcf3e
addl $0x2, %r14d
jmp 0xe7fe
incl %r12d
jmp 0xe7e4
movq 0x217aa(%rip), %r12     # 0x2ffd0
leaq 0x14ff1(%rip), %rsi     # 0x2381e
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
leaq 0x15185(%rip), %rsi     # 0x239c9
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xdb63
leaq 0x15165(%rip), %rsi     # 0x239cb
movq %r12, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x31d0
movq 0x38(%rbx), %rax
movq (%rax), %rsi
leaq 0x58(%rsp), %rdi
callq 0xa5a2
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xa5a2
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0xa5a2
movl 0x10(%rsp), %r14d
leaq 0x178(%rsp), %rdi
callq 0x15294
leaq 0x98(%rsp), %rdi
callq 0x15294
leaq 0x128(%rsp), %rdi
callq 0x15294
movq 0x216ef(%rip), %rdi     # 0x2ffd0
leaq 0x13a19(%rip), %rsi     # 0x22301
callq 0x32c0
movq 0x216dc(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
cmpl %ebp, 0x28(%rax)
jne 0xea5f
cmpl %r14d, 0x2c(%rax)
jne 0xea5f
movq 0x120(%rsp), %rsi
leaq 0x98(%rsp), %rdi
pushq $0x1
popq %r8
movl %ebp, %edx
movl %r14d, %ecx
callq 0x17960
movq 0x21697(%rip), %rdi     # 0x2ffd0
leaq 0x138b5(%rip), %rsi     # 0x221f5
callq 0x32c0
movq 0x21684(%rip), %rdi     # 0x2ffd0
callq 0x31d0
pushq $0x2
popq %r13
movq 0x21674(%rip), %r14     # 0x2ffd0
leaq 0x128(%rsp), %rbp
leaq 0x58(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r14, %rdi
leaq 0x147d1(%rip), %rsi     # 0x23149
callq 0x32c0
movq %r14, %rdi
callq 0x31d0
movq 0x38(%rbx), %rax
movq (%rax), %r9
movq 0x10(%rax), %rax
movq %rax, (%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0xe275
decl %r13d
jne 0xe96e
movq 0x21612(%rip), %rdi     # 0x2ffd0
leaq 0x13fc4(%rip), %rsi     # 0x22989
callq 0x32c0
movq 0x215ff(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x38(%rbx), %rax
movq (%rax), %rdi
leaq 0x58(%rsp), %rsi
callq 0xa5e6
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xa5e6
movq 0x215d0(%rip), %rdi     # 0x2ffd0
callq 0x3120
leaq 0x128(%rsp), %rdi
callq 0x155fc
leaq 0x98(%rsp), %rdi
callq 0x155fc
leaq 0x178(%rsp), %rdi
callq 0x155fc
leaq 0x18(%rsp), %rdi
callq 0xa6a6
leaq 0xe8(%rsp), %rdi
callq 0xa6a6
leaq 0x58(%rsp), %rdi
callq 0xa6a6
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14f67(%rip), %rdi     # 0x239cd
leaq 0x14a33(%rip), %rsi     # 0x234a0
leaq 0x14f9a(%rip), %rcx     # 0x23a0e
movl $0x346, %edx            # imm = 0x346
callq 0x31a0
movq %rax, %rbx
jmp 0xeaad
movq %rax, %rbx
jmp 0xeaba
movq %rax, %rbx
jmp 0xeac7
movq %rax, %rbx
jmp 0xead1
movq %rax, %rbx
jmp 0xeade
jmp 0xea9d
jmp 0xea9d
jmp 0xea9d
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x155fc
leaq 0x98(%rsp), %rdi
callq 0x155fc
leaq 0x178(%rsp), %rdi
callq 0x155fc
leaq 0x18(%rsp), %rdi
callq 0xa6a6
leaq 0xe8(%rsp), %rdi
callq 0xa6a6
leaq 0x58(%rsp), %rdi
callq 0xa6a6
movq %rbx, %rdi
callq 0x34d0