// File: 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
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %r8, -0x268(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movq %r8, -0x30(%rbp)
leaq -0x230(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x45e0
movq -0x268(%rbp), %rdi
callq 0x4160
movq -0x260(%rbp), %rdi
movq %rax, %rsi
movl $0x10, %edx
callq 0x4070
jmp 0x31573
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x269(%rbp)
jmp 0x31593
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x3159f
jmp 0x315a1
jmp 0x315e3
leaq 0x18d12(%rip), %rdi     # 0x4a2ba
leaq 0x1ada7(%rip), %rsi     # 0x4c356
movl $0x79d, %edx            # imm = 0x79D
leaq 0x1beb9(%rip), %rcx     # 0x4d474
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x4540
jmp 0x31817
leaq 0x18c1a(%rip), %rsi     # 0x4a204
leaq -0x230(%rbp), %rdi
callq 0x4390
movq %rax, -0x278(%rbp)
jmp 0x315ff
movq -0x278(%rbp), %rdi
movq 0x289ab(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x280(%rbp)
jmp 0x3161b
movq -0x280(%rbp), %rdi
leaq 0x18cb3(%rip), %rsi     # 0x4a2dc
callq 0x4390
movq %rax, -0x288(%rbp)
jmp 0x31637
movq -0x288(%rbp), %rdi
movq 0x28973(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x290(%rbp)
jmp 0x31653
movq -0x290(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4600
movq %rax, -0x298(%rbp)
jmp 0x3166b
movq -0x298(%rbp), %rdi
leaq 0x18335(%rip), %rsi     # 0x499ae
callq 0x4390
movq %rax, -0x2a0(%rbp)
jmp 0x31687
movq -0x2a0(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x4600
movq %rax, -0x2a8(%rbp)
jmp 0x3169f
movq -0x2a8(%rbp), %rdi
movq 0x2890b(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x2b0(%rbp)
jmp 0x316bb
movq -0x2b0(%rbp), %rdi
movl $0xff, %esi
callq 0x4600
movq %rax, -0x2b8(%rbp)
jmp 0x316d5
movq -0x2b8(%rbp), %rdi
movq 0x288d5(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x316ea
movl -0x14(%rbp), %eax
imull -0x18(%rbp), %eax
movl %eax, -0x240(%rbp)
movl $0x0, -0x254(%rbp)
movl -0x254(%rbp), %eax
cmpl -0x240(%rbp), %eax
jge 0x317f4
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x10(%rbp)
movsd (%rax), %xmm0
movsd -0x20(%rbp), %xmm2
movsd -0x28(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x248(%rbp)
movsd 0x195a0(%rip), %xmm0    # 0x4ace8
mulsd -0x248(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x5, %edx
jl 0x31779
movsd 0x178b7(%rip), %xmm0    # 0x49020
addsd -0x248(%rbp), %xmm0
movsd %xmm0, -0x248(%rbp)
cvttsd2si -0x248(%rbp), %eax
movl %eax, -0x24c(%rbp)
cmpl $0x0, -0x24c(%rbp)
jl 0x3179c
cmpl $0xff, -0x24c(%rbp)
jle 0x317bd
cmpl $0x0, -0x24c(%rbp)
jge 0x317b1
movl $0x0, -0x24c(%rbp)
jmp 0x317bb
movl $0xff, -0x24c(%rbp)
jmp 0x317bd
movb -0x24c(%rbp), %al
movb %al, -0x24d(%rbp)
movsbl -0x24d(%rbp), %esi
leaq -0x230(%rbp), %rdi
callq 0x4050
jmp 0x317de
jmp 0x317e0
movl -0x254(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0x31701
leaq -0x230(%rbp), %rdi
callq 0x4520
jmp 0x31802
leaq -0x230(%rbp), %rdi
callq 0x4540
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)