// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::WriteRAWFile(char const*) const
int R2Image::
WriteRAWFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    RNFail("Unable to open raw image file %s", filename);
    return 0;
  }

  // Write header
  static const unsigned int magic = 54321;
  unsigned int header[4] = { magic, (unsigned int) width, (unsigned int) height, (unsigned int) ncomponents };
  if (fwrite(header, sizeof(unsigned int), 4, fp) != 4) {
    RNFail("Unable to write header to raw image file %s", filename);
    return 0;
  }

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

  // Write data for each component
  for (int i = 0; i < ncomponents; i++) {
    for (int j = 0; j < height; j++) {
      // Load buffer
      const unsigned char *pixelsp = Pixels(j);
      for (int k = 0; k < width; k++) {
        for (int c = 0; c < i; c++) pixelsp++;
        buf[k] = *(pixelsp++) / 255.0F;
        for (int c = i+1; c < ncomponents; c++) pixelsp++;
      }

      // Write buffer
      if (fwrite(buf, sizeof(float), width, fp) != (unsigned int) width) {
        RNFail("Unable to write data to raw image file %s", filename);
        return 0;
      }
    }
  }

  // Free buffer
  delete [] buf;

  // Close file
  fclose(fp);

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

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x6a256(%rip), %rsi     # 0x133aba
callq 0xf5e0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xc9896
movq 0x60(%rsp), %rsi
leaq 0x6c483(%rip), %rdi     # 0x135d05
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc9af6
movq 0x10(%rsp), %rax
movl $0xd431, 0x40(%rsp)     # imm = 0xD431
movl (%rax), %ecx
movl %ecx, 0x44(%rsp)
movl 0x4(%rax), %ecx
movl %ecx, 0x48(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x4c(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rcx
movl $0x4, %edx
movq %rdx, %rsi
callq 0xf980
cmpq $0x4, %rax
je 0xc98f4
movq 0x60(%rsp), %rsi
leaq 0x6ceee(%rip), %rdi     # 0x1367ce
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc9af6
movq 0x10(%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, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xc994a
movq 0x60(%rsp), %rsi
leaq 0x6ce3d(%rip), %rdi     # 0x136773
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc9af6
movl $0x0, 0x34(%rsp)
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0xc9ab9
movl $0x0, 0x30(%rsp)
movq 0x10(%rsp), %rcx
movl 0x30(%rsp), %eax
cmpl 0x4(%rcx), %eax
jge 0xc9aa7
movq 0x10(%rsp), %rdi
movl 0x30(%rsp), %esi
callq 0xca8c0
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl (%rcx), %eax
jge 0xc9a50
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0xc99d7
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0xc99b2
movq 0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rsp)
movzbl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x6c9ad(%rip), %xmm1    # 0x1363a4
divss %xmm1, %xmm0
movq 0x38(%rsp), %rax
movslq 0x24(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0xc9a3e
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xc9a15
jmp 0xc9a40
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xc9999
movq 0x10(%rsp), %rax
movq 0x38(%rsp), %rdi
movslq (%rax), %rdx
movq 0x58(%rsp), %rcx
movl $0x4, %esi
callq 0xf980
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
je 0xc9a95
movq 0x60(%rsp), %rsi
leaq 0x6cd76(%rip), %rdi     # 0x1367fa
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0xc9af6
jmp 0xc9a97
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xc996c
jmp 0xc9aa9
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0xc9952
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xc9ad3
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0x58(%rsp), %rdi
callq 0xf4b0
movq 0x10(%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
nop