// File: kebwi[P]Trifid/src/ImageProcs.cpp
// Function: calcNonoutlierMeanPixelValue(ImageReal&, double)
Float_t calcNonoutlierMeanPixelValue(ImageReal &grayImage, Float_t outlierSigmas)
{
	int totalPixels = grayImage.totalPixels();
	
	//Find the mean pixel value
	Float_t meanVal = 0;
	for (int i = 0; i < totalPixels; i++)
		meanVal += grayImage[i];
	meanVal /= totalPixels;
	
	//Find the variance of pixel value
	Float_t variance = 0;
	for (int i = 0; i < totalPixels; i++)
		variance += pow(grayImage[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 i = 0; i < totalPixels; i++)
			if (grayImage[i] >= minOutlier && grayImage[i] <= maxOutlier)
			{
				nonOutlierMeanVal += grayImage[i];
				numNonOutliers++;
			}
	nonOutlierMeanVal /= numNonOutliers;
	
	return nonOutlierMeanVal;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %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)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fb49
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1fb1c
cvtsi2sdl -0x14(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fba7
movq -0x8(%rbp), %rdi
movl -0x34(%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 -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1fb6b
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)
movl -0x60(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1fc9b
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
ucomisd -0x48(%rbp), %xmm0
jb 0x1fc8e
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm1
movsd -0x50(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x1fc8e
movq -0x8(%rbp), %rdi
movl -0x60(%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 0x1fc90
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1fc31
cvtsi2sdl -0x5c(%rbp), %xmm1
movsd -0x58(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)