// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/SteerablePyramid.cpp
// Function: SteerablePyramid::GeneratePGMimage(double*, int, int, double, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void SteerablePyramid::GeneratePGMimage(Float_t *coeffs, int horDim, int verDim, Float_t scalar, Float_t shift, string filename)
{
	ofstream ofs;
	ofs.open(filename.c_str());
	assert(ofs);
	
	//Write pgm header
	ofs << "P5" << endl
		<< "#." << endl
		<< horDim << " " << verDim << endl
		<< 255 << endl;
	
	int totalPixels = horDim * verDim;
	Float_t pixelF;
	int pixelInt;
	unsigned char pixel;
	for (int i = 0; i < totalPixels; i++)
	{
		pixelF = *coeffs++ * scalar + shift;
		if ((int)(pixelF * 10.0) % 10 >= 5)
			pixelF++;
		pixelInt = pixelF;
		if (pixelInt < 0 || pixelInt > 255)
		{
			/*
			cout << "Out of bounds pixel: "
				<< horDim << "    " << verDim << "    "
				<< i % horDim << "    " << i / verDim << "    "
				<< *coeffs << "    " << pixelInt << endl;
			*/
			if (pixelInt < 0)
				pixelInt = 0;
			else pixelInt = 255;
		}
		pixel = pixelInt;
		ofs.put(pixel);
	}
	
	ofs.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %r8, %r15
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, %rbx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x34d0
movq (%r15), %rsi
movq %r12, %rdi
movl $0x10, %edx
callq 0x3050
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x38(%rsp,%rax)
jne 0x20cd1
leaq 0xa646(%rip), %rsi      # 0x2b12c
leaq 0x18(%rsp), %r15
movl $0x2, %edx
movq %r15, %rdi
callq 0x3360
movq 0x18(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
leaq 0x18(%rsp), %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %r15
leaq 0xa6d7(%rip), %rsi      # 0x2b204
movl $0x2, %edx
movq %rax, %rdi
callq 0x3360
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
movl %r14d, %esi
callq 0x34e0
movq %rax, %r15
leaq 0x9d79(%rip), %rsi      # 0x2a8ef
movl $0x1, %edx
movq %rax, %rdi
callq 0x3360
movq %r15, %rdi
movl %ebp, %esi
callq 0x34e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
movl $0xff, %esi
callq 0x34e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
imull %r14d, %ebp
testl %ebp, %ebp
jle 0x20c83
movl %ebp, %r15d
xorl %r12d, %r12d
movl $0xff, %ebp
leaq 0x18(%rsp), %r14
xorl %r13d, %r13d
movsd (%rbx,%r13,8), %xmm0
mulsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd 0xb035(%rip), %xmm1     # 0x2bc60
cvttsd2si %xmm1, %eax
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
movapd %xmm0, %xmm1
addsd 0x9400(%rip), %xmm1     # 0x2a058
cmpl $0x5, %eax
jge 0x20c61
movapd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
testl %eax, %eax
cmovlel %r12d, %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
incq %r13
cmpl %r13d, %r15d
jne 0x20c0d
leaq 0x18(%rsp), %rdi
callq 0x33e0
movq 0x16334(%rip), %rax     # 0x36fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x20(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x18(%rsp,%rcx)
callq 0x3270
leaq 0x110(%rsp), %rdi
callq 0x30e0
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa50a(%rip), %rdi      # 0x2b1e2
leaq 0xc643(%rip), %rsi      # 0x2d322
leaq 0xd6cc(%rip), %rcx      # 0x2e3b2
movl $0x79d, %edx            # imm = 0x79D
callq 0x31c0
jmp 0x20cf4
jmp 0x20cf4
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3400
movq %rbx, %rdi
callq 0x3500
nop