// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x3895a(%rip), %rsi     # 0xa25ab
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x69dff
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x4, %rax
jne 0x69e08
cmpl $0xd431, 0x10(%rsp)     # imm = 0xD431
jne 0x69e11
movq %rbx, 0x8(%rsp)
movl 0x14(%rsp), %r13d
movslq %r13d, %rbx
movl %ebx, (%r14)
movl 0x18(%rsp), %edx
movl %edx, 0x4(%r14)
movl 0x1c(%rsp), %r12d
movl %r12d, 0x8(%r14)
movl %r12d, %eax
imull %ebx, %eax
testb $0x3, %al
movq %r15, 0x28(%rsp)
je 0x69cd4
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
addl $0x4, %ecx
movl %ecx, %eax
movl %eax, 0xc(%r14)
imull %edx, %eax
movslq %eax, %rdi
movl %edx, %ebp
callq 0xe030
movq %rax, 0x10(%r14)
leaq (,%rbx,4), %rax
testl %ebx, %ebx
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %rdi
testl %r12d, %r12d
jle 0x69dde
movl %ebp, %ecx
movl %r12d, %eax
xorl %ebp, %ebp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %rdi, 0x30(%rsp)
movl %r12d, %r12d
testl %ecx, %ecx
jle 0x69dcc
xorl %esi, %esi
movl %esi, 0x4(%rsp)
movslq %r13d, %rdx
movl $0x4, %esi
movq 0x8(%rsp), %rcx
callq 0xe6c0
movl (%r14), %r13d
cmpq %r13, %rax
jne 0x69e35
testl %r13d, %r13d
movq 0x30(%rsp), %rdi
movss 0x3fde5(%rip), %xmm1    # 0xa9b50
movl 0x4(%rsp), %esi
jle 0x69dba
movslq 0xc(%r14), %rax
movslq %esi, %rcx
imulq %rax, %rcx
addq 0x10(%r14), %rcx
xorl %eax, %eax
testl %ebp, %ebp
leaq 0x1(%rcx,%r12), %rdx
cmoveq %rcx, %rdx
movss (%rdi,%rax,4), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movb %cl, (%rdx)
movl 0x8(%r14), %ecx
cmpl %r15d, %ecx
cmovlel %r15d, %ecx
addl %ebx, %ecx
addq %rdx, %rcx
incq %rcx
incq %rax
movslq (%r14), %r13
cmpq %r13, %rax
jl 0x69d82
incl %esi
movl 0x4(%r14), %ecx
cmpl %ecx, %esi
jl 0x69d39
movl 0x8(%r14), %eax
incl %ebp
incl %r12d
decl %ebx
incl %r15d
cmpl %eax, %ebp
jl 0x69d2c
callq 0xe5b0
movq 0x8(%rsp), %rdi
callq 0xe430
movl (%r14), %ebp
imull 0x4(%r14), %ebp
imull 0x8(%r14), %ebp
shll $0x2, %ebp
jmp 0x69e24
leaq 0x3f61f(%rip), %rdi     # 0xa9425
jmp 0x69e18
leaq 0x40012(%rip), %rdi     # 0xa9e21
jmp 0x69e18
leaq 0x3f654(%rip), %rdi     # 0xa946c
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40010(%rip), %rdi     # 0xa9e4c
xorl %ebp, %ebp
movq 0x28(%rsp), %rsi
jmp 0x69e1d
nop