// File: 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
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x3d246(%rip), %rdi     # 0x59fd0
leaq 0x2dcdc(%rip), %rsi     # 0x4aa6d
callq 0x4390
movq 0x3d233(%rip), %rdi     # 0x59fd0
callq 0x4250
movq -0x8(%rbp), %rdi
callq 0xedd0
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
callq 0xedf0
movl %eax, -0x18(%rbp)
movq 0x3d20f(%rip), %rdi     # 0x59fd0
leaq 0x2dccd(%rip), %rsi     # 0x4aa95
callq 0x4390
movq 0x3d1fc(%rip), %rdi     # 0x59fd0
callq 0x4250
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1a630
movq 0x3d1e3(%rip), %rdi     # 0x59fd0
leaq 0x2dca3(%rip), %rsi     # 0x4aa97
callq 0x4390
movq 0x3d1d0(%rip), %rdi     # 0x59fd0
callq 0x4250
movq 0x3d1c4(%rip), %rdi     # 0x59fd0
leaq 0x2c617(%rip), %rsi     # 0x4942a
callq 0x4390
movq 0x3d1b1(%rip), %rdi     # 0x59fd0
callq 0x4250
xorl %esi, %esi
movl %esi, %edi
callq 0x380a0
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1ceb9
movl -0x20(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1ce69
movl -0x1c(%rbp), %edx
movl %edx, -0x1f4(%rbp)
jmp 0x1ce7d
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x1f4(%rbp)
movl -0x1f4(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1ceac
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x24(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x1a850
movl -0x24(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ce86
jmp 0x1ceae
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1ce46
movq 0x3d110(%rip), %rdi     # 0x59fd0
movq %rdi, -0x208(%rbp)
leaq 0x2da20(%rip), %rsi     # 0x4a8ee
callq 0x4390
movq -0x208(%rbp), %rdi
callq 0x4250
movq -0x208(%rbp), %rdi
leaq 0x2dbac(%rip), %rsi     # 0x4aa99
callq 0x4390
movq -0x208(%rbp), %rdi
callq 0x4250
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1b9d0
movq -0x208(%rbp), %rdi
leaq 0x2db82(%rip), %rsi     # 0x4aa9b
callq 0x4390
movq -0x208(%rbp), %rdi
callq 0x4250
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x15620
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x200(%rbp)
jmp 0x1cf58
movq -0x200(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x15620
jmp 0x1cf6d
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq %rax, -0x210(%rbp)
jmp 0x1cf84
movq -0x210(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x15620
jmp 0x1cf99
leaq -0x148(%rbp), %rdi
callq 0x293b0
jmp 0x1cfa7
leaq -0x198(%rbp), %rdi
callq 0x293b0
jmp 0x1cfb5
leaq -0x1e8(%rbp), %rdi
callq 0x293b0
jmp 0x1cfc3
movq 0x3d006(%rip), %rdi     # 0x59fd0
leaq 0x2c3c9(%rip), %rsi     # 0x4939a
callq 0x4390
jmp 0x1cfd8
movq 0x3cff1(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1cfe6
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x218(%rbp)
jmp 0x1cffd
movq -0x218(%rbp), %rdi
callq 0xedd0
movl %eax, -0x21c(%rbp)
jmp 0x1d011
movl -0x21c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x14(%rbp), %ecx
movb %al, -0x21d(%rbp)
jne 0x1d061
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x228(%rbp)
jmp 0x1d03b
movq -0x228(%rbp), %rdi
callq 0xedf0
movl %eax, -0x22c(%rbp)
jmp 0x1d04f
movl -0x22c(%rbp), %eax
cmpl -0x18(%rbp), %eax
sete %al
movb %al, -0x21d(%rbp)
movb -0x21d(%rbp), %al
testb $0x1, %al
jne 0x1d06d
jmp 0x1d072
jmp 0x1d127
leaq 0x2da24(%rip), %rdi     # 0x4aa9d
leaq 0x2d4f0(%rip), %rsi     # 0x4a570
movl $0x346, %edx            # imm = 0x346
leaq 0x2da52(%rip), %rcx     # 0x4aade
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1d34c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1d340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1d334
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1d328
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1d31c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x299e0
jmp 0x1d31c
leaq -0xa8(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x238(%rbp)
jmp 0x1d13c
movq -0x238(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq -0x198(%rbp), %rdi
movl $0x1, %r8d
callq 0x2d2c0
jmp 0x1d15d
movq 0x3ce6c(%rip), %rdi     # 0x59fd0
leaq 0x2c112(%rip), %rsi     # 0x4927d
callq 0x4390
jmp 0x1d172
movq 0x3ce57(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1d180
movl $0x0, -0x1ec(%rbp)
jmp 0x1d18c
movq 0x3ce3d(%rip), %rdi     # 0x59fd0
leaq 0x2d097(%rip), %rsi     # 0x4a231
callq 0x4390
jmp 0x1d1a1
movq 0x3ce28(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1d1af
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq %rax, -0x240(%rbp)
jmp 0x1d1c3
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq %rax, -0x248(%rbp)
jmp 0x1d1da
movq -0x240(%rbp), %r9
movq -0x248(%rbp), %rcx
movq %rsp, %rax
movq %rcx, (%rax)
leaq -0x148(%rbp), %rdi
leaq -0x198(%rbp), %rsi
leaq -0x1e8(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0xf8(%rbp), %r8
callq 0x1c4f0
movb %al, -0x249(%rbp)
jmp 0x1d21b
movb -0x249(%rbp), %al
andb $0x1, %al
movb %al, -0x1ed(%rbp)
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
cmpl $0x2, %eax
jne 0x1d23f
jmp 0x1d244
jmp 0x1d18a
movq 0x3cd85(%rip), %rdi     # 0x59fd0
leaq 0x2c821(%rip), %rsi     # 0x49a73
callq 0x4390
jmp 0x1d259
movq 0x3cd70(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1d267
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq %rax, -0x258(%rbp)
jmp 0x1d27b
movq -0x258(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x15690
jmp 0x1d28d
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq %rax, -0x260(%rbp)
jmp 0x1d2a4
movq -0x260(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x15690
jmp 0x1d2b9
movq 0x3cd10(%rip), %rdi     # 0x59fd0
movq 0x3ccf1(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x1d2ce
leaq -0x1e8(%rbp), %rdi
callq 0x299e0
leaq -0x198(%rbp), %rdi
callq 0x299e0
leaq -0x148(%rbp), %rdi
callq 0x299e0
leaq -0xf8(%rbp), %rdi
callq 0x157e0
leaq -0xa8(%rbp), %rdi
callq 0x157e0
leaq -0x68(%rbp), %rdi
callq 0x157e0
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
leaq -0x198(%rbp), %rdi
callq 0x299e0
leaq -0x148(%rbp), %rdi
callq 0x299e0
leaq -0xf8(%rbp), %rdi
callq 0x157e0
leaq -0xa8(%rbp), %rdi
callq 0x157e0
leaq -0x68(%rbp), %rdi
callq 0x157e0
movq -0xb0(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)