// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: hue_constructGreenChannelEdgeSensitive(ImageReal&, Image3ChnlReal&, int, int)
void hue_constructGreenChannelEdgeSensitive(
	ImageReal &grayImage, Image3ChnlReal &rgbImage,
	int x, int y)
{
	assert(determineBayerColor(x, y) != G);
	
	//Use bilinear interpolation along the edges of the image
	if (x == 0 || y == 0 || x == grayImage.width() - 1 || y == grayImage.height() - 1)
		return hue_constructGreenChannel(grayImage, rgbImage, x, y);
	
	//Use only two of the surrounding four green pixels to interpolate,
	//either the two horizontal neighbors or the two vertical neighbors,
	//depending on which pair preserves the edge best, i.e., interpolate
	//along the edge instead of across it.  If there is no edge here,
	//use all four pixels, i.e., bilinear interpolation.
	
	Float_t horDiff = grayImage(x - 1, y) - grayImage(x + 1, y);
	if (horDiff < 0)
		horDiff = -horDiff;
	Float_t verDiff = grayImage(x, y - 1) - grayImage(x, y + 1);
	if (verDiff < 0)
		verDiff = -verDiff;
	
	//If the horizontal and vertical differences are similar, then assume no edge and bilinear interpolate.
	//For unnoisy lena128, edgeRatioThreshold=.25 yields the best PSNR and edgeRatioThreshold=.75 yields
	//the best lab delta error.  Nonedge hue yields better PSNR than .75, but worst lab delta error than
	//either .25 or .75.  Overall, I like the appearance .75 best. Not sure if this will hold for other
	//images or for noisy bayered images.
	Float_t edgeRatioThreshold = .75;
	if ((horDiff < verDiff && horDiff > verDiff * edgeRatioThreshold) ||
		(verDiff < horDiff && verDiff > horDiff * edgeRatioThreshold))
		return hue_constructGreenChannel(grayImage, rgbImage, x, y);
	
	Float_t colorL = 0;
	if (horDiff < verDiff)	//The edge is vertical, so interpolate horizontally
	{
		colorL += grayImage(x - 1, y);
		colorL += grayImage(x + 1, y);
	}
	else if (verDiff < horDiff)	//The edge is horizontal, so interpolate vertically
	{
		colorL += grayImage(x, y - 1);
		colorL += grayImage(x, y + 1);
	}
	else return hue_constructGreenChannel(grayImage, rgbImage, x, y);
	
	rgbImage(G, x, y) = colorL / 2.0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebx
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %r12
movl %edx, %edi
movl %ecx, %esi
callq 0x1c294
cmpl $0x1, %eax
je 0xd0a7
testl %r14d, %r14d
sete %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0xd08c
movl 0x28(%r12), %eax
leal -0x1(%rax), %ecx
cmpl %r14d, %ecx
je 0xd08c
movl 0x2c(%r12), %ecx
decl %ecx
cmpl %ebx, %ecx
je 0xd08c
movq 0x38(%r12), %rcx
movl %eax, %edx
imull %ebx, %edx
leal (%r14,%rdx), %esi
decl %esi
movsd (%rcx,%rsi,8), %xmm0
addl %r14d, %edx
incl %edx
movsd (%rcx,%rdx,8), %xmm1
leal -0x1(%rbx), %edx
imull %eax, %edx
addl %r14d, %edx
movsd (%rcx,%rdx,8), %xmm2
leal 0x1(%rbx), %edx
imull %eax, %edx
addl %r14d, %edx
movsd (%rcx,%rdx,8), %xmm4
movapd %xmm0, %xmm5
unpcklpd %xmm2, %xmm5            # xmm5 = xmm5[0],xmm2[0]
movapd %xmm1, %xmm3
unpcklpd %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0]
subpd %xmm3, %xmm5
movapd 0x16483(%rip), %xmm3    # 0x23470
xorpd %xmm5, %xmm3
maxpd %xmm5, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
ucomisd %xmm3, %xmm5
jbe 0xd015
movsd 0x16455(%rip), %xmm6    # 0x23460
mulsd %xmm5, %xmm6
ucomisd %xmm6, %xmm3
ja 0xd08c
ucomisd %xmm5, %xmm3
jbe 0xd02d
movsd 0x1643d(%rip), %xmm6    # 0x23460
mulsd %xmm3, %xmm6
ucomisd %xmm6, %xmm5
ja 0xd08c
ucomisd %xmm5, %xmm3
je 0xd08c
ucomisd %xmm3, %xmm5
cmpltsd %xmm5, %xmm3
andpd %xmm3, %xmm1
andnpd %xmm4, %xmm3
orpd %xmm1, %xmm3
ja 0xd04e
movapd %xmm2, %xmm0
xorpd %xmm1, %xmm1
addsd %xmm0, %xmm1
addsd %xmm3, %xmm1
mulsd 0x14fe6(%rip), %xmm1    # 0x22048
movsd %xmm1, (%rsp)
pushq $0x1
popq %rsi
movq %r15, %rdi
movl %r14d, %edx
movl %ebx, %ecx
callq 0xb868
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %r12, %rdi
movq %r15, %rsi
movl %r14d, %edx
movl %ebx, %ecx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xce21
leaq 0x164ac(%rip), %rdi     # 0x2355a
leaq 0x163eb(%rip), %rsi     # 0x234a0
leaq 0x16505(%rip), %rcx     # 0x235c1
movl $0x179, %edx            # imm = 0x179
callq 0x31a0