// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/SteerablePyramid.cpp
// Function: SteerablePyramid::GenerateHistograms(int, char*)
void SteerablePyramid::GenerateHistograms(int numBins, char* filename)
{
	vector<Float_t> maxCoeffs, maxThumbnailPixels;
	FindStrongestCoefficients(maxCoeffs, maxThumbnailPixels);
	Float_t maxCoeff = 0;
	for (int i = 0; i < maxCoeffs.size(); i++)
		if (maxCoeffs[i] > maxCoeff)
			maxCoeff = maxCoeffs[i];
	Float_t maxThumbnailPixel = 0;
	for (int i = 0; i < maxThumbnailPixels.size(); i++)
		if (maxThumbnailPixels[i] > maxThumbnailPixel)
			maxThumbnailPixel = maxThumbnailPixels[i];
	
	Float_t binSpan = maxCoeff / numBins;
	
	//Generate coefficient histograms
	vector<vector<int> > coeffHistograms(numLevels_);
	for (int i = 0; i < numLevels_; i++)
	{
		coeffHistograms[i].resize(numBins);
		
		int horDim = width_ / pow(2, i);
		int verDim = height_ / pow(2, i);
		
		if (i == 0)
			AccumulateHistograms(h0Coeffs_, horDim, verDim, coeffHistograms[i], binSpan);
		AccumulateHistograms(b1Coeffs_[i], horDim, verDim, coeffHistograms[i], binSpan);
		AccumulateHistograms(b2Coeffs_[i], horDim, verDim, coeffHistograms[i], binSpan);
		AccumulateHistograms(b3Coeffs_[i], horDim, verDim, coeffHistograms[i], binSpan);
		AccumulateHistograms(b4Coeffs_[i], horDim, verDim, coeffHistograms[i], binSpan);
	}
	
	binSpan = maxThumbnailPixel / numBins;
	
	//Generate thumbnail histograms
	vector<vector<int> > thumbnailHistograms(numLevels_);
	for (int i = 0; i < numLevels_; i++)
	{
		thumbnailHistograms[i].resize(numBins);
		
		int horDim = width_ / pow(2, i + 1);
		int verDim = height_ / pow(2, i + 1);
		
		AccumulateHistograms(l1Coeffs_[i], horDim, verDim, thumbnailHistograms[i], binSpan);
	}
	
	//Dump the histograms to a tab delimited file
	ofstream ofs;
	ofs.open(filename);
	assert(ofs);
	
	cout << "Histograms:" << endl;
	for (int i = 0; i < numBins; i++)
	{
		for (int j = 0; j < numLevels_; j++)
		{
			ofs << coeffHistograms[j][i] << '\t';
			cout << coeffHistograms[j][i] << '\t';
		}
		for (int j = 0; j < numLevels_; j++)
		{
			ofs << thumbnailHistograms[j][i] << '\t';
			cout << thumbnailHistograms[j][i] << '\t';
		}
		ofs << endl;
		
		cout << endl;
	}
	cout << "Done" << endl;
	
	ofs.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdx, %rbx
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x70(%rsp), %rdx
movapd %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
callq 0x191a8
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x1a200
movsd (%rax,%rdx,8), %xmm0
maxsd %xmm1, %xmm0
incq %rdx
movapd %xmm0, %xmm1
jmp 0x1a1e9
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x1a238
movsd (%rax,%rdx,8), %xmm0
maxsd 0x10(%rsp), %xmm0
incq %rdx
movsd %xmm0, 0x10(%rsp)
jmp 0x1a21d
movsd %xmm1, 0x8(%rsp)
movq %rbx, 0x60(%rsp)
movslq 0x38(%r14), %rsi
leaq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x1a9e2
movq 0x18(%rsp), %rax
cvtsi2sd %eax, %xmm1
movsd 0x8(%rsp), %xmm0
movsd %xmm1, 0x68(%rsp)
divsd %xmm1, %xmm0
movslq %eax, %r12
xorl %r15d, %r15d
xorl %ebx, %ebx
movsd %xmm0, 0x8(%rsp)
movslq 0x38(%r14), %rsi
cmpq %rsi, %rbx
jge 0x1a395
movq 0x30(%rsp), %rdi
addq %r15, %rdi
movq %r12, %rsi
callq 0x1aa34
cvtsi2sdl 0x30(%r14), %xmm0
movsd %xmm0, 0x20(%rsp)
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x3280
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %ebp
cvtsi2sdl 0x34(%r14), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x3280
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %r13d
testq %rbx, %rbx
jne 0x1a309
movq (%r14), %rsi
movq 0x30(%rsp), %r8
movl %ebp, %edx
movl %r13d, %ecx
movsd 0x8(%rsp), %xmm0
callq 0x1a6a6
movq 0x8(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq 0x30(%rsp), %r8
addq %r15, %r8
movl %ebp, %edx
movl %r13d, %ecx
movsd 0x8(%rsp), %xmm0
callq 0x1a6a6
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq 0x30(%rsp), %r8
addq %r15, %r8
movl %ebp, %edx
movl %r13d, %ecx
movsd 0x8(%rsp), %xmm0
callq 0x1a6a6
movq 0x18(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq 0x30(%rsp), %r8
addq %r15, %r8
movl %ebp, %edx
movl %r13d, %ecx
movsd 0x8(%rsp), %xmm0
callq 0x1a6a6
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq 0x30(%rsp), %r8
addq %r15, %r8
movl %ebp, %edx
movl %r13d, %ecx
movsd 0x8(%rsp), %xmm0
callq 0x1a6a6
incq %rbx
addq $0x18, %r15
jmp 0x1a280
leaq 0x48(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x1a9e2
movsd 0x10(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r14, %r15
movslq 0x38(%r14), %rax
cmpq %rax, %rbx
jge 0x1a460
movq 0x48(%rsp), %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x1aa34
cvtsi2sdl 0x30(%r14), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x1(%rbx), %r14
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x3280
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %ebp
cvtsi2sdl 0x34(%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x3280
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movq 0x28(%r15), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x18(%r13), %rbx
addq 0x48(%rsp), %r13
movl %ebp, %edx
movq %r13, %r8
movsd 0x10(%rsp), %xmm0
callq 0x1a6a6
movq %rbx, %r13
movq %r14, %rbx
movq %r15, %r14
jmp 0x1a3c1
leaq 0xa8(%rsp), %rdi
callq 0x34a0
leaq 0xa8(%rsp), %rdi
pushq $0x10
popq %rdx
movq 0x60(%rsp), %rsi
callq 0x3050
movq 0xa8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xc8(%rsp,%rax)
jne 0x1a624
movq 0x15b2d(%rip), %rdi     # 0x2ffd0
leaq 0xbf36(%rip), %rsi      # 0x263e0
callq 0x32c0
movq %rax, %rdi
callq 0x3120
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
testl %eax, %eax
cmovlel %r13d, %eax
movq %rax, 0x18(%rsp)
pushq $0x9
popq %rbp
movq 0x15afc(%rip), %r12     # 0x2ffd0
cmpq 0x18(%rsp), %r13
je 0x1a5b2
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq 0x38(%r15), %rax
cmpq %rax, %r14
jge 0x1a539
movq 0x30(%rsp), %rax
movq (%rax,%rbx), %rax
movl (%rax,%r13,4), %esi
leaq 0xa8(%rsp), %rdi
callq 0x34b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3320
movq 0x30(%rsp), %rax
movq (%rax,%rbx), %rax
movl (%rax,%r13,4), %esi
movq %r12, %rdi
callq 0x34b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3320
incq %r14
addq $0x18, %rbx
jmp 0x1a4e4
xorl %ebx, %ebx
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0x1a595
movq 0x48(%rsp), %rax
movq (%rax,%rbx), %rax
movl (%rax,%r13,4), %esi
leaq 0xa8(%rsp), %rdi
callq 0x34b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3320
movq 0x48(%rsp), %rax
movq (%rax,%rbx), %rax
movl (%rax,%r13,4), %esi
movq %r12, %rdi
callq 0x34b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3320
incq %r14
movl 0x38(%r15), %eax
addq $0x18, %rbx
jmp 0x1a53e
leaq 0xa8(%rsp), %rdi
callq 0x3120
movq %r12, %rdi
callq 0x3120
incq %r13
jmp 0x1a4d4
movq 0x15a17(%rip), %rdi     # 0x2ffd0
leaq 0xbe2c(%rip), %rsi      # 0x263ec
callq 0x32c0
movq %rax, %rdi
callq 0x3120
leaq 0xa8(%rsp), %rdi
callq 0x33c0
leaq 0xa8(%rsp), %rdi
callq 0x33e0
leaq 0x48(%rsp), %rdi
callq 0x1aa66
leaq 0x30(%rsp), %rdi
callq 0x1aa66
leaq 0x70(%rsp), %rdi
callq 0x8a42
leaq 0x90(%rsp), %rdi
callq 0x8a42
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8ba7(%rip), %rdi      # 0x231d2
leaq 0xac90(%rip), %rsi      # 0x252c2
leaq 0xbd70(%rip), %rcx      # 0x263a9
movl $0x7f3, %edx            # imm = 0x7F3
callq 0x31a0
jmp 0x1a652
jmp 0x1a65b
jmp 0x1a649
movq %rax, %rbx
jmp 0x1a686
jmp 0x1a662
jmp 0x1a652
movq %rax, %rbx
jmp 0x1a672
jmp 0x1a65b
jmp 0x1a662
movq %rax, %rbx
jmp 0x1a67c
jmp 0x1a662
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x33e0
leaq 0x48(%rsp), %rdi
callq 0x1aa66
leaq 0x30(%rsp), %rdi
callq 0x1aa66
leaq 0x70(%rsp), %rdi
callq 0x8a42
leaq 0x90(%rsp), %rdi
callq 0x8a42
movq %rbx, %rdi
callq 0x34d0
nop