// File: srajangarg[P]image-morph/src/morph.cpp
// Function: blendImages(Image const&, Image const&, double)
Image
blendImages(Image const & img1, Image const & img2, double t)
{
  assert(img1.hasSameDimsAs(img2));

  std::cout << "Blending images..." << std::endl;

  int w = img1.width();
  int h = img1.height();
  int n = img1.numChannels();
  unsigned char *res_pix;
  const unsigned char *pix_1, *pix_2;

  Image result(w, h, n);

  for (int row = 0; row < h; ++row)
  {
    for (int col = 0; col < w; ++col)
    { 
      res_pix = result.pixel(row, col);
      pix_1 = img1.pixel(row, col);
      pix_2 = img2.pixel(row, col);

      for (int channel = 0; channel < n; ++channel)
      { 
        // weighted average of the value of the pixel
        double z = ((double)pix_1[channel] * t)
                 + ((double)pix_2[channel] * (1-t));
        res_pix[channel] = floor(z);
      }
    }
  }

  return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x80(%rbp)
movq %rdi, %rax
movq %rax, -0x78(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3dc0
testb $0x1, %al
jne 0x4c9a
jmp 0x4c9c
jmp 0x4cbb
leaq 0x174c8(%rip), %rdi     # 0x1c16b
leaq 0x17419(%rip), %rsi     # 0x1c0c3
movl $0xb9, %edx
leaq 0x174ce(%rip), %rcx     # 0x1c184
callq 0x31e0
movq 0x1d306(%rip), %rdi     # 0x21fc8
leaq 0x174f3(%rip), %rsi     # 0x1c1bc
callq 0x32c0
movq %rax, %rdi
movq 0x1d2d8(%rip), %rsi     # 0x21fb0
callq 0x3310
movq -0x10(%rbp), %rdi
callq 0x5f80
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5f90
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5fb0
movq -0x80(%rbp), %rdi
movl %eax, -0x2c(%rbp)
movb $0x0, -0x49(%rbp)
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x37e0
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x4e50
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x4e40
movq -0x80(%rbp), %rdi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
callq 0x6840
movq %rax, -0x88(%rbp)
jmp 0x4d55
movq -0x88(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
callq 0x6090
movq %rax, -0x90(%rbp)
jmp 0x4d78
movq -0x90(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
callq 0x6090
movq %rax, -0x98(%rbp)
jmp 0x4d9b
movq -0x98(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x4e30
movq -0x40(%rbp), %rax
movslq -0x68(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
movsd -0x20(%rbp), %xmm2
movq -0x48(%rbp), %rax
movzbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
movsd 0x172aa(%rip), %xmm3    # 0x1c088
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x34d0
cvttsd2si %xmm0, %eax
movb %al, %dl
movq -0x38(%rbp), %rax
movslq -0x68(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x4dad
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x3b30
jmp 0x4e70
jmp 0x4e32
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4d31
jmp 0x4e42
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4d1e
movb $0x1, -0x49(%rbp)
testb $0x1, -0x49(%rbp)
jne 0x4e63
movq -0x80(%rbp), %rdi
callq 0x3b30
movq -0x78(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x3520
nopl (%rax)