// File: /workspace/github_cpp_makefile_star2_O1/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 %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x3cefb(%rip), %rdi     # 0xa9789
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8c841
movq %rax, (%rsp)
testq %rax, %rax
je 0x6ca91
movq %rax, %rdi
callq 0x87bdd
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6ca9b
movl 0x8(%r15), %ecx
decl %ecx
cmpl $0x4, %ecx
jae 0x6cab2
movl %ecx, %ecx
leaq 0x3d249(%rip), %rdx     # 0xa9b20
movl (%rdx,%rcx,4), %r9d
movq (%rsp), %rdi
movl (%r15), %edx
movl 0x4(%r15), %ecx
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rax, %rsi
movl $0x8, %r8d
pushq %r10
pushq %r10
pushq %r10
callq 0x88b68
addq $0x20, %rsp
movq (%rsp), %rdi
movslq 0x4(%r15), %rsi
shlq $0x3, %rsi
callq 0x92cb5
movq %rax, %r14
movslq 0x4(%r15), %rdx
testq %rdx, %rdx
jle 0x6ca3f
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 0x3591f(%rip), %xmm3    # 0xa2280
xorl %edx, %edx
movdqa 0x35925(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x3591c(%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 0x6ca0a
movq %xmm8, %rsi
addq %rax, %rsi
movq %rsi, (%r14,%rdx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %esi
testb $0x1, %sil
je 0x6ca2e
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 0x6c984
movq %rsp, %r15
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x88160
movq (%r15), %rdi
leaq 0x8(%rsp), %rbx
movq (%rbx), %rsi
callq 0x8c378
movq (%r15), %rdi
movq %r14, %rsi
callq 0x8cfc2
movq (%r15), %rdi
movq (%rbx), %rsi
callq 0x8c659
movq (%r15), %rdi
movq %r14, %rsi
callq 0x92d32
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8d10f
movl $0x1, %r14d
jmp 0x6cac3
movq %rbx, %rdi
callq 0xe430
jmp 0x6cac3
movq %rbx, %rdi
callq 0xe430
xorl %r14d, %r14d
movq %rsp, %rdi
xorl %esi, %esi
callq 0x8d10f
jmp 0x6cac3
leaq 0x3d31e(%rip), %rdi     # 0xa9dd7
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x72a1e
movl %r14d, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq