// File: kebwi[P]Trifid/src/ImageProcs.cpp
// Function: calcNonoutlierMeanPixelValue(Image3ChnlReal&, double)
Float_t calcNonoutlierMeanPixelValue(Image3ChnlReal &rgbImage, Float_t outlierSigmas)
{
	int totalPixels = rgbImage.totalPixels();
	
	//Find the mean pixel value
	Float_t meanVal = 0;
	for (int c = 0; c < 3; c++)
		for (int i = 0; i < totalPixels; i++)
			meanVal += (*rgbImage[c])[i];
	meanVal /= (totalPixels * 3);
	
	//Find the variance of pixel value
	Float_t variance = 0;
	for (int c = 0; c < 3; c++)
		for (int i = 0; i < totalPixels; i++)
			variance += pow((*rgbImage[c])[i] - meanVal, 2);
	variance /= (totalPixels * 3);
	
	//Find the standard deviation of pixel value
	Float_t stddev = sqrt(variance);
	
	//Define outliers as some number of standard deviations
	Float_t minOutlier = meanVal - stddev * outlierSigmas;
	Float_t maxOutlier = meanVal + stddev * outlierSigmas;
	
	//Find the mean pixel value of nonoutliers
	Float_t nonOutlierMeanVal = 0;
	int numNonOutliers = 0;
	for (int c = 0; c < 3; c++)
		for (int i = 0; i < totalPixels; i++)
			if ((*rgbImage[c])[i] >= minOutlier && (*rgbImage[c])[i] <= maxOutlier)
			{
				nonOutlierMeanVal += (*rgbImage[c])[i];
				numNonOutliers++;
			}
	nonOutlierMeanVal /= numNonOutliers;
	
	return nonOutlierMeanVal;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xf200
movl %eax, -0x14(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x3, -0x24(%rbp)
jge 0x1fd3e
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fd31
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xef40
movq %rax, %rdi
movl -0x28(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1fcf9
jmp 0x1fd33
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1fcec
imull $0x3, -0x14(%rbp), %eax
cvtsi2sd %eax, %xmm1
movsd -0x20(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x1fdc4
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fdb7
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0xef40
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
subsd -0x20(%rbp), %xmm0
movl $0x2, %edi
callq 0x258f0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1fd70
jmp 0x1fdb9
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1fd63
imull $0x3, -0x14(%rbp), %eax
cvtsi2sd %eax, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq 0x42c0
movsd %xmm0, -0x40(%rbp)
movsd -0x20(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
movsd -0x10(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x20(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
movsd -0x10(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
cmpl $0x3, -0x60(%rbp)
jge 0x1ff01
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fef1
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0xef40
movq %rax, %rdi
movl -0x64(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
ucomisd -0x48(%rbp), %xmm0
jb 0x1fee1
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0xef40
movq %rax, %rdi
movl -0x64(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm1
movsd -0x50(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x1fee1
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0xef40
movq %rax, %rdi
movl -0x64(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1fee3
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1fe5f
jmp 0x1fef3
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1fe4e
cvtsi2sdl -0x5c(%rbp), %xmm1
movsd -0x58(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
addq $0x70, %rsp
popq %rbp
retq
nop