// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2PixelDatabase.cpp
// Function: gaps::R2PixelDatabase::InsertGrid(char const*, gaps::R2Grid const&, double, double, double, int)
int R2PixelDatabase::
InsertGrid(const char *key, const R2Grid& grid,
  double offset, double scale, double exponent, RNBoolean apply_transformation)
{
  // Seek to end of entries
  unsigned long long seek = entries_seek;
  RNFileSeek(fp, seek, RN_FILE_SEEK_SET);

  // Check if need to apply offset, scale, or exponent
  if ((apply_transformation) &&
      ((offset != 0) || (scale != 1) || (exponent != 1))) {
    // Write processed pixels to file
    R2Grid tmp(grid);
    if (exponent != 1) tmp.Pow(exponent);
    if (scale != 1) tmp.Multiply(scale);
    if (offset != 0) tmp.Add(offset);
    if (!tmp.WritePNGStream(fp)) return FALSE;
  }
  else {
    // Write original pixels to file
    if (!grid.WritePNGStream(fp)) return FALSE;
  }

  // Update entries seek
  entries_seek = RNFileTell(fp);
  unsigned int size = entries_seek - seek;
  
  // Insert entry into map
  R2PixelDatabaseEntry entry(key, R2_PIXEL_DATABASE_1_16_PNG_FORMAT, size, seek, offset, scale, exponent);
  map.Insert(key, entry);
  
  // Insert key
  keys.push_back(key);
  
  // Increment number of entries
  entries_count++;

  // Return success
  return 1;
}

// Assembly:
subq $0x3c8, %rsp            # imm = 0x3C8
movq %rdi, 0x3b8(%rsp)
movq %rsi, 0x3b0(%rsp)
movq %rdx, 0x3a8(%rsp)
movsd %xmm0, 0x3a0(%rsp)
movsd %xmm1, 0x398(%rsp)
movsd %xmm2, 0x390(%rsp)
movl %ecx, 0x38c(%rsp)
movq 0x3b8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x38(%rax), %rcx
movq %rcx, 0x380(%rsp)
movq 0x8(%rax), %rdi
movq 0x380(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
cmpl $0x0, 0x38c(%rsp)
je 0x125570
movsd 0x3a0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x125429
jp 0x125429
movsd 0x398(%rsp), %xmm0
movsd 0x7ed65(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x125429
jp 0x125429
movsd 0x390(%rsp), %xmm0
movsd 0x7ed4c(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x125429
jp 0x125429
jmp 0x125570
movq 0x3a8(%rsp), %rsi
leaq 0x2b0(%rsp), %rdi
callq 0x1124c0
movsd 0x390(%rsp), %xmm0
movsd 0x7ed19(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x125459
jp 0x125459
jmp 0x125499
movsd 0x390(%rsp), %xmm0
leaq 0x2b0(%rsp), %rdi
callq 0x119be0
jmp 0x125471
jmp 0x125499
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x2b0(%rsp), %rdi
callq 0x1127b0
jmp 0x12581a
movsd 0x398(%rsp), %xmm0
movsd 0x7ecbe(%rip), %xmm1    # 0x1a4168
ucomisd %xmm1, %xmm0
jne 0x1254b4
jp 0x1254b4
jmp 0x1254ce
movsd 0x398(%rsp), %xmm0
leaq 0x2b0(%rsp), %rdi
callq 0x1198a0
jmp 0x1254cc
jmp 0x1254ce
movsd 0x3a0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1254e4
jp 0x1254e4
jmp 0x1254fe
movsd 0x3a0(%rsp), %xmm0
leaq 0x2b0(%rsp), %rdi
callq 0x1195f0
jmp 0x1254fc
jmp 0x1254fe
movq 0xf0(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x2b0(%rsp), %rdi
callq 0x123de0
movl %eax, 0xec(%rsp)
jmp 0x125520
movl 0xec(%rsp), %eax
cmpl $0x0, %eax
jne 0x125544
movl $0x0, 0x3c4(%rsp)
movl $0x1, 0x2a0(%rsp)
jmp 0x12554f
movl $0x0, 0x2a0(%rsp)
leaq 0x2b0(%rsp), %rdi
callq 0x1127b0
movl 0x2a0(%rsp), %eax
testl %eax, %eax
je 0x12556e
jmp 0x125569
jmp 0x12580b
jmp 0x1255a0
movq 0xf0(%rsp), %rax
movq 0x3a8(%rsp), %rdi
movq 0x8(%rax), %rsi
callq 0x123de0
cmpl $0x0, %eax
jne 0x12559e
movl $0x0, 0x3c4(%rsp)
jmp 0x12580b
jmp 0x1255a0
movq 0xf0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x1453b0
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, 0x38(%rax)
movl 0x38(%rax), %eax
movl 0x380(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x29c(%rsp)
movq 0x3b0(%rsp), %rsi
movl 0x29c(%rsp), %ecx
movq 0x380(%rsp), %r8
movsd 0x3a0(%rsp), %xmm0
movsd 0x398(%rsp), %xmm1
movsd 0x390(%rsp), %xmm2
leaq 0x1f0(%rsp), %rdi
movl $0x1, %edx
callq 0x126bf0
movq 0xf0(%rsp), %rax
addq $0x40, %rax
movq %rax, 0xd0(%rsp)
movq 0x3b0(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x1cf(%rsp), %rdi
movq %rdi, 0xe0(%rsp)
callq 0x13980
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
leaq 0x1d0(%rsp), %rdi
callq 0xbd8c0
jmp 0x12566f
leaq 0x120(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
leaq 0x1f0(%rsp), %rsi
movl $0xa8, %edx
callq 0x13460
movq 0xc8(%rsp), %rsi
movq %rsp, %rdi
movl $0x15, %ecx
rep  movsq (%rsi), %es:(%rdi)
movq 0xd0(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x126d40
jmp 0x1256bb
leaq 0x1d0(%rsp), %rdi
callq 0x13340
leaq 0x1cf(%rsp), %rdi
callq 0x135f0
movq 0xf0(%rsp), %rax
addq $0x48, %rax
movq %rax, 0xb0(%rsp)
movq 0x3b0(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0xff(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x13980
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0x100(%rsp), %rdi
callq 0xbd8c0
jmp 0x12572d
movq 0xb0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x126d80
jmp 0x125744
leaq 0x100(%rsp), %rdi
callq 0x13340
leaq 0xff(%rsp), %rdi
callq 0x135f0
movq 0xf0(%rsp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
movl $0x1, 0x3c4(%rsp)
jmp 0x12580b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x1257b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x13340
leaq 0x1cf(%rsp), %rdi
callq 0x135f0
jmp 0x12581a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x1257fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x13340
leaq 0xff(%rsp), %rdi
callq 0x135f0
jmp 0x12581a
movl 0x3c4(%rsp), %eax
addq $0x3c8, %rsp            # imm = 0x3C8
retq
movq 0x2a8(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)