// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %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 0x23198
xorl %ecx, %ecx
cmpl $0x1, %eax
setne %cl
movl %ecx, 0x14(%rsp)
movl %ecx, (%rsp)
movl %r15d, 0x10(%rsp)
testl %r15d, %r15d
jle 0x106bf
xorl %r12d, %r12d
cmpl $0x1, %eax
sete %r12b
testb $0x1, %bpl
movl %r12d, %r15d
cmovel 0x14(%rsp), %r15d
cmpl %r13d, %r15d
jge 0x106b7
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %edx
movl %ebp, %ecx
callq 0xe49a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r15d
jmp 0x10677
incl %ebp
cmpl 0x10(%rsp), %ebp
jne 0x1066a
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
xorl %edi, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %ebp
movl 0x10(%rsp), %r12d
cmpl %r12d, %ebp
jge 0x10724
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x1071f
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movl %r15d, %edx
movl %ebp, %ecx
callq 0xe49a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r15d
jmp 0x106e2
addl $0x2, %ebp
jmp 0x106d9
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
movl $0x2, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %ebp
cmpl %r12d, %ebp
jge 0x1078a
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x10785
movq 0x38(%r14), %rax
movl 0x28(%r14), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
movl %r15d, %edx
movl %ebp, %ecx
callq 0xe49a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
addl $0x2, %r15d
jmp 0x10745
addl $0x2, %ebp
jmp 0x1073c
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x23198
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
movl %ecx, (%rsp)
testl %r12d, %r12d
jle 0x107e8
movl (%rsp), %r15d
testb $0x1, %bpl
je 0x107dc
movl %r15d, %eax
incl %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %r15d, %ecx
incl %ecx
andl $-0x2, %ecx
subl %ecx, %eax
movl %eax, %r15d
jmp 0x107dc
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %ebp, %ecx
callq 0xfdae
addl $0x2, %r15d
cmpl %r13d, %r15d
jl 0x107c8
incl %ebp
cmpl %r12d, %ebp
jne 0x107a5
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
movl $0x2, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %ebp
cmpl %r12d, %ebp
jge 0x10831
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x1082c
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0xff38
addl $0x2, %r15d
jmp 0x10809
addl $0x2, %ebp
jmp 0x10800
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
xorl %edi, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %ebp
cmpl %r12d, %ebp
jge 0x10878
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x10873
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0xff38
addl $0x2, %r15d
jmp 0x1084f
addl $0x2, %ebp
jmp 0x10846
movq %rsp, %r15
leaq 0x4(%rsp), %rbp
xorl %edi, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x231fe
movl (%r15), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
incl %ecx
andl $-0x2, %ecx
negl %ecx
addl %ecx, %eax
incl %eax
movl %eax, (%r15)
movl (%rbp), %ebp
cmpl %r12d, %ebp
jge 0x108db
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x108d6
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0x101cf
addl $0x2, %r15d
jmp 0x108b3
addl $0x2, %ebp
jmp 0x108aa
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
xorl %edi, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
incl %ecx
andl $-0x2, %ecx
negl %ecx
leal (%rax,%rcx), %ebp
incl %ebp
movl %ebp, (%r15)
cmpl %r12d, %ebp
jge 0x10939
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x10934
movq 0x38(%rbx), %rax
movq (%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0x1040d
addl $0x2, %r15d
jmp 0x10911
addl $0x2, %ebp
jmp 0x10908
movq %rsp, %r15
leaq 0x4(%rsp), %rbp
movl $0x2, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x231fe
movl (%r15), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
incl %ecx
andl $-0x2, %ecx
negl %ecx
addl %ecx, %eax
incl %eax
movl %eax, (%r15)
movl (%rbp), %ebp
cmpl %r12d, %ebp
jge 0x109a0
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x1099b
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0x101cf
addl $0x2, %r15d
jmp 0x10977
addl $0x2, %ebp
jmp 0x1096e
movq %rsp, %rsi
leaq 0x4(%rsp), %r15
movl $0x2, %edi
movq %r15, %rdx
callq 0x231fe
movl (%r15), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
incl %ecx
andl $-0x2, %ecx
negl %ecx
leal (%rax,%rcx), %ebp
incl %ebp
movl %ebp, (%r15)
cmpl %r12d, %ebp
jge 0x10a02
movl (%rsp), %r15d
cmpl %r13d, %r15d
jge 0x109fd
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl %ebp, %r8d
callq 0x1040d
addl $0x2, %r15d
jmp 0x109d9
addl $0x2, %ebp
jmp 0x109d0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq