// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2PixelDatabase.cpp
// Function: gaps::R2PixelDatabase::FindGrid(char const*, gaps::R2Grid*, int) const
int R2PixelDatabase::
FindGrid(const char *key, R2Grid *grid, RNBoolean apply_transformation) const
{
  // Find entry
  R2PixelDatabaseEntry entry;
  if (!map.Find(key, &entry)) return FALSE;

  // Read grid
  if (grid) {
    // Seek to start of entry
    RNFileSeek(fp, entry.seek, RN_FILE_SEEK_SET);
  
    // Read entry
    if (!grid->ReadPNGStream(fp)) {
      RNFail("Error reading %s from pixel database\n", key);
      return FALSE;
    }

    // Check if should apply transformation
    if (apply_transformation) {
      // Apply offset 
      if (entry.offset != 0) {
        grid->Subtract(entry.offset);
      }

      // Apply scale 
      if ((entry.scale != 0) && (entry.scale != 1)) {
        grid->Multiply(1.0 / entry.scale);
      }

      // Apply exponent
      if ((entry.exponent != 0) && (entry.exponent != 1)) {
        grid->Pow(1.0 / entry.exponent);
      }
    }
  }

  // Return success
  return TRUE;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq %rdx, 0x118(%rsp)
movl %ecx, 0x114(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
movl %ecx, %r8d
leaq 0x68(%rsp), %rdi
xorps %xmm0, %xmm0
movsd 0x7f490(%rip), %xmm2    # 0x1a4168
movq %r8, %rsi
movl %ecx, %edx
movaps %xmm2, %xmm1
callq 0x126bf0
movq 0x10(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x18(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x47(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x13980
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0xbd8c0
jmp 0x124d25
movq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x126ca0
movl %eax, 0xc(%rsp)
jmp 0x124d3f
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0xb(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x13340
leaq 0x47(%rsp), %rdi
callq 0x135f0
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x124d6d
jmp 0x124db4
movl $0x0, 0x134(%rsp)
jmp 0x124f03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x124da5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x13340
leaq 0x47(%rsp), %rdi
callq 0x135f0
jmp 0x124f12
cmpq $0x0, 0x118(%rsp)
je 0x124ef8
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0xf0(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
movq 0x10(%rsp), %rax
movq 0x118(%rsp), %rdi
movq 0x8(%rax), %rsi
callq 0x123800
cmpl $0x0, %eax
jne 0x124e1c
movq 0x120(%rsp), %rsi
leaq 0x83dc3(%rip), %rdi     # 0x1a8bc8
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x134(%rsp)
jmp 0x124f03
cmpl $0x0, 0x114(%rsp)
je 0x124ef6
movsd 0xf8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x124e40
jp 0x124e40
jmp 0x124e56
movq 0x118(%rsp), %rdi
movsd 0xf8(%rsp), %xmm0
callq 0x119770
movsd 0x100(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x124e6c
jp 0x124e6c
jmp 0x124ea5
movsd 0x100(%rsp), %xmm0
movsd 0x7f2eb(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x124e87
jp 0x124e87
jmp 0x124ea5
movq 0x118(%rsp), %rdi
movsd 0x7f2d1(%rip), %xmm0    # 0x1a4168
divsd 0x100(%rsp), %xmm0
callq 0x1198a0
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x124ebb
jp 0x124ebb
jmp 0x124ef4
movsd 0x108(%rsp), %xmm0
movsd 0x7f29c(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x124ed6
jp 0x124ed6
jmp 0x124ef4
movq 0x118(%rsp), %rdi
movsd 0x7f282(%rip), %xmm0    # 0x1a4168
divsd 0x108(%rsp), %xmm0
callq 0x119be0
jmp 0x124ef6
jmp 0x124ef8
movl $0x1, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x38(%rsp), %rdi
callq 0x13970
nopl (%rax)