// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::R2Image(gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&)
R2Image::
R2Image(const R2Grid& red, const R2Grid& green, const R2Grid& blue)
  : width(red.XResolution()), 
    height(red.YResolution()),
    ncomponents(3),
    rowsize(0),
    pixels(NULL)
{
  // Just checking
  assert(red.XResolution() == green.XResolution());
  assert(red.YResolution() == green.YResolution());
  assert(red.XResolution() == blue.XResolution());
  assert(red.YResolution() == blue.YResolution());

  // Copy pixels (first pixel is lower-left)
  rowsize = ncomponents * width;
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;
  int nbytes = rowsize * height;
  if (nbytes > 0) {
    pixels = new unsigned char [nbytes];
    assert(pixels);
    for (int i = 0; i < width*height; i++) {
      int r = 255.0 * red.GridValue(i);
      int g = 255.0 * green.GridValue(i);
      int b = 255.0 * blue.GridValue(i);
      r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
      g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
      b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
      pixels[i*3+0] = r;
      pixels[i*3+1] = g;
      pixels[i*3+2] = b;
    }
  }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rdi
callq 0xb9260
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
movq 0x48(%rsp), %rdi
callq 0xb9280
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movl $0x3, 0x8(%rax)
movl $0x0, 0xc(%rax)
movq $0x0, 0x10(%rax)
movl 0x8(%rax), %ecx
imull (%rax), %ecx
movl %ecx, 0xc(%rax)
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xc5278
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0x18(%rsp), %rax
addl $0x1, %ecx
shll $0x2, %ecx
movl %ecx, 0xc(%rax)
movq 0x18(%rsp), %rcx
movl 0xc(%rcx), %eax
imull 0x4(%rcx), %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jle 0xc544b
movslq 0x34(%rsp), %rdi
callq 0xf030
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, 0x30(%rsp)
movq 0x18(%rsp), %rdx
movl 0x30(%rsp), %eax
movl (%rdx), %ecx
imull 0x4(%rdx), %ecx
cmpl %ecx, %eax
jge 0xc5449
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %esi
callq 0xb94c0
movaps %xmm0, %xmm1
movsd 0x6df3f(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %esi
callq 0xb94c0
movaps %xmm0, %xmm1
movsd 0x6df1a(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rsp)
movq 0x38(%rsp), %rdi
movl 0x30(%rsp), %esi
callq 0xb94c0
movaps %xmm0, %xmm1
movsd 0x6def5(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x2c(%rsp)
jge 0xc5346
xorl %eax, %eax
movl %eax, 0x14(%rsp)
jmp 0xc536b
cmpl $0xff, 0x2c(%rsp)
jle 0xc535b
movl $0xff, %eax
movl %eax, 0x10(%rsp)
jmp 0xc5363
movl 0x2c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x28(%rsp)
jge 0xc5382
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0xc53a7
cmpl $0xff, 0x28(%rsp)
jle 0xc5397
movl $0xff, %eax
movl %eax, 0x8(%rsp)
jmp 0xc539f
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0xc53be
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0xc53e0
cmpl $0xff, 0x24(%rsp)
jle 0xc53d2
movl $0xff, %eax
movl %eax, (%rsp)
jmp 0xc53d9
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x18(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, 0x24(%rsp)
movl 0x2c(%rsp), %ecx
movb %cl, %sil
movq 0x10(%rax), %rcx
imull $0x3, 0x30(%rsp), %edx
addl $0x0, %edx
movslq %edx, %rdx
movb %sil, (%rcx,%rdx)
movl 0x28(%rsp), %ecx
movb %cl, %sil
movq 0x10(%rax), %rcx
imull $0x3, 0x30(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movb %sil, (%rcx,%rdx)
movl 0x24(%rsp), %ecx
movb %cl, %dl
movq 0x10(%rax), %rax
imull $0x3, 0x30(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xc52b1
jmp 0xc544b
addq $0x58, %rsp
retq