// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/jpeg/jcsample.c
// Function: fullsize_smooth_downsample
METHODDEF(void)
fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
			    JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int outrow;
  JDIMENSION colctr;
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  INT32 membersum, neighsum, memberscale, neighscale;
  int colsum, lastcolsum, nextcolsum;

  /* Expand input data enough to let all the output samples be generated
   * by the standard loop.  Special-casing padded output would be more
   * efficient.
   */
  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
		    cinfo->image_width, output_cols);

  /* Each of the eight neighbor pixels contributes a fraction SF to the
   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
   * Also recall that SF = smoothing_factor / 1024.
   */

  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */

  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
    outptr = output_data[outrow];
    inptr = input_data[outrow];
    above_ptr = input_data[outrow-1];
    below_ptr = input_data[outrow+1];

    /* Special case for first column */
    colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
	     GETJSAMPLE(*inptr);
    membersum = GETJSAMPLE(*inptr++);
    nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
		 GETJSAMPLE(*inptr);
    neighsum = colsum + (colsum - membersum) + nextcolsum;
    membersum = membersum * memberscale + neighsum * neighscale;
    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
    lastcolsum = colsum; colsum = nextcolsum;

    for (colctr = output_cols - 2; colctr > 0; colctr--) {
      membersum = GETJSAMPLE(*inptr++);
      above_ptr++; below_ptr++;
      nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
		   GETJSAMPLE(*inptr);
      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
      membersum = membersum * memberscale + neighsum * neighscale;
      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
      lastcolsum = colsum; colsum = nextcolsum;
    }

    /* Special case for last column */
    membersum = GETJSAMPLE(*inptr);
    neighsum = lastcolsum + (colsum - membersum) + colsum;
    membersum = membersum * memberscale + neighsum * neighscale;
    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);

  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rdi, %r12
movq %rsi, 0x10(%rsp)
movl 0x1c(%rsi), %r13d
leal (,%r13,8), %ecx
leaq -0x8(%rdx), %rdi
movl 0x30(%r12), %edx
movl 0x13c(%r12), %esi
addl $0x2, %esi
callq 0x6c3d0
movslq 0x110(%r12), %rbx
movq %rbx, %rdx
shlq $0x9, %rdx
movl $0x10000, %ecx          # imm = 0x10000
subq %rdx, %rcx
shll $0x6, %ebx
leal -0x8(,%r13,8), %edx
orq $0x6, %rdx
xorl %esi, %esi
movq 0x10(%rsp), %rax
movslq 0xc(%rax), %rdi
cmpq %rdi, %rsi
jge 0x6bea2
movq 0x8(%rsp), %rax
movq (%rax,%rsi,8), %rdi
movq -0x8(%r14,%rsi,8), %r9
movq (%r14,%rsi,8), %r8
movq 0x8(%r14,%rsi,8), %r10
incq %rsi
movzbl (%r9), %ebp
movzbl (%r10), %r11d
addl %ebp, %r11d
movzbl (%r8), %r12d
addl %r12d, %r11d
movzbl 0x1(%r9), %ebp
movzbl 0x1(%r10), %r13d
addl %ebp, %r13d
movzbl 0x1(%r8), %ebp
addl %r13d, %ebp
leal (%r11,%r11), %r13d
subl %r12d, %r13d
addl %ebp, %r13d
imull %ecx, %r12d
movl %ebx, %eax
imull %r13d, %eax
addl %r12d, %eax
addl $0x8000, %eax           # imm = 0x8000
shrl $0x10, %eax
movb %al, (%rdi)
xorl %r12d, %r12d
movl %ebp, %r13d
cmpl %r12d, %edx
je 0x6be76
movzbl 0x1(%r8,%r12), %eax
movzbl 0x2(%r9,%r12), %ebp
movzbl 0x2(%r10,%r12), %r15d
addl %ebp, %r15d
movzbl 0x2(%r8,%r12), %ebp
addl %r15d, %ebp
addl %r13d, %r11d
subl %eax, %r11d
addl %ebp, %r11d
imull %ecx, %eax
movl %ebx, %r15d
imull %r11d, %r15d
addl %r15d, %eax
addl $0x8000, %eax           # imm = 0x8000
shrl $0x10, %eax
movb %al, 0x1(%rdi,%r12)
incq %r12
movl %r13d, %r11d
jmp 0x6be25
movzbl 0x1(%r8,%r12), %eax
leal (%r11,%r13,2), %r8d
subl %eax, %r8d
imull %ecx, %eax
movl %ebx, %r9d
imull %r8d, %r9d
addl %r9d, %eax
addl $0x8000, %eax           # imm = 0x8000
shrl $0x10, %eax
movb %al, 0x1(%rdi,%r12)
jmp 0x6bdae
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq