// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadGridStream(_IO_FILE*)
int R2Grid::
ReadGridStream(FILE *fp)
{
  // Read grid resolution from file
  if (fread(&grid_resolution, sizeof(int), 2, fp) != 2) {
    RNFail("Unable to read resolution from grid file");
    return 0;
  }

  // Update grid resolution variables
  grid_row_size = grid_resolution[0];
  grid_size = grid_row_size * grid_resolution[1];
  if (grid_size <= 0) {
    RNFail("Invalid grid size (%d) in file", grid_size);
    return 0;
  }

  // Read world_to_grid transformation from file
  RNScalar m[9];
  if (fread(m, sizeof(RNScalar), 9, fp) != 9) {
    RNFail("Unable to read transformation matrix from file");
    return 0;
  }

  // Update transformation variables
  world_to_grid_transform.Reset(R3Matrix(m));
  world_to_grid_scale_factor = world_to_grid_transform.ScaleFactor();
  grid_to_world_transform = world_to_grid_transform.Inverse();

  // Allocate grid values
  grid_values = new RNScalar [ grid_size ];
  assert(grid_values);

  // Read values
  int count = grid_size;
  while (count > 0) {
    int n = fread(&grid_values[grid_size-count], sizeof(RNScalar), count, fp);
    if (n < 0) { RNFail("Unable to read pixels from grid\n"); return 0; }
    count -= n;
  }

  // Just to be sure
  for (int i = 0; i < grid_size; i++) {
    if (RNIsEqual(grid_values[i], R2_GRID_UNKNOWN_VALUE)) grid_values[i] = R2_GRID_UNKNOWN_VALUE;
  }

  // Return number of grid values read
  return grid_size;
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x138(%rsp)
movq %rsi, 0x130(%rsp)
movq 0x138(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0xc0, %rdi
movq 0x130(%rsp), %rcx
movl $0x4, %esi
movl $0x2, %edx
callq 0xf780
cmpq $0x2, %rax
je 0xb8196
leaq 0x7de99(%rip), %rdi     # 0x136018
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x144(%rsp)
jmp 0xb8419
movq 0x20(%rsp), %rax
movl 0xc0(%rax), %ecx
movl %ecx, 0xc8(%rax)
movl 0xc8(%rax), %ecx
imull 0xc4(%rax), %ecx
movl %ecx, 0xcc(%rax)
cmpl $0x0, 0xcc(%rax)
jg 0xb81ec
movq 0x20(%rsp), %rax
movl 0xcc(%rax), %esi
leaq 0x7c1a4(%rip), %rdi     # 0x134379
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x144(%rsp)
jmp 0xb8419
leaq 0xe0(%rsp), %rdi
movq 0x130(%rsp), %rcx
movl $0x8, %esi
movl $0x9, %edx
callq 0xf780
cmpq $0x9, %rax
je 0xb822f
leaq 0x7de29(%rip), %rdi     # 0x136041
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x144(%rsp)
jmp 0xb8419
movq 0x20(%rsp), %rax
addq $0x58, %rax
movq %rax, 0x10(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0xe0(%rsp), %rsi
callq 0xbf880
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0xbf1f0
movq 0x10(%rsp), %rdi
callq 0xbee10
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rax
movsd %xmm0, 0xb0(%rax)
leaq 0x40(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xb9310
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xb9200
jmp 0xb82a4
leaq 0x40(%rsp), %rdi
callq 0xb9250
movq 0x20(%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 0x20(%rsp), %rax
movq %rcx, 0xb8(%rax)
movl 0xcc(%rax), %eax
movl %eax, 0x30(%rsp)
cmpl $0x0, 0x30(%rsp)
jle 0xb838e
movq 0x20(%rsp), %rax
movq 0xb8(%rax), %rdi
movl 0xcc(%rax), %eax
subl 0x30(%rsp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
movslq 0x30(%rsp), %rdx
movq 0x130(%rsp), %rcx
movl $0x8, %esi
callq 0xf780
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jge 0xb837b
leaq 0x7dd29(%rip), %rdi     # 0x136070
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x144(%rsp)
jmp 0xb8419
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x40(%rsp), %rdi
callq 0xb9250
jmp 0xb8428
movl 0x2c(%rsp), %ecx
movl 0x30(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x30(%rsp)
jmp 0xb82f4
movl $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rcx
movl 0x28(%rsp), %eax
cmpl 0xcc(%rcx), %eax
jge 0xb8407
movq 0x20(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x28(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xc011c(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
movsd 0x7bae0(%rip), %xmm1    # 0x133eb0
callq 0x37170
cmpl $0x0, %eax
je 0xb83f8
movq 0x20(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x28(%rsp), %rcx
movsd 0x7babd(%rip), %xmm0    # 0x133eb0
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb83fa
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xb8396
movq 0x20(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x144(%rsp)
movl 0x144(%rsp), %eax
addq $0x148, %rsp            # imm = 0x148
retq
movq 0x38(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)