// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: compute_color
LOCAL(void)
compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
/* Compute representative color for a box, put it in colormap[icolor] */
{
  /* Current algorithm: mean weighted by pixels (not colors) */
  /* Note it is important to get the rounding correct! */
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  histptr histp;
  int c0,c1,c2;
  int c0min,c0max,c1min,c1max,c2min,c2max;
  long count;
  long total = 0;
  long c0total = 0;
  long c1total = 0;
  long c2total = 0;
  
  c0min = boxp->c0min;  c0max = boxp->c0max;
  c1min = boxp->c1min;  c1max = boxp->c1max;
  c2min = boxp->c2min;  c2max = boxp->c2max;
  
  for (c0 = c0min; c0 <= c0max; c0++)
    for (c1 = c1min; c1 <= c1max; c1++) {
      histp = & histogram[c0][c1][c2min];
      for (c2 = c2min; c2 <= c2max; c2++) {
	if ((count = *histp++) != 0) {
	  total += count;
	  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
	  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
	  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
	}
      }
    }
  
  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
}

// Assembly:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movl %edx, -0x14(%rsp)
movq -0x8(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, -0x20(%rsp)
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x28(%rsp)
movq $0x0, -0x68(%rsp)
movq $0x0, -0x70(%rsp)
movq $0x0, -0x78(%rsp)
movq $0x0, -0x80(%rsp)
movq -0x10(%rsp), %rax
movl (%rax), %eax
movl %eax, -0x40(%rsp)
movq -0x10(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x44(%rsp)
movq -0x10(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x48(%rsp)
movq -0x10(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4c(%rsp)
movq -0x10(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x50(%rsp)
movq -0x10(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x54(%rsp)
movl -0x40(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf757c
movl -0x48(%rsp), %eax
movl %eax, -0x38(%rsp)
movl -0x38(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf756a
movq -0x28(%rsp), %rax
movslq -0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x50(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x30(%rsp)
movl -0x50(%rsp), %eax
movl %eax, -0x3c(%rsp)
movl -0x3c(%rsp), %eax
cmpl -0x54(%rsp), %eax
jg 0xf7558
movq -0x30(%rsp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x30(%rsp)
movzwl (%rax), %eax
movq %rax, -0x60(%rsp)
cmpq $0x0, %rax
je 0xf7546
movq -0x60(%rsp), %rax
addq -0x68(%rsp), %rax
movq %rax, -0x68(%rsp)
movl -0x34(%rsp), %eax
shll $0x3, %eax
addl $0x4, %eax
cltq
imulq -0x60(%rsp), %rax
addq -0x70(%rsp), %rax
movq %rax, -0x70(%rsp)
movl -0x38(%rsp), %eax
shll $0x2, %eax
addl $0x2, %eax
cltq
imulq -0x60(%rsp), %rax
addq -0x78(%rsp), %rax
movq %rax, -0x78(%rsp)
movl -0x3c(%rsp), %eax
shll $0x3, %eax
addl $0x4, %eax
cltq
imulq -0x60(%rsp), %rax
addq -0x80(%rsp), %rax
movq %rax, -0x80(%rsp)
jmp 0xf7548
movl -0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rsp)
jmp 0xf74b6
jmp 0xf755a
movl -0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rsp)
jmp 0xf7476
jmp 0xf756c
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf7460
movq -0x70(%rsp), %rax
movq -0x68(%rsp), %rcx
sarq %rcx
addq %rcx, %rax
cqto
idivq -0x68(%rsp)
movb %al, %dl
movq -0x8(%rsp), %rax
movq 0xa0(%rax), %rax
movq (%rax), %rax
movslq -0x14(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq -0x78(%rsp), %rax
movq -0x68(%rsp), %rcx
sarq %rcx
addq %rcx, %rax
cqto
idivq -0x68(%rsp)
movb %al, %dl
movq -0x8(%rsp), %rax
movq 0xa0(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq -0x80(%rsp), %rax
movq -0x68(%rsp), %rcx
sarq %rcx
addq %rcx, %rax
cqto
idivq -0x68(%rsp)
movb %al, %dl
movq -0x8(%rsp), %rax
movq 0xa0(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rsp), %rcx
movb %dl, (%rax,%rcx)
retq
nop