// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::DrawImageInset() const
void R3SurfelViewer::
DrawImageInset(void) const
{
  // Get/check stuff
  if (!selected_image) return;
  if (selected_image->ImageWidth() <= 0) return;
  if (selected_image->ImageHeight() <= 0) return;
  if (!image_inset_visibility) return;
  if (image_inset_size <= 0) return;
  
  // Get/check color image
  ((R3SurfelViewer *) this)->UpdateSelectedImageColorPixels();
  int color_width = selected_image_color_pixels.Width();
  int color_height = selected_image_color_pixels.Height();
  if ((color_width <= 0) || (color_height <= 0)) return;
  const unsigned char *color_pixels = selected_image_color_pixels.Pixels();
  if (!color_pixels) return;

  // Determine image coordinates
  int w = viewer.Viewport().Width();
  int h = viewer.Viewport().Height();
  double x2 = w;
  double y2 = h;
  double aspect = (double) selected_image->ImageHeight() / (double) selected_image->ImageWidth();
  double x1 = x2 - image_inset_size * w;
  double y1 = y2 - image_inset_size * w * aspect;

  // Determine focal point
  R2Point focal_point(0.5*selected_image->ImageWidth(), 0.5*selected_image->ImageHeight());
  if (selected_point) {
    focal_point = selected_image->ImagePosition(selected_point->Position());
    if ((focal_point.X() >= 0) && (focal_point.Y() >= 0) &&
        (focal_point.X() <= selected_image->ImageWidth()-0.5) &&
        (focal_point.Y() <= selected_image->ImageHeight()-0.5)) {
      focal_point[0] = x1 + (x2 - x1) * focal_point.X()/selected_image->ImageWidth();
      focal_point[1] = y1 + (y2 - y1) * focal_point.Y()/selected_image->ImageHeight();
    }
  }

  // Push ortho viewing matrices
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0, w, 0, h, 0.1, 1);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  // Get image translation (so that focal point is in view)
  double tx = 0;
  double ty = 0;
  if (x1 < 0) {
    if (focal_point.X() < 0.25*w) {
      tx = 0.25*w - focal_point.X();
    }
  }
  if (y1 < 0) {
    if (focal_point.Y() < 0.25*h) {
      ty = 0.25*h - focal_point.Y();
    }
  }

#if (RN_OS == RN_MAC)
  // For some unknown reason, drawing a textured quad hangs the GPU for some images
  // (very rarely, but it is repeatable).   So, draw the with glPixelDraw instead.
  
  // Set viewport 
  glViewport(x1, y1, x2 - x1, y2 - y1);

  // Set image scale 
  GLfloat sx = (x2 - x1) / color_width;
  GLfloat sy = (y2 - y1) / color_height;
  glPixelZoom(sx, sy);

  // Set image translation
  // Can't send point outside viewport to glRasterPos, so add translation this way
  glRasterPos3d(0, 0, -0.5);
  glBitmap(0, 0, 0, 0, tx, ty, NULL);
  
  // Draw image
  glDrawPixels(color_width, color_height, GL_RGB, GL_UNSIGNED_BYTE, color_pixels);

  // Reset everything
  glPixelZoom(1, 1);
  glViewport(0, 0, w, h);
#else
  // Update selected image texture
  ((R3SurfelViewer *) this)->UpdateSelectedImageTexture();

  // Draw image as textured quad
  if (selected_image_texture_id > 0) {
    // Bind texture
    glBindTexture(GL_TEXTURE_2D, selected_image_texture_id);
    glEnable(GL_TEXTURE_2D);

    // Draw quad
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0.0, 0.0);
    R3LoadPoint(x1 + tx, y1 + ty, -0.5);
    R3LoadTextureCoords(1.0, 0.0);
    R3LoadPoint(x2 + tx, y1 + ty, -0.5);
    R3LoadTextureCoords(1.0, 1.0);
    R3LoadPoint(x2 + tx, y2 + ty, -0.5);
    R3LoadTextureCoords(0.0, 1.0);
    R3LoadPoint(x1 + tx, y2 + ty, -0.5);
    RNGrfxEnd();

    // Unbind texture
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
  }
#endif
  
  // Draw projected center point
  if (selected_point) {
    RNLoadRgb(1.0, 0.0, 0.0);
    R3Point p(focal_point.X() + tx, focal_point.Y() + ty, -0.25);
    R3Box(p[0]-4, p[1]-4, -0.25, p[0]+4, p[1]+4, -0.25).Draw();
  }

  // Pop ortho viewing matrices
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0x2d0(%rax)
jne 0x34cbe
jmp 0x356c9
movq 0xc8(%rsp), %rax
movq 0x2d0(%rax), %rdi
callq 0x3e330
cmpl $0x0, %eax
jg 0x34cdc
jmp 0x356c9
movq 0xc8(%rsp), %rax
movq 0x2d0(%rax), %rdi
callq 0x3e350
cmpl $0x0, %eax
jg 0x34cfa
jmp 0x356c9
movq 0xc8(%rsp), %rax
cmpl $0x0, 0x32c(%rax)
jne 0x34d10
jmp 0x356c9
movq 0xc8(%rsp), %rax
xorps %xmm0, %xmm0
ucomisd 0x2d8(%rax), %xmm0
jb 0x34d2a
jmp 0x356c9
movq 0xc8(%rsp), %rdi
movq (%rdi), %rax
callq *0x258(%rax)
movq 0xc8(%rsp), %rdi
addq $0x5c8, %rdi            # imm = 0x5C8
callq 0x3de90
movq 0xc8(%rsp), %rdi
movl %eax, 0x1bc(%rsp)
addq $0x5c8, %rdi            # imm = 0x5C8
callq 0x3dea0
movl %eax, 0x1b8(%rsp)
cmpl $0x0, 0x1bc(%rsp)
jle 0x34d85
cmpl $0x0, 0x1b8(%rsp)
jg 0x34d8a
jmp 0x356c9
movq 0xc8(%rsp), %rdi
addq $0x5c8, %rdi            # imm = 0x5C8
callq 0x3dec0
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
jne 0x34db6
jmp 0x356c9
movq 0xc8(%rsp), %rdi
addq $0x78, %rdi
callq 0x3def0
movq %rax, %rdi
callq 0x3df10
movq 0xc8(%rsp), %rdi
movl %eax, 0x1ac(%rsp)
addq $0x78, %rdi
callq 0x3def0
movq %rax, %rdi
callq 0x3e370
movl %eax, %ecx
movq 0xc8(%rsp), %rax
movl %ecx, 0x1a8(%rsp)
cvtsi2sdl 0x1ac(%rsp), %xmm0
movsd %xmm0, 0x1a0(%rsp)
cvtsi2sdl 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x198(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e350
movl %eax, %ecx
movq 0xc8(%rsp), %rax
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e330
movsd 0xb8(%rsp), %xmm0
movl %eax, %ecx
movq 0xc8(%rsp), %rax
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
movsd 0x1a0(%rsp), %xmm1
movsd 0x2d8(%rax), %xmm0
cvtsi2sdl 0x1ac(%rsp), %xmm2
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x188(%rsp)
movsd 0x198(%rsp), %xmm1
movsd 0x2d8(%rax), %xmm0
cvtsi2sdl 0x1ac(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x190(%rsp), %xmm2
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e330
movl %eax, %ecx
movq 0xc8(%rsp), %rax
cvtsi2sd %ecx, %xmm1
movsd 0x20c10e(%rip), %xmm0   # 0x241038
mulsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e350
movsd 0xc0(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
movsd 0x20c0e0(%rip), %xmm1   # 0x241038
mulsd %xmm2, %xmm1
leaq 0x170(%rsp), %rdi
callq 0x194a20
movq 0xc8(%rsp), %rax
cmpq $0x0, 0x2c8(%rax)
je 0x351eb
movq 0xc8(%rsp), %rax
movq 0x2d0(%rax), %rcx
movq %rcx, 0xb0(%rsp)
movq 0x2c8(%rax), %rsi
leaq 0x148(%rsp), %rdi
callq 0x3e1d0
movq 0xb0(%rsp), %rsi
leaq 0x160(%rsp), %rdi
leaq 0x148(%rsp), %rdx
callq 0x3e380
leaq 0x170(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x194cc0
leaq 0x170(%rsp), %rdi
callq 0x3dc80
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x351e9
leaq 0x170(%rsp), %rdi
callq 0x3dcb0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x351e9
leaq 0x170(%rsp), %rdi
callq 0x3dc80
movq 0xc8(%rsp), %rax
movsd %xmm0, 0xa8(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e330
movsd 0xa8(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x20bfe9(%rip), %xmm2   # 0x241038
subsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x351e9
leaq 0x170(%rsp), %rdi
callq 0x3dcb0
movq 0xc8(%rsp), %rax
movsd %xmm0, 0xa0(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e350
movsd 0xa0(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x20bf9c(%rip), %xmm2   # 0x241038
subsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x351e9
movsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x1a0(%rsp), %xmm0
subsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x3dc80
movq 0xc8(%rsp), %rax
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e330
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x3e3c0
movsd 0x78(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x198(%rsp), %xmm0
subsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x3dcb0
movq 0xc8(%rsp), %rax
movaps %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x2d0(%rax), %rdi
callq 0x3e350
movsd 0x88(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0x170(%rsp), %rdi
movl $0x1, %esi
callq 0x3e3c0
movsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x351eb
movl $0x1701, %edi           # imm = 0x1701
callq 0x28eb0
callq 0x29170
callq 0x28e30
cvtsi2sdl 0x1ac(%rsp), %xmm1
cvtsi2sdl 0x1a8(%rsp), %xmm3
xorps %xmm2, %xmm2
movsd 0x20c2cc(%rip), %xmm4   # 0x2414e8
movsd 0x20bdec(%rip), %xmm5   # 0x241010
movaps %xmm2, %xmm0
callq 0x29410
movl $0x1700, %edi           # imm = 0x1700
callq 0x28eb0
callq 0x29170
callq 0x28e30
xorps %xmm0, %xmm0
movsd %xmm0, 0x140(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x138(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x188(%rsp), %xmm0
jbe 0x352ec
leaq 0x170(%rsp), %rdi
callq 0x3dc80
movaps %xmm0, %xmm1
cvtsi2sdl 0x1ac(%rsp), %xmm2
movsd 0x20c2e5(%rip), %xmm0   # 0x241570
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x352ea
cvtsi2sdl 0x1ac(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x3dc80
movaps %xmm0, %xmm1
movsd 0x58(%rsp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x20c297(%rip), %xmm2   # 0x241570
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x140(%rsp)
jmp 0x352ec
xorps %xmm0, %xmm0
ucomisd 0x180(%rsp), %xmm0
jbe 0x35380
leaq 0x170(%rsp), %rdi
callq 0x3dcb0
movaps %xmm0, %xmm1
cvtsi2sdl 0x1a8(%rsp), %xmm2
movsd 0x20c251(%rip), %xmm0   # 0x241570
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3537e
cvtsi2sdl 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x3dcb0
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x20c203(%rip), %xmm2   # 0x241570
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x138(%rsp)
jmp 0x35380
movq 0xc8(%rsp), %rdi
movq (%rdi), %rax
callq *0x260(%rax)
movq 0xc8(%rsp), %rax
cmpl $0x0, 0x5e8(%rax)
jbe 0x35509
movq 0xc8(%rsp), %rax
movl 0x5e8(%rax), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28d30
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28960
movl $0xb50, %edi            # imm = 0xB50
callq 0x28240
movsd 0x20bc36(%rip), %xmm2   # 0x241010
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2f2e0
movl $0x7, %edi
callq 0x3e030
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x3e3e0
movsd 0x188(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
addsd 0x138(%rsp), %xmm1
movsd 0x20c0ea(%rip), %xmm2   # 0x241510
callq 0x3e0e0
movsd 0x20bbdd(%rip), %xmm0   # 0x241010
xorps %xmm1, %xmm1
callq 0x3e3e0
movsd 0x1a0(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
addsd 0x138(%rsp), %xmm1
movsd 0x20c0a9(%rip), %xmm2   # 0x241510
callq 0x3e0e0
movsd 0x20bb9c(%rip), %xmm1   # 0x241010
movaps %xmm1, %xmm0
callq 0x3e3e0
movsd 0x1a0(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
movsd 0x198(%rsp), %xmm1
addsd 0x138(%rsp), %xmm1
movsd 0x20c068(%rip), %xmm2   # 0x241510
callq 0x3e0e0
xorps %xmm0, %xmm0
movsd 0x20bb58(%rip), %xmm1   # 0x241010
callq 0x3e3e0
movsd 0x188(%rsp), %xmm0
addsd 0x140(%rsp), %xmm0
movsd 0x198(%rsp), %xmm1
addsd 0x138(%rsp), %xmm1
movsd 0x20c027(%rip), %xmm2   # 0x241510
callq 0x3e0e0
callq 0x3e090
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x28d30
movl $0xde1, %edi            # imm = 0xDE1
callq 0x28240
movq 0xc8(%rsp), %rax
cmpq $0x0, 0x2c8(%rax)
je 0x356ab
movsd 0x20bae9(%rip), %xmm0   # 0x241010
xorps %xmm2, %xmm2
movaps %xmm2, %xmm1
callq 0x2f2e0
leaq 0x170(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x3dc80
movq (%rsp), %rdi
movsd 0x140(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x3dcb0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
movsd 0x138(%rsp), %xmm2
addsd %xmm2, %xmm1
leaq 0x120(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movsd 0x20bfee(%rip), %xmm2   # 0x241578
movsd %xmm2, 0x48(%rsp)
callq 0x1622c0
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl %esi, 0x14(%rsp)
callq 0x3e410
movq 0x18(%rsp), %rdi
movsd (%rax), %xmm0
movsd 0x20bfca(%rip), %xmm1   # 0x241580
movsd %xmm1, 0x28(%rsp)
subsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x1, %esi
movl %esi, 0x24(%rsp)
callq 0x3e410
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rdi
movsd 0x28(%rsp), %xmm1
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
callq 0x3e410
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
movsd 0x28(%rsp), %xmm1
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
callq 0x3e410
movsd 0x28(%rsp), %xmm2
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm5
movsd (%rax), %xmm4
addsd %xmm2, %xmm4
leaq 0xe8(%rsp), %rdi
movaps %xmm5, %xmm2
callq 0x133f40
leaq 0xe0(%rsp), %rdi
movl $0x772, %esi            # imm = 0x772
callq 0x2f310
jmp 0x35662
movq 0xe0(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xdab40
jmp 0x35679
leaq 0xe8(%rsp), %rdi
callq 0x3d220
jmp 0x356ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x3d220
jmp 0x356d1
movl $0x1701, %edi           # imm = 0x1701
callq 0x28eb0
callq 0x293f0
movl $0x1700, %edi           # imm = 0x1700
callq 0x28eb0
callq 0x293f0
addq $0x1c8, %rsp            # imm = 0x1C8
retq
movq 0xd8(%rsp), %rdi
callq 0x292e0
nop