// File: /workspace/github_cpp_makefile_star2_O2/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 %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x262c
leaq 0x38(%rbx), %rax
movq %rax, 0x28(%rbx)
andq $0x0, 0x30(%rbx)
movb $0x0, 0x38(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x48(%rbx)
andq $0x0, 0x58(%rbx)
movq (%r14), %rdi
leaq 0x382c(%rip), %rsi      # 0x63e6
callq 0x2320
testq %rax, %rax
je 0x2d3f
movq %rax, %r15
leaq 0x28(%rbx), %r14
leaq 0x3472(%rip), %rdi      # 0x6048
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2070
movq %rax, (%rsp)
testq %rax, %rax
je 0x2d3f
movq %rax, %rdi
callq 0x2410
movq %rax, %r13
movq %rax, 0x80(%rsp)
movq (%rsp), %rdi
testq %rax, %rax
je 0x2d2e
pushq $0x2
popq %rsi
pushq $0x3
popq %rdx
callq 0x23a0
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x2490
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2250
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2370
movl %eax, %ebp
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2200
movl %eax, %r12d
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x24d0
movl %eax, 0xc(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2050
movb %al, 0xb(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x24c0
movb %al, 0xa(%rsp)
cmpb $0x3, %bpl
je 0x2d3f
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2060
movq %r14, 0x10(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x23d0
testl %eax, %eax
je 0x2d55
leaq 0x48(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x18(%rsp), %r9
andl $0x0, (%r9)
movq (%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2270
testl %eax, %eax
je 0x2e5c
movl %r12d, %ebp
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x28(%rsp), %rdi
callq 0x3544
leaq 0x48(%rbx), %r14
leaq 0x28(%rsp), %r12
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x2340
movq %r12, %rdi
callq 0x2140
movl 0x18(%rsp), %esi
movq %r14, %rdi
callq 0x343a
movl %ebp, %r12d
movl $0x1, 0x88(%rbx)
jmp 0x2eb3
testq %rdi, %rdi
je 0x2d3f
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
movb $0x0, 0x18(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
movq %r13, %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x23d0
testl %eax, %eax
je 0x2d99
movq (%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %r13, %rsi
callq 0x2030
testl %eax, %eax
je 0x2e5c
movb 0x28(%rsp), %al
movb %al, 0x20(%rbx)
movl $0x2, 0x88(%rbx)
jmp 0x2eb3
movq (%rsp), %rdi
pushq $0x4
popq %rdx
movq %r13, %rsi
callq 0x23d0
testl %eax, %eax
je 0x2e5c
movq (%rsp), %rdi
leaq 0x58(%rsp), %rax
leaq 0x60(%rsp), %r10
leaq 0x68(%rsp), %r11
leaq 0x70(%rsp), %r14
leaq 0x28(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x78(%rsp), %r9
movq %r13, %rsi
pushq %rax
pushq %r10
pushq %r11
pushq %r14
callq 0x2460
addq $0x20, %rsp
testl %eax, %eax
setne %bpl
je 0x2e5e
movsd 0x28(%rsp), %xmm0
movhpd 0x48(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movsd 0x18(%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 0x2e5e
xorl %ebp, %ebp
movq (%rsp), %rdi
pushq $0x1
popq %rdx
movq %r13, %rsi
callq 0x23d0
testl %eax, %eax
je 0x2eb3
movq (%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %r13, %rsi
callq 0x2180
testl %eax, %eax
je 0x2eb3
movsd 0x318a(%rip), %xmm0     # 0x6018
divsd 0x28(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x60(%rbx)
testb %bpl, %bpl
jne 0x2eac
movl $0x4, 0x88(%rbx)
movb $0x1, 0x84(%rbx)
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x2080
movq %rax, %r14
movl %r12d, 0x24(%rsp)
movq %r15, 0x50(%rsp)
movl 0xc(%rsp), %r15d
movl %r15d, %ebp
imull %r14d, %ebp
movq %rbp, %rdi
callq 0x2040
movq %rax, %r13
movl %r15d, %r15d
leaq (,%r15,8), %rdi
callq 0x2040
movq %rax, %r12
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x2f10
movl %eax, %edx
addq %r13, %rdx
movq %rdx, (%r12,%rcx,8)
incq %rcx
addq %r14, %rax
jmp 0x2efa
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x20d0
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x22e0
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x22a0
movq (%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2450
cmpq $0x0, (%rsp)
je 0x2f56
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
movq 0x50(%rsp), %rdi
callq 0x22c0
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r13, %rdx
addq %rbp, %rdx
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x3544
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x22e0
leaq 0x88(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2360
movl 0x24(%rsp), %esi
movl 0xc(%rsp), %edx
movsbl 0xa(%rsp), %ecx
movsbl 0xb(%rsp), %r8d
leaq 0x88(%rsp), %r9
movq %rbx, %rdi
callq 0x3466
leaq 0x88(%rsp), %rdi
callq 0x2140
leaq 0x28(%rsp), %rdi
callq 0x2140
jmp 0x2d43
jmp 0x3010
jmp 0x3010
jmp 0x3010
jmp 0x3010
movq %rax, %r15
leaq 0x88(%rsp), %rdi
callq 0x2140
jmp 0x2ff5
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x2140
jmp 0x3013
jmp 0x3010
jmp 0x3010
jmp 0x3010
jmp 0x3010
jmp 0x3010
movq %r14, 0x10(%rsp)
movq %rax, %r15
movq 0x10(%rsp), %rdi
callq 0x3502
movq %rbx, %rdi
callq 0x28f4
movq %r15, %rdi
callq 0x2420
nop