// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x3a4c2(%rip), %rsi     # 0x9355b
movq %r12, %rdi
callq 0xe550
testq %rax, %rax
je 0x5913d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rdx
movq %rdx, %rsi
movq %rax, %rcx
callq 0xe6f0
cmpq $0x4, %rax
jne 0x59152
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x5915b
movl 0x1c(%rsp), %esi
cmpl $0x1, %esi
jne 0x59180
movq %rbx, 0x20(%rsp)
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
movl %ecx, 0x8(%rsp)
movl %ecx, %ebp
movl %eax, 0xc(%rsp)
imull %eax, %ebp
leaq (,%rbp,4), %rdi
callq 0xe030
movq %rax, %r15
pushq $0x4
popq %r13
movl %ebp, %ebx
testl %ebx, %ebx
jle 0x59193
movl %ebp, %eax
subl %ebx, %eax
leaq (%r15,%rax,4), %rdi
movl %ebx, %edx
movq %r13, %rsi
movq %r14, %rcx
callq 0xe6f0
subl %eax, %ebx
testl %eax, %eax
jns 0x59115
leaq 0x41319(%rip), %rdi     # 0x9a454
jmp 0x59144
leaq 0x41271(%rip), %rdi     # 0x9a3b5
xorl %ebx, %ebx
movq %r12, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x5916f
leaq 0x4127d(%rip), %rdi     # 0x9a3d6
jmp 0x59162
leaq 0x4129a(%rip), %rdi     # 0x9a3fc
movq %r12, %rsi
xorl %eax, %eax
callq 0x681ca
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41299(%rip), %rdi     # 0x9a420
movq %r12, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x5916c
movq 0x20(%rsp), %r13
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%r13)
movl 0x8(%rsp), %ecx
movl %ecx, 0xc4(%r13)
movl %eax, 0xc8(%r13)
movl %ebp, 0xcc(%r13)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%r13)
leaq 0x58(%r13), %rdi
leaq 0x483d90(%rip), %r12    # 0x4dcf68
movq %r12, %rsi
callq 0x5ac32
movq %r13, %rdi
movq %r12, %rsi
callq 0x5ac32
movq 0xb8(%r13), %rdi
testq %rdi, %rdi
je 0x591fc
callq 0xe5d0
movslq 0xcc(%r13), %rbx
leaq (,%rbx,8), %rax
testq %rbx, %rbx
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%r13)
leaq 0x81980(%rip), %rcx     # 0xdaba8
movsd (%rcx), %xmm0
movapd 0x39dfc(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %edx
cmovgl %ebx, %edx
movsd 0x41084(%rip), %xmm2    # 0x9a2d0
movsd 0x3ad14(%rip), %xmm3    # 0x93f68
cmpq %rcx, %rdx
je 0x5929a
cvtss2sd (%r15,%rcx,4), %xmm4
movaps %xmm4, %xmm5
addsd %xmm2, %xmm5
movapd %xmm1, %xmm6
cmpnlesd %xmm5, %xmm6
movapd %xmm6, %xmm7
andpd %xmm4, %xmm7
cmpnlesd %xmm0, %xmm5
andpd %xmm5, %xmm4
andnpd %xmm3, %xmm5
orpd %xmm4, %xmm5
andnpd %xmm5, %xmm6
orpd %xmm7, %xmm6
movlpd %xmm6, (%rax,%rcx,8)
incq %rcx
jmp 0x59254
movq %r15, %rdi
callq 0xe5d0
movq %r14, %rdi
callq 0xe440
movl 0xcc(%r13), %ebx
jmp 0x5916f