// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadPNGStream(_IO_FILE*)
int R2Image::
ReadPNGStream(FILE *fp)
{
#ifdef RN_USE_PNG
  // Create and initialize the png_struct 
  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png_ptr == NULL) {
    fclose(fp);
    return 0;
  }

  // Allocate/initialize the memory for image information. 
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == NULL) {
    fclose(fp);
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    return 0;
  }

  // Set up the input control if you are using standard C streams 
  png_init_io(png_ptr, fp);

  // Read the png info 
  png_read_info(png_ptr, info_ptr);

  // Extract image info 
  png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  png_byte color_type = png_get_color_type(png_ptr, info_ptr);
  width = png_get_image_width(png_ptr, info_ptr);
  height = png_get_image_height(png_ptr, info_ptr);
  rowsize = png_get_rowbytes(png_ptr, info_ptr);
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;

  // Set ncomponents
  // ncomponents = bit_depth/8; ???
  ncomponents = 0;
  if (color_type == PNG_COLOR_TYPE_GRAY) ncomponents = 1;
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ncomponents = 2;
  else if (color_type == PNG_COLOR_TYPE_PALETTE) ncomponents = bit_depth/8;
  else if (color_type == PNG_COLOR_TYPE_RGB) ncomponents = 3;
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) ncomponents = 4;
  else { 
    fclose(fp);
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    return 0;
  }

  // Allocate the pixels and row pointers
  pixels = new unsigned char [ height * rowsize ]; 
  png_bytep *row_pointers = (png_bytep *) png_malloc(png_ptr, height * sizeof(png_bytep));
  for (int i = 0; i < height; i++) row_pointers[i] = &pixels[ (height - i - 1) * rowsize ];

  // Read the pixels 
  png_read_image(png_ptr, row_pointers);

  // Finish reading 
  png_read_end(png_ptr, info_ptr);

  // Free the row pointers 
  png_free(png_ptr, row_pointers);

  // Clean up after the read, and free any memory allocated  
  png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

  // Return success 
  return 1;
#else
  RNFail("PNG not supported");
  return 0;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x3d1d8(%rip), %rdi     # 0xa9789
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8aa30
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6c693
movq %rax, %rdi
callq 0x87bdd
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x6c6a0
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x88160
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8ad8e
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a0cc
movl %eax, %ebp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a0e3
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a0a1
movl %eax, (%rbx)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a0b6
movl %eax, 0x4(%rbx)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x8a06e
testb $0x3, %al
je 0x6c668
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
addl $0x4, %ecx
movl %ecx, %eax
movl %eax, 0xc(%rbx)
movl $0x0, 0x8(%rbx)
cmpb $0x6, %r15b
ja 0x6c6a0
movzbl %r15b, %ecx
leaq 0x3d44d(%rip), %rdx     # 0xa9ad0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x1, %ecx
jmp 0x6c6db
movq %r14, %rdi
callq 0xe430
jmp 0x6c86a
movq %r14, %rdi
callq 0xe430
xorl %r15d, %r15d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8bada
jmp 0x6c86a
shrb $0x3, %bpl
movzbl %bpl, %ecx
jmp 0x6c6db
movl $0x3, %ecx
jmp 0x6c6db
movl $0x2, %ecx
jmp 0x6c6db
movl $0x4, %ecx
movl %ecx, 0x8(%rbx)
movslq 0x4(%rbx), %r14
movslq %eax, %rdi
imulq %r14, %rdi
callq 0xe030
movq %rax, 0x10(%rbx)
movq 0x8(%rsp), %rdi
shlq $0x3, %r14
movq %r14, %rsi
callq 0x92cb5
movq %rax, %r14
movslq 0x4(%rbx), %rdx
testq %rdx, %rdx
jle 0x6c82c
movq 0x10(%rbx), %rax
movslq 0xc(%rbx), %rsi
leal 0x1(%rdx), %ecx
leaq -0x1(%rdx), %rdi
movq %rdi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq %rdx, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
movq %rsi, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
shrl %ecx
shlq $0x4, %rcx
movdqa 0x35b32(%rip), %xmm3    # 0xa2280
xorl %edx, %edx
movdqa 0x35b38(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x35b2f(%rip), %xmm7    # 0xa22a0
movdqa %xmm3, %xmm8
pxor %xmm4, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm0, %xmm9
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
pand %xmm9, %xmm8
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
por %xmm8, %xmm9
movdqa %xmm3, %xmm8
pxor %xmm5, %xmm8
paddq %xmm1, %xmm8
movdqa %xmm8, %xmm10
pmuludq %xmm6, %xmm10
movdqa %xmm8, %xmm11
psrlq $0x20, %xmm11
pmuludq %xmm2, %xmm11
paddq %xmm10, %xmm11
psllq $0x20, %xmm11
pmuludq %xmm2, %xmm8
paddq %xmm11, %xmm8
movd %xmm9, %esi
notl %esi
testb $0x1, %sil
je 0x6c7f7
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, (%r14,%rdx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %esi
testb $0x1, %sil
je 0x6c81b
pshufd $0xee, %xmm8, %xmm8     # xmm8 = xmm8[2,3,2,3]
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, 0x8(%r14,%rdx)
paddq %xmm7, %xmm3
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x6c771
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x8b71f
movq (%rbx), %rdi
leaq 0x10(%rsp), %r15
movq (%r15), %rsi
callq 0x8b786
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x92d32
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x8bada
movl $0x1, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq