// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdsample.c
// Function: sep_upsample
METHODDEF(void)
sep_upsample (j_decompress_ptr cinfo,
	      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
	      JDIMENSION in_row_groups_avail,
	      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
	      JDIMENSION out_rows_avail)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  int ci;
  jpeg_component_info * compptr;
  JDIMENSION num_rows;

  /* Fill the conversion buffer, if it's empty */
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
      /* Invoke per-component upsample method.  Notice we pass a POINTER
       * to color_buf[ci], so that fullsize_upsample can change it.
       */
      (*upsample->methods[ci]) (cinfo, compptr,
	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
	upsample->color_buf + ci);
    }
    upsample->next_row_out = 0;
  }

  /* Color-convert and emit rows */

  /* How many we have in the buffer: */
  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  /* Not more than the distance to the end of the image.  Need this test
   * in case the image height is not a multiple of max_v_samp_factor:
   */
  if (num_rows > upsample->rows_to_go) 
    num_rows = upsample->rows_to_go;
  /* And not more than what the client can accept: */
  out_rows_avail -= *out_row_ctr;
  if (num_rows > out_rows_avail)
    num_rows = out_rows_avail;

  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
				     (JDIMENSION) upsample->next_row_out,
				     output_buf + *out_row_ctr,
				     (int) num_rows);

  /* Adjust counts */
  *out_row_ctr += num_rows;
  upsample->rows_to_go -= num_rows;
  upsample->next_row_out += num_rows;
  /* When the buffer is emptied, declare this input row group consumed */
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
    (*in_row_group_ctr)++;
}

// Assembly:
subq $0x58, %rsp
movl 0x60(%rsp), %eax
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movl %ecx, 0x3c(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq 0x260(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movl 0xb8(%rax), %eax
movq 0x50(%rsp), %rcx
cmpl 0x19c(%rcx), %eax
jl 0xf2da7
movl $0x0, 0x1c(%rsp)
movq 0x50(%rsp), %rax
movq 0x130(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0xf2d98
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq 0x68(%rax,%rcx,8), %rax
movq 0x50(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rcx
movslq 0x1c(%rsp), %rdx
movq (%rcx,%rdx,8), %rdx
movq 0x40(%rsp), %rcx
movl (%rcx), %ecx
movq 0x20(%rsp), %r8
movslq 0x1c(%rsp), %r9
imull 0xc0(%r8,%r9,4), %ecx
movl %ecx, %ecx
shlq $0x3, %rcx
addq %rcx, %rdx
movq 0x20(%rsp), %rcx
addq $0x18, %rcx
movslq 0x1c(%rsp), %r8
shlq $0x3, %r8
addq %r8, %rcx
callq *%rax
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
jmp 0xf2d0b
movq 0x20(%rsp), %rax
movl $0x0, 0xb8(%rax)
movq 0x50(%rsp), %rax
movl 0x19c(%rax), %eax
movq 0x20(%rsp), %rcx
subl 0xb8(%rcx), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0xbc(%rcx), %eax
jbe 0xf2de1
movq 0x20(%rsp), %rax
movl 0xbc(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %ecx
movl 0x60(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x60(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x60(%rsp), %eax
jbe 0xf2e04
movl 0x60(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x50(%rsp), %rax
movq 0x268(%rax), %rax
movq 0x8(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x18, %rsi
movq 0x20(%rsp), %rcx
movl 0xb8(%rcx), %edx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl (%r8), %r8d
shlq $0x3, %r8
addq %r8, %rcx
movl 0xc(%rsp), %r8d
callq *%rax
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %rax
movl 0xbc(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0xbc(%rax)
movl 0xc(%rsp), %ecx
movq 0x20(%rsp), %rax
addl 0xb8(%rax), %ecx
movl %ecx, 0xb8(%rax)
movq 0x20(%rsp), %rax
movl 0xb8(%rax), %eax
movq 0x50(%rsp), %rcx
cmpl 0x19c(%rcx), %eax
jl 0xf2ea5
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x58, %rsp
retq
nopw (%rax,%rax)