// File: 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
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xedd0
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
callq 0xedf0
movl %eax, -0x18(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq %rsp, %rax
leaq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq -0x3c(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x34(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x1c(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
leaq -0x28(%rbp), %rcx
leaq -0x2c(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x382b0
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x80(%rbp), %rdi
callq 0x15710
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0xc0(%rbp), %rdi
callq 0x15710
jmp 0x1ba79
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x110(%rbp), %rdi
callq 0x15710
jmp 0x1ba8d
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x150(%rbp), %rdi
callq 0x15710
jmp 0x1baa1
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq -0x80(%rbp), %rsi
callq 0x15f70
jmp 0x1bab6
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq -0xc0(%rbp), %rsi
callq 0x15f70
jmp 0x1bace
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x380(%rbp)
jmp 0x1bae5
movq -0x380(%rbp), %rdi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq -0x110(%rbp), %rsi
callq 0x15f70
jmp 0x1bb00
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x388(%rbp)
jmp 0x1bb17
movq -0x388(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq -0x150(%rbp), %rsi
callq 0x15f70
jmp 0x1bb32
movl -0x14(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x154(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x158(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xedd0
movl %eax, -0x38c(%rbp)
jmp 0x1bb67
movl -0x38c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x154(%rbp), %ecx
movb %al, -0x38d(%rbp)
jne 0x1bba3
leaq -0x80(%rbp), %rdi
callq 0xedf0
movl %eax, -0x394(%rbp)
jmp 0x1bb8e
movl -0x394(%rbp), %eax
cmpl -0x158(%rbp), %eax
sete %al
movb %al, -0x38d(%rbp)
movb -0x38d(%rbp), %al
testb $0x1, %al
jne 0x1bbaf
jmp 0x1bbb1
jmp 0x1bc2c
leaq 0x2ed38(%rip), %rdi     # 0x4a8f0
leaq 0x2e9b1(%rip), %rsi     # 0x4a570
movl $0x27a, %edx            # imm = 0x27A
leaq 0x2ed6e(%rip), %rcx     # 0x4a939
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c4d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c4c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c4bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c4b1
leaq -0x1a8(%rbp), %rdi
callq 0x293b0
jmp 0x1bc3a
leaq -0x1f8(%rbp), %rdi
callq 0x293b0
jmp 0x1bc48
leaq -0x248(%rbp), %rdi
callq 0x293b0
jmp 0x1bc56
leaq -0x298(%rbp), %rdi
callq 0x293b0
jmp 0x1bc64
movl $0x1, -0x29c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x3a0(%rbp)
jmp 0x1bc80
movq -0x3a0(%rbp), %rsi
movl -0x154(%rbp), %edx
movl -0x158(%rbp), %ecx
leaq -0x1a8(%rbp), %rdi
movl $0x1, %r8d
callq 0x2d2c0
jmp 0x1bca7
movq 0x3e322(%rip), %rdi     # 0x59fd0
leaq 0x2e628(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1bcbc
movq 0x3e30d(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1bcca
leaq -0xc0(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x3a8(%rbp)
jmp 0x1bcdf
movq -0x3a8(%rbp), %rsi
movl -0x154(%rbp), %edx
movl -0x158(%rbp), %ecx
leaq -0x1f8(%rbp), %rdi
movl $0x1, %r8d
callq 0x2d2c0
jmp 0x1bd06
movq 0x3e2c3(%rip), %rdi     # 0x59fd0
leaq 0x2e5c9(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1bd1b
movq 0x3e2ae(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1bd29
leaq -0x110(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x3b0(%rbp)
jmp 0x1bd3e
movq -0x3b0(%rbp), %rsi
movl -0x154(%rbp), %edx
movl -0x158(%rbp), %ecx
leaq -0x248(%rbp), %rdi
movl $0x1, %r8d
callq 0x2d2c0
jmp 0x1bd65
movq 0x3e264(%rip), %rdi     # 0x59fd0
leaq 0x2e56a(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1bd7a
movq 0x3e24f(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1bd88
leaq -0x150(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x3b8(%rbp)
jmp 0x1bd9d
movq -0x3b8(%rbp), %rsi
movl -0x154(%rbp), %edx
movl -0x158(%rbp), %ecx
leaq -0x298(%rbp), %rdi
movl $0x1, %r8d
callq 0x2d2c0
jmp 0x1bdc4
movq 0x3e205(%rip), %rdi     # 0x59fd0
leaq 0x2e50b(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1bdd9
movq 0x3e1f0(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1bde7
leaq -0x248(%rbp), %rdi
callq 0x2bac0
movl %eax, -0x3bc(%rbp)
jmp 0x1bdfb
movl -0x3bc(%rbp), %eax
movl %eax, -0x2a0(%rbp)
leaq -0x2b8(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0xe240
leaq -0x2d0(%rbp), %rdi
callq 0xe240
movq -0x3c8(%rbp), %rdx
leaq -0x248(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x2f2e0
jmp 0x1be42
leaq -0x298(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
leaq -0x2d0(%rbp), %rdx
callq 0x2f2e0
jmp 0x1be5e
leaq -0x2e8(%rbp), %rdi
callq 0xe240
leaq -0x300(%rbp), %rdi
callq 0xe240
movslq -0x2a0(%rbp), %rax
movq %rax, -0x3d8(%rbp)
leaq -0x319(%rbp), %rdi
movq %rdi, -0x3d0(%rbp)
callq 0xf440
movq -0x3d8(%rbp), %rsi
movq -0x3d0(%rbp), %rdx
leaq -0x318(%rbp), %rdi
callq 0x1d820
jmp 0x1beb3
leaq -0x319(%rbp), %rdi
callq 0xff00
movslq -0x2a0(%rbp), %rax
movq %rax, -0x3e8(%rbp)
leaq -0x339(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0xf440
movq -0x3e8(%rbp), %rsi
movq -0x3e0(%rbp), %rdx
leaq -0x338(%rbp), %rdi
callq 0x1d820
jmp 0x1befc
leaq -0x339(%rbp), %rdi
callq 0xff00
leaq -0x248(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
leaq -0x300(%rbp), %rdx
callq 0x2e630
jmp 0x1bf24
movl $0x0, -0x340(%rbp)
movl -0x340(%rbp), %eax
cmpl -0x2a0(%rbp), %eax
jge 0x1c0d8
cmpl $0x0, -0x340(%rbp)
jne 0x1c0a3
movslq -0x340(%rbp), %rsi
leaq -0x2b8(%rbp), %rdi
callq 0xe670
movsd 0x2d0b8(%rip), %xmm0    # 0x49020
addsd (%rax), %xmm0
movsd 0x2e5d4(%rip), %xmm1    # 0x4a548
divsd %xmm1, %xmm0
movsd %xmm0, -0x3f8(%rbp)
movl -0x340(%rbp), %eax
addl -0x29c(%rbp), %eax
movslq %eax, %rsi
leaq -0x2e8(%rbp), %rdi
callq 0xe670
movsd -0x3f8(%rbp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, -0x3f0(%rbp)
movslq -0x340(%rbp), %rsi
leaq -0x318(%rbp), %rdi
callq 0xe670
movsd -0x3f0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x1c0c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c4a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c499
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c48d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c481
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c469
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x319(%rbp), %rdi
callq 0xff00
jmp 0x1c451
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x339(%rbp), %rdi
callq 0xff00
jmp 0x1c445
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x1c439
movslq -0x340(%rbp), %rsi
leaq -0x318(%rbp), %rdi
callq 0xe670
movsd 0x2cf7a(%rip), %xmm0    # 0x49038
movsd %xmm0, (%rax)
jmp 0x1c0c4
movl -0x340(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x340(%rbp)
jmp 0x1bf2e
leaq -0x298(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
leaq -0x300(%rbp), %rdx
callq 0x2e630
jmp 0x1c0f4
movl $0x0, -0x344(%rbp)
movl -0x344(%rbp), %eax
cmpl -0x2a0(%rbp), %eax
jge 0x1c1d5
cmpl $0x0, -0x344(%rbp)
jne 0x1c1a0
movslq -0x344(%rbp), %rsi
leaq -0x2d0(%rbp), %rdi
callq 0xe670
movsd 0x2cee8(%rip), %xmm0    # 0x49020
addsd (%rax), %xmm0
movsd 0x2e404(%rip), %xmm1    # 0x4a548
divsd %xmm1, %xmm0
movsd %xmm0, -0x408(%rbp)
movl -0x344(%rbp), %eax
addl -0x29c(%rbp), %eax
movslq %eax, %rsi
leaq -0x2e8(%rbp), %rdi
callq 0xe670
movsd -0x408(%rbp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, -0x400(%rbp)
movslq -0x344(%rbp), %rsi
leaq -0x338(%rbp), %rdi
callq 0xe670
movsd -0x400(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x1c1bf
movslq -0x344(%rbp), %rsi
leaq -0x338(%rbp), %rdi
callq 0xe670
movsd 0x2ce7d(%rip), %xmm0    # 0x49038
movsd %xmm0, (%rax)
jmp 0x1c1c1
movl -0x344(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x1c0fe
leaq -0x360(%rbp), %rdi
leaq -0x318(%rbp), %rsi
callq 0x1d890
jmp 0x1c1ea
leaq -0x248(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
leaq -0x360(%rbp), %rdx
callq 0x2fa20
jmp 0x1c206
leaq -0x360(%rbp), %rdi
callq 0xe260
leaq -0x378(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x1d890
jmp 0x1c227
leaq -0x298(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
leaq -0x378(%rbp), %rdx
callq 0x2fa20
jmp 0x1c243
leaq -0x378(%rbp), %rdi
callq 0xe260
leaq -0x110(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x410(%rbp)
jmp 0x1c264
movq -0x410(%rbp), %rsi
leaq -0x248(%rbp), %rdi
callq 0x2e250
jmp 0x1c279
movq 0x3dd50(%rip), %rdi     # 0x59fd0
leaq 0x2e056(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1c28e
movq 0x3dd3b(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1c29c
leaq -0x150(%rbp), %rdi
callq 0xf2c0
movq %rax, -0x418(%rbp)
jmp 0x1c2b1
movq -0x418(%rbp), %rsi
leaq -0x298(%rbp), %rdi
callq 0x2e250
jmp 0x1c2c6
movq 0x3dd03(%rip), %rdi     # 0x59fd0
leaq 0x2e009(%rip), %rsi     # 0x4a2dd
callq 0x4390
jmp 0x1c2db
movq 0x3dcee(%rip), %rdi     # 0x59fd0
callq 0x4250
jmp 0x1c2e9
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x420(%rbp)
jmp 0x1c300
movq -0x420(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq -0x110(%rbp), %rdi
callq 0x160c0
jmp 0x1c31b
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq %rax, -0x428(%rbp)
jmp 0x1c332
movq -0x428(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq -0x150(%rbp), %rdi
callq 0x160c0
jmp 0x1c34d
leaq -0x338(%rbp), %rdi
callq 0xe260
leaq -0x318(%rbp), %rdi
callq 0xe260
leaq -0x300(%rbp), %rdi
callq 0xe260
leaq -0x2e8(%rbp), %rdi
callq 0xe260
leaq -0x2d0(%rbp), %rdi
callq 0xe260
leaq -0x2b8(%rbp), %rdi
callq 0xe260
leaq -0x298(%rbp), %rdi
callq 0x299e0
leaq -0x248(%rbp), %rdi
callq 0x299e0
leaq -0x1f8(%rbp), %rdi
callq 0x299e0
leaq -0x1a8(%rbp), %rdi
callq 0x299e0
leaq -0x150(%rbp), %rdi
callq 0x157e0
leaq -0x110(%rbp), %rdi
callq 0x157e0
leaq -0xc0(%rbp), %rdi
callq 0x157e0
leaq -0x80(%rbp), %rdi
callq 0x157e0
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x360(%rbp), %rdi
callq 0xe260
jmp 0x1c439
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x378(%rbp), %rdi
callq 0xe260
leaq -0x338(%rbp), %rdi
callq 0xe260
leaq -0x318(%rbp), %rdi
callq 0xe260
leaq -0x300(%rbp), %rdi
callq 0xe260
leaq -0x2e8(%rbp), %rdi
callq 0xe260
leaq -0x2d0(%rbp), %rdi
callq 0xe260
leaq -0x2b8(%rbp), %rdi
callq 0xe260
leaq -0x298(%rbp), %rdi
callq 0x299e0
leaq -0x248(%rbp), %rdi
callq 0x299e0
leaq -0x1f8(%rbp), %rdi
callq 0x299e0
leaq -0x1a8(%rbp), %rdi
callq 0x299e0
leaq -0x150(%rbp), %rdi
callq 0x157e0
leaq -0x110(%rbp), %rdi
callq 0x157e0
leaq -0xc0(%rbp), %rdi
callq 0x157e0
leaq -0x80(%rbp), %rdi
callq 0x157e0
movq -0xc8(%rbp), %rdi
callq 0x4620
nopw (%rax,%rax)