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

  // Read header
  unsigned int header[4];
  if (fread(header, sizeof(unsigned int), 4, fp) != 4) {
    RNFail("Unable to header to raw image file %s", filename);
    return -1;
  }

  // Check magic number
  const unsigned int magic = 54321;
  if (header[0] != magic) {
    RNFail("Invalid header in raw image file %s", filename);
    return -1;
  }

  // Check number of channels
  if (header[3] != 1) {
    RNFail("Invalid number of channels (%d)in raw image file %s", header[3], filename);
    return -1;
  }

  // Parse header
  int width = header[1];
  int height = header[2];

  // Allocate pixels
  unsigned int npixels = width * height;
  float *pixels = new float [ npixels ];
  if (!pixels) {
    RNFail("Unable to allocate data for raw image file %s", filename);
    return -1;
  }

  // Read pixels
  int count = npixels;
  while (count > 0) {
    int n = fread(&pixels[npixels-count], sizeof(float), count, fp);
    if (n < 0) { RNFail("Unable to read pixels from %s\n", filename); return 0; }
    count -= n;
  }

  // Fill in grid info
  grid_resolution[0] = width;
  grid_resolution[1] = height;
  grid_row_size = width;
  grid_size = npixels;
  world_to_grid_scale_factor = 1;
  world_to_grid_transform = R2identity_affine;
  grid_to_world_transform = R2identity_affine;
  if (grid_values) delete [] grid_values;
  grid_values = new RNScalar [ grid_size ];
  for (int i = 0; i < grid_size; i++) {
    if (RNIsEqual(pixels[i], R2_GRID_UNKNOWN_VALUE)) grid_values[i] = R2_GRID_UNKNOWN_VALUE;
    else grid_values[i] = pixels[i];
  }

  // 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, 0x10(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x7d6e5(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xb5f26
movq 0x60(%rsp), %rsi
leaq 0x7fdf3(%rip), %rdi     # 0x135d05
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xb6248
leaq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rcx
movl $0x4, %edx
movq %rdx, %rsi
callq 0xf780
cmpq $0x4, %rax
je 0xb5f63
movq 0x60(%rsp), %rsi
leaq 0x7fdd7(%rip), %rdi     # 0x135d26
movb $0x0, %al
callq 0xd4f30
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xb6248
movl $0xd431, 0x3c(%rsp)     # imm = 0xD431
cmpl $0xd431, 0x40(%rsp)     # imm = 0xD431
je 0xb5f95
movq 0x60(%rsp), %rsi
leaq 0x7fdcb(%rip), %rdi     # 0x135d4c
movb $0x0, %al
callq 0xd4f30
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xb6248
cmpl $0x1, 0x4c(%rsp)
je 0xb5fc0
movl 0x4c(%rsp), %esi
movq 0x60(%rsp), %rdx
leaq 0x7fdc4(%rip), %rdi     # 0x135d70
movb $0x0, %al
callq 0xd4f30
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xb6248
movl 0x44(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x38(%rsp), %eax
imull 0x34(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
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)
cmpq $0x0, 0x28(%rsp)
jne 0xb602f
movq 0x60(%rsp), %rsi
leaq 0x7fd89(%rip), %rdi     # 0x135da4
movb $0x0, %al
callq 0xd4f30
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xb6248
movl 0x30(%rsp), %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jle 0xb60a3
movq 0x28(%rsp), %rdi
movl 0x30(%rsp), %eax
subl 0x24(%rsp), %eax
movl %eax, %eax
shlq $0x2, %rax
addq %rax, %rdi
movslq 0x24(%rsp), %rdx
movq 0x58(%rsp), %rcx
movl $0x4, %esi
callq 0xf780
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
jge 0xb6093
movq 0x60(%rsp), %rsi
leaq 0x7fd53(%rip), %rdi     # 0x135dd2
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xb6248
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
jmp 0xb6037
movq 0x10(%rsp), %rdi
movl 0x38(%rsp), %eax
movl %eax, 0xc0(%rdi)
movl 0x34(%rsp), %eax
movl %eax, 0xc4(%rdi)
movl 0x38(%rsp), %eax
movl %eax, 0xc8(%rdi)
movl 0x30(%rsp), %eax
movl %eax, 0xcc(%rdi)
movsd 0x7cf30(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rdi)
addq $0x58, %rdi
leaq 0x4c497d(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x10(%rsp), %rdi
leaq 0x4c496c(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x10(%rsp), %rax
cmpq $0x0, 0xb8(%rax)
je 0xb6133
movq 0x10(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb6131
movq 0x8(%rsp), %rdi
callq 0xf670
jmp 0xb6133
movq 0x10(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0xb8(%rax)
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl 0xcc(%rcx), %eax
jge 0xb6217
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xc233a(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
movsd 0x7dcfe(%rip), %xmm1    # 0x133eb0
callq 0x37170
cmpl $0x0, %eax
je 0xb61dc
movq 0x10(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x7dcdb(%rip), %xmm0    # 0x133eb0
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb6205
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
movslq 0x1c(%rsp), %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb6207
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xb6177
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xb622f
movq (%rsp), %rdi
callq 0xf670
movq 0x58(%rsp), %rdi
callq 0xf4b0
movq 0x10(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)