// File: cloudinary[P]fuif/transform/palette.h
// Function: inv_palette(Image&, std::vector<int, std::allocator<int>>)
bool inv_palette(Image &input, std::vector<int> parameters) {
    if (input.nb_meta_channels < 1) {
        e_printf("Error: Palette transform without palette.\n");
        return false;
    }
    if (parameters.size() != 3) {
        e_printf("Error: Palette transform with incorrect parameters.\n");
        return false;
    }
    int nb = input.channel[0].h;
    int c0 = input.nb_meta_channels + parameters[0];
    if (c0 >= input.channel.size()) {
        e_printf("Error: Palette transform with incorrect parameters.\n");
        return false;
    }
    int w = input.channel[c0].w;
    int h = input.channel[c0].h;
    // might be false in case of lossy
    //assert(input.channel[c0].minval == 0);
    //assert(input.channel[c0].maxval == palette.w-1);
    for (int i=1; i<nb; i++) {
        input.channel.insert(input.channel.begin()+c0+1, Channel(w,h,0,1));
        input.channel[c0+i].component = parameters[0]+i;
    }
    const Channel &palette = input.channel[0];
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int index = CLAMP(input.channel[c0].value(y,x),0,palette.w-1);
            for (int c=0; c<nb; c++)
                input.channel[c0+c].value(y,x) = palette.value(c,index);
        }
    }
    input.nb_channels += nb-1;
    input.nb_meta_channels--;
    input.channel.erase(input.channel.begin(),input.channel.begin()+1);
    return true;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rsi, 0x60(%rsp)
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movq 0x148(%rsp), %rax
cmpl $0x1, 0x6c(%rax)
jge 0xa405
leaq 0x3c62b(%rip), %rdi     # 0x46a1c
movb $0x0, %al
callq 0x3a670
movb $0x0, 0x157(%rsp)
jmp 0xa921
movq 0x60(%rsp), %rdi
callq 0x4d50
cmpq $0x3, %rax
je 0xa430
leaq 0x3c62b(%rip), %rdi     # 0x46a47
movb $0x0, %al
callq 0x3a670
movb $0x0, 0x157(%rsp)
jmp 0xa921
movq 0x148(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ba0
movq 0x60(%rsp), %rdi
movl 0x1c(%rax), %eax
movl %eax, 0x13c(%rsp)
movq 0x148(%rsp), %rax
movl 0x6c(%rax), %eax
movl %eax, 0x54(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x12190
movq %rax, %rcx
movl 0x54(%rsp), %eax
addl (%rcx), %eax
movl %eax, 0x138(%rsp)
movslq 0x138(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x148(%rsp), %rdi
callq 0x4b80
movq %rax, %rcx
movq 0x58(%rsp), %rax
cmpq %rcx, %rax
jb 0xa4ba
leaq 0x3c5a1(%rip), %rdi     # 0x46a47
movb $0x0, %al
callq 0x3a670
movb $0x0, 0x157(%rsp)
jmp 0xa921
movq 0x148(%rsp), %rdi
movslq 0x138(%rsp), %rsi
callq 0x4ba0
movl 0x18(%rax), %eax
movl %eax, 0x134(%rsp)
movq 0x148(%rsp), %rdi
movslq 0x138(%rsp), %rsi
callq 0x4ba0
movl 0x1c(%rax), %eax
movl %eax, 0x130(%rsp)
movl $0x1, 0x12c(%rsp)
movl 0x12c(%rsp), %eax
cmpl 0x13c(%rsp), %eax
jge 0xa67a
movq 0x148(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x127d0
movq %rax, 0x108(%rsp)
movslq 0x138(%rsp), %rsi
leaq 0x108(%rsp), %rdi
callq 0x12800
movq %rax, 0x110(%rsp)
leaq 0x110(%rsp), %rdi
movl $0x1, %esi
callq 0x12800
movq %rax, 0x118(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x12840
movl 0x134(%rsp), %esi
movl 0x130(%rsp), %edx
movq %rsp, %rax
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
xorl %ecx, %ecx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x12200
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq 0x120(%rsp), %rsi
callq 0x12b60
movq %rax, 0x48(%rsp)
jmp 0xa5e4
movq 0x48(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x12380
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12190
movl (%rax), %eax
addl 0x12c(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x148(%rsp), %rdi
movl 0x138(%rsp), %eax
addl 0x12c(%rsp), %eax
movslq %eax, %rsi
callq 0x4ba0
movl 0x34(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0xa503
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x12380
jmp 0xa932
movq 0x148(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ba0
movq %rax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x130(%rsp), %eax
jge 0xa862
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x134(%rsp), %eax
jge 0xa84a
movq 0x148(%rsp), %rdi
movslq 0x138(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0xa4(%rsp), %esi
movl 0xa0(%rsp), %edx
callq 0x12110
movswl (%rax), %eax
cmpl $0x0, %eax
jge 0xa70f
xorl %eax, %eax
movl %eax, 0x30(%rsp)
jmp 0xa79d
movq 0x148(%rsp), %rdi
movslq 0x138(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0xa4(%rsp), %esi
movl 0xa0(%rsp), %edx
callq 0x12110
movswl (%rax), %eax
movq 0xa8(%rsp), %rcx
movl 0x18(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0xa763
movq 0xa8(%rsp), %rax
movl 0x18(%rax), %eax
subl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xa795
movq 0x148(%rsp), %rdi
movslq 0x138(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0xa4(%rsp), %esi
movl 0xa0(%rsp), %edx
callq 0x12110
movswl (%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0x13c(%rsp), %eax
jge 0xa832
movq 0xa8(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x9c(%rsp), %edx
callq 0x12ba0
movw %ax, 0x2a(%rsp)
movq 0x148(%rsp), %rdi
movl 0x138(%rsp), %eax
addl 0x98(%rsp), %eax
movslq %eax, %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0xa4(%rsp), %esi
movl 0xa0(%rsp), %edx
callq 0x12110
movw 0x2a(%rsp), %cx
movw %cx, (%rax)
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0xa7b3
jmp 0xa834
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0xa6bd
jmp 0xa84c
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xa69e
movl 0x13c(%rsp), %ecx
subl $0x1, %ecx
movq 0x148(%rsp), %rax
addl 0x64(%rax), %ecx
movl %ecx, 0x64(%rax)
movq 0x148(%rsp), %rax
movl 0x6c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x6c(%rax)
movq 0x148(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x148(%rsp), %rdi
callq 0x127d0
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x12840
movq 0x148(%rsp), %rdi
callq 0x127d0
movq %rax, 0x70(%rsp)
leaq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x12800
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x12840
movq 0x20(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x80(%rsp), %rdx
callq 0x12730
movq %rax, 0x68(%rsp)
movb $0x1, 0x157(%rsp)
movb 0x157(%rsp), %al
andb $0x1, %al
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xc0(%rsp), %rdi
callq 0x36a0
nop