// File: kebwi[P]Trifid/src/SteerablePyramid.cpp
// Function: SteerablePyramid::InitFilters()
void SteerablePyramid::InitFilters()
{
	//Only init the filters once
	static bool alreadyInited = false;
	if (alreadyInited)
		return;
	alreadyInited = true;
	
	readFilterSet();
	
	//Normalize the filters
	//This is definitely required for the first filter set, which is defined using integers
	//I am unsure why the third filter set used an L0 and L1 that sum to 2 instead of 1.  Perhaps normalizing them is not correct.
	/*
	for (int filter = 0; filter < 7; filter++)
	{
		Float_t divisor = 0;
		int numTaps = skFilterDims[filter] * skFilterDims[filter];
		for (int i = 0; i < numTaps; i++)
			divisor += fabs(skFilters[filter][i]);
		cout << "Filter " << filter << " has " << numTaps << " taps with sum: " << divisor << endl;
		for (int i = 0; i < numTaps; i++)
			skFilters[filter][i] /= divisor;
	}
	cout << endl;
	*/
	if (skFilterSetFilename == "filterSet1.txt")
	{
		//The filters are integers with power 10000.
		//Therefore they must be scaled by 1/10000 to achieve unit power.
		//Note, however, that L0 and L1 do not quite sum to 10000.
		//Therefore, they are scaled slightly differently.
		for (int filter = 0; filter < 7; filter++)
		{
			for (int i = 0; i < 289; i++)
			{
				if (filter == 1)
					skFilters[filter][i] /= 9996.0;
				else if (filter == 2)
					skFilters[filter][i] /= 10005.0;
				else skFilters[filter][i] /= 10000.0;
			}
		}
	}
	if (skFilterSetFilename == "steerablePyramidFilterSet.txt")
	{
		//H0 sums to 0
		//L0 and L1 sum to 2
		//B1-4 sum to 0
		//To get correct reconstruction, it is necessary to scale just L1 to unit power but to leave L0 at power 2.  Why?
		for (int i = 0; i < 289; i++)
			skFilters[2][i] /= 2.0;
	}
	
	//Define the inverted filters
	for (int filter = 0; filter < 7; filter++)
	{
		int numTaps = skFilterDims[filter] * skFilterDims[filter];
		for (int i = 0; i < numTaps; i++)
			skFiltersInv[filter][i] = skFilters[filter][(numTaps - 1) - i];
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
testb $0x1, 0x33829(%rip)     # 0x5cc68
je 0x29446
jmp 0x2962c
movb $0x1, 0x3381b(%rip)     # 0x5cc68
callq 0x2a390
leaq 0x3371f(%rip), %rdi     # 0x5cb78
leaq 0x22f70(%rip), %rsi     # 0x4c3d0
callq 0xe570
testb $0x1, %al
jne 0x2946e
jmp 0x29542
movl $0x0, -0x4(%rbp)
cmpl $0x7, -0x4(%rbp)
jge 0x29540
movl $0x0, -0x8(%rbp)
cmpl $0x121, -0x8(%rbp)      # imm = 0x121
jge 0x29530
cmpl $0x1, -0x4(%rbp)
jne 0x294c4
movslq -0x4(%rbp), %rcx
leaq 0x3374c(%rip), %rax     # 0x5cbf0
movq (%rax,%rcx,8), %rax
movslq -0x8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x22d67(%rip), %xmm1    # 0x4c220
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x29520
cmpl $0x2, -0x4(%rbp)
jne 0x294f5
movslq -0x4(%rbp), %rcx
leaq 0x3371b(%rip), %rax     # 0x5cbf0
movq (%rax,%rcx,8), %rax
movslq -0x8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x22d2e(%rip), %xmm1    # 0x4c218
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x2951e
movslq -0x4(%rbp), %rcx
leaq 0x336f0(%rip), %rax     # 0x5cbf0
movq (%rax,%rcx,8), %rax
movslq -0x8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x22cfb(%rip), %xmm1    # 0x4c210
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x29520
jmp 0x29522
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x29486
jmp 0x29532
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x29475
jmp 0x29542
leaq 0x3362f(%rip), %rdi     # 0x5cb78
leaq 0x22d14(%rip), %rsi     # 0x4c264
callq 0xe570
testb $0x1, %al
jne 0x2955b
jmp 0x29599
movl $0x0, -0xc(%rbp)
cmpl $0x121, -0xc(%rbp)      # imm = 0x121
jge 0x29597
movq 0x3368e(%rip), %rax     # 0x5cc00
movslq -0xc(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x20fc5(%rip), %xmm1    # 0x4a548
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x29562
jmp 0x29599
movl $0x0, -0x10(%rbp)
cmpl $0x7, -0x10(%rbp)
jge 0x2962c
movslq -0x10(%rbp), %rcx
leaq 0x3361b(%rip), %rax     # 0x5cbd0
movl (%rax,%rcx,4), %eax
movslq -0x10(%rbp), %rdx
leaq 0x3360d(%rip), %rcx     # 0x5cbd0
imull (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2961c
movslq -0x10(%rbp), %rcx
leaq 0x3360c(%rip), %rax     # 0x5cbf0
movq (%rax,%rcx,8), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
subl -0x18(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x10(%rbp), %rcx
leaq 0x3362c(%rip), %rax     # 0x5cc30
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x295d1
jmp 0x2961e
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x295a0
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)