// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: hue_deBayer(ImageReal&, Image3ChnlReal&)
void hue_deBayer(ImageReal &grayImage, Image3ChnlReal &rgbImage)
{
	int startX, startY;
	int width = grayImage.width(), height = grayImage.height();
	
	//Transfer the green channel from Bayer to output
	startX = (determineBayerColor(0, 0) == G) ? 0 : 1;
	for (int y = 0; y < height; y++)
		for (int x = (y % 2 == 0 ? startX : (startX + 1) % 2); x < width; x += 2)
			rgbImage(G, x, y) = grayImage(x, y);
	
	//Transfer the red channel from Bayer to output
	determineBlockCoord(R, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			rgbImage(R, x, y) = grayImage(x, y);
	
	//Transfer the blue channel from Bayer to output
	determineBlockCoord(B, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			rgbImage(B, x, y) = grayImage(x, y);
	
	//Use bilinear or edge-sensitive interpolation to construct the incomplete green channel
	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_constructGreenChannel(grayImage, rgbImage, x, y);
			hue_constructGreenChannelEdgeSensitive(grayImage, rgbImage, x, y);
	
	//Treating the completed green channel as a luminance channel,
	//assume the red and blue channels are chrominance channels and construct them.
	
	//Calculate red pixels at Bayer blue locations
	determineBlockCoord(B, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructRedOrBlueAtBlueOrRed(grayImage, rgbImage, *rgbImage[0], x, y);
	
	//Calculate blue pixels at Bayer red locations
	determineBlockCoord(R, startX, startY);
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructRedOrBlueAtBlueOrRed(grayImage, rgbImage, *rgbImage[2], x, y);
	
	//Calculate red pixels at Bayer green locations
	determineBlockCoord(R, startX, startY);
	startX = ++startX % 2;
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructHorizontalRedOrBlueAtGreen(grayImage, rgbImage, *rgbImage[0], x, y);
	
	determineBlockCoord(R, startX, startY);
	startY = ++startY % 2;
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructVerticalRedOrBlueAtGreen(grayImage, rgbImage, *rgbImage[0], x, y);
	
	//Calculate blue pixels at Bayer green locations
	determineBlockCoord(B, startX, startY);
	startX = ++startX % 2;
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructHorizontalRedOrBlueAtGreen(grayImage, rgbImage, *rgbImage[2], x, y);
	
	determineBlockCoord(B, startX, startY);
	startY = ++startY % 2;
	for (int y = startY; y < height; y += 2)
		for (int x = startX; x < width; x += 2)
			hue_constructVerticalRedOrBlueAtGreen(grayImage, rgbImage, *rgbImage[2], x, y);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x28(%rdi), %r13d
movl 0x2c(%rdi), %r15d
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x1c294
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x1, %eax
sete %cl
movl %ecx, 0x24(%rsp)
setne %dl
movl %edx, 0x20(%rsp)
movl %edx, 0x4(%rsp)
testl %r15d, %r15d
movl $0x0, %eax
movl %r15d, 0x8(%rsp)
cmovgl %r15d, %eax
movl %eax, 0x1c(%rsp)
pushq $0x1
popq %r15
cmpl 0x1c(%rsp), %ebp
je 0xd82d
testb $0x1, %bpl
movl 0x24(%rsp), %r12d
cmovel 0x20(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xd829
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %ebp, %ecx
addl %r12d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %ebp, %ecx
callq 0xb868
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r12d
jmp 0xd7eb
incl %ebp
jmp 0xd7d6
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r15
xorl %edi, %edi
movq %r15, %rdx
callq 0x1c2ea
movl (%r15), %ebp
cmpl 0x8(%rsp), %ebp
jge 0xd891
movl 0x4(%rsp), %r15d
cmpl %r13d, %r15d
jge 0xd88c
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movl %r15d, %edx
movl %ebp, %ecx
callq 0xb868
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r15d
jmp 0xd84f
addl $0x2, %ebp
jmp 0xd844
pushq $0x2
popq %rbp
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r15
movl %ebp, %edi
movq %r15, %rdx
callq 0x1c2ea
movl (%r15), %r15d
cmpl 0x8(%rsp), %r15d
jge 0xd8fc
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xd8f6
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %r15d, %ecx
addl %r12d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0xb868
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r12d
jmp 0xd8b7
addl $0x2, %r15d
jmp 0xd8ab
xorl %r15d, %r15d
xorl %edi, %edi
xorl %esi, %esi
callq 0x1c294
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
movl %ecx, 0x4(%rsp)
cmpl 0x1c(%rsp), %r15d
je 0xd953
movl 0x4(%rsp), %r12d
testb $0x1, %r15b
je 0xd949
incl %r12d
movl %r12d, %eax
cltd
idivl %ebp
movl %edx, %r12d
jmp 0xd949
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movl %r15d, %ecx
callq 0xcf3e
addl $0x2, %r12d
cmpl %r13d, %r12d
jl 0xd934
incl %r15d
jmp 0xd914
pushq $0x2
popq %rbp
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r15
movl %ebp, %edi
movq %r15, %rdx
callq 0x1c2ea
movl (%r15), %r15d
cmpl 0x8(%rsp), %r15d
jge 0xd9a2
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xd99c
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xd0c6
addl $0x2, %r12d
jmp 0xd979
addl $0x2, %r15d
jmp 0xd96d
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r15
xorl %edi, %edi
movq %r15, %rdx
callq 0x1c2ea
movl (%r15), %r15d
cmpl 0x8(%rsp), %r15d
jge 0xd9ef
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xd9e9
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xd0c6
addl $0x2, %r12d
jmp 0xd9c5
addl $0x2, %r15d
jmp 0xd9b9
leaq 0x4(%rsp), %r15
leaq 0xc(%rsp), %r12
xorl %edi, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1c2ea
movl (%r15), %eax
incl %eax
cltd
idivl %ebp
movl %edx, (%r15)
movl (%r12), %r15d
cmpl 0x8(%rsp), %r15d
jge 0xda4a
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xda44
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xd35d
addl $0x2, %r12d
jmp 0xda21
addl $0x2, %r15d
jmp 0xda15
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r12
xorl %edi, %edi
movq %r12, %rdx
callq 0x1c2ea
movl (%r12), %eax
incl %eax
cltd
idivl %ebp
movl %edx, %r15d
movl %edx, (%r12)
cmpl 0x8(%rsp), %r15d
jge 0xdaa3
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xda9d
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xd574
addl $0x2, %r12d
jmp 0xda7a
addl $0x2, %r15d
jmp 0xda6e
leaq 0x4(%rsp), %r15
leaq 0xc(%rsp), %r12
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1c2ea
movl (%r15), %eax
incl %eax
cltd
idivl %ebp
movl %edx, (%r15)
movl (%r12), %r15d
cmpl 0x8(%rsp), %r15d
jge 0xdaff
movl 0x4(%rsp), %r12d
cmpl %r13d, %r12d
jge 0xdaf9
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xd35d
addl $0x2, %r12d
jmp 0xdad5
addl $0x2, %r15d
jmp 0xdac9
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %r15
movl %ebp, %edi
movq %r15, %rdx
callq 0x1c2ea
movl (%r15), %eax
incl %eax
cltd
idivl %ebp
movl %edx, %ebp
movl %edx, (%r15)
cmpl 0x8(%rsp), %ebp
jge 0xdb54
movl 0x4(%rsp), %r15d
cmpl %r13d, %r15d
jge 0xdb4f
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0xd574
addl $0x2, %r15d
jmp 0xdb2b
addl $0x2, %ebp
jmp 0xdb20
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq