// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r15
leaq 0x3df86(%rip), %rdi     # 0xa9789
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x89c28
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6b8d5
movq %rax, %rbx
movq %rax, %rdi
callq 0x86dfd
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x6b8e2
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x87383
movq %rbx, %rdi
movq %r14, %rsi
callq 0x89f96
movq %rbx, %rdi
movq %r14, %rsi
callq 0x892c8
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x892df
movl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8929d
movl %eax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x892b2
movl %eax, 0x4(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8926a
testb $0x3, %al
je 0x6b8a8
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
addl $0x4, %ecx
movl %ecx, %eax
movl %eax, 0xc(%r15)
movl $0x0, 0x8(%r15)
cmpb $0x6, %r13b
ja 0x6b8e2
movzbl %r13b, %ecx
leaq 0x3e233(%rip), %rdx     # 0xa9af8
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x1, %ecx
jmp 0x6b91d
movq %r12, %rdi
callq 0xe430
jmp 0x6baa5
movq %r12, %rdi
callq 0xe430
xorl %r14d, %r14d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8ad0d
jmp 0x6baa5
shrb $0x3, %bpl
movzbl %bpl, %ecx
jmp 0x6b91d
movl $0x3, %ecx
jmp 0x6b91d
movl $0x2, %ecx
jmp 0x6b91d
movl $0x4, %ecx
movl %ecx, 0x8(%r15)
movslq 0x4(%r15), %r12
movslq %eax, %rdi
imulq %r12, %rdi
callq 0xe030
movq %rax, 0x10(%r15)
shlq $0x3, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x91fa8
movq %rax, %r12
movslq 0x4(%r15), %rdx
testq %rdx, %rdx
jle 0x6ba6d
movq 0x10(%r15), %rax
movslq 0xc(%r15), %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 0x368f1(%rip), %xmm3    # 0xa2280
xorl %edx, %edx
movdqa 0x368f7(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x368ee(%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 0x6ba38
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, (%r12,%rdx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %esi
testb $0x1, %sil
je 0x6ba5c
pshufd $0xee, %xmm8, %xmm8     # xmm8 = xmm8[2,3,2,3]
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, 0x8(%r12,%rdx)
paddq %xmm7, %xmm3
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x6b9b2
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8a952
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8a9b9
movq %rbx, %rdi
movq %r12, %rsi
callq 0x92016
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x8ad0d
movl $0x1, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop