// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %rbx
leaq 0x37bc3(%rip), %rsi     # 0xa25ae
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x6ab6d
movq %rax, %r12
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x4, %rax
jne 0x6ab76
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x6ab7f
movslq 0x14(%rsp), %r14
movl %r14d, (%rbx)
movl 0x18(%rsp), %eax
movl %eax, 0x4(%rbx)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x8(%rbx)
imull %r14d, %ecx
testb $0x3, %cl
movq %r15, 0x20(%rsp)
movq %r12, 0x8(%rsp)
je 0x6aa67
leal 0x3(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
andl $-0x4, %edx
addl $0x4, %edx
movl %edx, %ecx
movl %ecx, 0xc(%rbx)
imull %ecx, %eax
movslq %eax, %rdi
callq 0xe030
movq %rax, 0x10(%rbx)
movq %r14, %rax
shlq $0x2, %rax
testl %r14d, %r14d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
cmpl $0x0, 0x8(%rbx)
setg %al
jle 0x6abbf
xorl %ebp, %ebp
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x1, %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movb %al, 0x3(%rsp)
movl %r15d, %r15d
cmpl $0x0, 0x4(%rbx)
jle 0x6ab54
xorl %esi, %esi
movl %esi, 0x4(%rsp)
movslq (%rbx), %rdx
movl $0x4, %esi
movq %r12, %rdi
movq 0x8(%rsp), %rcx
callq 0xe6c0
movl (%rbx), %ecx
cmpq %rcx, %rax
jne 0x6aba3
cmpl $0x0, (%rbx)
movss 0x3f037(%rip), %xmm1    # 0xa9b30
movl 0x4(%rsp), %esi
jle 0x6ab49
movslq 0xc(%rbx), %rax
movslq %esi, %rcx
imulq %rax, %rcx
addq 0x10(%rbx), %rcx
xorl %eax, %eax
testl %ebp, %ebp
leaq 0x1(%rcx,%r15), %rdx
cmoveq %rcx, %rdx
movss (%r12,%rax,4), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movb %cl, (%rdx)
movl 0x8(%rbx), %ecx
cmpl %r14d, %ecx
cmovlel %r14d, %ecx
addl %r13d, %ecx
addq %rdx, %rcx
incq %rcx
incq %rax
movslq (%rbx), %rdx
cmpq %rdx, %rax
jl 0x6ab10
incl %esi
cmpl 0x4(%rbx), %esi
jl 0x6aaca
incl %ebp
incl %r15d
decl %r13d
incl %r14d
cmpl 0x8(%rbx), %ebp
setl %al
jl 0x6aab7
jmp 0x6abbf
leaq 0x3e8b1(%rip), %rdi     # 0xa9425
jmp 0x6ab86
leaq 0x3f284(%rip), %rdi     # 0xa9e01
jmp 0x6ab86
leaq 0x3e8e6(%rip), %rdi     # 0xa946c
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f282(%rip), %rdi     # 0xa9e2c
xorl %ebp, %ebp
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x72a1e
testb $0x1, 0x3(%rsp)
jne 0x6ab92
movq %r12, %rdi
callq 0xe5b0
movq 0x8(%rsp), %rdi
callq 0xe430
movl (%rbx), %ebp
imull 0x4(%rbx), %ebp
imull 0x8(%rbx), %ebp
shll $0x2, %ebp
jmp 0x6ab92