// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/GSV/GSVCamera.cpp
// Function: gaps::GSVCamera::DistortImage(gaps::R2Image const&, gaps::R2Image&) const
int GSVCamera::
DistortImage(const R2Image& input, R2Image& output) const
{
  // Get convenient variables
  double xscale = (double) output.Width() / (double) input.Width();
  double yscale = (double) output.Height() / (double) input.Height();
  R2Box input_bbox(0, 0, input.Width()-1, input.Height()-1);

  // Create distorted image (output) from undistorted image (input)
  // Adds radial distortion
  for (int x = 0; x < output.Width(); x++) {
    for (int y = 0; y < output.Height(); y++) {
      R2Point output_position(x,y);
      output_position[0] *= xscale;
      output_position[1] *= yscale;
      R2Point input_position = UndistortedPosition(output_position);
      if (R2Contains(input_bbox, input_position)) {
        int ix1 = (int) input_position[0];
        int iy1 = (int) input_position[1];
        int ix2 = ix1 + 1;
        int iy2 = iy1 + 1;
        if (ix2 >= input.Width()) ix2 = ix1;
        if (iy2 >= input.Height()) iy2 = iy1;
        double dx = input_position[0] - ix1;
        double dy = input_position[1] - iy1;
        RNRgb pixel1 = (1-dx) * input.PixelRGB(ix1, iy1) + dx * input.PixelRGB(ix2, iy1);
        RNRgb pixel2 = (1-dx) * input.PixelRGB(ix1, iy2) + dx * input.PixelRGB(ix2, iy2);
        RNRgb pixel = (1-dy) * pixel1 + dy * pixel2;
        output.SetPixelRGB(x, y, pixel);
      }
      else {
        output.SetPixelRGB(x, y, RNblack_rgb);
      }
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, %rbx
cvtdq2pd (%rdx), %xmm0
movaps %xmm0, 0x90(%rsp)
movq %rdi, 0xb8(%rsp)
movq %rsi, 0x18(%rsp)
movq (%rsi), %rax
movq %rax, %xmm0
decl %eax
cvtsi2sd %eax, %xmm2
movdqa %xmm0, 0x50(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %eax
decl %eax
cvtsi2sd %eax, %xmm3
leaq 0x120(%rsp), %rdi
pxor %xmm0, %xmm0
xorpd %xmm1, %xmm1
callq 0xeea08
movq %rbx, 0x10(%rsp)
movl (%rbx), %eax
testl %eax, %eax
jle 0x3c7b3
cvtdq2pd 0x50(%rsp), %xmm0
movapd 0x90(%rsp), %xmm1
divpd %xmm0, %xmm1
movapd %xmm1, 0x90(%rsp)
movq 0x10(%rsp), %rcx
movl 0x4(%rcx), %ecx
movl $0x0, 0xc(%rsp)
leaq 0xe0(%rsp), %rbx
testl %ecx, %ecx
jle 0x3c7a1
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
xorl %r12d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movq %rbx, %rdi
movsd 0xc0(%rsp), %xmm0
callq 0xf1712
movapd 0xe0(%rsp), %xmm0
mulpd 0x90(%rsp), %xmm0
movapd %xmm0, 0xe0(%rsp)
leaq 0xd0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %rbx, %r14
movq %rbx, %rdx
callq 0x3bebe
leaq 0x120(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xde3c0
testl %eax, %eax
je 0x3c76b
movsd 0xd0(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm0
movapd %xmm1, 0x50(%rsp)
cvttsd2si %xmm1, %ebp
movapd %xmm0, 0xa0(%rsp)
cvttsd2si %xmm0, %ebx
leal 0x1(%rbp), %r13d
leal 0x1(%rbx), %r15d
movq 0x18(%rsp), %rsi
cmpl (%rsi), %r13d
cmovgel %ebp, %r13d
cmpl 0x4(%rsi), %r15d
cmovgel %ebx, %r15d
leaq 0x60(%rsp), %rdi
movl %ebp, %edx
movl %ebx, %ecx
callq 0xf252e
movapd 0x50(%rsp), %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movsd 0x10fb4c(%rip), %xmm3   # 0x14c078
movapd %xmm1, 0x50(%rsp)
subsd %xmm1, %xmm3
movsd 0x60(%rsp), %xmm0
mulsd %xmm3, %xmm0
movsd 0x68(%rsp), %xmm1
mulsd %xmm3, %xmm1
movsd 0x70(%rsp), %xmm2
movsd %xmm3, 0xc8(%rsp)
mulsd %xmm3, %xmm2
leaq 0x78(%rsp), %rdi
callq 0xf89f4
leaq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movl %ebx, %ecx
callq 0xf252e
movsd 0x20(%rsp), %xmm0
movapd 0x50(%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x28(%rsp), %xmm1
mulsd %xmm3, %xmm1
movsd 0x30(%rsp), %xmm2
mulsd %xmm3, %xmm2
leaq 0x38(%rsp), %rdi
callq 0xf89f4
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
addsd 0x38(%rsp), %xmm0
addsd 0x40(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
addsd 0x48(%rsp), %xmm2
leaq 0x108(%rsp), %rdi
callq 0xf89f4
leaq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0xf252e
movsd 0x38(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x40(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x48(%rsp), %xmm2
leaq 0x60(%rsp), %rdi
callq 0xf89f4
leaq 0xf0(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movl %r15d, %ecx
callq 0xf252e
movsd 0xf0(%rsp), %xmm0
movapd 0x50(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0xf8(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x100(%rsp), %xmm2
leaq 0x20(%rsp), %rdi
callq 0xf89f4
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
addsd 0x20(%rsp), %xmm0
addsd 0x28(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
addsd 0x30(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0xf89f4
movapd 0xa0(%rsp), %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movsd 0x10f9c1(%rip), %xmm2   # 0x14c078
movapd %xmm1, 0xa0(%rsp)
subsd %xmm1, %xmm2
movsd 0x108(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd 0x110(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x118(%rsp), %xmm2
leaq 0x38(%rsp), %rdi
callq 0xf89f4
movsd 0x78(%rsp), %xmm0
movapd 0xa0(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x80(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x88(%rsp), %xmm2
leaq 0x20(%rsp), %rdi
callq 0xf89f4
leaq 0x60(%rsp), %rbx
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
addsd 0x20(%rsp), %xmm0
addsd 0x28(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
addsd 0x30(%rsp), %xmm2
movq %rbx, %rdi
callq 0xf89f4
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movq %rbx, %rcx
callq 0xf2676
jmp 0x3c783
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
leaq 0x5a38a2(%rip), %rcx    # 0x5e0020
callq 0xf2676
incl %r12d
movq 0x10(%rsp), %rax
movl 0x4(%rax), %ecx
cmpl %ecx, %r12d
movq %r14, %rbx
jl 0x3c453
movq 0x10(%rsp), %rax
movl (%rax), %eax
movl 0xc(%rsp), %edx
incl %edx
movl %edx, 0xc(%rsp)
cmpl %eax, %edx
jl 0x3c436
leaq 0x120(%rsp), %rdi
callq 0xef280
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
jmp 0x3c7e7
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0xef280
movq %rbx, %rdi
callq 0x28150
nop