// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %rbx
movq %rdi, %r14
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
je 0x5fe5a
callq 0xe5d0
andq $0x0, 0x10(%r14)
andq $0x0, (%r14)
leaq 0x336f6(%rip), %rsi     # 0x9355b
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x5fec2
movq %rax, %r15
pxor %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movdqa %xmm0, (%rdi)
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %rax, %rcx
callq 0xe6f0
testl %eax, %eax
je 0x5fec2
cmpl $0x2, %eax
setge %sil
movb 0x10(%rsp), %cl
movb 0x11(%rsp), %dl
movl %ecx, %edi
notb %dil
movl %edx, %r8d
xorb $-0x28, %r8b
orb %dil, %r8b
sete %dil
testb %sil, %dil
je 0x5feca
pushq $0x1
jmp 0x5ff44
xorl %r12d, %r12d
jmp 0x5ff4e
movd 0x12(%rsp), %xmm0
movd %xmm0, %esi
cmpl $0xa0d474e, %esi        # imm = 0xA0D474E
jne 0x5fefd
cmpb $-0x77, %cl
jne 0x5fefd
cmpb $0x50, %dl
jne 0x5fefd
cmpb $0x1a, 0x16(%rsp)
jne 0x5fefd
cmpl $0x8, %eax
jl 0x5fefd
cmpb $0xa, 0x17(%rsp)
jne 0x5fefd
pushq $0x2
jmp 0x5ff44
cmpl $0x4, %eax
jl 0x5ff1e
cmpb $0x49, %cl
jne 0x5ff1e
cmpb $0x49, %dl
jne 0x5ff1e
cmpb $0x2a, %sil
jne 0x5ff1e
shrl $0x8, %esi
testb %sil, %sil
jne 0x5ff1e
pushq $0x4
jmp 0x5ff44
cmpl $0x2, %eax
setge %sil
movl %ecx, %edi
xorb $0x42, %dil
movl %edx, %r8d
xorb $0x4d, %r8b
orb %dil, %r8b
sete %dil
testb %sil, %dil
je 0x6003c
pushq $0x3
popq %r12
movq %r15, %rdi
callq 0xe440
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0xe4e0
testq %rax, %rax
je 0x5ffd4
movq %rax, %r15
movq %r14, 0x8(%rsp)
movq %rbx, %r14
movq %rax, %rdi
callq 0xe1e0
movl $0x3ff, %ebp            # imm = 0x3FF
cmpl %ebp, %eax
cmovll %eax, %ebp
xorl %r13d, %r13d
testl %ebp, %ebp
movl $0x0, %ebx
cmovgl %ebp, %ebx
cmpq %r13, %rbx
je 0x5ffa1
movsbl (%r15,%r13), %edi
callq 0xe6c0
movb %al, 0x10(%rsp,%r13)
incq %r13
jmp 0x5ff88
movslq %ebp, %rax
movb $0x0, 0x10(%rsp,%rax)
decl %r12d
cmpl $0x4, %r12d
ja 0x5ffec
leaq 0x3aa6f(%rip), %rax     # 0x9aa28
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x60c7c
jmp 0x600f0
leaq 0x3ab3d(%rip), %rdi     # 0x9ab18
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x600f2
movl 0x10(%rsp), %ebp
cmpl $0x67706a2e, %ebp       # imm = 0x67706A2E
je 0x5ffc2
cmpl $0x6d66702e, %ebp       # imm = 0x6D66702E
je 0x6005c
cmpl $0x6d67702e, %ebp       # imm = 0x6D67702E
je 0x6002a
cmpl $0x6d70702e, %ebp       # imm = 0x6D70702E
je 0x6002a
cmpl $0x706d622e, %ebp       # imm = 0x706D622E
jne 0x6006e
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x60d40
jmp 0x600f0
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x61194
jmp 0x600f0
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
xorb $-0x2c, %cl
xorb $0x31, %dl
leal (%rsi,%rsi,4), %eax
xorl %r12d, %r12d
orb %cl, %dl
cmovel %eax, %r12d
jmp 0x5ff46
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6155c
jmp 0x600f0
leaq 0x3aad6(%rip), %rsi     # 0x9ab4b
leaq 0x10(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x5ffc2
cmpl $0x6669742e, %ebp       # imm = 0x6669742E
je 0x600d1
leaq 0x3aab8(%rip), %rsi     # 0x9ab51
leaq 0x10(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x600d1
cmpl $0x6472672e, %ebp       # imm = 0x6472672E
je 0x60106
cmpl $0x7761722e, %ebp       # imm = 0x7761722E
je 0x600e3
cmpl $0x676e702e, %ebp       # imm = 0x676E702E
jne 0x60115
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x60cd8
jmp 0x600f0
leaq 0x3ac8c(%rip), %rdi     # 0x9ad64
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
jmp 0x600f2
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x60fb6
movl %eax, %ebp
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x61a2a
jmp 0x600f0
leaq 0x3aa3b(%rip), %rdi     # 0x9ab57
jmp 0x600d8