// File: /workspace/github_cpp_makefile_star2_O1/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 0xf05f4
cmpl $0x0, (%rbx)
jle 0x3caea
cvtdq2pd 0x50(%rsp), %xmm0
movapd 0x90(%rsp), %xmm1
divpd %xmm0, %xmm1
movapd %xmm1, 0x90(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0xe0(%rsp), %r14
movq %rbx, 0x10(%rsp)
cmpl $0x0, 0x4(%rbx)
jle 0x3cad3
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
xorl %r12d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movq %r14, %rbx
movq %r14, %rdi
movsd 0xc0(%rsp), %xmm0
callq 0xf3306
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 0x3c20c
leaq 0x120(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xdfe7f
testl %eax, %eax
je 0x3caa9
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 0xf4194
movapd 0x50(%rsp), %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movsd 0x11181e(%rip), %xmm3   # 0x14e088
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 0xfa640
leaq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movl %ebx, %ecx
callq 0xf4194
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 0xfa640
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 0xfa640
leaq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0xf4194
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 0xfa640
leaq 0xf0(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movl %r15d, %ecx
callq 0xf4194
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 0xfa640
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 0xfa640
movapd 0xa0(%rsp), %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
movsd 0x111693(%rip), %xmm2   # 0x14e088
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 0xfa640
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 0xfa640
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 0xfa640
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movq %rbx, %rcx
callq 0xf42ce
jmp 0x3cac1
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
leaq 0x5a5574(%rip), %rcx    # 0x5e2030
callq 0xf42ce
incl %r12d
movq 0x10(%rsp), %rax
cmpl 0x4(%rax), %r12d
jl 0x3c78e
movl 0xc(%rsp), %eax
incl %eax
movq 0x10(%rsp), %rbx
movl %eax, 0xc(%rsp)
cmpl (%rbx), %eax
jl 0x3c76f
leaq 0x120(%rsp), %rdi
callq 0xf0e74
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
jmp 0x3cb1e
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0xf0e74
movq %rbx, %rdi
callq 0x28160
nop