// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x6c605(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xc7006
movq 0x60(%rsp), %rsi
leaq 0x6ed13(%rip), %rdi     # 0x135d05
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
leaq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rcx
movl $0x4, %edx
movq %rdx, %rsi
callq 0xf780
cmpq $0x4, %rax
je 0xc7043
movq 0x60(%rsp), %rsi
leaq 0x6f6f2(%rip), %rdi     # 0x136721
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
cmpl $0xd431, 0x40(%rsp)     # imm = 0xD431
je 0xc706d
movq 0x60(%rsp), %rsi
leaq 0x6ecf3(%rip), %rdi     # 0x135d4c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
movq 0x8(%rsp), %rax
movl 0x44(%rsp), %ecx
movl %ecx, (%rax)
movl 0x48(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl 0x4c(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0x8(%rax), %ecx
imull (%rax), %ecx
movl %ecx, 0xc(%rax)
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xc70bf
movq 0x8(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0x8(%rsp), %rax
addl $0x1, %ecx
shll $0x2, %ecx
movl %ecx, 0xc(%rax)
movq 0x8(%rsp), %rcx
movl 0xc(%rcx), %eax
imull 0x4(%rcx), %eax
movl %eax, 0x3c(%rsp)
movslq 0x3c(%rsp), %rdi
callq 0xf030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, 0x10(%rax)
jne 0xc7107
leaq 0x6f659(%rip), %rdi     # 0x13674c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
movq 0x8(%rsp), %rax
movslq (%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0xc715d
movq 0x60(%rsp), %rsi
leaq 0x6f62a(%rip), %rdi     # 0x136773
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
movl $0x0, 0x2c(%rsp)
movq 0x8(%rsp), %rcx
movl 0x2c(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0xc72cc
movl $0x0, 0x28(%rsp)
movq 0x8(%rsp), %rcx
movl 0x28(%rsp), %eax
cmpl 0x4(%rcx), %eax
jge 0xc72ba
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rdi
movslq (%rax), %rdx
movq 0x58(%rsp), %rcx
movl $0x4, %esi
callq 0xf780
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
je 0xc71d9
movq 0x60(%rsp), %rsi
leaq 0x6f5de(%rip), %rdi     # 0x1367a3
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc7307
movq 0x8(%rsp), %rdi
movl 0x28(%rsp), %esi
callq 0xca8c0
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x8(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl (%rcx), %eax
jge 0xc72a8
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xc7232
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xc720d
movq 0x30(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movss 0x6f160(%rip), %xmm0    # 0x1363a4
mulss (%rax,%rcx,4), %xmm0
cvttss2si %xmm0, %eax
movb %al, %cl
movq 0x20(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x20(%rsp)
movb %cl, (%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0xc7296
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xc726d
jmp 0xc7298
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xc71f4
jmp 0xc72aa
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xc717f
jmp 0xc72bc
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xc7165
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xc72e4
movq (%rsp), %rdi
callq 0xf670
movq 0x58(%rsp), %rdi
callq 0xf4b0
movq 0x8(%rsp), %rcx
movl (%rcx), %eax
imull 0x4(%rcx), %eax
imull 0x8(%rcx), %eax
cltq
shlq $0x2, %rax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq