// File: /workspace/github_cpp_makefile_star2_O2/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 0xa8(%rsp), %rdi
callq 0xa588
xorl %r12d, %r12d
leaq 0x188(%rsp), %rbp
pushq $0x1
popq %rbx
xorl %r14d, %r14d
movq 0x28cb5(%rip), %rax     # 0x30530
subq 0x28ca6(%rip), %rax     # 0x30528
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x79ef
movl %ebx, %r15d
leaq 0x28(%rsp), %rdi
callq 0xa588
movq 0x28c85(%rip), %rsi     # 0x30528
addq %r12, %rsi
leaq 0x108(%rsp), %rbx
movq %rbx, %rdi
callq 0x33b0
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0xac74
movq %rbx, %rdi
callq 0x3548
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0xa588
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x141c3
movl %r15d, %ebx
movq %rbp, %rdi
callq 0xa588
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
pushq $0x3
popq %rdx
movl %ebx, %ecx
callq 0x13b3a
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x33b0
movq %rbp, %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xa4ee
movq %r15, %rdi
callq 0x3548
movq %rbp, %rdi
leaq 0x1b163(%rip), %rsi     # 0x22a96
callq 0x8686
leaq 0xe8(%rsp), %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0xb028
leaq 0xe8(%rsp), %rdi
callq 0x3548
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movl %ebx, %ecx
callq 0x13b3a
testq %r12, %r12
je 0x79b7
leaq 0x148(%rsp), %r15
movq %r15, %rdi
callq 0xa588
leaq 0xa8(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x136ef
leaq 0x128(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0xb028
movq %r13, %rdi
callq 0x3548
movq %r15, %rdi
callq 0xa6a6
jmp 0x79c7
leaq 0xa8(%rsp), %rdi
movq %rbp, %rsi
callq 0xa5e6
movq %rbp, %rdi
callq 0xa6a6
leaq 0x68(%rsp), %rdi
callq 0xa6a6
leaq 0x28(%rsp), %rdi
callq 0xa6a6
incq %r14
addq $0x20, %r12
jmp 0x7874
leaq 0xa8(%rsp), %rdi
callq 0xa6a6
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7a58
jmp 0x7a12
movq %rax, %rbx
jmp 0x7a72
movq %rax, %rbx
jmp 0x7a7c
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x3548
jmp 0x7a72
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3548
jmp 0x7a5b
jmp 0x7a3f
movq %rax, %rbx
jmp 0x7a68
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0xa6a6
jmp 0x7a5b
jmp 0x7a58
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0xa6a6
leaq 0x68(%rsp), %rdi
callq 0xa6a6
leaq 0x28(%rsp), %rdi
callq 0xa6a6
leaq 0xa8(%rsp), %rdi
callq 0xa6a6
movq %rbx, %rdi
callq 0x34d0