// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadRAWFile(char const*)
int R2Image::
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 read header to raw image file %s", filename);
    return 0;
  }

  // Check magic number
  static const unsigned int magic = 54321;
  if (header[0] != magic) {
    RNFail("Invalid header in raw image file %s", filename);
    return 0;
  }

  // Parse header
  width = header[1];
  height = header[2];
  ncomponents = header[3];
  rowsize = ncomponents * width;
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;

  // Allocate image pixels
  int nbytes = rowsize * height;
  pixels = new unsigned char [nbytes];
  if (!pixels) {
    RNFail("Unable to allocate memory for RAW file");
    return 0;
  }

  // Allocate buffer
  float *buf = new float [ width ];
  if (!buf) {
    RNFail("Unable to allocate buffer for raw image file %s", filename);
    return 0;
  }

  // Read data for each component
  for (int i = 0; i < ncomponents; i++) {
    for (int j = 0; j < height; j++) {
      // Read buffer for one row
      if (fread(buf, sizeof(float), width, fp) != (unsigned int) width) {
        RNFail("Unable to read data from raw image file %s", filename);
        return 0;
      }

      // Parse row
      unsigned char *pixelsp = (unsigned char *) Pixels(j);
      for (int k = 0; k < width; k++) {
        for (int c = 0; c < i; c++) pixelsp++;
        *(pixelsp++) = (int) (buf[k] * 255);
        for (int c = i+1; c < ncomponents; c++) pixelsp++;
      }
    }

  }

  // Free buffer
  delete [] buf;

  // Close file
  fclose(fp);

  // Return number of bytes read
  return width * height * ncomponents * sizeof(float);  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x3258a(%rip), %rsi     # 0x9355b
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x6112a
movq %rax, %r15
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rdx
movq %rdx, %rsi
movq %rax, %rcx
callq 0xe6f0
cmpq $0x4, %rax
jne 0x61133
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x6113c
movq %rbx, 0x20(%rsp)
movl 0x14(%rsp), %ebp
movslq %ebp, %rbx
movl %ebx, (%r14)
movl 0x18(%rsp), %esi
movl %esi, 0x4(%r14)
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%r14)
movl %eax, 0x4(%rsp)
imull %ebx, %eax
testb $0x3, %al
movq %r15, 0x8(%rsp)
je 0x6104c
pushq $0x4
popq %rcx
cltd
idivl %ecx
leal 0x4(,%rax,4), %eax
movl %eax, 0xc(%r14)
movl %esi, (%rsp)
imull %esi, %eax
movslq %eax, %rdi
callq 0xe030
movq %rax, 0x10(%r14)
leaq (,%rbx,4), %rax
testl %ebx, %ebx
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
pushq $0x1
popq %rbx
pushq $-0x1
popq %r13
xorl %r15d, %r15d
movl 0x4(%rsp), %eax
cmpl %r15d, %eax
jle 0x61170
xorl %esi, %esi
movl (%rsp), %eax
cmpl %eax, %esi
jge 0x61115
movl %esi, (%rsp)
movslq %ebp, %rdx
movq %r12, %rdi
pushq $0x4
popq %rsi
movq 0x8(%rsp), %rcx
callq 0xe6f0
movl (%r14), %ebp
cmpq %rbp, %rax
jne 0x61160
movslq 0xc(%r14), %rcx
movl (%rsp), %esi
movslq %esi, %rax
imulq %rcx, %rax
addq 0x10(%r14), %rax
xorl %ecx, %ecx
movss 0x399f7(%rip), %xmm1    # 0x9aad0
movslq %ebp, %rdx
cmpq %rdx, %rcx
jge 0x6110d
movss (%r12,%rcx,4), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
movb %dl, (%rax,%r15)
movl (%r14), %ebp
movl 0x8(%r14), %edx
cmpl %ebx, %edx
cmovlel %ebx, %edx
addl %r13d, %edx
addq %rbx, %rax
addq %rdx, %rax
incq %rcx
jmp 0x610d9
incl %esi
movl 0x4(%r14), %eax
jmp 0x61097
movl %eax, (%rsp)
incq %r15
incq %rbx
decl %r13d
movl 0x8(%r14), %eax
jmp 0x61089
leaq 0x39284(%rip), %rdi     # 0x9a3b5
jmp 0x61143
leaq 0x39c67(%rip), %rdi     # 0x9ada1
jmp 0x61143
leaq 0x392b9(%rip), %rdi     # 0x9a3fc
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39c65(%rip), %rdi     # 0x9adcc
xorl %ebp, %ebp
movq 0x20(%rsp), %rsi
jmp 0x61148
movq %r12, %rdi
callq 0xe5d0
movq 0x8(%rsp), %rdi
callq 0xe440
movl (%r14), %ebp
imull 0x4(%r14), %ebp
imull 0x8(%r14), %ebp
shll $0x2, %ebp
jmp 0x6114f