// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/main.cpp
// Function: testPGMFourierAnalysis()
void testPGMFourierAnalysis()
{	
	ImageReal firstImagePowerSpectrum;
	
	//PGM
	for (int imageIdx = 0; imageIdx < sInputFilenames.size(); imageIdx++)
	{
		//Open pgm
		ImageReal grayImg;
		grayImg.readFile(sInputFilenames[imageIdx]);
		int width = grayImg.width(), height = grayImg.height();
		int halfWidth = width / 2, halfHeight = height / 2;
		
		ImageReal fourierTransform;
		bool err = doFourierAnalysis(grayImg, fourierTransform);
		
		//Make an image of the power spectrum
		ImageReal powerSpectrum;
		generatePowerSpectrum(fourierTransform, powerSpectrum, GT_LOG10, true);
		
		powerSpectrum.setFilename(grayImg.filename());
		powerSpectrum.appendFilename("_ps");
		powerSpectrum.writeFile(1);
		
		bool shiftCenter = false;
		/*
		//Make a 1D histogram of the power spectrum, binned by frequency
		myAssert(!shiftCenter);
		generatePowerSpectrum(fourierTransform, powerSpectrum, GT_NONE, shiftCenter);
		if (imageIdx == 0)
			firstImagePowerSpectrum = powerSpectrum;
		
		int numBins = sqrt(halfWidth * halfWidth + halfHeight * halfHeight);
		cout << "Num bins: " << numBins << endl;
		int numBinsScalar = 4;
		numBins /= numBinsScalar;
		numBins++;
		cout << "Num bins scaled: " << numBins << endl;
		
		Float_t *powerSpectrumHistrogram = new Float_t[numBins];
		memset(powerSpectrumHistrogram, 0, numBins * sizeof(Float_t));
		
		int *binCounts = new int[numBins];
		memset(binCounts, 0, numBins * sizeof(int));
		
		//This code only works if the power spectrum has NOT been center-shifted.
		for (int y = 0; y < height; y++)
		{
			int yCenter = (y <= height / 2) ? 0 : height;
			Float_t yDistSqr = pow((Float_t)y - (Float_t)yCenter, 2);
			for (int x = 0; x <= halfWidth; x++)	//Step one column past the center (Nyquist)
			{
				if (y == 0 && x == 0)
					continue;	//Skip the DC term
				
				Float_t xDistSqr = pow((Float_t)x, 2);
				int dcTermDistance = roundToNearestInt(sqrt(yDistSqr + xDistSqr), 0, 0);
				dcTermDistance--;	//Since we skipped the DC term
				dcTermDistance /= numBinsScalar;
				myAssert(dcTermDistance >= 0 && dcTermDistance < numBins);
				
				powerSpectrumHistrogram[dcTermDistance] += powerSpectrum(x, y);
				binCounts[dcTermDistance]++;
			}
		}
		
		//Divide each bin by its number of contributing Fourier coefficients.
		//The result will be a histogram in which the value in each bin
		//represents the mean power at the frequency corresponding to that bin,
		//i.e., the mean power in a very thin annulus surrounding the DC term
		//in the power spectrum.
		int maxBinCount = 0;
		for (int i = 0; i < numBins; i++)
			if (binCounts[i] > maxBinCount)
				maxBinCount = binCounts[i];
		for (int i = 0; i < numBins; i++)
			powerSpectrumHistrogram[i] /= (Float_t)maxBinCount;
		
		string filename = sInputFilenames[imageIdx] + "_hist.txt";
		ofstream ofs;
		ofs.open(filename.c_str());
		myAssert(ofs);
		
		for (int i = 0; i < numBins; i++)
			ofs << i << '\t' << powerSpectrumHistrogram[i] << endl;
		
		ofs.close();
		
		delete [] powerSpectrumHistrogram;
		*/
		//Diff the power spectra
		shiftCenter = true;
		generatePowerSpectrum(fourierTransform, powerSpectrum, GT_NONE, shiftCenter);
		if (imageIdx == 0)
			firstImagePowerSpectrum = powerSpectrum;
		else
		{
			ImageReal powerSpectrumDiff;
			diffImages(firstImagePowerSpectrum, powerSpectrum, powerSpectrumDiff);
			powerSpectrumDiff.writeFile(1);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
leaq 0xc8(%rsp), %rdi
callq 0xcf0c
movq 0x2e0be(%rip), %rax     # 0x37540
cmpq 0x2e0af(%rip), %rax     # 0x37538
je 0x96d8
xorl %r14d, %r14d
leaq 0x108(%rsp), %rbx
leaq 0x188(%rsp), %r15
leaq 0x148(%rsp), %r12
xorl %r13d, %r13d
movq %rbx, %rbp
movq %rbx, %rdi
callq 0xcf0c
movq 0x2e079(%rip), %rax     # 0x37538
leaq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq (%rax,%r14), %rsi
movq 0x8(%rax,%r14), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xa92c
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xd5da
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x9509
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3310
movq %r15, %rdi
callq 0xcf0c
movq %rbp, %rbx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x195c0
movq %r12, %rdi
callq 0xcf0c
movq %r15, %rdi
movq %r12, %rsi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x18ece
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xa92c
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xce40
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x958c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x21508(%rip), %rsi     # 0x2aaaa
leaq 0x21504(%rip), %rdx     # 0x2aaad
callq 0xa9c2
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0xce5a
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x95d7
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xd9fc
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x9611
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3310
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x18ece
testq %r14, %r14
je 0x968e
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
callq 0xcf0c
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x18949
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xd9fc
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x9682
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x48(%rsp), %rdi
callq 0xd038
jmp 0x969e
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
callq 0xcf72
movq %r12, %rdi
callq 0xd038
movq %r15, %rdi
callq 0xd038
movq %rbx, %rdi
callq 0xd038
movq 0x2de83(%rip), %rax     # 0x37540
subq 0x2de74(%rip), %rax     # 0x37538
incq %r13
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r13, %rax
ja 0x94ad
leaq 0xc8(%rsp), %rdi
callq 0xd038
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9782
movq %rax, %rbx
jmp 0x97ac
jmp 0x972b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x979f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x979f
movq %rax, %rbx
jmp 0x979f
jmp 0x9732
movq %rax, %rbx
jmp 0x9792
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x9785
movq 0x18(%rsp), %rsi
jmp 0x9767
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x9785
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x9785
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xd038
jmp 0x9785
jmp 0x9782
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0xd038
leaq 0x188(%rsp), %rdi
callq 0xd038
leaq 0x108(%rsp), %rdi
callq 0xd038
leaq 0xc8(%rsp), %rdi
callq 0xd038
movq %rbx, %rdi
callq 0x3500