// File: /workspace/github_cpp_makefile_star2_O2/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 0x37d6a(%rip), %rdi     # 0x9a719
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7ca10
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x62a78
movq %rax, %rbx
movq %rax, %rdi
callq 0x7a260
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x62a85
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7a79c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7cd59
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c0e8
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c0ff
movl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c0bd
movl %eax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c0d2
movl %eax, 0x4(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c08a
pushq $0x4
popq %rcx
testb $0x3, %al
je 0x62a51
cltd
idivl %ecx
leal 0x4(,%rax,4), %eax
movl %eax, 0xc(%r15)
andl $0x0, 0x8(%r15)
cmpb $0x6, %r13b
ja 0x62a85
movzbl %r13b, %edx
leaq 0x38015(%rip), %rsi     # 0x9aa80
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
pushq $0x1
jmp 0x62ab3
movq %r12, %rdi
callq 0xe440
jmp 0x62b48
movq %r12, %rdi
callq 0xe440
xorl %r14d, %r14d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7daad
jmp 0x62b48
pushq $0x3
jmp 0x62ab3
shrb $0x3, %bpl
movzbl %bpl, %ecx
jmp 0x62ab4
pushq $0x2
popq %rcx
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 0x848bb
movq %rax, %r12
movq 0x10(%r15), %rax
movl 0x4(%r15), %ecx
movl 0xc(%r15), %edx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
decl %ecx
imull %edx, %ecx
cmpq %rsi, %rdi
je 0x62b12
movslq %ecx, %rcx
leaq (%rax,%rcx), %r8
movq %r8, (%r12,%rsi,8)
incq %rsi
subl %edx, %ecx
jmp 0x62afb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7d6fd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7d765
movq %rbx, %rdi
movq %r12, %rsi
callq 0x84923
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x7daad
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq