// File: /workspace/github_cpp_makefile_star2_O3/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 0x4118a(%rip), %rsi     # 0xa25ab
movq %r12, %rdi
callq 0xe530
testq %rax, %rax
je 0x615f1
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x4, %rax
jne 0x61606
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x6160f
movl 0x1c(%rsp), %esi
cmpl $0x1, %esi
jne 0x61636
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
movl %ecx, 0x8(%rsp)
movl %ecx, %r13d
movl %eax, 0xc(%rsp)
imull %eax, %r13d
leaq (,%r13,4), %rdi
callq 0xe030
movq %rax, %r15
testl %r13d, %r13d
jle 0x614c4
movl %r13d, %ebp
movl %r13d, %eax
subl %ebp, %eax
leaq (%r15,%rax,4), %rdi
movl %ebp, %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0xe6c0
testl %eax, %eax
js 0x61649
subl %eax, %ebp
jg 0x614a0
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0xc4(%rbx)
movl %eax, 0xc8(%rbx)
movl %r13d, 0xcc(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%rbx)
leaq 0x60(%rbx), %rdi
leaq 0x486a57(%rip), %r13    # 0x4e7f58
leaq 0x8(%r13), %r12
movq %r12, %rsi
callq 0x66bbc
movl 0x50(%r13), %eax
movl %eax, 0xa8(%rbx)
leaq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x66bbc
movl 0x50(%r13), %eax
movl %eax, 0x50(%rbx)
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x6153b
callq 0xe5b0
movslq 0xcc(%rbx), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
testq %r12, %r12
jle 0x615d9
leaq 0x84628(%rip), %rcx     # 0xe5b98
movsd (%rcx), %xmm0
movapd 0x40aa4(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
movsd 0x47d96(%rip), %xmm2    # 0xa9320
movsd 0x41a26(%rip), %xmm3    # 0xa2fb8
xorps %xmm4, %xmm4
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
cmpq %rcx, %r12
jne 0x61592
movq %r15, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe430
movl 0xcc(%rbx), %ebx
jmp 0x61625
leaq 0x47e2d(%rip), %rdi     # 0xa9425
xorl %ebx, %ebx
movq %r12, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x61625
leaq 0x47e39(%rip), %rdi     # 0xa9446
jmp 0x61616
leaq 0x47e56(%rip), %rdi     # 0xa946c
movq %r12, %rsi
xorl %eax, %eax
callq 0x719d2
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x47e53(%rip), %rdi     # 0xa9490
movq %r12, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x61620
leaq 0x47e74(%rip), %rdi     # 0xa94c4
jmp 0x615f8