// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: postProcessScanlines(unsigned char*, unsigned char*, unsigned int, unsigned int, LodePNGInfo const*)
static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
                                     unsigned w, unsigned h, const LodePNGInfo* info_png)
{
  /*
  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_get_bpp(&info_png->color);
  if(bpp == 0) return 31; /*error: invalid colortype*/

  if(info_png->interlace_method == 0)
  {
    if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
    {
      CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp));
      removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
    }
    /*we can immediately filter into the out buffer, no other steps needed*/
    else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp));
  }
  else /*interlace_method 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)
    {
      CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
      /*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*/
      if(bpp < 8)
      {
        /*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 0;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movl %edx, 0x13c(%rsp)
movl %ecx, 0x138(%rsp)
movq %r8, 0x130(%rsp)
movq 0x130(%rsp), %rdi
addq $0x10, %rdi
callq 0x8cd0
movl %eax, 0x12c(%rsp)
cmpl $0x0, 0x12c(%rsp)
jne 0x1720f
movl $0x1f, 0x154(%rsp)
jmp 0x174c9
movq 0x130(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x17351
cmpl $0x8, 0x12c(%rsp)
jae 0x172fb
movl 0x13c(%rsp), %eax
imull 0x12c(%rsp), %eax
movl 0x13c(%rsp), %ecx
imull 0x12c(%rsp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
je 0x172fb
movq 0x140(%rsp), %rdi
movq 0x140(%rsp), %rsi
movl 0x13c(%rsp), %edx
movl 0x138(%rsp), %ecx
movl 0x12c(%rsp), %r8d
callq 0x174e0
movl %eax, 0x128(%rsp)
cmpl $0x0, 0x128(%rsp)
je 0x172ad
movl 0x128(%rsp), %eax
movl %eax, 0x154(%rsp)
jmp 0x174c9
movq 0x148(%rsp), %rdi
movq 0x140(%rsp), %rsi
movl 0x13c(%rsp), %eax
imull 0x12c(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
movl 0x13c(%rsp), %eax
imull 0x12c(%rsp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movl 0x138(%rsp), %r8d
callq 0x17600
jmp 0x1734c
movq 0x148(%rsp), %rdi
movq 0x140(%rsp), %rsi
movl 0x13c(%rsp), %edx
movl 0x138(%rsp), %ecx
movl 0x12c(%rsp), %r8d
callq 0x174e0
movl %eax, 0x124(%rsp)
cmpl $0x0, 0x124(%rsp)
je 0x1734a
movl 0x124(%rsp), %eax
movl %eax, 0x154(%rsp)
jmp 0x174c9
jmp 0x1734c
jmp 0x174be
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x20(%rsp), %r8
movl 0x13c(%rsp), %r9d
movl 0x138(%rsp), %r10d
movl 0x12c(%rsp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x176c0
movl $0x0, 0x1c(%rsp)
cmpl $0x7, 0x1c(%rsp)
je 0x17493
movq 0x140(%rsp), %rdi
movl 0x1c(%rsp), %eax
addq 0x60(%rsp,%rax,8), %rdi
movq 0x140(%rsp), %rsi
movl 0x1c(%rsp), %eax
addq 0xa0(%rsp,%rax,8), %rsi
movl 0x1c(%rsp), %eax
movl 0x100(%rsp,%rax,4), %edx
movl 0x1c(%rsp), %eax
movl 0xe0(%rsp,%rax,4), %ecx
movl 0x12c(%rsp), %r8d
callq 0x174e0
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x1740d
movl 0x18(%rsp), %eax
movl %eax, 0x154(%rsp)
jmp 0x174c9
cmpl $0x8, 0x12c(%rsp)
jae 0x17481
movq 0x140(%rsp), %rdi
movl 0x1c(%rsp), %eax
addq 0x20(%rsp,%rax,8), %rdi
movq 0x140(%rsp), %rsi
movl 0x1c(%rsp), %eax
addq 0x60(%rsp,%rax,8), %rsi
movl 0x1c(%rsp), %eax
movl 0x100(%rsp,%rax,4), %eax
imull 0x12c(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
movl 0x1c(%rsp), %eax
movl 0x100(%rsp,%rax,4), %eax
imull 0x12c(%rsp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movl 0x1c(%rsp), %eax
movl 0xe0(%rsp,%rax,4), %r8d
callq 0x17600
jmp 0x17483
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x1739f
movq 0x148(%rsp), %rdi
movq 0x140(%rsp), %rsi
movl 0x13c(%rsp), %edx
movl 0x138(%rsp), %ecx
movl 0x12c(%rsp), %r8d
callq 0x17940
movl $0x0, 0x154(%rsp)
movl 0x154(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
retq
nopl (%rax,%rax)