// File: 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:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq 0x60(%rsp), %rdi
callq 0x4b80
movl %eax, 0x54(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x11860
movq 0x58(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12190
movl (%rax), %eax
movl %eax, 0x50(%rsp)
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x12190
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
movl 0x50(%rsp), %edx
movl 0x4c(%rsp), %ecx
movl $0x3, %edi
leaq 0x356b5(%rip), %rsi     # 0x4716d
movb $0x0, %al
callq 0x3a770
movl $0x0, 0x48(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jg 0x11d0b
movl 0x44(%rsp), %eax
addl $0x2, %eax
subl 0x50(%rsp), %eax
cltq
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rdi
callq 0x4d50
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x11b26
movq 0x58(%rsp), %rdi
movl 0x44(%rsp), %eax
addl $0x2, %eax
subl 0x50(%rsp), %eax
movslq %eax, %rsi
callq 0x12190
movl (%rax), %eax
movl %eax, 0x14(%rsp)
jmp 0x11b36
movq 0x58(%rsp), %rdi
callq 0x12b00
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
cmpl $0x1, 0x40(%rsp)
jne 0x11b55
jmp 0x11cfb
movq 0x60(%rsp), %rdi
movslq 0x44(%rsp), %rsi
callq 0x4ba0
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %eax
addl 0x48(%rsp), %eax
movslq %eax, %rsi
callq 0x4ba0
movq %rax, 0x30(%rsp)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x11c8f
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x11c7d
movq 0x38(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
callq 0x12110
movw (%rax), %ax
movw %ax, 0x26(%rsp)
movswl 0x26(%rsp), %eax
cltd
idivl 0x40(%rsp)
movw %ax, 0x24(%rsp)
movswl 0x26(%rsp), %eax
cltd
idivl 0x40(%rsp)
movw %dx, %ax
movw %ax, 0x22(%rsp)
movswl 0x22(%rsp), %eax
cmpl $0x0, %eax
jge 0x11c25
movw 0x24(%rsp), %ax
addw $-0x1, %ax
movw %ax, 0x24(%rsp)
movl 0x40(%rsp), %ecx
movswl 0x22(%rsp), %eax
addl %ecx, %eax
movw %ax, 0x22(%rsp)
movw 0x24(%rsp), %ax
movw %ax, 0x10(%rsp)
movq 0x38(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
callq 0x12110
movw 0x10(%rsp), %cx
movw %cx, (%rax)
movw 0x22(%rsp), %ax
movw %ax, 0x12(%rsp)
movq 0x30(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
callq 0x12110
movw 0x12(%rsp), %cx
movw %cx, (%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x11bb0
jmp 0x11c7f
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x11b96
movl 0x40(%rsp), %ecx
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movswl 0x20(%rax), %eax
cltd
idivl %ecx
movl %eax, %ecx
movq (%rsp), %rax
movw %cx, 0x20(%rax)
movl 0x40(%rsp), %ecx
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movswl 0x22(%rax), %eax
cltd
idivl %ecx
movl %eax, %ecx
movq 0x8(%rsp), %rax
movw %cx, 0x22(%rax)
movq 0x30(%rsp), %rax
movw $0x0, 0x20(%rax)
movl 0x40(%rsp), %eax
subl $0x1, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x22(%rax)
movq 0x38(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x28(%rax)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x11acf
movb $0x1, %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)