// File: mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: postProcessScanlines(unsigned char*, unsigned char*, LodePNG_InfoPng const*)
static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, const LodePNG_InfoPng* infoPng) /*return value is error*/
{
  /*
  This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. Steps:
  *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8)
  *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
  NOTE: the in buffer will be overwritten with intermediate data!
  */
  unsigned bpp = LodePNG_InfoColor_getBpp(&infoPng->color);
  unsigned w = infoPng->width;
  unsigned h = infoPng->height;
  unsigned error = 0;
  if(bpp == 0) return 31; /*error: invalid colortype*/
  
  if(infoPng->interlaceMethod == 0)
  {
    if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
    {
      error = unfilter(in, in, w, h, bpp);
      if(error) return error;
      removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
    }
    else error = unfilter(out, in, w, h, bpp); /*we can immediatly filter into the out buffer, no other steps needed*/
  }
  else /*interlaceMethod is 1 (Adam7)*/
  {
    unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
    unsigned i;
    
    Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
    
    for(i = 0; i < 7; i++)
    {
      error = unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp);
      if(error) return error;
      if(bpp < 8) /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, move bytes instead of bits or move not at all*/
      {
        /*remove padding bits in scanlines; after this there still may be padding bits between the different reduced images: each reduced image still starts nicely at a byte*/
        removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, ((passw[i] * bpp + 7) / 8) * 8, passh[i]);
      }
    }
    
    Adam7_deinterlace(out, in, w, h, bpp);
  }
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x26220
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x2f8f3
movl $0x1f, -0x4(%rbp)
jmp 0x2fad9
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x2f99e
cmpl $0x8, -0x24(%rbp)
jae 0x2f97f
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
imull -0x24(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
je 0x2f97f
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0x2faf0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x2f94d
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2fad9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x2c(%rbp), %r8d
callq 0x2fbf0
jmp 0x2f999
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0x2faf0
movl %eax, -0x30(%rbp)
jmp 0x2fad3
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
leaq -0xf0(%rbp), %rcx
leaq -0x130(%rbp), %r8
movl -0x28(%rbp), %r9d
movl -0x2c(%rbp), %r10d
movl -0x24(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x2fca0
movl $0x0, -0x134(%rbp)
cmpl $0x7, -0x134(%rbp)
jae 0x2fabc
movq -0x18(%rbp), %rdi
movl -0x134(%rbp), %eax
addq -0xf0(%rbp,%rax,8), %rdi
movq -0x18(%rbp), %rsi
movl -0x134(%rbp), %eax
addq -0xb0(%rbp,%rax,8), %rsi
movl -0x134(%rbp), %eax
movl -0x50(%rbp,%rax,4), %edx
movl -0x134(%rbp), %eax
movl -0x70(%rbp,%rax,4), %ecx
movl -0x24(%rbp), %r8d
callq 0x2faf0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x2fa3f
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2fad9
cmpl $0x8, -0x24(%rbp)
jae 0x2faa6
movq -0x18(%rbp), %rdi
movl -0x134(%rbp), %eax
addq -0x130(%rbp,%rax,8), %rdi
movq -0x18(%rbp), %rsi
movl -0x134(%rbp), %eax
addq -0xf0(%rbp,%rax,8), %rsi
movl -0x134(%rbp), %eax
movl -0x50(%rbp,%rax,4), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
movl -0x134(%rbp), %eax
movl -0x50(%rbp,%rax,4), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x134(%rbp), %eax
movl -0x70(%rbp,%rax,4), %r8d
callq 0x2fbf0
jmp 0x2faa8
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x2f9dd
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0x2fee0
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)