// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movl (%rdi), %r13d
movl 0x4(%rdi), %ebp
movslq 0x8(%rdi), %r12
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x23b0
movapd %xmm0, 0x20(%rsp)
cvttsd2si %xmm0, %r15d
leal 0x1(%r15), %eax
movl %eax, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x23b0
cvttsd2si %xmm0, %eax
leal 0x1(%rax), %edi
movq %r13, %rsi
leal -0x1(%r13), %edx
cmpl %r15d, %edx
cmovll %edx, %r15d
xorl %r13d, %r13d
testl %r15d, %r15d
cmovlel %r13d, %r15d
leal 0x1(%r15), %ecx
cmpl %ecx, %edx
cmovll %edx, %ecx
decl %ebp
cmpl %eax, %ebp
cmovll %ebp, %eax
testl %eax, %eax
cmovlel %r13d, %eax
leal 0x1(%rax), %edx
cmpl %edx, %ebp
cmovgel %edx, %ebp
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
imull %esi, %eax
movq %rsi, %r8
leal (%rax,%r15), %esi
imull %r8d, %ebp
cvtsi2sdl (%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %edi, %xmm1
movsd %xmm1, (%rsp)
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
cvttpd2dq 0x20(%rsp), %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
testl %r12d, %r12d
movl $0x0, %edi
cmovgl %r12d, %edi
movq %rdi, 0x20(%rsp)
leal (%rcx,%rbp), %edi
imull %r12d, %edi
movslq %edi, %rdi
addq %rdx, %rdi
movq %rdi, 0x38(%rsp)
addl %r15d, %ebp
imull %r12d, %ebp
movslq %ebp, %rdi
addq %rdx, %rdi
movq %rdi, 0x30(%rsp)
addl %ecx, %eax
imull %r12d, %eax
movslq %eax, %rbp
addq %rdx, %rbp
imull %r12d, %esi
movslq %esi, %r15
addq %rdx, %r15
cmpq %r13, 0x20(%rsp)
je 0x2b8d
movzbl (%r15,%r13), %eax
cvtsi2sd %eax, %xmm0
movsd (%r14), %xmm2
movsd 0x8(%r14), %xmm1
movsd 0x8(%rsp), %xmm3
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
movsd (%rsp), %xmm4
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm0
addsd 0xc582(%rip), %xmm0     # 0xf088
movzbl (%rbp,%r13), %eax
cvtsi2sd %eax, %xmm5
mulsd %xmm3, %xmm5
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm5
addsd %xmm0, %xmm5
movq 0x30(%rsp), %rax
movzbl (%rax,%r13), %eax
cvtsi2sd %eax, %xmm3
subsd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm3
mulsd %xmm4, %xmm3
movq 0x38(%rsp), %rax
movzbl (%rax,%r13), %eax
cvtsi2sd %eax, %xmm0
addsd %xmm5, %xmm3
mulsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
callq 0x23b0
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x2b6b
xorl %eax, %eax
movl $0xff, %ecx
cmpl %ecx, %eax
jl 0x2b79
movl $0xff, %eax
movb %al, (%rbx,%r13)
incq %r13
jmp 0x2ac4
movb $0x0, (%rbx,%r12)
incq %r12
cmpq $0x4, %r12
jl 0x2b85
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq