// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::SetPixelRGB(int, int, gaps::RNRgb const&)
void R2Image::
SetPixelRGB(int x, int y, const RNRgb& rgb)
{
  // Clamp rgb
  unsigned char r = (unsigned char) ((rgb.R() > 0) ? ((rgb.R() < 1.0) ? 255.0 * rgb.R() : 255) : 0);
  unsigned char g = (unsigned char) ((rgb.G() > 0) ? ((rgb.G() < 1.0) ? 255.0 * rgb.G() : 255) : 0);
  unsigned char b = (unsigned char) ((rgb.B() > 0) ? ((rgb.B() < 1.0) ? 255.0 * rgb.B() : 255) : 0);
  static const unsigned char a = 255;
  
  // Set pixel color
  unsigned char *pixel = &(pixels[y*rowsize + x*ncomponents]);
  switch (ncomponents) {
  case 1: 
    *(pixel++) = (unsigned char) (0.3*r + 0.59*g + 0.11*b);
    break;

  case 2:
    *(pixel++) = (unsigned char) (0.3*r + 0.59*g + 0.11*b);
    *(pixel++) = a;
    break;

  case 3:
    *(pixel++) = r;
    *(pixel++) = g;
    *(pixel++) = b;
    break;

  case 4:
    *(pixel++) = r;
    *(pixel++) = g;
    *(pixel++) = b;
    *(pixel++) = a;
    break;
  }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movl %esi, 0x5c(%rsp)
movl %edx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rdi
callq 0x38cb0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc58dd
movq 0x50(%rsp), %rdi
callq 0x38cb0
movaps %xmm0, %xmm1
movsd 0x6d770(%rip), %xmm0    # 0x133008
ucomisd %xmm1, %xmm0
jbe 0xc58bf
movq 0x50(%rsp), %rdi
callq 0x38cb0
movaps %xmm0, %xmm1
movsd 0x6d96d(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0xc58cf
movsd 0x6d959(%rip), %xmm0    # 0x133220
movsd %xmm0, 0x30(%rsp)
jmp 0xc58cf
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0xc58e8
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0xc58e8
movsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, 0x4f(%rsp)
movq 0x50(%rsp), %rdi
callq 0x38cc0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc5963
movq 0x50(%rsp), %rdi
callq 0x38cc0
movaps %xmm0, %xmm1
movsd 0x6d6ea(%rip), %xmm0    # 0x133008
ucomisd %xmm1, %xmm0
jbe 0xc5945
movq 0x50(%rsp), %rdi
callq 0x38cc0
movaps %xmm0, %xmm1
movsd 0x6d8e7(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xc5955
movsd 0x6d8d3(%rip), %xmm0    # 0x133220
movsd %xmm0, 0x20(%rsp)
jmp 0xc5955
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0xc596e
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0xc596e
movsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, 0x4e(%rsp)
movq 0x50(%rsp), %rdi
callq 0x38cd0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc59e9
movq 0x50(%rsp), %rdi
callq 0x38cd0
movaps %xmm0, %xmm1
movsd 0x6d664(%rip), %xmm0    # 0x133008
ucomisd %xmm1, %xmm0
jbe 0xc59cb
movq 0x50(%rsp), %rdi
callq 0x38cd0
movaps %xmm0, %xmm1
movsd 0x6d861(%rip), %xmm0    # 0x133220
mulsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0xc59db
movsd 0x6d84d(%rip), %xmm0    # 0x133220
movsd %xmm0, 0x10(%rsp)
jmp 0xc59db
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0xc59f4
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0xc59f4
movq 0x38(%rsp), %rax
movsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movb %cl, 0x4d(%rsp)
movq 0x10(%rax), %rcx
movl 0x58(%rsp), %edx
movl 0x8(%rax), %edi
movl 0xc(%rax), %esi
imull %esi, %edx
movl 0x5c(%rsp), %esi
imull %edi, %esi
addl %esi, %edx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x40(%rsp)
movl 0x8(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, (%rsp)
subl $0x3, %eax
ja 0xc5bd2
movq (%rsp), %rax
leaq 0x7091d(%rip), %rcx     # 0x136368
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl 0x4f(%rsp), %eax
cvtsi2sd %eax, %xmm1
movzbl 0x4e(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x701f2(%rip), %xmm2    # 0x135c60
mulsd %xmm2, %xmm0
movsd 0x701de(%rip), %xmm2    # 0x135c58
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl 0x4d(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x701bd(%rip), %xmm2    # 0x135c50
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
jmp 0xc5bd2
movzbl 0x4f(%rsp), %eax
cvtsi2sd %eax, %xmm1
movzbl 0x4e(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x7018d(%rip), %xmm2    # 0x135c60
mulsd %xmm2, %xmm0
movsd 0x70179(%rip), %xmm2    # 0x135c58
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl 0x4d(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x70158(%rip), %xmm2    # 0x135c50
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $-0x1, (%rax)
jmp 0xc5bd2
movb 0x4f(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movb 0x4e(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movb 0x4d(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
jmp 0xc5bd2
movb 0x4f(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movb 0x4e(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movb 0x4d(%rsp), %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $-0x1, (%rax)
addq $0x68, %rsp
retq
nopw (%rax,%rax)