// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::WritePNGStream(_IO_FILE*) const
int R2Image::
WritePNGStream(FILE *fp) const
{
#ifdef RN_USE_PNG
  // Create and initialize the png_struct 
  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png_ptr == NULL) {
    fclose(fp);
    return 0;
  }
  
  // Allocate/initialize the image information data. 
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == NULL) {
    fclose(fp);
    png_destroy_write_struct(&png_ptr,  NULL);
    return 0;
  }
  
  // Determine color type  
  png_byte color_type = 0;
  if (ncomponents == 1) color_type = PNG_COLOR_TYPE_GRAY;
  else if (ncomponents == 2) color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  else if (ncomponents == 3) color_type = PNG_COLOR_TYPE_RGB;
  else if (ncomponents == 4) color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  else { RNFail("Invalid number of components in PNG file\n"); return 0; }

  // Fill in the image data 
  png_set_IHDR(png_ptr, info_ptr, width, height,
    8, color_type, PNG_INTERLACE_NONE,
    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

  // Allocate the row pointers 
  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];
  
  // Set up the output control 
  png_init_io(png_ptr, fp);
  
  // Write the png info 
  png_write_info(png_ptr, info_ptr);
  
  // Write the pixels 
  png_write_image(png_ptr, row_pointers);
  
  // Finish writing 
  png_write_end(png_ptr, info_ptr);
  
  // Free the row pointers 
  png_free(png_ptr, row_pointers);

  // Clean up after the write, and free any memory allocated 
  png_destroy_write_struct(&png_ptr, &info_ptr);

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

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
movq %rdi, %r12
leaq 0x37ba5(%rip), %rdi     # 0x9a719
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7e7b7
movq %rax, (%rsp)
testq %rax, %rax
je 0x62c3a
movq %rax, %rbx
movq %rax, %rdi
callq 0x7a260
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x62c44
movq %rax, %r15
movl 0x8(%r12), %eax
decl %eax
cmpl $0x4, %eax
jae 0x62c5b
movl %eax, %eax
leaq 0x37efc(%rip), %rcx     # 0x9aac0
movl (%rcx,%rax,4), %r9d
movl (%r12), %edx
movl 0x4(%r12), %ecx
subq $0x8, %rsp
pushq $0x8
popq %r8
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x0
pushq $0x0
pushq $0x0
callq 0x7b0d1
addq $0x20, %rsp
movslq 0x4(%r12), %rsi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x848bb
movq %rax, %r13
movq 0x10(%r12), %rax
movl 0x4(%r12), %ecx
movl 0xc(%r12), %edx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
decl %ecx
imull %edx, %ecx
cmpq %rsi, %rdi
je 0x62c6e
movslq %ecx, %rcx
leaq (%rax,%rcx), %r8
movq %r8, (%r13,%rsi,8)
incq %rsi
subl %edx, %ecx
jmp 0x62c22
movq %r14, %rdi
callq 0xe440
jmp 0x62cb6
movq %r14, %rdi
callq 0xe440
xorl %r15d, %r15d
movq %rsp, %rdi
xorl %esi, %esi
callq 0x7f05b
jmp 0x62cb6
leaq 0x38115(%rip), %rdi     # 0x9ad77
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x681ca
jmp 0x62cb6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7a79c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7e303
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7ef09
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7e5f1
movq %rbx, %rdi
movq %r13, %rsi
callq 0x84923
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x7f05b
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop