// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, (%rsp)
movl 0x1c(%rsi), %ebp
shll $0x3, %ebp
movl 0x30(%rdi), %r13d
movq %rdi, 0x8(%rsp)
movl 0x13c(%rdi), %ebx
movl %ebp, %eax
subl %r13d, %eax
testl %eax, %eax
setle %al
cmpl $-0x1, %ebx
setl %cl
orb %al, %cl
jne 0x772e7
addl $0x2, %ebx
movl %r13d, %r12d
notl %r12d
addl %ebp, %r12d
incq %r12
xorl %r15d, %r15d
movq -0x8(%r14,%r15,8), %rdi
movzbl -0x1(%rdi,%r13), %esi
addq %r13, %rdi
movq %r12, %rdx
callq 0xe260
incq %r15
cmpq %r15, %rbx
jne 0x772c9
movq (%rsp), %rax
cmpl $0x0, 0xc(%rax)
jle 0x77404
movq 0x8(%rsp), %rax
movslq 0x110(%rax), %rax
movq %rax, %rdx
shlq $0x9, %rdx
movl $0x10000, %ecx          # imm = 0x10000
subq %rdx, %rcx
shll $0x6, %eax
addl $-0x8, %ebp
addq $0x6, %rbp
xorl %edx, %edx
movq 0x10(%rsp), %rsi
movq (%rsi,%rdx,8), %rsi
movq -0x8(%r14,%rdx,8), %r8
movq (%r14,%rdx,8), %rdi
movq 0x8(%r14,%rdx,8), %r9
incq %rdx
movzbl (%r8), %r11d
movzbl (%r9), %r10d
addl %r11d, %r10d
movzbl (%rdi), %ebx
addl %ebx, %r10d
movzbl 0x1(%r8), %r11d
movzbl 0x1(%r9), %r15d
addl %r11d, %r15d
movzbl 0x1(%rdi), %r11d
addl %r15d, %r11d
leal (%r10,%r10), %r15d
subl %ebx, %r15d
addl %r11d, %r15d
imull %ecx, %ebx
imull %eax, %r15d
addl %r15d, %ebx
addl $0x8000, %ebx           # imm = 0x8000
shrl $0x10, %ebx
movb %bl, (%rsi)
xorl %r12d, %r12d
movl %r11d, %ebx
movzbl 0x1(%rdi,%r12), %r15d
movzbl 0x2(%r8,%r12), %r11d
movzbl 0x2(%r9,%r12), %r13d
addl %r11d, %r13d
movzbl 0x2(%rdi,%r12), %r11d
addl %r13d, %r11d
addl %ebx, %r10d
subl %r15d, %r10d
addl %r11d, %r10d
imull %ecx, %r15d
imull %eax, %r10d
addl %r15d, %r10d
addl $0x8000, %r10d          # imm = 0x8000
shrl $0x10, %r10d
movb %r10b, 0x1(%rsi,%r12)
incq %r12
movl %ebx, %r10d
cmpl %r12d, %ebp
jne 0x7737e
movzbl 0x1(%rdi,%r12), %edi
leal (%rbx,%r11,2), %r8d
subl %edi, %r8d
imull %ecx, %edi
imull %eax, %r8d
addl %r8d, %edi
addl $0x8000, %edi           # imm = 0x8000
shrl $0x10, %edi
movb %dil, 0x1(%rsi,%r12)
movq (%rsp), %rsi
movslq 0xc(%rsi), %rsi
cmpq %rsi, %rdx
jl 0x7731c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq