// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/DebayerProcs.cpp
// Function: altProj_repairGreen(ImageReal&, Image3ChnlReal&)
void altProj_repairGreen(ImageReal &grayImage, Image3ChnlReal &rgbImage)
{
	int width = grayImage.width(), height = grayImage.height();
	int rx, ry, g1x, g2y, bx, by;
	int redArrayPos = 0, greenArrayPos = 0, blueArrayPos = 0, secondGreenUnusedPos = 0;
	determineBlockColorOrder(rx, ry, g1x, g2y, bx, by, redArrayPos, greenArrayPos, blueArrayPos, secondGreenUnusedPos);
	
	//Generate downsampled images of red, blue, and two greens, one that overlaps red and one that overlaps blue
	ImageReal downsampledR(width, height), downsampledB(width, height);
	ImageReal downsampledGR(width, height), downsampledGB(width, height);
	
	grayImage.downsample(downsampledR, rx, ry);
	grayImage.downsample(downsampledB, bx, by);
	rgbImage[1]->downsample(downsampledGR, rx, ry);
	rgbImage[1]->downsample(downsampledGB, bx, by);
	
	int halfWidth = width / 2, halfHeight = height / 2;
	assert(downsampledR.width() == halfWidth && downsampledR.height() == halfHeight);
	
	//Perform a wavelet analysis of red, blue, and each of the interpolated greens that overlapped red and blue
	SteerablePyramid waveletR, waveletB, waveletGR, waveletGB;		int arrayOffset = 1;
	//Daubechies waveletR, waveletB, waveletGR, waveletGB;		int arrayOffset = 0;
	
	waveletR.DoFullWaveletAnalysis(downsampledR.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletB.DoFullWaveletAnalysis(downsampledB.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletGR.DoFullWaveletAnalysis(downsampledGR.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	waveletGB.DoFullWaveletAnalysis(downsampledGB.img(), halfWidth, halfHeight, true);
	cout << ".";	cout.flush();
	
	int numLevels = waveletGR.NumLevels();
	
	//Find the correlation between green and red and between green and blue on a per level basis
	vector<Float_t> greenRedCorrs, greenBlueCorrs;	//One correlation per level
	waveletGR.findCorrelations(waveletR, greenRedCorrs);
	waveletGB.findCorrelations(waveletB, greenBlueCorrs);
	/*
	cout << "Green/Red correlations: ";
	for (int i = 0; i < greenRedCorrs.size(); i++)
		cout << setw(15) << greenRedCorrs[i];
	cout << endl;
	cout << "Green/Blue correlations:";
	for (int i = 0; i < greenBlueCorrs.size(); i++)
		cout << setw(15) << greenBlueCorrs[i];
	cout << endl;
	*/
	vector<Float_t> means, stdDevs;
	vector<Float_t> thresholdsR(numLevels), thresholdsB(numLevels);
	
	waveletGR.CalcStats(means, stdDevs);
	for (int i = 0; i < numLevels; i++)
		if (i == 0)
			thresholdsR[i] = (((greenRedCorrs[i] + 1) / 2) * means[i + arrayOffset]);
		else thresholdsR[i] = -1;
	
	waveletGB.CalcStats(means, stdDevs);
	for (int i = 0; i < numLevels; i++)
		if (i == 0)
			thresholdsB[i] = (((greenBlueCorrs[i] + 1) / 2) * means[i + arrayOffset]);
		else thresholdsB[i] = -1;
	
	//for (int i = 0; i < numLevels; i++)
	//	cout << "Red/Blue thresholds: " << thresholdsR[i] << "    " << thresholdsB[i] << endl;
	
	//Replace the high frequency coefficients of green with those of red and blue
	waveletGR.CopyCoefficients(waveletR, thresholdsR);
	waveletGB.CopyCoefficients(waveletB, thresholdsB);
	
	//Reconstruct green.  Note that this will only affect the interpolated green pixels.
	waveletGR.DoFullWaveletSynthesis(downsampledGR.img());
	cout << ".";	cout.flush();
	waveletGB.DoFullWaveletSynthesis(downsampledGB.img());
	cout << ".";	cout.flush();
	
	downsampledGR.upsample(*rgbImage[1], rx, ry);
	downsampledGB.upsample(*rgbImage[1], bx, by);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x350, %rsp            # imm = 0x350
movq %rsi, %rbx
movq %rdi, %r15
movl 0x28(%rdi), %ebp
movl 0x2c(%rdi), %r14d
leaq 0x9c(%rsp), %rax
andl $0x0, (%rax)
leaq 0x98(%rsp), %r10
andl $0x0, (%r10)
leaq 0x94(%rsp), %r11
andl $0x0, (%r11)
leaq 0x90(%rsp), %r12
andl $0x0, (%r12)
leaq 0x18(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0xa4(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
pushq %r12
pushq %r11
pushq %r10
pushq %rax
callq 0x1c368
addq $0x20, %rsp
leaq 0x1e0(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa62e
leaq 0x270(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa62e
leaq 0x1a0(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa62e
leaq 0x160(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa62e
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1e0(%rsp), %rsi
movq %r15, %rdi
callq 0xaad0
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x270(%rsp), %rsi
movq %r15, %rdi
callq 0xaad0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1a0(%rsp), %rsi
callq 0xaad0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x160(%rsp), %rsi
callq 0xaad0
pushq $0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %ebp
movl %r14d, %eax
cltd
idivl %ecx
cmpl %ebp, 0x208(%rsp)
jne 0xe132
movl %eax, %r14d
cmpl %eax, 0x20c(%rsp)
jne 0xe132
leaq 0x300(%rsp), %rdi
callq 0x15294
leaq 0x2b0(%rsp), %rdi
callq 0x15294
leaq 0x110(%rsp), %rdi
callq 0x15294
leaq 0x220(%rsp), %rdi
callq 0x15294
movq 0x218(%rsp), %rsi
leaq 0x300(%rsp), %rdi
pushq $0x1
popq %r8
movl %ebp, %edx
movl %r14d, %ecx
callq 0x17960
movq 0x222b5(%rip), %rdi     # 0x2ffd0
leaq 0x154d3(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x222a2(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x2a8(%rsp), %rsi
leaq 0x2b0(%rsp), %rdi
pushq $0x1
popq %r8
movl %ebp, %edx
movl %r14d, %ecx
callq 0x17960
movq 0x22278(%rip), %rdi     # 0x2ffd0
leaq 0x15496(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x22265(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x1d8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
pushq $0x1
popq %r8
movl %ebp, %edx
movl %r14d, %ecx
callq 0x17960
movq 0x2223b(%rip), %rdi     # 0x2ffd0
leaq 0x15459(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x22228(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x198(%rsp), %rsi
leaq 0x220(%rsp), %rdi
pushq $0x1
popq %r8
movl %ebp, %edx
movl %r14d, %ecx
callq 0x17960
movq 0x221fe(%rip), %rdi     # 0x2ffd0
leaq 0x1541c(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x221eb(%rip), %rdi     # 0x2ffd0
callq 0x31d0
leaq 0x110(%rsp), %rdi
callq 0x16762
movl %eax, %r14d
xorpd %xmm0, %xmm0
leaq 0xc0(%rsp), %rdx
movapd %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x300(%rsp), %rsi
callq 0x18b3a
leaq 0x220(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x18b3a
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movslq %r14d, %r15
leaq 0xa8(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movq %r15, %rsi
callq 0xed70
leaq 0x78(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
movq %r15, %rsi
callq 0xed70
leaq 0x110(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1853a
movq 0xa8(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r14d
movq 0x20(%rsp), %rsi
movabsq $-0x4010000000000000, %r15 # imm = 0xBFF0000000000000
cmpq %rdx, %r14
je 0xdf01
testq %rdx, %rdx
je 0xdedf
movq %r15, (%rax,%rdx,8)
jmp 0xdefc
movsd (%rcx), %xmm0
addsd 0x1416d(%rip), %xmm0    # 0x22058
mulsd 0x14155(%rip), %xmm0    # 0x22048
mulsd 0x8(%rsi), %xmm0
movsd %xmm0, (%rax)
incq %rdx
jmp 0xdecf
leaq 0x220(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1853a
movq 0x78(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %r14
je 0xdf5b
testq %rsi, %rsi
je 0xdf39
movq %r15, (%rax,%rsi,8)
jmp 0xdf56
movsd (%rcx), %xmm0
addsd 0x14113(%rip), %xmm0    # 0x22058
mulsd 0x140fb(%rip), %xmm0    # 0x22048
mulsd 0x8(%rdx), %xmm0
movsd %xmm0, (%rax)
incq %rsi
jmp 0xdf29
leaq 0xf8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xedc2
leaq 0x110(%rsp), %rdi
leaq 0x300(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0x18f56
leaq 0xf8(%rsp), %rdi
callq 0x8a42
leaq 0xe0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xedc2
leaq 0x220(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x18f56
leaq 0xe0(%rsp), %rdi
callq 0x8a42
movq 0x1d8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x183d8
movq 0x21fde(%rip), %rdi     # 0x2ffd0
leaq 0x151fc(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x21fcb(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x198(%rsp), %rsi
leaq 0x220(%rsp), %rdi
callq 0x183d8
movq 0x21faa(%rip), %rdi     # 0x2ffd0
leaq 0x151c8(%rip), %rsi     # 0x231f5
callq 0x32c0
movq 0x21f97(%rip), %rdi     # 0x2ffd0
callq 0x31d0
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x18(%rsp), %edx
movl 0x14(%rsp), %ecx
leaq 0x1a0(%rsp), %rdi
callq 0xaba2
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x160(%rsp), %rdi
callq 0xaba2
leaq 0x78(%rsp), %rdi
callq 0x8a42
leaq 0xa8(%rsp), %rdi
callq 0x8a42
leaq 0x40(%rsp), %rdi
callq 0x8a42
leaq 0x20(%rsp), %rdi
callq 0x8a42
leaq 0x60(%rsp), %rdi
callq 0x8a42
leaq 0xc0(%rsp), %rdi
callq 0x8a42
leaq 0x220(%rsp), %rdi
callq 0x155fc
leaq 0x110(%rsp), %rdi
callq 0x155fc
leaq 0x2b0(%rsp), %rdi
callq 0x155fc
leaq 0x300(%rsp), %rdi
callq 0x155fc
leaq 0x160(%rsp), %rdi
callq 0xa6a6
leaq 0x1a0(%rsp), %rdi
callq 0xa6a6
leaq 0x270(%rsp), %rdi
callq 0xa6a6
leaq 0x1e0(%rsp), %rdi
callq 0xa6a6
addq $0x350, %rsp            # imm = 0x350
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x156e7(%rip), %rdi     # 0x23820
leaq 0x15360(%rip), %rsi     # 0x234a0
leaq 0x15722(%rip), %rcx     # 0x23869
movl $0x27a, %edx            # imm = 0x27A
callq 0x31a0
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
jmp 0xe169
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8a42
jmp 0xe1be
movq %rax, %rbx
jmp 0xe1c8
movq %rax, %rbx
jmp 0xe1d5
jmp 0xe202
movq %rax, %rbx
jmp 0xe212
movq %rax, %rbx
jmp 0xe21f
movq %rax, %rbx
jmp 0xe22c
jmp 0xe1b6
movq %rax, %rbx
jmp 0xe246
movq %rax, %rbx
jmp 0xe253
movq %rax, %rbx
jmp 0xe260
movq %rax, %rbx
jmp 0xe1e9
movq %rax, %rbx
jmp 0xe239
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x8a42
leaq 0xa8(%rsp), %rdi
callq 0x8a42
leaq 0x40(%rsp), %rdi
callq 0x8a42
leaq 0x20(%rsp), %rdi
callq 0x8a42
leaq 0x60(%rsp), %rdi
callq 0x8a42
leaq 0xc0(%rsp), %rdi
callq 0x8a42
jmp 0xe205
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0x155fc
leaq 0x110(%rsp), %rdi
callq 0x155fc
leaq 0x2b0(%rsp), %rdi
callq 0x155fc
leaq 0x300(%rsp), %rdi
callq 0x155fc
leaq 0x160(%rsp), %rdi
callq 0xa6a6
leaq 0x1a0(%rsp), %rdi
callq 0xa6a6
leaq 0x270(%rsp), %rdi
callq 0xa6a6
leaq 0x1e0(%rsp), %rdi
callq 0xa6a6
movq %rbx, %rdi
callq 0x34d0