// File: /workspace/github_cpp_makefile_star2_O1/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)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rbx)
movq %r15, 0x58(%rbx)
movq (%r14), %rdi
leaq 0x3744(%rip), %rsi      # 0x6421
callq 0x2330
testq %rax, %rax
je 0x2e9a
movq %rax, %r14
leaq 0x33c3(%rip), %rdi      # 0x60b8
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2080
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x2e84
movq %rax, %rdi
callq 0x23f0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2e84
movq 0x8(%rsp), %rdi
movl $0x2, %esi
movl $0x3, %edx
callq 0x23a0
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2470
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2240
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2360
movl %eax, %ebp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x21e0
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2490
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2050
movl %eax, %r12d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2480
cmpb $0x3, %bpl
je 0x2e9a
movl %eax, %r13d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2070
movb %r13b, 0x1a(%rsp)
movb %r12b, 0x1b(%rsp)
movl %r15d, 0x4c(%rsp)
movq %r14, 0x58(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x23c0
movl 0x1c(%rsp), %r15d
testl %eax, %eax
je 0x2eb0
xorl %eax, %eax
leaq 0x50(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x40(%rsp), %r9
movl %eax, (%r9)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2260
testl %eax, %eax
setne %r12b
je 0x2ef9
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x20(%rsp), %rdi
callq 0x3782
leaq 0x28(%rbx), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x2350
movq (%r15), %rdi
cmpq %r14, %rdi
je 0x2e66
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x22d0
leaq 0x48(%rbx), %rdi
movl 0x40(%rsp), %esi
callq 0x3656
movl 0x1c(%rsp), %r15d
movl $0x1, 0x88(%rbx)
jmp 0x2ef9
cmpq $0x0, 0x8(%rsp)
je 0x2e9a
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 0x10(%rsp), %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x23c0
testl %eax, %eax
je 0x30c4
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x2030
testl %eax, %eax
setne %r12b
je 0x2ef9
movb 0x20(%rsp), %al
movb %al, 0x20(%rbx)
movl $0x2, 0x88(%rbx)
xorl %ebp, %ebp
testb %r12b, %r12b
jne 0x2f5d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x23c0
testl %eax, %eax
je 0x2f5d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x2160
testl %eax, %eax
je 0x2f5d
movsd 0x3150(%rip), %xmm0     # 0x6088
divsd 0x20(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x60(%rbx)
testb %bpl, %bpl
jne 0x2f56
movl $0x4, 0x88(%rbx)
movb $0x1, 0x84(%rbx)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2090
movq %rax, %r13
movl %r15d, %r12d
imull %r13d, %r12d
movq %r12, %rdi
callq 0x2040
movq %rax, %rbp
movl %r15d, %r14d
leaq (,%r14,8), %rdi
callq 0x2040
movq %rax, %r15
cmpl $0x0, 0x1c(%rsp)
je 0x2fb3
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %edx
addq %rbp, %rdx
movq %rdx, (%r15,%rcx,8)
incq %rcx
addq %r13, %rax
cmpq %rcx, %r14
jne 0x2f9f
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x20d0
movl $0x8, %esi
movq %r15, %rdi
callq 0x22d0
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2280
cmpq $0x0, 0x10(%rsp)
je 0x2ff0
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2430
cmpq $0x0, 0x8(%rsp)
je 0x3006
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21b0
movq 0x58(%rsp), %rdi
callq 0x22b0
leaq 0x30(%rsp), %r15
movq %r15, -0x10(%r15)
addq %rbp, %r12
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3782
movl $0x1, %esi
movq %rbp, %rdi
callq 0x22d0
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x2bfe
movl 0x4c(%rsp), %esi
movb 0x1b(%rsp), %al
movb 0x1a(%rsp), %cl
movsbl %cl, %ecx
movsbl %al, %r8d
leaq 0x88(%rsp), %r9
movq %rbx, %rdi
movl 0x1c(%rsp), %edx
callq 0x3682
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x30a4
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x22d0
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x2e9e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x2e9e
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x23c0
testl %eax, %eax
je 0x31b7
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x60(%rsp), %rax
leaq 0x68(%rsp), %r10
leaq 0x70(%rsp), %r11
leaq 0x78(%rsp), %r14
leaq 0x20(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %rax
pushq %r10
pushq %r11
pushq %r14
callq 0x2440
addq $0x20, %rsp
testl %eax, %eax
setne %bpl
je 0x31b9
cvtsd2ss 0x20(%rsp), %xmm0
movss %xmm0, 0x64(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x50(%rsp), %xmm0
movss %xmm0, 0x68(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x40(%rsp), %xmm0
movss %xmm0, 0x6c(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x80(%rsp), %xmm0
movss %xmm0, 0x70(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x78(%rsp), %xmm0
movss %xmm0, 0x74(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x70(%rsp), %xmm0
movss %xmm0, 0x78(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x68(%rsp), %xmm0
movss %xmm0, 0x7c(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x60(%rsp), %xmm0
movss %xmm0, 0x80(%rbx)
movl $0x400ccccd, 0x60(%rbx) # imm = 0x400CCCCD
movl $0x3, 0x88(%rbx)
jmp 0x31b9
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x2efb
jmp 0x3211
jmp 0x3211
jmp 0x3211
jmp 0x3211
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x31ee
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x31ee
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x3214
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x22d0
jmp 0x3214
jmp 0x3211
jmp 0x3211
jmp 0x3211
jmp 0x3211
jmp 0x3211
movq %rax, %r14
leaq 0x20(%rbx), %rdi
callq 0x374e
movq %rbx, %rdi
callq 0x2a0a
movq %r14, %rdi
callq 0x2400
nop