// File: /workspace/github_cpp_makefile_star2_O1/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 0x4038d(%rip), %rsi     # 0xa25ae
movq %r12, %rdi
callq 0xe530
testq %rax, %rax
je 0x622e4
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x4, %rax
jne 0x622f9
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x62302
movl 0x1c(%rsp), %esi
cmpl $0x1, %esi
jne 0x62329
movq %rbx, 0x20(%rsp)
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
movl %r13d, %ebx
testl %ebx, %ebx
jle 0x6233c
movl %r13d, %eax
subl %ebx, %eax
leaq (%r15,%rax,4), %rdi
movl %ebx, %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0xe6c0
movq %rax, %rbp
testl %ebp, %ebp
js 0x622cb
subl %ebp, %ebx
jmp 0x622dc
leaq 0x471f2(%rip), %rdi     # 0xa94c4
movq %r12, %rsi
xorl %eax, %eax
callq 0x72a1e
testl %ebp, %ebp
jns 0x622a0
xorl %ebx, %ebx
jmp 0x62318
leaq 0x4713a(%rip), %rdi     # 0xa9425
xorl %ebx, %ebx
movq %r12, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x62318
leaq 0x47146(%rip), %rdi     # 0xa9446
jmp 0x62309
leaq 0x47163(%rip), %rdi     # 0xa946c
movq %r12, %rsi
xorl %eax, %eax
callq 0x72a1e
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 0x47160(%rip), %rdi     # 0xa9490
movq %r12, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x62313
movq 0x20(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, 0xc0(%rax)
movl 0x8(%rsp), %edx
movl %edx, 0xc4(%rax)
movl %ecx, 0xc8(%rax)
movl %r13d, 0xcc(%rax)
movq %rax, %r13
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%r13)
leaq 0x60(%r13), %rdi
leaq 0x485bd7(%rip), %rbx    # 0x4e7f58
leaq 0x8(%rbx), %r12
movq %r12, %rsi
callq 0x67944
movl 0x50(%rbx), %eax
movl %eax, 0xa8(%r13)
leaq 0x8(%r13), %rdi
movq %r12, %rsi
callq 0x67944
movl 0x50(%rbx), %eax
movl %eax, 0x50(%r13)
movq 0xb8(%r13), %rdi
testq %rdi, %rdi
je 0x623bb
callq 0xe5b0
movslq 0xcc(%r13), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%r13)
testq %r12, %r12
jle 0x62459
leaq 0x837a8(%rip), %rcx     # 0xe5b98
movsd (%rcx), %xmm0
movapd 0x3fc24(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
movsd 0x46f16(%rip), %xmm2    # 0xa9320
movsd 0x40ba6(%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 0x62412
movq %r15, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe430
movl 0xcc(%r13), %ebx
jmp 0x62318
nop