// File: /workspace/github_cpp_makefile_star2_O1/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 0x23404
cmpl $0x1, %eax
je 0xffbe
testl %r14d, %r14d
sete %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0xffa3
movl 0x28(%r12), %eax
leal -0x1(%rax), %ecx
cmpl %r14d, %ecx
je 0xffa3
movl 0x2c(%r12), %ecx
decl %ecx
cmpl %ebx, %ecx
je 0xffa3
movq 0x38(%r12), %rcx
movl %eax, %edx
imull %ebx, %edx
leal (%r14,%rdx), %esi
decl %esi
movsd (%rcx,%rsi,8), %xmm0
leal 0x1(%r14,%rdx), %edx
movsd (%rcx,%rdx,8), %xmm2
movapd %xmm0, %xmm4
subsd %xmm2, %xmm4
movapd 0x1c5b6(%rip), %xmm3    # 0x2c490
movapd %xmm4, %xmm1
xorpd %xmm3, %xmm1
maxsd %xmm4, %xmm1
leal -0x1(%rbx), %edx
imull %eax, %edx
addl %r14d, %edx
movsd (%rcx,%rdx,8), %xmm4
leal 0x1(%rbx), %edx
imull %eax, %edx
addl %r14d, %edx
movsd (%rcx,%rdx,8), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
xorpd %xmm6, %xmm3
maxsd %xmm6, %xmm3
ucomisd %xmm1, %xmm3
jbe 0xff2a
movsd 0x1c580(%rip), %xmm6    # 0x2c4a0
mulsd %xmm3, %xmm6
ucomisd %xmm6, %xmm1
ja 0xffa3
ucomisd %xmm3, %xmm1
jbe 0xff42
movsd 0x1c568(%rip), %xmm6    # 0x2c4a0
mulsd %xmm1, %xmm6
ucomisd %xmm6, %xmm3
ja 0xffa3
ucomisd %xmm3, %xmm1
je 0xffa3
ucomisd %xmm1, %xmm3
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm5, %xmm1
orpd %xmm2, %xmm1
ja 0xff63
movapd %xmm4, %xmm0
xorpd %xmm2, %xmm2
addsd %xmm0, %xmm2
addsd %xmm1, %xmm2
mulsd 0x1b0d1(%rip), %xmm2    # 0x2b048
movsd %xmm2, (%rsp)
movq %r15, %rdi
movl $0x1, %esi
movl %r14d, %edx
movl %ebx, %ecx
callq 0xe55c
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 0xfd34
leaq 0x1c5bd(%rip), %rdi     # 0x2c582
leaq 0x1c4fc(%rip), %rsi     # 0x2c4c8
leaq 0x1c616(%rip), %rcx     # 0x2c5e9
movl $0x179, %edx            # imm = 0x179
callq 0x31c0