// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xedd0
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
callq 0xedf0
movl %eax, -0x20(%rbp)
xorl %esi, %esi
movl %esi, %edi
callq 0x380a0
movl %eax, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b4c0
movl -0x24(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1b451
movl -0x14(%rbp), %edx
movl %edx, -0x74(%rbp)
jmp 0x1b462
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b4b0
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x24(%rbp), %edx
callq 0x16fd0
movsd (%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
movl $0x1, %esi
callq 0x17990
movsd -0x80(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x1b468
jmp 0x1b4b2
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1b42d
xorl %edi, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b53b
movl -0x14(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b52e
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x16fd0
movsd (%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
xorl %esi, %esi
callq 0x17990
movsd -0x88(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x30(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x30(%rbp)
jmp 0x1b4e3
jmp 0x1b530
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1b4d5
movl $0x2, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b5bc
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b5af
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0x16fd0
movsd (%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x2, %esi
callq 0x17990
movsd -0x90(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x38(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
jmp 0x1b561
jmp 0x1b5b1
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
jmp 0x1b553
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, -0x14(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b651
movl -0x3c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1b601
movl -0x14(%rbp), %edx
movl %edx, -0x94(%rbp)
jmp 0x1b615
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b644
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x40(%rbp), %edx
movl -0x3c(%rbp), %ecx
callq 0x1a850
movl -0x40(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x40(%rbp)
jmp 0x1b61e
jmp 0x1b646
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1b5de
movl $0x2, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b6d5
movl -0x14(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b6c8
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
movl -0x48(%rbp), %ecx
movl -0x44(%rbp), %r8d
callq 0x1ab30
movl -0x48(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x48(%rbp)
jmp 0x1b677
jmp 0x1b6ca
movl -0x44(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x44(%rbp)
jmp 0x1b669
xorl %edi, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b759
movl -0x14(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b74c
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq %rax, %rdx
movl -0x50(%rbp), %ecx
movl -0x4c(%rbp), %r8d
callq 0x1ab30
movl -0x50(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x50(%rbp)
jmp 0x1b6f8
jmp 0x1b74e
movl -0x4c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b6ea
xorl %edi, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x14(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b7ee
movl -0x14(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b7e1
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq %rax, %rdx
movl -0x58(%rbp), %ecx
movl -0x54(%rbp), %r8d
callq 0x1ae80
movl -0x58(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x58(%rbp)
jmp 0x1b790
jmp 0x1b7e3
movl -0x54(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b782
xorl %edi, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b883
movl -0x14(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b876
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xef40
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq %rax, %rdx
movl -0x60(%rbp), %ecx
movl -0x5c(%rbp), %r8d
callq 0x1b130
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x60(%rbp)
jmp 0x1b825
jmp 0x1b878
movl -0x5c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1b817
movl $0x2, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x14(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b91e
movl -0x14(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b911
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq %rax, %rdx
movl -0x68(%rbp), %ecx
movl -0x64(%rbp), %r8d
callq 0x1ae80
movl -0x68(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x68(%rbp)
jmp 0x1b8bd
jmp 0x1b913
movl -0x64(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x64(%rbp)
jmp 0x1b8af
movl $0x2, %edi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x38180
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1b9b9
movl -0x14(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b9ac
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq %rax, %rdx
movl -0x70(%rbp), %ecx
movl -0x6c(%rbp), %r8d
callq 0x1b130
movl -0x70(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x70(%rbp)
jmp 0x1b958
jmp 0x1b9ae
movl -0x6c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1b94a
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)