// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadCCP4File(char const*)
int R3Grid::
ReadCCP4File(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open CCP4 file %s\n", filename);
    return 0;
  }

  // Read header
  R3GridCCP4Header header;
  if (fread(&header, sizeof(R3GridCCP4Header), 1, fp) != 1) {
    RNFail("Unable to read header from CCP4 file: %s", filename);
    return 0;
  }

  // Check dimensions
  if ((header.nc <= 0) || (header.nr <= 0) || (header.ns <= 0)) {
    RNFail("Invalid dimensions (%d %d %d) in CCP4 file: %s", header.nc, header.nr, header.ns, filename);
    return 0;
  }


  // Check mode
  if (header.mode != 2) { // Reals
    RNFail("Invalid mode (%d rather than 2) in CCP4 file: %s", header.mode, filename);
    return 0;
  }

  // Check symmetry information
  if (header.nsymbt < 0) {
    RNFail("Invalid number of symmetry bytes (%d) in CCP4 file: %s", header.nsymbt, filename);
    return 0;
  }

#if 0
  // Read symmetry information
  for (int i = 0; i < header.nsymbt; i++) {
    char c;
    if (fread(&c, sizeof(char), 1, fp) != 1) {
      RNFail("Unable to read symmetry info from CCP4 file: %s", filename);
      return 0;
    }
  }
#endif

  // Re-allocate grid values
  int new_size = header.nc * header.nr * header.ns;
  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] = (header.mapc == 1) ? header.nc : ((header.mapr == 1) ? header.nr : header.ns);
  grid_resolution[1] = (header.mapc == 2) ? header.nc : ((header.mapr == 2) ? header.nr : header.ns);
  grid_resolution[2] = (header.mapc == 3) ? header.nc : ((header.mapr == 3) ? header.nr : header.ns);
  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 map 
  int x = 0;
  int y = 0;
  int z = 0;
  for (int k = 0; k < header.ns; k++) {
    if (header.maps == 1) x = k;
    else if (header.maps == 2) y = k;
    else if (header.maps == 3) z = k;
    for (int j = 0; j < header.nr; j++) {
      if (header.mapr == 1) x = j;
      else if (header.mapr == 2) y = j;
      else if (header.mapr == 3) z = j;
      for (int i = 0; i < header.nc; i++) {
        if (header.mapc == 1) x = i;
        else if (header.mapc == 2) y = i;
        else if (header.mapc == 3) z = i;

        // Read float
        float value;
        if (fread(&value, sizeof(float), 1, fp) != 1) {
          RNFail("Unable to read map from CCP4 file: %s", filename);
          return 0;
        }

        // Store in grid as double
        SetGridValue(x, y, z, value);
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6b336(%rip), %rsi     # 0xa25ae
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x372e7
movq %rax, %r12
leaq 0x38(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x372f0
movl 0x38(%rsp), %edx
movl 0x3c(%rsp), %ebp
testl %edx, %edx
setle %al
testl %ebp, %ebp
setle %cl
orb %al, %cl
movl 0x40(%rsp), %r15d
testl %r15d, %r15d
setle %al
orb %cl, %al
cmpb $0x1, %al
jne 0x37319
leaq 0x70b29(%rip), %rdi     # 0xa7dfa
xorl %r14d, %r14d
movl %edx, %esi
movl %ebp, %edx
movl %r15d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x72a1e
jmp 0x37304
leaq 0x70ac6(%rip), %rdi     # 0xa7db4
jmp 0x372f7
leaq 0x70ada(%rip), %rdi     # 0xa7dd1
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %r14d, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x44(%rsp), %esi
cmpl $0x2, %esi
jne 0x375dd
movl 0x94(%rsp), %esi
testl %esi, %esi
js 0x375e6
movq %rbx, 0x30(%rsp)
movl %ebp, %ebx
imull %edx, %ebx
imull %r15d, %ebx
movq 0x230(%r14), %rdi
testq %rdi, %rdi
movl %edx, 0x24(%rsp)
je 0x3735c
cmpl 0x24c(%r14), %ebx
jle 0x3737c
testq %rdi, %rdi
je 0x37366
callq 0xe5b0
movl %ebx, %edi
shlq $0x3, %rdi
callq 0xe030
movq %rax, 0x230(%r14)
movl 0x24(%rsp), %edx
cmpl $0x1, 0x7c(%rsp)
movl %r15d, %eax
cmovel %ebp, %eax
movl 0x78(%rsp), %ebx
cmpl $0x1, %ebx
cmovel %edx, %eax
movl %eax, 0x238(%r14)
cmpl $0x2, 0x7c(%rsp)
movl %r15d, %esi
cmovel %ebp, %esi
cmpl $0x2, %ebx
cmovel %edx, %esi
movl %esi, 0x23c(%r14)
cmpl $0x3, 0x7c(%rsp)
movl %r15d, %ecx
cmovel %ebp, %ecx
cmpl $0x3, %ebx
cmovel %edx, %ecx
movl %ecx, 0x240(%r14)
movl %eax, 0x244(%r14)
imull %eax, %esi
movl %esi, 0x248(%r14)
imull %ecx, %esi
movl %esi, 0x24c(%r14)
testl %esi, %esi
jle 0x375ff
testl %r15d, %r15d
jle 0x375ca
movl 0x7c(%rsp), %eax
movl 0x80(%rsp), %ecx
testl %ebp, %ebp
setg 0x1(%rsp)
testl %edx, %edx
setg 0x2(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movl %ebp, 0x20(%rsp)
movl %r15d, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
cmpl $0x1, %ecx
je 0x37461
cmpl $0x3, %ecx
je 0x37457
cmpl $0x2, %ecx
jne 0x37469
movl 0x4(%rsp), %esi
movl %esi, 0xc(%rsp)
jmp 0x37469
movl 0x4(%rsp), %esi
movl %esi, 0x8(%rsp)
jmp 0x37469
movl 0x4(%rsp), %esi
movl %esi, 0x10(%rsp)
testl %ebp, %ebp
jle 0x37594
xorl %edi, %edi
movb 0x1(%rsp), %sil
cmpl $0x1, %eax
je 0x37493
cmpl $0x3, %eax
je 0x3748d
cmpl $0x2, %eax
jne 0x37497
movl %edi, 0xc(%rsp)
jmp 0x37497
movl %edi, 0x8(%rsp)
jmp 0x37497
movl %edi, 0x10(%rsp)
movl %edi, 0x28(%rsp)
movb %sil, 0x3(%rsp)
testl %edx, %edx
jle 0x3755f
xorl %r13d, %r13d
movb 0x2(%rsp), %r15b
cmpl $0x3, %ebx
je 0x374cd
cmpl $0x2, %ebx
je 0x374c6
cmpl $0x1, %ebx
jne 0x374d2
movl %r13d, 0x10(%rsp)
jmp 0x374d2
movl %r13d, 0xc(%rsp)
jmp 0x374d2
movl %r13d, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x2c(%rsp), %rdi
movq %r12, %rbp
movq %r12, %rcx
callq 0xe6c0
movq %rax, %r12
cmpq $0x1, %rax
jne 0x3752d
xorps %xmm0, %xmm0
cvtss2sd 0x2c(%rsp), %xmm0
movq 0x230(%r14), %rax
movl 0x248(%r14), %ecx
imull 0x8(%rsp), %ecx
movl 0x244(%r14), %edx
imull 0xc(%rsp), %edx
addl 0x10(%rsp), %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x37540
leaq 0x7095d(%rip), %rdi     # 0xa7e91
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %r12
movl 0x24(%rsp), %edx
jne 0x37566
incl %r13d
cmpl %edx, %r13d
setl %r15b
movq %rbp, %r12
jne 0x374b0
jmp 0x37569
movb 0x2(%rsp), %r15b
jmp 0x37569
movq %rbp, %r12
testb $0x1, %r15b
jne 0x3759b
movl 0x28(%rsp), %edi
incl %edi
movl 0x20(%rsp), %ebp
cmpl %ebp, %edi
setl %sil
movl 0x1c(%rsp), %r15d
movl 0x18(%rsp), %eax
movl 0x14(%rsp), %ecx
jne 0x37478
jmp 0x375b1
movb 0x1(%rsp), %sil
jmp 0x375b1
movl 0x20(%rsp), %ebp
movl 0x1c(%rsp), %r15d
movl 0x18(%rsp), %eax
movl 0x14(%rsp), %ecx
movb 0x3(%rsp), %sil
testb $0x1, %sil
jne 0x37615
movl 0x4(%rsp), %esi
incl %esi
movl %esi, 0x4(%rsp)
cmpl %r15d, %esi
jne 0x3743e
movq %r12, %rdi
callq 0xe430
movl $0x1, %r14d
jmp 0x37304
leaq 0x70845(%rip), %rdi     # 0xa7e29
jmp 0x375ed
leaq 0x7086d(%rip), %rdi     # 0xa7e5a
xorl %r14d, %r14d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x37304
leaq 0x7023d(%rip), %rdi     # 0xa7843
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x72a1e
jmp 0x37304
xorl %r14d, %r14d
jmp 0x37304
nop