// File: /workspace/github_cpp_makefile_star2_O1/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 0xcfd0
movq 0x2e06d(%rip), %rax     # 0x37540
cmpq 0x2e05e(%rip), %rax     # 0x37538
je 0x9729
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 0xcfd0
movq 0x2e028(%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 0xa97e
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xd69c
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x955a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3310
movq %r15, %rdi
callq 0xcfd0
movq %rbp, %rbx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x19a5c
movq %r12, %rdi
callq 0xcfd0
movq %r15, %rdi
movq %r12, %rsi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x192f1
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xa97e
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xcf04
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x95dd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x224d9(%rip), %rsi     # 0x2bacc
leaq 0x224d5(%rip), %rdx     # 0x2bacf
callq 0xaa0e
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0xcf1e
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x9628
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xdabe
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x9662
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3310
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x192f1
testq %r14, %r14
je 0x96df
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
callq 0xcfd0
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x18e70
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xdabe
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x96d3
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x48(%rsp), %rdi
callq 0xd0fc
jmp 0x96ef
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
callq 0xd036
movq %r12, %rdi
callq 0xd0fc
movq %r15, %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0xd0fc
movq 0x2de32(%rip), %rax     # 0x37540
subq 0x2de23(%rip), %rax     # 0x37538
incq %r13
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r13, %rax
ja 0x94fe
leaq 0xc8(%rsp), %rdi
callq 0xd0fc
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x97d3
movq %rax, %rbx
jmp 0x97fd
jmp 0x977c
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x97f0
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x97f0
movq %rax, %rbx
jmp 0x97f0
jmp 0x9783
movq %rax, %rbx
jmp 0x97e3
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x97d6
movq 0x18(%rsp), %rsi
jmp 0x97b8
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x97d6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
jmp 0x97d6
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xd0fc
jmp 0x97d6
jmp 0x97d3
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0xd0fc
leaq 0x188(%rsp), %rdi
callq 0xd0fc
leaq 0x108(%rsp), %rdi
callq 0xd0fc
leaq 0xc8(%rsp), %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0x3500