// File: /workspace/github_cpp_makefile_star2_O3/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 0x25752(%rip), %r12     # 0x36fd0
leaq 0x1a128(%rip), %rsi     # 0x2b9ad
movl $0x27, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movl 0x28(%r15), %ebp
movl 0x2c(%r15), %r13d
leaq 0x1a12c(%rip), %rsi     # 0x2b9d5
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xfc37
leaq 0x1a107(%rip), %rsi     # 0x2b9d7
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
leaq 0x18ab9(%rip), %rsi     # 0x2a3a5
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
xorl %r12d, %r12d
xorl %edi, %edi
xorl %esi, %esi
callq 0x23198
movl %eax, 0x14(%rsp)
testl %r13d, %r13d
jle 0x11952
cmpl $0x1, 0x14(%rsp)
sete %al
movl %r12d, %ecx
xorb %al, %cl
movzbl %cl, %r14d
andl $0x1, %r14d
cmpl %r14d, %ebp
jle 0x1194a
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r12d, %ecx
callq 0xfdae
addl $0x2, %r14d
cmpl %ebp, %r14d
jl 0x11930
incl %r12d
cmpl %r13d, %r12d
jne 0x11916
movq 0x25677(%rip), %r12     # 0x36fd0
leaq 0x19ece(%rip), %rsi     # 0x2b82e
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
leaq 0x1a05d(%rip), %rsi     # 0x2b9d9
movl $0x1, %edx
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
callq 0x31f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10a11
leaq 0x1a038(%rip), %rsi     # 0x2b9db
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 0xcf2a
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xcf2a
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0xcf2a
leaq 0x178(%rsp), %rdi
callq 0x1ab34
leaq 0x98(%rsp), %rdi
callq 0x1ab34
leaq 0x128(%rsp), %rdi
callq 0x1ab34
movq 0x255b2(%rip), %rdi     # 0x36fd0
leaq 0x188f0(%rip), %rsi     # 0x2a315
movl $0x1, %edx
callq 0x3360
movq 0x2559a(%rip), %rdi     # 0x36fd0
callq 0x31f0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
cmpl %ebp, 0x28(%rax)
jne 0x11bd6
cmpl %r13d, 0x2c(%rax)
jne 0x11bd6
movq 0x120(%rsp), %rsi
leaq 0x98(%rsp), %rdi
movl %ebp, %edx
movl %r13d, %ecx
movl $0x1, %r8d
callq 0x1d788
movq 0x25553(%rip), %rdi     # 0x36fd0
leaq 0x18785(%rip), %rsi     # 0x2a209
movl $0x1, %edx
callq 0x3360
movq 0x2553b(%rip), %rdi     # 0x36fd0
callq 0x31f0
movl $0x2, %r13d
movq 0x25529(%rip), %r14     # 0x36fd0
leaq 0x128(%rsp), %rbp
leaq 0x58(%rsp), %r15
leaq 0x18(%rsp), %r12
movl $0x1, %edx
movq %r14, %rdi
leaq 0x19691(%rip), %rsi     # 0x2b159
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 0x1126d
decl %r13d
jne 0x11ab9
movq 0x254c2(%rip), %rdi     # 0x36fd0
leaq 0x18e88(%rip), %rsi     # 0x2a99d
movl $0x1, %edx
callq 0x3360
movq 0x254aa(%rip), %r14     # 0x36fd0
movq %r14, %rdi
callq 0x31f0
movq 0x38(%rbx), %rax
movq (%rax), %rdi
leaq 0x58(%rsp), %rsi
callq 0xcf72
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xcf72
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x25461(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
leaq 0x128(%rsp), %rdi
callq 0x1aeae
leaq 0x98(%rsp), %rdi
callq 0x1aeae
leaq 0x178(%rsp), %rdi
callq 0x1aeae
leaq 0x18(%rsp), %rdi
callq 0xd038
leaq 0xe8(%rsp), %rdi
callq 0xd038
leaq 0x58(%rsp), %rdi
callq 0xd038
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19e00(%rip), %rdi     # 0x2b9dd
leaq 0x198cc(%rip), %rsi     # 0x2b4b0
leaq 0x19e33(%rip), %rcx     # 0x2ba1e
movl $0x346, %edx            # imm = 0x346
callq 0x31c0
movq %rax, %rbx
jmp 0x11c24
movq %rax, %rbx
jmp 0x11c31
movq %rax, %rbx
jmp 0x11c3e
movq %rax, %rbx
jmp 0x11c48
movq %rax, %rbx
jmp 0x11c55
jmp 0x11c14
jmp 0x11c14
jmp 0x11c14
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x1aeae
leaq 0x98(%rsp), %rdi
callq 0x1aeae
leaq 0x178(%rsp), %rdi
callq 0x1aeae
leaq 0x18(%rsp), %rdi
callq 0xd038
leaq 0xe8(%rsp), %rdi
callq 0xd038
leaq 0x58(%rsp), %rdi
callq 0xd038
movq %rbx, %rdi
callq 0x3500