// File: mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: preProcessScanlines(unsigned char**, unsigned long*, unsigned char const*, LodePNG_InfoPng const*)
static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, const LodePNG_InfoPng* infoPng) /*return value is error*/
{
  /*
  This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
  *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter
  *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
  */
  unsigned bpp = LodePNG_InfoColor_getBpp(&infoPng->color);
  unsigned w = infoPng->width;
  unsigned h = infoPng->height;
  unsigned error = 0;
  
  if(infoPng->interlaceMethod == 0)
  {
    *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/
    *out = (unsigned char*)malloc(*outsize);
    if(!(*out) && (*outsize)) error = 9950;

    if(!error)
    {
      if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
      {
        ucvector padded;
        ucvector_init(&padded);
        if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9951;
        if(!error)
        {
          addPaddingBits(padded.data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
          error = filter(*out, padded.data, w, h, &infoPng->color);
        }
        ucvector_cleanup(&padded);
      }
      else error = filter(*out, in, w, h, &infoPng->color); /*we can immediatly filter into the out buffer, no other steps needed*/
    }
  }
  else /*interlaceMethod is 1 (Adam7)*/
  {
    unsigned char* adam7 = (unsigned char*)malloc((h * w * bpp + 7) / 8);
    if(!adam7 && ((h * w * bpp + 7) / 8)) error = 9952; /*malloc failed*/
    
    while(!error) /*not a real while loop, used to break out to cleanup to avoid a goto*/
    {
      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);
      
      *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
      *out = (unsigned char*)malloc(*outsize);
      if(!(*out) && (*outsize)) { error = 9953; break; }
      
      Adam7_interlace(adam7, in, w, h, bpp);
      
      for(i = 0; i < 7; i++)
      {
        if(bpp < 8)
        {
          ucvector padded;
          ucvector_init(&padded);
          if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9954;
          if(!error)
          {
            addPaddingBits(&padded.data[padded_passstart[i]], &adam7[passstart[i]], ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
            error = filter(&(*out)[filter_passstart[i]], &padded.data[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
          }
          
          ucvector_cleanup(&padded);
        }
        else
        {
          error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
        }
      }
      
      break;
    }

    free(adam7);
  }
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -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)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x2a919
movl -0x2c(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl -0x28(%rbp), %edx
imull -0x24(%rbp), %edx
addl $0x7, %edx
shrl $0x3, %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5570
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2a827
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2a827
movl $0x26de, -0x30(%rbp)    # imm = 0x26DE
cmpl $0x0, -0x30(%rbp)
jne 0x2a914
cmpl $0x8, -0x24(%rbp)
jae 0x2a8f1
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 0x2a8f1
leaq -0x48(%rbp), %rdi
callq 0x25960
movl -0x2c(%rbp), %eax
movl -0x28(%rbp), %ecx
imull -0x24(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x24470
cmpl $0x0, %eax
jne 0x2a88f
movl $0x26df, -0x30(%rbp)    # imm = 0x26DF
cmpl $0x0, -0x30(%rbp)
jne 0x2a8e6
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x2c(%rbp), %r8d
callq 0x30900
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x18, %r8
callq 0x309e0
movl %eax, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x25990
jmp 0x2a912
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x18, %r8
callq 0x309e0
movl %eax, -0x30(%rbp)
jmp 0x2a914
jmp 0x2ab9d
movl -0x2c(%rbp), %eax
imull -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x5570
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2a95b
movl -0x2c(%rbp), %eax
imull -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
cmpl $0x0, %eax
je 0x2a95b
movl $0x26e0, -0x30(%rbp)    # imm = 0x26E0
jmp 0x2a95d
cmpl $0x0, -0x30(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2a96f
jmp 0x2ab94
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
leaq -0x110(%rbp), %rcx
leaq -0x150(%rbp), %r8
movl -0x28(%rbp), %r9d
movl -0x2c(%rbp), %r10d
movl -0x24(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x2fca0
movq -0x98(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5570
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2a9eb
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2a9eb
movl $0x26e1, -0x30(%rbp)    # imm = 0x26E1
jmp 0x2ab94
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0x30df0
movl $0x0, -0x154(%rbp)
cmpl $0x7, -0x154(%rbp)
jae 0x2ab92
cmpl $0x8, -0x24(%rbp)
jae 0x2ab2e
leaq -0x170(%rbp), %rdi
callq 0x25960
movl -0x2c(%rbp), %eax
movl -0x28(%rbp), %ecx
imull -0x24(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x170(%rbp), %rdi
callq 0x24470
cmpl $0x0, %eax
jne 0x2aa5e
movl $0x26e2, -0x30(%rbp)    # imm = 0x26E2
cmpl $0x0, -0x30(%rbp)
jne 0x2ab20
movq -0x170(%rbp), %rdi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rdi
movq -0x50(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x150(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %eax
imull -0x24(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %r8d
callq 0x30900
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x154(%rbp), %eax
addq -0xd0(%rbp,%rax,8), %rdi
movq -0x170(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %edx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %ecx
movq -0x20(%rbp), %r8
addq $0x18, %r8
callq 0x309e0
movl %eax, -0x30(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x25990
jmp 0x2ab7c
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x154(%rbp), %eax
addq -0xd0(%rbp,%rax,8), %rdi
movq -0x50(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %edx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %ecx
movq -0x20(%rbp), %r8
addq $0x18, %r8
callq 0x309e0
movl %eax, -0x30(%rbp)
jmp 0x2ab7e
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x2aa0c
jmp 0x2ab94
movq -0x50(%rbp), %rdi
callq 0x54b0
movl -0x30(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)