// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/./import/read_png.h
// Function: read_PNG_file(char const*)
Image read_PNG_file(const char *input_image) {

  FILE *fp = fopen(input_image,"rb");
  if (!fp) return Image();

  png_byte header[8];
  int rr = fread(header,1,8,fp);
  int is_png = !png_sig_cmp(header,0,rr);
  if (!is_png) {
    fclose(fp);
    return Image();
  }
  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,(png_voidp) NULL,NULL,NULL);

  if (!png_ptr) {
    fclose(fp);
    return Image();
  }
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr,(png_infopp) NULL,(png_infopp) NULL);
    fclose(fp);
    return Image();
  }
  if (setjmp(png_jmpbuf(png_ptr))) {
       png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
       fclose(fp);
       return Image();
  }
  png_init_io(png_ptr,fp);
  png_set_sig_bytes(png_ptr,8);


//  int png_flags = PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_GRAY_TO_RGB;
//  png_read_png(png_ptr,info_ptr, png_flags, NULL);
  png_read_info(png_ptr,info_ptr);

  size_t width = png_get_image_width(png_ptr,info_ptr);
  size_t height = png_get_image_height(png_ptr,info_ptr);
  int bit_depth = png_get_bit_depth(png_ptr,info_ptr);
  int color_type = png_get_color_type(png_ptr,info_ptr);

  unsigned int nbplanes;

  if (color_type == PNG_COLOR_TYPE_PALETTE)
        png_set_palette_to_rgb(png_ptr);

  if (png_get_valid(png_ptr, info_ptr,
        PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);

  if (color_type == PNG_COLOR_TYPE_GRAY &&
        bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
/*
  if (bit_depth == 16)
#if PNG_LIBPNG_VER >= 10504
       png_set_scale_16(png_ptr);
#else
       png_set_strip_16(png_ptr);
#endif
*/
  if (bit_depth < 8)
       png_set_packing(png_ptr);
//  if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
//       png_set_gray_to_rgb(png_ptr);
  png_read_update_info(png_ptr, info_ptr);
  color_type = png_get_color_type(png_ptr,info_ptr);


  if (color_type == PNG_COLOR_TYPE_GRAY) nbplanes=1;
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nbplanes=2;
  else if (color_type == PNG_COLOR_TYPE_RGB) nbplanes=3;
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) nbplanes=4;
  else { png_destroy_read_struct(&png_ptr,(png_infopp) NULL,(png_infopp) NULL); fclose(fp); return Image(); }

  int w = width, h = height, maxval = (bit_depth > 8 ? 65535 : 255);

  int bytes_per_pixel = (bit_depth > 8 ? 2 : 1);
  Image image(w,h,maxval,nbplanes);

  std::vector<unsigned char> one_row(w*nbplanes*bytes_per_pixel,0);
  png_bytep rowptr = &(one_row[0]);
  if (bit_depth <= 8) {
    int y=0;
    while (y < h) {
      png_read_row(png_ptr, rowptr, NULL);
      for(int x=0; x < w ; x++) {
       for (int k=0; k<nbplanes; k++) {
        image.channel[k].data[x+y*w] = one_row[x*nbplanes+k];
       }
      }
      y++;
    }
  } else {
    int y=0;
    while (y < h) {
      png_read_row(png_ptr, rowptr, NULL);
      int index=0;
      for(int x=0; x < w ; x++) {
       for (int k=0; k<nbplanes; k++) {
        image.channel[k].data[x+y*w] = (one_row[index++] << 8);
        image.channel[k].data[x+y*w] += one_row[index++];
       }
      }
      y++;
    }
  }
  png_read_end(png_ptr, info_ptr);
  png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp) NULL);
  fclose(fp);
  return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rax
movq %rdi, %rbx
leaq 0x6e93(%rip), %rsi      # 0x1ea1f
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0x17e60
movq %rax, %r14
leaq 0x60(%rsp), %r15
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %rax, %rcx
callq 0x3480
movslq %eax, %rdx
xorl %eax, %eax
movl %eax, %esi
movq %r15, %rdi
callq 0x3210
testl %eax, %eax
jne 0x17e58
leaq 0x753c(%rip), %rdi      # 0x1f110
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x3070
movq %rax, (%rsp)
testq %rax, %rax
je 0x17e58
movq %rax, %rdi
callq 0x35c0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x17e49
movq (%rsp), %rdi
movq 0x1439f(%rip), %rsi     # 0x2bfb0
movl $0xc8, %edx
callq 0x3330
movq %rax, %rdi
callq 0x3460
testl %eax, %eax
je 0x17c38
xorl %eax, %eax
movl %eax, %edx
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
jmp 0x17e53
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x3600
movq (%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x30c0
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3270
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3200
movl %eax, %ebp
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3630
movl %eax, %r13d
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3050
movl %eax, %r12d
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3440
movl %eax, %r15d
cmpb $0x3, %al
jne 0x17cae
movq (%rsp), %rdi
callq 0x34c0
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3520
testl %eax, %eax
je 0x17ccc
movq (%rsp), %rdi
callq 0x3110
testb %r15b, %r15b
sete %al
cmpb $0x8, %r12b
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x17ceb
movq (%rsp), %rdi
callq 0x31d0
jmp 0x17cf1
cmpb $0x7, %r12b
ja 0x17cfa
movq (%rsp), %rdi
callq 0x31b0
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3060
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3440
cmpb $0x7, %al
jae 0x17e49
movzbl %al, %eax
pushq $0x55
popq %rcx
btl %eax, %ecx
jae 0x17e49
movl %ebp, %esi
movl %r13d, %edx
leaq 0x7387(%rip), %rcx      # 0x1f0c0
movl (%rcx,%rax,4), %r15d
cmpb $0x9, %r12b
movb %r12b, 0xf(%rsp)
setae %r12b
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovael %eax, %ecx
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
xorl %ebx, %ebx
movl %r15d, %r8d
movl %ebx, %r9d
callq 0xfe5a
movl %r15d, %eax
imull %ebp, %eax
movl %r12d, %ecx
shll %cl, %eax
movl %eax, %esi
leaq 0x1f(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x48(%rsp), %rdi
leaq 0x1e(%rsp), %rcx
callq 0x1c5d0
movq %r14, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
testl %ebp, %ebp
movl %ebx, %eax
cmovgl %ebp, %eax
testl %r13d, %r13d
cmovgl %r13d, %ebx
movslq %ebp, %rdx
movq %rdx, 0x30(%rsp)
movl %ebx, %ecx
movq %rcx, 0x28(%rsp)
movl %eax, %r13d
cmpb $0x9, 0xf(%rsp)
jae 0x17e7a
movq %r15, %r12
shlq $0x6, %r12
xorl %ebp, %ebp
movl %ebp, %r14d
cmpq 0x28(%rsp), %r14
je 0x17f09
movq (%rsp), %rdi
movl %ebp, %edx
movq 0x38(%rsp), %rsi
callq 0x3490
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %r14, %rdx
imulq 0x30(%rsp), %rdx
xorl %esi, %esi
movl %esi, %edi
cmpq %r13, %rdi
je 0x17e44
movl %ebp, %r10d
leaq (%rdi,%rdi), %r8
movl %esi, %r9d
cmpq %r10, %r12
je 0x17e3c
movl %r9d, %r11d
movzbl (%rax,%r11), %r11d
movq (%rcx,%r10), %rbx
addq %r8, %rbx
movw %r11w, (%rbx,%rdx,2)
addq $0x40, %r10
incl %r9d
jmp 0x17e1a
incq %rdi
addl %r15d, %esi
jmp 0x17e0b
incq %r14
jmp 0x17dd6
xorl %eax, %eax
movl %eax, %edx
movq %rsp, %rdi
movq %rdx, %rsi
callq 0x31c0
movq %r14, %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0xd18a
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shlq $0x5, %r15
xorl %r12d, %r12d
movl %r12d, %ebp
cmpq 0x28(%rsp), %rbp
je 0x17f09
movq (%rsp), %rdi
movl %r12d, %edx
movq 0x38(%rsp), %rsi
callq 0x3490
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %rbp, %rdx
imulq 0x30(%rsp), %rdx
movl %r12d, %esi
movq %rsi, %rdi
cmpq %r13, %rsi
je 0x17f01
movslq %edi, %r9
movl %r12d, %r8d
leaq (%rsi,%rsi), %rdi
cmpq %r8, %r15
je 0x17ef9
movzbl (%rax,%r9), %r10d
shll $0x8, %r10d
movq (%rcx,%r8,2), %rbx
addq %rdi, %rbx
movw %r10w, (%rbx,%rdx,2)
movzbl 0x1(%rax,%r9), %r11d
addq $0x2, %r9
orl %r10d, %r11d
movw %r11w, (%rbx,%rdx,2)
addq $0x20, %r8
jmp 0x17ec7
incq %rsi
movq %r9, %rdi
jmp 0x17eb8
incq %rbp
jmp 0x17e84
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x32a0
xorl %eax, %eax
movl %eax, %edx
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x31c0
movq 0x40(%rsp), %rdi
callq 0x32c0
leaq 0x48(%rsp), %rdi
callq 0xaeea
movq 0x20(%rsp), %rbx
jmp 0x17e68
movq %rax, %r14
jmp 0x17f5c
jmp 0x17f4f
jmp 0x17f4f
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0xaeea
movq 0x20(%rsp), %rdi
callq 0xa776
movq %r14, %rdi
callq 0x35d0