// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadRawFile(char const*)
int R3Grid::
ReadRawFile(const char *filename)
{
  // Get size file name
  char size_name[2048];
  strncpy(size_name, filename, 2047);
  char *ext = strrchr(size_name, '.');
  if (ext) *ext = '\0';
  strncat(size_name, ".size", 2047);

  // Get transformation file name
  char transformation_name[2048];
  strncpy(transformation_name, filename, 2047);
  ext = strrchr(transformation_name, '.');
  if (ext) *ext = '\0';
  strncat(transformation_name, ".xf", 2047);

  // Read size file
  char format[2048];
  int xres, yres, zres;
  if (!ReadRawSizeFile(size_name, xres, yres, zres, format)) return 0;
  int new_size = xres * yres * zres;
  if (new_size == 0) {
    RNFail("Zero dimensions in size file %s", size_name);
    return 0;
  }

  // Open raw file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open raw file %s", filename);
    return 0;
  }

  // Re-allocate grid values
  if (!grid_values || (new_size > grid_size)) { 
    if (grid_values) delete [] grid_values;
    grid_values = new RNScalar [ new_size ];
    assert(grid_values);
  }

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

  // Read raw grid values
  if (!strcmp(format, "Double64") || !strcmp(format, "double64")) {
    if (!ReadRawValues<RNScalar64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Float64") || !strcmp(format, "float64")) {
    if (!ReadRawValues<RNScalar64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Float32") || !strcmp(format, "float32")) {
    if (!ReadRawValues<RNScalar32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint64") || !strcmp(format, "Uint64")) {
    if (!ReadRawValues<RNUInt64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int64") || !strcmp(format, "int64")) {
    if (!ReadRawValues<RNInt64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint32") || !strcmp(format, "uint32")) {
    if (!ReadRawValues<RNUInt32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int32") || !strcmp(format, "int32")) {
    if (!ReadRawValues<RNInt32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint16") || !strcmp(format, "uint16")) {
    if (!ReadRawValues<RNUInt16>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int16") || !strcmp(format, "int16")) {
    if (!ReadRawValues<RNInt16>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uchar8") || !strcmp(format, "uchar8")) {
    if (!ReadRawValues<RNUChar8>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Char8") || !strcmp(format, "char8")) {
    if (!ReadRawValues<RNChar8>(fp, grid_values, grid_size)) return 0;
  } else {
    RNFail("Unrecognized format %s in %s\n", format, size_name);
    return 0;
  }

  // Close file
  fclose(fp);

  // Read transformation file
  R3Affine transformation;
  if (ReadRawTransformationFile(transformation_name, transformation)) {
    SetWorldToGridTransformation(transformation);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x1988, %rsp           # imm = 0x1988
movq %rdi, 0x1978(%rsp)
movq %rsi, 0x1970(%rsp)
movq 0x1978(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x1170(%rsp), %rdi
movq 0x1970(%rsp), %rsi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf260
leaq 0x1170(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x1168(%rsp)
cmpq $0x0, 0x1168(%rsp)
je 0x6568e
movq 0x1168(%rsp), %rax
movb $0x0, (%rax)
leaq 0x1170(%rsp), %rdi
leaq 0xcedd5(%rip), %rsi     # 0x134472
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf330
leaq 0x960(%rsp), %rdi
movq 0x1970(%rsp), %rsi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf260
leaq 0x960(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x1168(%rsp)
cmpq $0x0, 0x1168(%rsp)
je 0x656f1
movq 0x1168(%rsp), %rax
movb $0x0, (%rax)
leaq 0x960(%rsp), %rdi
leaq 0xced78(%rip), %rsi     # 0x134478
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf330
leaq 0x1170(%rsp), %rdi
leaq 0x160(%rsp), %r8
leaq 0x15c(%rsp), %rsi
leaq 0x158(%rsp), %rdx
leaq 0x154(%rsp), %rcx
callq 0x674b0
cmpl $0x0, %eax
jne 0x6574c
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
movl 0x15c(%rsp), %eax
imull 0x158(%rsp), %eax
imull 0x154(%rsp), %eax
movl %eax, 0x150(%rsp)
cmpl $0x0, 0x150(%rsp)
jne 0x6579a
leaq 0x1170(%rsp), %rsi
leaq 0xcecf9(%rip), %rdi     # 0x13447c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
movq 0x1970(%rsp), %rdi
leaq 0xcde30(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x148(%rsp)
cmpq $0x0, 0x148(%rsp)
jne 0x657e7
movq 0x1970(%rsp), %rsi
leaq 0xceccc(%rip), %rdi     # 0x13449c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
movq 0x20(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x6580a
movq 0x20(%rsp), %rcx
movl 0x150(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jle 0x65874
movq 0x20(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x6583c
movq 0x20(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x6583a
movq 0x18(%rsp), %rdi
callq 0xf670
jmp 0x6583c
movslq 0x150(%rsp), %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, 0x230(%rax)
movq 0x20(%rsp), %rax
movl 0x15c(%rsp), %ecx
movl %ecx, 0x238(%rax)
movl 0x158(%rsp), %ecx
movl %ecx, 0x23c(%rax)
movl 0x154(%rsp), %ecx
movl %ecx, 0x240(%rax)
movl 0x238(%rax), %ecx
movl %ecx, 0x244(%rax)
movl 0x244(%rax), %ecx
imull 0x23c(%rax), %ecx
movl %ecx, 0x248(%rax)
movl 0x248(%rax), %ecx
imull 0x240(%rax), %ecx
movl %ecx, 0x24c(%rax)
cmpl $0x0, 0x24c(%rax)
jg 0x65904
movq 0x20(%rsp), %rax
movl 0x24c(%rax), %esi
leaq 0xcea8c(%rip), %rdi     # 0x134379
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
leaq 0x160(%rsp), %rdi
leaq 0xceba4(%rip), %rsi     # 0x1344b7
callq 0xf700
cmpl $0x0, %eax
je 0x65936
leaq 0x160(%rsp), %rdi
leaq 0xceb94(%rip), %rsi     # 0x1344c0
callq 0xf700
cmpl $0x0, %eax
jne 0x6596f
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67540
cmpl $0x0, %eax
jne 0x6596a
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65ddc
leaq 0x160(%rsp), %rdi
leaq 0xceb4b(%rip), %rsi     # 0x1344c9
callq 0xf700
cmpl $0x0, %eax
je 0x659a1
leaq 0x160(%rsp), %rdi
leaq 0xceb3a(%rip), %rsi     # 0x1344d1
callq 0xf700
cmpl $0x0, %eax
jne 0x659da
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67540
cmpl $0x0, %eax
jne 0x659d5
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dda
leaq 0x160(%rsp), %rdi
leaq 0xce90c(%rip), %rsi     # 0x1342f5
callq 0xf700
cmpl $0x0, %eax
je 0x65a0c
leaq 0x160(%rsp), %rdi
leaq 0xcead7(%rip), %rsi     # 0x1344d9
callq 0xf700
cmpl $0x0, %eax
jne 0x65a45
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x675c0
cmpl $0x0, %eax
jne 0x65a40
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dd8
leaq 0x160(%rsp), %rdi
leaq 0xcea8d(%rip), %rsi     # 0x1344e1
callq 0xf700
cmpl $0x0, %eax
je 0x65a77
leaq 0x160(%rsp), %rdi
leaq 0xcea74(%rip), %rsi     # 0x1344e1
callq 0xf700
cmpl $0x0, %eax
jne 0x65ab0
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67650
cmpl $0x0, %eax
jne 0x65aab
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dd6
leaq 0x160(%rsp), %rdi
leaq 0xcea29(%rip), %rsi     # 0x1344e8
callq 0xf700
cmpl $0x0, %eax
je 0x65ae2
leaq 0x160(%rsp), %rdi
leaq 0xcea0a(%rip), %rsi     # 0x1344e2
callq 0xf700
cmpl $0x0, %eax
jne 0x65b1b
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x676f0
cmpl $0x0, %eax
jne 0x65b16
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dd4
leaq 0x160(%rsp), %rdi
leaq 0xce9c4(%rip), %rsi     # 0x1344ee
callq 0xf700
cmpl $0x0, %eax
je 0x65b4d
leaq 0x160(%rsp), %rdi
leaq 0xce9b2(%rip), %rsi     # 0x1344f5
callq 0xf700
cmpl $0x0, %eax
jne 0x65b86
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67770
cmpl $0x0, %eax
jne 0x65b81
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dd2
leaq 0x160(%rsp), %rdi
leaq 0xce967(%rip), %rsi     # 0x1344fc
callq 0xf700
cmpl $0x0, %eax
je 0x65bb8
leaq 0x160(%rsp), %rdi
leaq 0xce941(%rip), %rsi     # 0x1344ef
callq 0xf700
cmpl $0x0, %eax
jne 0x65bf1
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67800
cmpl $0x0, %eax
jne 0x65bec
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dd0
leaq 0x160(%rsp), %rdi
leaq 0xce902(%rip), %rsi     # 0x134502
callq 0xf700
cmpl $0x0, %eax
je 0x65c23
leaq 0x160(%rsp), %rdi
leaq 0xce8f0(%rip), %rsi     # 0x134509
callq 0xf700
cmpl $0x0, %eax
jne 0x65c5c
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67880
cmpl $0x0, %eax
jne 0x65c57
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dce
leaq 0x160(%rsp), %rdi
leaq 0xce8a5(%rip), %rsi     # 0x134510
callq 0xf700
cmpl $0x0, %eax
je 0x65c8e
leaq 0x160(%rsp), %rdi
leaq 0xce87f(%rip), %rsi     # 0x134503
callq 0xf700
cmpl $0x0, %eax
jne 0x65cc7
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67910
cmpl $0x0, %eax
jne 0x65cc2
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dcc
leaq 0x160(%rsp), %rdi
leaq 0xce840(%rip), %rsi     # 0x134516
callq 0xf700
cmpl $0x0, %eax
je 0x65cf9
leaq 0x160(%rsp), %rdi
leaq 0xce82e(%rip), %rsi     # 0x13451d
callq 0xf700
cmpl $0x0, %eax
jne 0x65d32
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x679a0
cmpl $0x0, %eax
jne 0x65d2d
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dca
leaq 0x160(%rsp), %rdi
leaq 0xce7e3(%rip), %rsi     # 0x134524
callq 0xf700
cmpl $0x0, %eax
je 0x65d64
leaq 0x160(%rsp), %rdi
leaq 0xce7bd(%rip), %rsi     # 0x134517
callq 0xf700
cmpl $0x0, %eax
jne 0x65d9a
movq 0x20(%rsp), %rax
movq 0x148(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x67a30
cmpl $0x0, %eax
jne 0x65d98
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dc8
leaq 0x160(%rsp), %rsi
leaq 0x1170(%rsp), %rdx
leaq 0xce779(%rip), %rdi     # 0x13452a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1984(%rsp)
jmp 0x65e5b
jmp 0x65dca
jmp 0x65dcc
jmp 0x65dce
jmp 0x65dd0
jmp 0x65dd2
jmp 0x65dd4
jmp 0x65dd6
jmp 0x65dd8
jmp 0x65dda
jmp 0x65ddc
movq 0x148(%rsp), %rdi
callq 0xf4b0
leaq 0x38(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x4d310
movq 0x8(%rsp), %rsi
leaq 0x960(%rsp), %rdi
callq 0x67ac0
movl %eax, 0x14(%rsp)
jmp 0x65e10
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x65e46
movq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x5e430
jmp 0x65e2a
jmp 0x65e46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x4d430
jmp 0x65e6a
movl $0x1, 0x1984(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x4d430
movl 0x1984(%rsp), %eax
addq $0x1988, %rsp           # imm = 0x1988
retq
movq 0x30(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)