// File: /workspace/github_cpp_makefile_star2_O1/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 0x256e9(%rip), %r12     # 0x36fd0
leaq 0x1b0d7(%rip), %rsi     # 0x2c9c5
movl $0x27, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movl 0x28(%r15), %ebp
movl 0x2c(%r15), %r13d
leaq 0x1b0db(%rip), %rsi     # 0x2c9ed
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xfcde
leaq 0x1b0b6(%rip), %rsi     # 0x2c9ef
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
leaq 0x19a38(%rip), %rsi     # 0x2b38d
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
xorl %r12d, %r12d
xorl %edi, %edi
xorl %esi, %esi
callq 0x23404
movl %eax, 0x14(%rsp)
testl %r13d, %r13d
jle 0x119bb
cmpl $0x1, 0x14(%rsp)
sete %al
movl %r12d, %ecx
xorb %al, %cl
movzbl %cl, %r14d
andl $0x1, %r14d
cmpl %r14d, %ebp
jle 0x119b3
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r12d, %ecx
callq 0xfe53
addl $0x2, %r14d
cmpl %ebp, %r14d
jl 0x11999
incl %r12d
cmpl %r13d, %r12d
jne 0x1197f
movq 0x2560e(%rip), %r12     # 0x36fd0
leaq 0x1ae7d(%rip), %rsi     # 0x2c846
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
leaq 0x1b00c(%rip), %rsi     # 0x2c9f1
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10a7a
leaq 0x1afe7(%rip), %rsi     # 0x2c9f3
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movq 0x38(%rbx), %rax
movq (%rax), %rsi
leaq 0x58(%rsp), %rdi
callq 0xcfee
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xcfee
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0xcfee
leaq 0x178(%rsp), %rdi
callq 0x1afc0
leaq 0x98(%rsp), %rdi
callq 0x1afc0
leaq 0x128(%rsp), %rdi
callq 0x1afc0
movq 0x25549(%rip), %rdi     # 0x36fd0
leaq 0x1986f(%rip), %rsi     # 0x2b2fd
movl $0x1, %edx
callq 0x3360
movq 0x25531(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
cmpl %ebp, 0x28(%rax)
jne 0x11c3f
cmpl %r13d, 0x2c(%rax)
jne 0x11c3f
movq 0x120(%rsp), %rsi
leaq 0x98(%rsp), %rdi
movl %ebp, %edx
movl %r13d, %ecx
movl $0x1, %r8d
callq 0x1db64
movq 0x254ea(%rip), %rdi     # 0x36fd0
leaq 0x19704(%rip), %rsi     # 0x2b1f1
movl $0x1, %edx
callq 0x3360
movq 0x254d2(%rip), %rdi     # 0x36fd0
callq 0x31f0
movl $0x2, %r13d
movq 0x254c0(%rip), %r14     # 0x36fd0
leaq 0x128(%rsp), %rbp
leaq 0x58(%rsp), %r15
leaq 0x18(%rsp), %r12
movl $0x1, %edx
movq %r14, %rdi
leaq 0x1a650(%rip), %rsi     # 0x2c181
callq 0x3360
movq %r14, %rdi
callq 0x31f0
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 0x112d6
decl %r13d
jne 0x11b22
movq 0x25459(%rip), %rdi     # 0x36fd0
leaq 0x19e41(%rip), %rsi     # 0x2b9bf
movl $0x1, %edx
callq 0x3360
movq 0x25441(%rip), %r14     # 0x36fd0
movq %r14, %rdi
callq 0x31f0
movq 0x38(%rbx), %rax
movq (%rax), %rdi
leaq 0x58(%rsp), %rsi
callq 0xd036
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xd036
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x253f8(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
leaq 0x128(%rsp), %rdi
callq 0x1b33a
leaq 0x98(%rsp), %rdi
callq 0x1b33a
leaq 0x178(%rsp), %rdi
callq 0x1b33a
leaq 0x18(%rsp), %rdi
callq 0xd0fc
leaq 0xe8(%rsp), %rdi
callq 0xd0fc
leaq 0x58(%rsp), %rdi
callq 0xd0fc
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1adaf(%rip), %rdi     # 0x2c9f5
leaq 0x1a87b(%rip), %rsi     # 0x2c4c8
leaq 0x1ade2(%rip), %rcx     # 0x2ca36
movl $0x346, %edx            # imm = 0x346
callq 0x31c0
movq %rax, %rbx
jmp 0x11c8d
movq %rax, %rbx
jmp 0x11c9a
movq %rax, %rbx
jmp 0x11ca7
movq %rax, %rbx
jmp 0x11cb1
movq %rax, %rbx
jmp 0x11cbe
jmp 0x11c7d
jmp 0x11c7d
jmp 0x11c7d
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x1b33a
leaq 0x98(%rsp), %rdi
callq 0x1b33a
leaq 0x178(%rsp), %rdi
callq 0x1b33a
leaq 0x18(%rsp), %rdi
callq 0xd0fc
leaq 0xe8(%rsp), %rdi
callq 0xd0fc
leaq 0x58(%rsp), %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0x3500