// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::WriteFile(char const*) const
int R2Image::
WriteFile(const char *filename) const
{
  // Parse input filename extension
  const char *input_extension;
  if (!(input_extension = strrchr(filename, '.'))) {
    RNFail("Output file has no extension (e.g., .jpg).\n");
    return 0;
  }
  
  // Make lower-case copy of extension
  char extension[1024];
  int len = strlen(input_extension);
  if (len > 1023) len = 1023;
  for (int i = 0; i < len; i++) 
    extension[i] = tolower(input_extension[i]);
  extension[len] = '\0';

  // Write file of appropriate type
  if (!strncmp(extension, ".bmp", 4)) return WriteBMPFile(filename);
  else if (!strncmp(extension, ".pgm", 4)) return WritePPMFile(filename, 0);
  else if (!strncmp(extension, ".ppm", 4)) return WritePPMFile(filename, 0);
  else if (!strncmp(extension, ".jpg", 4)) return WriteJPEGFile(filename);
  else if (!strncmp(extension, ".jpeg", 5)) return WriteJPEGFile(filename);
  else if (!strncmp(extension, ".tif", 4)) return WriteTIFFFile(filename);
  else if (!strncmp(extension, ".tiff", 5)) return WriteTIFFFile(filename);
  else if (!strncmp(extension, ".png", 4)) return WritePNGFile(filename);
  else if (!strncmp(extension, ".raw", 4)) return WriteRAWFile(filename);

  // Should never get here
  RNFail("Unrecognized image file extension");
  return 0;
}

// Assembly:
subq $0x438, %rsp            # imm = 0x438
movq %rdi, 0x428(%rsp)
movq %rsi, 0x420(%rsp)
movq 0x428(%rsp), %rax
movq %rax, (%rsp)
movq 0x420(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x418(%rsp)
cmpq $0x0, %rax
jne 0xc8c81
leaq 0x6d7f7(%rip), %rdi     # 0x136461
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x434(%rsp)
jmp 0xc8f0d
movq 0x418(%rsp), %rdi
callq 0xf200
movl %eax, 0xc(%rsp)
cmpl $0x3ff, 0xc(%rsp)       # imm = 0x3FF
jle 0xc8ca4
movl $0x3ff, 0xc(%rsp)       # imm = 0x3FF
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0xc(%rsp), %eax
jge 0xc8ce4
movq 0x418(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xf750
movb %al, %cl
movslq 0x8(%rsp), %rax
movb %cl, 0x10(%rsp,%rax)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xc8cac
movslq 0xc(%rsp), %rax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %rdi
leaq 0x6d720(%rip), %rsi     # 0x13641a
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8d26
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc8f20
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d6f2(%rip), %rsi     # 0x136424
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8d60
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
xorl %edx, %edx
callq 0xc9230
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d6b3(%rip), %rsi     # 0x13641f
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8d9a
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
xorl %edx, %edx
callq 0xc9230
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d683(%rip), %rsi     # 0x136429
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8dd2
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc96d0
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d650(%rip), %rsi     # 0x13642e
movl $0x5, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8e0a
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc96d0
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d61e(%rip), %rsi     # 0x136434
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8e42
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc9770
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6d5eb(%rip), %rsi     # 0x136439
movl $0x5, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8e7a
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc9770
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6ce4e(%rip), %rsi     # 0x135cd4
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8eaf
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc97a0
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
leaq 0x10(%rsp), %rdi
leaq 0x6b435(%rip), %rsi     # 0x1342f0
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc8ee4
movq (%rsp), %rdi
movq 0x420(%rsp), %rsi
callq 0xc9840
movl %eax, 0x434(%rsp)
jmp 0xc8f0d
jmp 0xc8ee6
jmp 0xc8ee8
jmp 0xc8eea
jmp 0xc8eec
jmp 0xc8eee
jmp 0xc8ef0
jmp 0xc8ef2
jmp 0xc8ef4
leaq 0x6d544(%rip), %rdi     # 0x13643f
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x434(%rsp)
movl 0x434(%rsp), %eax
addq $0x438, %rsp            # imm = 0x438
retq
nopl (%rax)