// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: jinit_2pass_quantizer
GLOBAL(void)
jinit_2pass_quantizer (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize;
  int i;

  cquantize = (my_cquantize_ptr)
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_cquantizer));
  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  cquantize->pub.start_pass = start_pass_2_quant;
  cquantize->pub.new_color_map = new_color_map_2_quant;
  cquantize->fserrors = NULL;	/* flag optional arrays not allocated */
  cquantize->error_limiter = NULL;

  /* Make sure jdmaster didn't give me a case I can't handle */
  if (cinfo->out_color_components != 3)
    ERREXIT(cinfo, JERR_NOTIMPL);

  /* Allocate the histogram/inverse colormap storage */
  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
    ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
  for (i = 0; i < HIST_C0_ELEMS; i++) {
    cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
       HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  }
  cquantize->needs_zeroed = TRUE; /* histogram is garbage now */

  /* Allocate storage for the completed colormap, if required.
   * We do this now since it is FAR storage and may affect
   * the memory manager's space calculations.
   */
  if (cinfo->enable_2pass_quant) {
    /* Make sure color count is acceptable */
    int desired = cinfo->desired_number_of_colors;
    /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
    if (desired < 8)
      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
    /* Make sure colormap indexes can be represented by JSAMPLEs */
    if (desired > MAXNUMCOLORS)
      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
    cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
      ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
    cquantize->desired = desired;
  } else
    cquantize->sv_colormap = NULL;

  /* Only F-S dithering or no dithering is supported. */
  /* If user asks for ordered dither, give him F-S. */
  if (cinfo->dither_mode != JDITHER_NONE)
    cinfo->dither_mode = JDITHER_FS;

  /* Allocate Floyd-Steinberg workspace if necessary.
   * This isn't really needed until pass 2, but again it is FAR storage.
   * Although we will cope with a later change in dither_mode,
   * we do not promise to honor max_memory_to_use if dither_mode changes.
   */
  if (cinfo->dither_mode == JDITHER_FS) {
    cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
       (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
    /* Might as well create the error-limiting table too. */
    init_error_limit(cinfo);
  }
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x1, %esi
movl $0x58, %edx
callq *%rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x270(%rax)
movq 0x8(%rsp), %rax
leaq 0x228(%rip), %rcx       # 0xf5bf0
movq %rcx, (%rax)
movq 0x8(%rsp), %rax
leaq 0x439(%rip), %rcx       # 0xf5e10
movq %rcx, 0x18(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x40(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x50(%rax)
movq 0x10(%rsp), %rax
cmpl $0x3, 0x90(%rax)
je 0xf5a24
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x2f, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x1, %esi
movl $0x100, %edx            # imm = 0x100
callq *%rax
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x30(%rax)
movl $0x0, 0x4(%rsp)
cmpl $0x20, 0x4(%rsp)
jge 0xf5a9c
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
callq *%rax
movq %rax, %rdx
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movslq 0x4(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0xf5a55
movq 0x8(%rsp), %rax
movl $0x1, 0x38(%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x84(%rax)
je 0xf5b6e
movq 0x10(%rsp), %rax
movl 0x78(%rax), %eax
movl %eax, (%rsp)
cmpl $0x8, (%rsp)
jge 0xf5afb
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x38, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x8, 0x2c(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
cmpl $0x100, (%rsp)          # imm = 0x100
jle 0xf5b34
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x39, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x100, 0x2c(%rax)      # imm = 0x100
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdi
movl (%rsp), %edx
movl $0x1, %esi
movl $0x3, %ecx
callq *%rax
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
movl (%rsp), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x28(%rax)
jmp 0xf5b7b
movq 0x8(%rsp), %rax
movq $0x0, 0x20(%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x70(%rax)
je 0xf5b92
movq 0x10(%rsp), %rax
movl $0x2, 0x70(%rax)
movq 0x10(%rsp), %rax
cmpl $0x2, 0x70(%rax)
jne 0xf5be0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rsp), %rdi
movq 0x10(%rsp), %rcx
movl 0x88(%rcx), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
imulq $0x6, %rcx, %rdx
movl $0x1, %esi
callq *%rax
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x10(%rsp), %rdi
callq 0xf5e40
addq $0x18, %rsp
retq
nopw %cs:(%rax,%rax)