// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::WritePFMFile(char const*) const
int R2Grid::
WritePFMFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    RNFail("Unable to open pfm image file %s", filename);
    return 0;
  }

  // Write header
  fprintf(fp, "Pf\n");
  fprintf(fp, "%d %d\n", grid_resolution[0], grid_resolution[1]);
  fprintf(fp, "-1.0\n");

  // Allocate pixels
  float *pixels = new float [ grid_resolution[0] ];

  // Write pixels (row by row to avoid large buffers)
  for (int i = 0; i < grid_resolution[1]; i++) {
    RNScalar *valuesp = &grid_values[i * grid_resolution[0]];
    for (int i = 0; i < grid_resolution[0]; i++) pixels[i] = valuesp[i];
    if (fwrite(pixels, sizeof(float), grid_resolution[0], fp) != (unsigned int) grid_resolution[0]) {
      RNFail("Unable to write grid values to file %s\n", filename);
      return 0;
    }
  }

  // Delete pixels
  delete [] pixels;

  // Close file
  fclose(fp);

  // Return success
  return grid_size;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x7c096(%rip), %rsi     # 0x133aba
callq 0xf5e0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xb7a56
movq 0x40(%rsp), %rsi
leaq 0x7e5ab(%rip), %rdi     # 0x135fed
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x54(%rsp)
jmp 0xb7bef
movq 0x38(%rsp), %rdi
leaq 0x7e5ac(%rip), %rsi     # 0x13600e
movb $0x0, %al
callq 0xf730
movq 0x10(%rsp), %rax
movq 0x38(%rsp), %rdi
movl 0xc0(%rax), %edx
movl 0xc4(%rax), %ecx
leaq 0x7bfea(%rip), %rsi     # 0x133a70
movb $0x0, %al
callq 0xf730
movq 0x38(%rsp), %rdi
leaq 0x7e579(%rip), %rsi     # 0x136012
movb $0x0, %al
callq 0xf730
movq 0x10(%rsp), %rax
movslq 0xc0(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x10(%rsp), %rcx
movl 0x2c(%rsp), %eax
cmpl 0xc4(%rcx), %eax
jge 0xb7bbc
movq 0x10(%rsp), %rdx
movq 0xb8(%rdx), %rax
movl 0x2c(%rsp), %ecx
imull 0xc0(%rdx), %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl 0xc0(%rcx), %eax
jge 0xb7b5d
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x30(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xb7b1d
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rdi
movslq 0xc0(%rax), %rdx
movq 0x38(%rsp), %rcx
movl $0x4, %esi
callq 0xf980
movq 0x10(%rsp), %rcx
movl 0xc0(%rcx), %ecx
cmpq %rcx, %rax
je 0xb7baa
movq 0x40(%rsp), %rsi
leaq 0x7e258(%rip), %rdi     # 0x135df1
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x54(%rsp)
jmp 0xb7bef
jmp 0xb7bac
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xb7ada
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb7bd6
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0x38(%rsp), %rdi
callq 0xf4b0
movq 0x10(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax,%rax)