// File: /workspace/github_cpp_makefile_star2_O3/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 0x23198
cmpl $0x1, %eax
je 0xff19
testl %r14d, %r14d
sete %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0xfefe
movl 0x28(%r12), %eax
leal -0x1(%rax), %ecx
cmpl %r14d, %ecx
je 0xfefe
movl 0x2c(%r12), %ecx
decl %ecx
cmpl %ebx, %ecx
je 0xfefe
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 0x1b623(%rip), %xmm3    # 0x2b480
xorpd %xmm5, %xmm3
maxpd %xmm5, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
ucomisd %xmm3, %xmm5
jbe 0xfe85
movsd 0x1b5f5(%rip), %xmm6    # 0x2b470
mulsd %xmm5, %xmm6
ucomisd %xmm6, %xmm3
ja 0xfefe
ucomisd %xmm5, %xmm3
jbe 0xfe9d
movsd 0x1b5dd(%rip), %xmm6    # 0x2b470
mulsd %xmm3, %xmm6
ucomisd %xmm6, %xmm5
ja 0xfefe
ucomisd %xmm5, %xmm3
je 0xfefe
ucomisd %xmm3, %xmm5
cmpltsd %xmm5, %xmm3
andpd %xmm3, %xmm1
andnpd %xmm4, %xmm3
orpd %xmm1, %xmm3
ja 0xfebe
movapd %xmm2, %xmm0
xorpd %xmm1, %xmm1
addsd %xmm0, %xmm1
addsd %xmm3, %xmm1
mulsd 0x1a176(%rip), %xmm1    # 0x2a048
movsd %xmm1, (%rsp)
movq %r15, %rdi
movl $0x1, %esi
movl %r14d, %edx
movl %ebx, %ecx
callq 0xe49a
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 0xfc8d
leaq 0x1b64a(%rip), %rdi     # 0x2b56a
leaq 0x1b589(%rip), %rsi     # 0x2b4b0
leaq 0x1b6a3(%rip), %rcx     # 0x2b5d1
movl $0x179, %edx            # imm = 0x179
callq 0x31c0