// File: /workspace/github_cpp_makefile_star2_O1/cloudinary[P]fuif/transform/approximate.h
// Function: fwd_approximate(Image&, std::vector<int, std::allocator<int>>&)
bool fwd_approximate(Image &input, std::vector<int> &parameters) {
    int offset = input.channel.size();
    meta_approximate(input,parameters);
    int beginc = parameters[0];
    int endc = parameters[1];
    v_printf(3,"Approximating channels %i-%i\n",beginc,endc);
    int i=0;
    for (int c=beginc; c<=endc; c++) {
        int q = (c+2-beginc < parameters.size() ? parameters[c+2-beginc] : parameters.back());
        q += 1;
        if (q == 1) continue;
        Channel &ch = input.channel[c];
        Channel &chr = input.channel[offset+i];
        i++;
        for (int y=0; y<ch.h; y++) {
          for (int x=0; x<ch.w; x++) {
            pixel_type p = ch.value(y,x);
            pixel_type quotient = p/q;
            pixel_type r = p%q;
            if (r < 0) { quotient--; r+=q; }
            ch.value(y,x) = quotient;
            chr.value(y,x) = r;
          }
        }
        ch.minval /= q;
        ch.maxval /= q;
        chr.minval = 0;
        chr.maxval = q-1;
        chr.q = ch.q; // need to duplicate the quantization factor in case approximated channel becomes all zeroes
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq 0x8(%rdi), %rax
subq (%rdi), %rax
movq %rax, 0x30(%rsp)
callq 0xd603
movq (%r15), %rax
movl (%rax), %ebx
movl 0x4(%rax), %ebp
leaq 0x18b2d(%rip), %rsi     # 0x2623d
xorl %r12d, %r12d
movl $0x3, %edi
movl %ebx, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1d0d6
movl %ebx, 0xc(%rsp)
movl %ebp, 0x8(%rsp)
cmpl %ebp, %ebx
jg 0xd95b
shrq $0x6, 0x30(%rsp)
movslq 0xc(%rsp), %rcx
movq (%r15), %rax
movq 0x8(%r15), %rdx
movq %rdx, %rsi
movq %rax, 0x60(%rsp)
subq %rax, %rsi
sarq $0x2, %rsi
movq %rsi, 0x50(%rsp)
addq $-0x4, %rdx
movq %rdx, 0x58(%rsp)
movq (%r14), %rax
movq %rax, 0x48(%rsp)
incl 0x8(%rsp)
movl %ecx, %eax
subl 0xc(%rsp), %eax
addl $0x2, %eax
cltq
cmpq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
cmovbeq 0x58(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
je 0xd94e
movq %rax, 0x68(%rsp)
leal 0x1(%rax), %r15d
movq %rcx, 0x70(%rsp)
movq %rcx, %rax
shlq $0x6, %rax
movq 0x48(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x30(%rsp), %rcx
movq %r12, 0x78(%rsp)
addl %r12d, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movslq 0x1c(%rdx,%rax), %rax
movq %rax, 0x90(%rsp)
testq %rax, %rax
jle 0xd90a
movq 0x18(%rsp), %rax
movl 0x18(%rax), %ecx
leaq 0x24(%rax), %r10
movq 0x10(%rsp), %rax
leaq 0x24(%rax), %rdi
movq %rcx, 0x40(%rsp)
movslq %ecx, %rax
movq %rax, 0x88(%rsp)
addq %rax, %rax
movq %rax, 0x80(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
xorl %esi, %esi
cmpl $0x0, 0x40(%rsp)
jle 0xd8cf
movq 0x18(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %r8
subq %r12, %r8
sarq %r8
movq 0x10(%rsp), %rcx
movslq 0x18(%rcx), %rbp
movq (%rcx), %rax
movq 0x8(%rcx), %r11
subq %rax, %r11
sarq %r11
addq 0x20(%rsp), %r12
movq %rsi, %rcx
imulq %rbp, %rcx
imulq 0x38(%rsp), %rbp
addq %rax, %rbp
movq 0x40(%rsp), %r14
movq 0x28(%rsp), %r9
cmpq %r9, %r8
movq %r10, %rbx
cmovaq %r12, %rbx
movswl (%rbx), %eax
cltd
idivl %r15d
movl %edx, %r13d
sarl $0x1f, %edx
addl %edx, %eax
andl %r15d, %edx
addl %r13d, %edx
cmpq %rcx, %r11
movq %rdi, %r13
cmovaq %rbp, %r13
movw %ax, (%rbx)
movw %dx, (%r13)
addq $0x2, %r12
incq %r9
incq %rcx
addq $0x2, %rbp
decq %r14
jne 0xd88b
incq %rsi
movq 0x20(%rsp), %rax
addq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x2, 0x38(%rsp)
cmpq 0x90(%rsp), %rsi
jne 0xd839
movq 0x78(%rsp), %r12
incl %r12d
movq 0x18(%rsp), %rcx
movswl 0x20(%rcx), %eax
cltd
idivl %r15d
movw %ax, 0x20(%rcx)
movswl 0x22(%rcx), %eax
cltd
idivl %r15d
movw %ax, 0x22(%rcx)
movq 0x10(%rsp), %rdx
movw $0x0, 0x20(%rdx)
movq 0x68(%rsp), %rax
movw %ax, 0x22(%rdx)
movl 0x28(%rcx), %eax
movl %eax, 0x28(%rdx)
movq 0x70(%rsp), %rcx
incq %rcx
cmpl %ecx, 0x8(%rsp)
jne 0xd76e
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq