// File: /workspace/github_cpp_makefile_star2_O3/shirat74[P]resample/PNGImage.cc
// Function: PNGImage::PNGImage(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
PNGImage::PNGImage (const std::string filename) : Image(0, 0, 0, 0) // dummy
{
  FILE       *fp;
  png_structp png_ptr;
  png_infop   png_info_ptr;
  png_byte    color_type, bpc, nComps;
  png_uint_32 width, height, rowbytes;

  fp = fopen(filename.c_str(), FOPEN_RBIN_MODE);
  if(!fp) {
    isValid = false;
    return;
  }

  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png_ptr == NULL ||
      (png_info_ptr = png_create_info_struct(png_ptr)) == NULL) {
    if (png_ptr)
      png_destroy_read_struct(&png_ptr, NULL, NULL);
    isValid = false;
    return;
  }

#if PNG_LIBPNG_VER >= 10603
  // ignore possibly incorrect CMF bytes
  png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);
#endif

  // Inititializing file IO.
  png_init_io(png_ptr, fp);

  // Read PNG info-header and get some info.
  png_read_info(png_ptr, png_info_ptr);
  color_type = png_get_color_type  (png_ptr, png_info_ptr);
  width      = png_get_image_width (png_ptr, png_info_ptr);
  height     = png_get_image_height(png_ptr, png_info_ptr);
  bpc        = png_get_bit_depth   (png_ptr, png_info_ptr);
  nComps     = png_get_channels    (png_ptr, png_info_ptr);

  if (color_type == PNG_COLOR_TYPE_PALETTE) {
    isValid = false;
    return;
  }
  png_read_update_info(png_ptr, png_info_ptr);

  // Read colorspace information.
  bool hassRGB, hasiCCP, hasgAMA, hascHRM;
  hassRGB = hasiCCP = hasgAMA = hascHRM = false;
  // Precedence in this order.
  if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_iCCP)) {
    png_charpp  name    = NULL;
    int         compression_type = 0;
    png_bytepp  profile = NULL;
    png_uint_32 proflen = 0;
    if (png_get_iCCP(png_ptr, png_info_ptr,
                     name, &compression_type, profile, &proflen)) {
      colorSpace.ICCP.name = std::string(*name, strlen(*name));
      colorSpace.ICCP.profile.resize(proflen);
      for (size_t i = 0; i < proflen; i++)
        colorSpace.ICCP.profile[i] = (*profile)[i];
      colorSpaceType = png_colorspace_iccp;
      hasiCCP = true;
    }
  } else if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_sRGB)) {
    int  intent;
    if (png_get_sRGB(png_ptr, png_info_ptr, &intent)) {
      colorSpace.sRGB = intent;
      colorSpaceType  = png_colorspace_srgb;
      hassRGB = true;
    }
  } else if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_cHRM)) {
    double xw, yw, xr, yr, xg, yg, xb, yb;
    if (png_get_cHRM(png_ptr, png_info_ptr, &xw, &yw,
                     &xr, &yr, &xg, &yg, &xb, &yb)) {
      colorSpace.calRGB.xw = xw;
      colorSpace.calRGB.yw = yw;
      colorSpace.calRGB.xr = xr;
      colorSpace.calRGB.yr = yr;
      colorSpace.calRGB.xg = xg;
      colorSpace.calRGB.yg = yg;
      colorSpace.calRGB.xb = xb;
      colorSpace.calRGB.yb = yb;
      colorSpace.gamma = 2.2;    // maybe wrong but PhotoShop assume this?
      colorSpaceType = png_colorspace_calibrated;
      hascHRM = true;
    }
  }
  if (!hasiCCP && !hassRGB &&
      png_get_valid(png_ptr, png_info_ptr, PNG_INFO_gAMA)) {
    double G;
    if (png_get_gAMA(png_ptr, png_info_ptr, &G)) {
      colorSpace.gamma = 1.0 / G;
      if (!hascHRM)
        colorSpaceType = png_colorspace_gamma_only;
      colorSpace.hasGamma = hasgAMA = true;
    }
  }

  // Read raster image data.
  rowbytes = png_get_rowbytes(png_ptr, png_info_ptr);
  png_bytep stream_data_ptr = new png_byte[rowbytes * height];

  png_bytepp rows_p = new png_bytep[height];
  for (uint32_t i = 0; i < height; i++)
    rows_p[i] = &(stream_data_ptr[rowbytes * i]);
  png_read_image(png_ptr, rows_p);
  delete rows_p;

  // Reading file finished.
  png_read_end(png_ptr, NULL);

  // Cleanup.
  if (png_info_ptr)
    png_destroy_info_struct(png_ptr, &png_info_ptr);
  if (png_ptr)
    png_destroy_read_struct(&png_ptr, NULL, NULL);
  fclose(fp);

  std::string raster(reinterpret_cast<const char *>(stream_data_ptr),
                     rowbytes * height);
  delete stream_data_ptr;

  // Actual timing of construction of Image base class is here.
  Image::reset(width, height, nComps, bpc, raster);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x260c
leaq 0x38(%rbx), %rax
movq %rax, 0x28(%rbx)
movq %r15, 0x30(%rbx)
movb $0x0, 0x38(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x48(%rbx)
movq %r15, 0x58(%rbx)
movq (%r14), %rdi
leaq 0x37a2(%rip), %rsi      # 0x6471
callq 0x2330
testq %rax, %rax
je 0x2e6e
movq %rax, %r13
leaq 0x3429(%rip), %rdi      # 0x6110
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2080
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x2e6e
movq %rax, %rdi
callq 0x23f0
movq %rax, %rbp
movq %rax, 0x80(%rsp)
movq 0x8(%rsp), %rdi
testq %rax, %rax
je 0x2e5b
movl $0x2, %esi
movl $0x3, %edx
callq 0x23a0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2470
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2240
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2360
movl %eax, %r12d
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x21e0
movl %eax, %r14d
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2490
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2050
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2480
movb %al, 0x13(%rsp)
cmpb $0x3, %r12b
je 0x2e6e
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2070
movb %r15b, 0x12(%rsp)
movl %r14d, 0x44(%rsp)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x23c0
testl %eax, %eax
je 0x2e84
xorl %eax, %eax
leaq 0x48(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x38(%rsp), %r9
movl %eax, (%r9)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2260
testl %eax, %eax
je 0x2f91
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x18(%rsp), %rdi
callq 0x37be
leaq 0x28(%rbx), %rdi
leaq 0x48(%rbx), %r15
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x2350
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x2e40
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x22d0
movl 0x38(%rsp), %esi
movq %r15, %rdi
callq 0x3698
movl $0x1, 0x88(%rbx)
jmp 0x2fed
testq %rdi, %rdi
je 0x2e6e
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21b0
movb $0x0, 0x18(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x23c0
testl %eax, %eax
je 0x2eca
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %rbp, %rsi
callq 0x2030
testl %eax, %eax
je 0x2f91
movb 0x18(%rsp), %al
movb %al, 0x20(%rbx)
movl $0x2, 0x88(%rbx)
jmp 0x2fed
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x4, %edx
callq 0x23c0
testl %eax, %eax
je 0x2f91
movq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rax
leaq 0x60(%rsp), %r10
leaq 0x68(%rsp), %r11
leaq 0x70(%rsp), %r14
leaq 0x18(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x78(%rsp), %r9
movq %rbp, %rsi
pushq %rax
pushq %r10
pushq %r11
pushq %r14
callq 0x2440
addq $0x20, %rsp
testl %eax, %eax
setne %r15b
je 0x2f94
movsd 0x18(%rsp), %xmm0
movhpd 0x48(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movsd 0x38(%rsp), %xmm1
movhpd 0x78(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd %xmm0, 0x64(%rbx)
movsd 0x70(%rsp), %xmm0
movhpd 0x68(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movsd 0x60(%rsp), %xmm1
movhpd 0x58(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd %xmm0, 0x74(%rbx)
movl $0x400ccccd, 0x60(%rbx) # imm = 0x400CCCCD
movl $0x3, 0x88(%rbx)
jmp 0x2f94
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x23c0
testl %eax, %eax
je 0x2fed
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %rbp, %rsi
callq 0x2160
testl %eax, %eax
je 0x2fed
movsd 0x30c0(%rip), %xmm0     # 0x6088
divsd 0x18(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x60(%rbx)
testb %r15b, %r15b
jne 0x2fe6
movl $0x4, 0x88(%rbx)
movb $0x1, 0x84(%rbx)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2090
movq %rax, %r15
movq %r13, 0x50(%rsp)
movl 0x14(%rsp), %r14d
movl %r14d, %r12d
imull %r15d, %r12d
movq %r12, %rdi
callq 0x2040
movq %rax, %rbp
movl %r14d, %r14d
leaq (,%r14,8), %rdi
callq 0x2040
movq %rax, %r13
cmpl $0x0, 0x14(%rsp)
je 0x3104
leaq -0x1(%r14), %rax
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq %r15, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
leaq 0x8(,%r14,8), %rax
andq $-0x10, %rax
movdqa 0x303d(%rip), %xmm2     # 0x60a0
xorl %ecx, %ecx
movdqa 0x3043(%rip), %xmm3     # 0x60b0
pxor %xmm3, %xmm0
movdqa 0x3047(%rip), %xmm4     # 0x60c0
pcmpeqd %xmm5, %xmm5
movdqa 0x304b(%rip), %xmm6     # 0x60d0
movdqa %xmm2, %xmm7
pxor %xmm3, %xmm7
movdqa %xmm7, %xmm8
pcmpgtd %xmm0, %xmm8
pcmpeqd %xmm0, %xmm7
pshufd $0xf5, %xmm7, %xmm7     # xmm7 = xmm7[1,1,3,3]
pand %xmm8, %xmm7
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
por %xmm7, %xmm8
movdqa %xmm2, %xmm7
pmuludq %xmm1, %xmm7
pand %xmm4, %xmm7
movd %xmm8, %edx
notl %edx
testb $0x1, %dl
je 0x30d5
movq %xmm7, %rdx
addq %rbp, %rdx
movq %rdx, (%r13,%rcx)
pxor %xmm5, %xmm8
pextrw $0x4, %xmm8, %edx
testb $0x1, %dl
je 0x30f7
pshufd $0xee, %xmm7, %xmm7     # xmm7 = xmm7[2,3,2,3]
movq %xmm7, %rdx
addq %rbp, %rdx
movq %rdx, 0x8(%r13,%rcx)
paddq %xmm6, %xmm2
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x3085
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x20d0
movl $0x8, %esi
movq %r13, %rdi
callq 0x22d0
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2280
movq 0x8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2430
cmpq $0x0, 0x8(%rsp)
je 0x3152
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21b0
movq 0x50(%rsp), %rdi
callq 0x22b0
leaq 0x28(%rsp), %r15
movq %r15, -0x10(%r15)
addq %rbp, %r12
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x37be
movl $0x1, %esi
movq %rbp, %rdi
callq 0x22d0
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x2be8
movl 0x14(%rsp), %edx
movsbl 0x13(%rsp), %ecx
movsbl 0x12(%rsp), %r8d
leaq 0x88(%rsp), %r9
movq %rbx, %rdi
movl 0x44(%rsp), %esi
callq 0x36c4
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x31ec
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x22d0
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x2e72
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x2e72
jmp 0x325c
jmp 0x325c
jmp 0x325c
jmp 0x325c
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x3239
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x3239
movq %rax, %r14
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x325f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x325f
jmp 0x325c
jmp 0x325c
jmp 0x325c
jmp 0x325c
jmp 0x325c
movq %rax, %r14
leaq 0x20(%rbx), %rdi
callq 0x378a
movq %rbx, %rdi
callq 0x29e4
movq %r14, %rdi
callq 0x2400