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

  // Write header
  const unsigned int magic = 54321;
  unsigned int xres = (unsigned int) grid_resolution[0];
  unsigned int yres = (unsigned int) grid_resolution[1];
  unsigned int header[4] = { magic, xres, yres, 1 };
  if (fwrite(header, sizeof(unsigned int), 4, fp) != 4) {
    RNFail("Unable to header to raw image file %s", filename);
    return -1;
  }

  // Allocate pixels
  float *pixels = new float [ grid_row_size ];

  // 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] = (float) 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 $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x7c2c6(%rip), %rsi     # 0x133aba
callq 0xf5e0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xb7826
movq 0x60(%rsp), %rsi
leaq 0x7e4f3(%rip), %rdi     # 0x135d05
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xb79f1
movq 0x8(%rsp), %rax
movl $0xd431, 0x54(%rsp)     # imm = 0xD431
movl 0xc0(%rax), %ecx
movl %ecx, 0x50(%rsp)
movl 0xc4(%rax), %eax
movl %eax, 0x4c(%rsp)
movl $0xd431, 0x30(%rsp)     # imm = 0xD431
movl 0x50(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x1, 0x3c(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rcx
movl $0x4, %edx
movq %rdx, %rsi
callq 0xf980
cmpq $0x4, %rax
je 0xb78a4
movq 0x60(%rsp), %rsi
leaq 0x7e496(%rip), %rdi     # 0x135d26
movb $0x0, %al
callq 0xd4f30
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xb79f1
movq 0x8(%rsp), %rax
movslq 0xc8(%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, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq 0x8(%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl 0xc4(%rcx), %eax
jge 0xb79c0
movq 0x8(%rsp), %rdx
movq 0xb8(%rdx), %rax
movl 0x24(%rsp), %ecx
imull 0xc0(%rdx), %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl 0xc0(%rcx), %eax
jge 0xb7961
movq 0x18(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x28(%rsp), %rax
movslq 0x14(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xb7921
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rdi
movslq 0xc0(%rax), %rdx
movq 0x58(%rsp), %rcx
movl $0x4, %esi
callq 0xf980
movq 0x8(%rsp), %rcx
movl 0xc0(%rcx), %ecx
cmpq %rcx, %rax
je 0xb79ae
movq 0x60(%rsp), %rsi
leaq 0x7e454(%rip), %rdi     # 0x135df1
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xb79f1
jmp 0xb79b0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xb78de
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xb79d8
movq (%rsp), %rdi
callq 0xf670
movq 0x58(%rsp), %rdi
callq 0xf4b0
movq 0x8(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw (%rax,%rax)