// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdsample.c
// Function: jinit_upsampler
GLOBAL(void)
jinit_upsampler (j_decompress_ptr cinfo)
{
  my_upsample_ptr upsample;
  int ci;
  jpeg_component_info * compptr;
  jboolean need_buffer, do_fancy;
  int h_in_group, v_in_group, h_out_group, v_out_group;

  upsample = (my_upsample_ptr)
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_upsampler));
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  upsample->pub.start_pass = start_pass_upsample;
  upsample->pub.upsample = sep_upsample;
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */

  if (cinfo->CCIR601_sampling)	/* this isn't supported */
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);

  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
   * so don't ask for it.
   */
  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;

  /* Verify we can handle the sampling factors, select per-component methods,
   * and create storage as needed.
   */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Compute size of an "input group" after IDCT scaling.  This many samples
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
     */
    h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
		 cinfo->min_DCT_scaled_size;
    v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
		 cinfo->min_DCT_scaled_size;
    h_out_group = cinfo->max_h_samp_factor;
    v_out_group = cinfo->max_v_samp_factor;
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
    need_buffer = TRUE;
    if (! compptr->component_needed) {
      /* Don't bother to upsample an uninteresting component. */
      upsample->methods[ci] = noop_upsample;
      need_buffer = FALSE;
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
      /* Fullsize components can be processed without any work. */
      upsample->methods[ci] = fullsize_upsample;
      need_buffer = FALSE;
    } else if (h_in_group * 2 == h_out_group &&
	       v_in_group == v_out_group) {
      /* Special cases for 2h1v upsampling */
      if (do_fancy && compptr->downsampled_width > 2)
	upsample->methods[ci] = h2v1_fancy_upsample;
      else
	upsample->methods[ci] = h2v1_upsample;
    } else if (h_in_group * 2 == h_out_group &&
	       v_in_group * 2 == v_out_group) {
      /* Special cases for 2h2v upsampling */
      if (do_fancy && compptr->downsampled_width > 2) {
	upsample->methods[ci] = h2v2_fancy_upsample;
	upsample->pub.need_context_rows = TRUE;
      } else
	upsample->methods[ci] = h2v2_upsample;
    } else if ((h_out_group % h_in_group) == 0 &&
	       (v_out_group % v_in_group) == 0) {
      /* Generic integral-factors upsampling method */
      upsample->methods[ci] = int_upsample;
      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
    } else
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
    if (need_buffer) {
      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 (JDIMENSION) jround_up((long) cinfo->output_width,
				(long) cinfo->max_h_samp_factor),
	 (JDIMENSION) cinfo->max_v_samp_factor);
    }
  }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x50(%rsp), %rdi
movl $0x1, %esi
movl $0x100, %edx            # imm = 0x100
callq *%rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x260(%rax)
movq 0x48(%rsp), %rax
leaq 0x388(%rip), %rcx       # 0xf2c50
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
leaq 0x3c9(%rip), %rcx       # 0xf2ca0
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x50(%rsp), %rax
cmpl $0x0, 0x188(%rax)
je 0xf2916
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl $0x19, 0x28(%rax)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x50(%rsp), %rdi
callq *%rax
movq 0x50(%rsp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x64(%rcx)
movb %al, 0x1f(%rsp)
je 0xf293a
movq 0x50(%rsp), %rax
cmpl $0x1, 0x1a0(%rax)
setg %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x44(%rsp)
movq 0x50(%rsp), %rax
movq 0x130(%rax), %rax
movq %rax, 0x38(%rsp)
movl 0x44(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0xf2c41
movq 0x38(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x38(%rsp), %rcx
imull 0x24(%rcx), %eax
movq 0x50(%rsp), %rcx
cltd
idivl 0x1a0(%rcx)
movl %eax, 0x2c(%rsp)
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x38(%rsp), %rcx
imull 0x24(%rcx), %eax
movq 0x50(%rsp), %rcx
cltd
idivl 0x1a0(%rcx)
movl %eax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movl 0x198(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x50(%rsp), %rax
movl 0x19c(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x28(%rsp), %edx
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
movl $0x1, 0x34(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x0, 0x30(%rax)
jne 0xf2a1d
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x4a5(%rip), %rdx       # 0xf2eb0
movq %rdx, 0x68(%rax,%rcx,8)
movl $0x0, 0x34(%rsp)
jmp 0xf2bad
movl 0x2c(%rsp), %eax
cmpl 0x24(%rsp), %eax
jne 0xf2a54
movl 0x28(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0xf2a54
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x49e(%rip), %rdx       # 0xf2ee0
movq %rdx, 0x68(%rax,%rcx,8)
movl $0x0, 0x34(%rsp)
jmp 0xf2bab
movl 0x2c(%rsp), %eax
shll %eax
cmpl 0x24(%rsp), %eax
jne 0xf2aaf
movl 0x28(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0xf2aaf
cmpl $0x0, 0x30(%rsp)
je 0xf2a94
movq 0x38(%rsp), %rax
cmpl $0x2, 0x28(%rax)
jbe 0xf2a94
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x483(%rip), %rdx       # 0xf2f10
movq %rdx, 0x68(%rax,%rcx,8)
jmp 0xf2aaa
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x62b(%rip), %rdx       # 0xf30d0
movq %rdx, 0x68(%rax,%rcx,8)
jmp 0xf2ba9
movl 0x2c(%rsp), %eax
shll %eax
cmpl 0x24(%rsp), %eax
jne 0xf2b18
movl 0x28(%rsp), %eax
shll %eax
cmpl 0x20(%rsp), %eax
jne 0xf2b18
cmpl $0x0, 0x30(%rsp)
je 0xf2afd
movq 0x38(%rsp), %rax
cmpl $0x2, 0x28(%rax)
jbe 0xf2afd
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x6d6(%rip), %rdx       # 0xf31c0
movq %rdx, 0x68(%rax,%rcx,8)
movq 0x48(%rsp), %rax
movl $0x1, 0x10(%rax)
jmp 0xf2b13
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0x952(%rip), %rdx       # 0xf3460
movq %rdx, 0x68(%rax,%rcx,8)
jmp 0xf2ba7
movl 0x24(%rsp), %eax
cltd
idivl 0x2c(%rsp)
cmpl $0x0, %edx
jne 0xf2b84
movl 0x20(%rsp), %eax
cltd
idivl 0x28(%rsp)
cmpl $0x0, %edx
jne 0xf2b84
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
leaq 0xa4b(%rip), %rdx       # 0xf3590
movq %rdx, 0x68(%rax,%rcx,8)
movl 0x24(%rsp), %eax
cltd
idivl 0x2c(%rsp)
movb %al, %dl
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
movb %dl, 0xe8(%rax,%rcx)
movl 0x20(%rsp), %eax
cltd
idivl 0x28(%rsp)
movb %al, %dl
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
movb %dl, 0xf2(%rax,%rcx)
jmp 0xf2ba5
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl $0x26, 0x28(%rax)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x50(%rsp), %rdi
callq *%rax
jmp 0xf2ba7
jmp 0xf2ba9
jmp 0xf2bab
jmp 0xf2bad
cmpl $0x0, 0x34(%rsp)
je 0xf2c21
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rax
movl 0x88(%rax), %eax
movl %eax, %edi
movq 0x50(%rsp), %rax
movslq 0x198(%rax), %rsi
callq 0xf9330
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movq 0x10(%rsp), %rax
movl %ecx, %edx
movq 0x50(%rsp), %rcx
movl 0x19c(%rcx), %ecx
movl $0x1, %esi
callq *%rax
movq %rax, %rdx
movq 0x48(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq %rdx, 0x18(%rax,%rcx,8)
jmp 0xf2c23
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movq 0x38(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x38(%rsp)
jmp 0xf2960
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)