// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant1.c
// Function: create_colormap
LOCAL(void)
create_colormap (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  JSAMPARRAY colormap;		/* Created colormap */
  int total_colors;		/* Number of distinct output colors */
  int i,j,k, nci, blksize, blkdist, ptr, val;

  /* Select number of colors for each component */
  total_colors = select_ncolors(cinfo, cquantize->Ncolors);

  /* Report selected color counts */
  if (cinfo->out_color_components == 3)
    TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
	     total_colors, cquantize->Ncolors[0],
	     cquantize->Ncolors[1], cquantize->Ncolors[2]);
  else
    TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);

  /* Allocate and fill in the colormap. */
  /* The colors are ordered in the map in standard row-major order, */
  /* i.e. rightmost (highest-indexed) color changes most rapidly. */

  colormap = (*cinfo->mem->alloc_sarray)
    ((j_common_ptr) cinfo, JPOOL_IMAGE,
     (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);

  /* blksize is number of adjacent repeated entries for a component */
  /* blkdist is distance between groups of identical entries for a component */
  blkdist = total_colors;

  for (i = 0; i < cinfo->out_color_components; i++) {
    /* fill in colormap entries for i'th color component */
    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
    blksize = blkdist / nci;
    for (j = 0; j < nci; j++) {
      /* Compute j'th output value (out of nci) for component */
      val = output_value(cinfo, i, j, nci-1);
      /* Fill in all colormap entries that have this value of this component */
      for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
	/* fill in blksize entries beginning at ptr */
	for (k = 0; k < blksize; k++)
	  colormap[i][ptr+k] = (JSAMPLE) val;
      }
    }
    blkdist = blksize;		/* blksize of this color is blkdist of next */
  }

  /* Save the colormap in private storage,
   * where it will survive color quantization mode changes.
   */
  cquantize->sv_colormap = colormap;
  cquantize->sv_actual = total_colors;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
addq $0x3c, %rsi
callq 0xf56f0
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x3, 0x90(%rax)
jne 0xf46a1
jmp 0xf4631
movq 0x40(%rsp), %rax
movq (%rax), %rax
addq $0x2c, %rax
movq %rax, (%rsp)
movl 0x2c(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rax
movl 0x3c(%rax), %ecx
movq (%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x38(%rsp), %rax
movl 0x40(%rax), %ecx
movq (%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x38(%rsp), %rax
movl 0x44(%rax), %ecx
movq (%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x5e, 0x28(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x40(%rsp), %rdi
movl $0x1, %esi
callq *%rax
jmp 0xf46d7
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x5f, 0x28(%rax)
movl 0x2c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x40(%rsp), %rdi
movl $0x1, %esi
callq *%rax
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x40(%rsp), %rdi
movl 0x2c(%rsp), %edx
movq 0x40(%rsp), %rcx
movl 0x90(%rcx), %ecx
movl $0x1, %esi
callq *%rax
movq %rax, 0x30(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x90(%rcx), %eax
jge 0xf480d
movq 0x38(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl 0x3c(%rax,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
movl 0x14(%rsp), %eax
cltd
idivl 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0xf47f5
movq 0x40(%rsp), %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
movl 0x1c(%rsp), %ecx
subl $0x1, %ecx
callq 0xf58f0
movl %eax, 0xc(%rsp)
movl 0x24(%rsp), %eax
imull 0x18(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xf47e3
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x18(%rsp), %eax
jge 0xf47d3
movl 0xc(%rsp), %eax
movb %al, %dl
movq 0x30(%rsp), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x10(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0xf479a
jmp 0xf47d5
movl 0x14(%rsp), %eax
addl 0x10(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0xf4788
jmp 0xf47e5
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xf4750
movl 0x18(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xf4714
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x20(%rax)
movl 0x2c(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x28(%rax)
addq $0x48, %rsp
retq
nopl (%rax)