// File: /workspace/github_cpp_makefile_star2_O1/srajangarg[P]image-morph/src/morph.cpp
// Function: sampleBilinear(Image const&, Vec2 const&, unsigned char*)
void
sampleBilinear(Image const & image, Vec2 const & loc, unsigned char sampled_color[4])
{  
  int w = image.width();
  int h = image.height();
  int n = image.numChannels();
 
  // raw cordinates
  int col0 = floor(loc.x());
  int col1 = col0 + 1;
  int row0 = floor(loc.y());
  int row1 = row0 + 1;

  // sanitized to get pixels form image
  int pc0 = max(0, min(col0, w-1));
  int pc1 = min(pc0 + 1, w-1);
  int pr0 = max(0, min(row0, h-1));
  int pr1 = min(pr0 + 1, h-1);
  
  // pixel values at the sanitized points
  const unsigned char * pix00 = image.pixel(pr0, pc0);
  const unsigned char * pix01 = image.pixel(pr0, pc1);
  const unsigned char * pix10 = image.pixel(pr1, pc0);
  const unsigned char * pix11 = image.pixel(pr1, pc1);
  
  // https://en.wikipedia.org/wiki/Bilinear_interpolation
  double res;
  for (int channel = 0; channel < n; ++channel)
  {
    res = 0;

    res += ((double)pix00[channel]) * (col1 - loc.x()) * (row1 - loc.y());
    res += ((double)pix01[channel]) * (col1 - loc.x()) * (loc.y() - row0);
    res += ((double)pix10[channel]) * (loc.x() - col0) * (row1 - loc.y());
    res += ((double)pix11[channel]) * (loc.x() - col0) * (loc.y() - row0);

    sampled_color[channel] = min(255, max(0, (int)floor(res)));
  }

  // if numchannels < 4, others set 4 
  for (int i = n; i < 4; ++i)
    sampled_color[i] = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl (%rdi), %r13d
movl 0x4(%rdi), %ebx
movslq 0x8(%rdi), %rbp
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm1
movsd %xmm1, 0x8(%rsp)
callq 0x2360
cvttsd2si %xmm0, %r12d
movsd 0x8(%rsp), %xmm0
callq 0x2360
movq %rbp, %r10
cvttsd2si %xmm0, %esi
leal -0x1(%r13), %edx
cmpl %r12d, %edx
movl %r12d, %eax
cmovll %edx, %eax
xorl %edi, %edi
testl %eax, %eax
cmovlel %edi, %eax
leal 0x1(%rax), %ecx
cmpl %ecx, %edx
cmovll %edx, %ecx
decl %ebx
cmpl %esi, %ebx
movl %esi, %edx
cmovll %ebx, %edx
testl %edx, %edx
cmovlel %edi, %edx
leal 0x1(%rdx), %edi
cmpl %edi, %ebx
cmovgel %edi, %ebx
testq %rbp, %rbp
jle 0x2d62
imull %r13d, %edx
leal (%rdx,%rax), %edi
imull %r13d, %ebx
leal 0x1(%rsi), %r8d
leal 0x1(%r12), %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x10(%r15), %rsi
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x20(%rsp)
leal (%rcx,%rbx), %r8d
imull %r10d, %r8d
movslq %r8d, %r8
addq %rsi, %r8
movq %r8, 0x18(%rsp)
addl %eax, %ebx
imull %r10d, %ebx
movslq %ebx, %r13
addq %rsi, %r13
addl %ecx, %edx
imull %r10d, %edx
movslq %edx, %rbp
addq %rsi, %rbp
imull %r10d, %edi
movslq %edi, %rbx
addq %rsi, %rbx
xorl %r12d, %r12d
movq %r10, %r15
movzbl (%rbx,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd (%r14), %xmm2
movsd 0x8(%r14), %xmm1
movsd 0x8(%rsp), %xmm3
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
movsd 0x30(%rsp), %xmm4
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm0
addsd 0x133de(%rip), %xmm0    # 0x160b0
movzbl (%rbp,%r12), %eax
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm3, %xmm5
subsd 0x28(%rsp), %xmm1
mulsd %xmm1, %xmm5
addsd %xmm0, %xmm5
movzbl (%r13,%r12), %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
subsd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm3
mulsd %xmm4, %xmm3
movq 0x18(%rsp), %rax
movzbl (%rax,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm5, %xmm3
mulsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
callq 0x2360
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x2d3c
xorl %eax, %eax
movl $0xff, %ecx
cmpl %ecx, %eax
movq %r15, %r10
jl 0x2d4d
movl $0xff, %eax
movq 0x10(%rsp), %rcx
movb %al, (%rcx,%r12)
incq %r12
cmpq %r12, %r10
jne 0x2c94
cmpl $0x3, %r10d
jg 0x2d90
movq 0x10(%rsp), %rdi
addq %r10, %rdi
movl $0x3, %edx
subl %r10d, %edx
incq %rdx
xorl %esi, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2110
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq