// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
leaq 0x3dcb7(%rip), %rdi     # 0xa9789
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8ba9f
movq %rax, (%rsp)
testq %rax, %rax
je 0x6bcd0
movq %rax, %rbx
movq %rax, %rdi
callq 0x86dfd
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6bcda
movq %rax, %r15
movl 0x8(%r13), %eax
decl %eax
cmpl $0x4, %eax
jae 0x6bcf1
movl %eax, %eax
leaq 0x3e01f(%rip), %rcx     # 0xa9b40
movl (%rcx,%rax,4), %r9d
movl (%r13), %edx
movl 0x4(%r13), %ecx
subq $0x8, %rsp
xorl %eax, %eax
movq %rbx, %rdi
movq %r15, %rsi
movl $0x8, %r8d
pushq %rax
pushq %rax
pushq %rax
callq 0x87d78
addq $0x20, %rsp
movslq 0x4(%r13), %rsi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x91fa8
movq %rax, %r12
movslq 0x4(%r13), %rdx
testq %rdx, %rdx
jle 0x6bc84
movq 0x10(%r13), %rax
movslq 0xc(%r13), %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 0x366da(%rip), %xmm3    # 0xa2280
xorl %edx, %edx
movdqa 0x366e0(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x366d7(%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 0x6bc4f
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, (%r12,%rdx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %esi
testb $0x1, %sil
je 0x6bc73
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 0x6bbc9
movq %rbx, %rdi
movq %r14, %rsi
callq 0x87383
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8b5d3
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8c1ff
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8b8bf
movq %rbx, %rdi
movq %r12, %rsi
callq 0x92016
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x8c355
movl $0x1, %r15d
jmp 0x6bd02
movq %r14, %rdi
callq 0xe430
jmp 0x6bd02
movq %r14, %rdi
callq 0xe430
xorl %r15d, %r15d
movq %rsp, %rdi
xorl %esi, %esi
callq 0x8c355
jmp 0x6bd02
leaq 0x3e0ff(%rip), %rdi     # 0xa9df7
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x719d2
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop