// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadFile(char const*)
int R2Image::
ReadFile(const char *filename)
{
  // Initialize everything
  if (pixels) { delete [] pixels; pixels = NULL; }
  width = height = 0;

  // Parse image format
  int format = R2ImageFileFormat(filename);
  
  // Parse input filename extension
  const char *input_extension;
  if (!(input_extension = strrchr(filename, '.'))) {
    RNFail("Input file %s has no extension (e.g., .jpg).\n", filename);
    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';

  // Read file of appropriate type
  if (format == R2_IMAGE_JPEG_FORMAT) return ReadJPEGFile(filename);
  else if (format == R2_IMAGE_PNG_FORMAT) return ReadPNGFile(filename);
  else if (format == R2_IMAGE_TIFF_FORMAT) return ReadTIFFFile(filename);
  else if (format == R2_IMAGE_BMP_FORMAT) return ReadBMPFile(filename);
  else if (format == R2_IMAGE_RAW_FORMAT) return ReadRAWFile(filename);
  else if (!strncmp(extension, ".bmp", 4)) return ReadBMPFile(filename);
  else if (!strncmp(extension, ".ppm", 4)) return ReadPPMFile(filename);
  else if (!strncmp(extension, ".pgm", 4)) return ReadPPMFile(filename);
  else if (!strncmp(extension, ".pfm", 4)) return ReadPFMFile(filename);
  else if (!strncmp(extension, ".jpg", 4)) return ReadJPEGFile(filename);
  else if (!strncmp(extension, ".jpeg", 5)) return ReadJPEGFile(filename);
  else if (!strncmp(extension, ".tif", 4)) return ReadTIFFFile(filename);
  else if (!strncmp(extension, ".tiff", 5)) return ReadTIFFFile(filename);
  else if (!strncmp(extension, ".png", 4)) return ReadPNGFile(filename);
  else if (!strncmp(extension, ".raw", 4)) return ReadRAWFile(filename);
  else if (!strncmp(extension, ".grd", 4)) return ReadGRDFile(filename);
  
  // Should never get here
  RNFail("Unrecognized image file extension");
  return 0;
}

// Assembly:
subq $0x448, %rsp            # imm = 0x448
movq %rdi, 0x438(%rsp)
movq %rsi, 0x430(%rsp)
movq 0x438(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rax)
je 0xc4ba6
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xc4b99
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0x10(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x4(%rax)
movl $0x0, (%rax)
movq 0x430(%rsp), %rdi
callq 0xc66d0
movl %eax, 0x42c(%rsp)
movq 0x430(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x420(%rsp)
cmpq $0x0, %rax
jne 0xc4c12
movq 0x430(%rsp), %rsi
leaq 0x717f1(%rip), %rdi     # 0x1363ec
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x444(%rsp)
jmp 0xc4feb
movq 0x420(%rsp), %rdi
callq 0xf200
movl %eax, 0x1c(%rsp)
cmpl $0x3ff, 0x1c(%rsp)      # imm = 0x3FF
jle 0xc4c35
movl $0x3ff, 0x1c(%rsp)      # imm = 0x3FF
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0xc4c75
movq 0x420(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xf750
movb %al, %cl
movslq 0x18(%rsp), %rax
movb %cl, 0x20(%rsp,%rax)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xc4c3d
movslq 0x1c(%rsp), %rax
movb $0x0, 0x20(%rsp,%rax)
cmpl $0x1, 0x42c(%rsp)
jne 0xc4ca7
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6890
movl %eax, 0x444(%rsp)
jmp 0xc4feb
cmpl $0x2, 0x42c(%rsp)
jne 0xc4ccf
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6930
movl %eax, 0x444(%rsp)
jmp 0xc4feb
cmpl $0x4, 0x42c(%rsp)
jne 0xc4cf7
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc69d0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
cmpl $0x3, 0x42c(%rsp)
jne 0xc4d1f
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6a00
movl %eax, 0x444(%rsp)
jmp 0xc4feb
cmpl $0x5, 0x42c(%rsp)
jne 0xc4d47
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6fb0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x716c7(%rip), %rsi     # 0x13641a
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4d80
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6a00
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x71693(%rip), %rsi     # 0x13641f
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4db9
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc7310
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x7165f(%rip), %rsi     # 0x136424
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4df2
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc7310
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x70ecc(%rip), %rsi     # 0x135cca
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4e2b
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc7b00
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x715f2(%rip), %rsi     # 0x136429
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4e64
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6890
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x715be(%rip), %rsi     # 0x13642e
movl $0x5, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4e9d
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6890
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x7158b(%rip), %rsi     # 0x136434
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4ed6
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc69d0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x71557(%rip), %rsi     # 0x136439
movl $0x5, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4f0f
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc69d0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x70db9(%rip), %rsi     # 0x135cd4
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4f48
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6930
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x6f39c(%rip), %rsi     # 0x1342f0
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4f7e
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc6fb0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
leaq 0x20(%rsp), %rdi
leaq 0x6f349(%rip), %rsi     # 0x1342d3
movl $0x4, %edx
callq 0xf230
cmpl $0x0, %eax
jne 0xc4fb4
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0xc85b0
movl %eax, 0x444(%rsp)
jmp 0xc4feb
jmp 0xc4fb6
jmp 0xc4fb8
jmp 0xc4fba
jmp 0xc4fbc
jmp 0xc4fbe
jmp 0xc4fc0
jmp 0xc4fc2
jmp 0xc4fc4
jmp 0xc4fc6
jmp 0xc4fc8
jmp 0xc4fca
jmp 0xc4fcc
jmp 0xc4fce
jmp 0xc4fd0
jmp 0xc4fd2
leaq 0x71466(%rip), %rdi     # 0x13643f
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x444(%rsp)
movl 0x444(%rsp), %eax
addq $0x448, %rsp            # imm = 0x448
retq
nopw (%rax,%rax)