// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::WritePNGStream(_IO_FILE*) const
int R2Grid::
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;
  }
  
  // Fill in the image data
  int width = grid_resolution[0];
  int height = grid_resolution[1];
  png_set_IHDR(png_ptr, info_ptr, width, height,
    16, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

  // Copy the data into RNUInt16
  png_bytep data = new png_byte [ 2*grid_size ];
  png_bytep datap = data;
  for (int i = 0; i < grid_size; i++) {
    RNScalar value = grid_values[i];
    if (value == R2_GRID_UNKNOWN_VALUE) {
      *datap++ = 0;
      *datap++ = 0;
    }
    else {
      if (value > 65535) value = 65535;
      unsigned int ivalue = (unsigned int) value;
      *datap++ = (ivalue >> 8) & 0xFF;
      *datap++ = ivalue & 0xFF;
    }
  }

  // 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] = (png_bytep) &data[(height - i - 1) * 2*width];
  
  // 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);

  // Delete copy of data
  delete [] data;

  // 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, %rbx
movq %rdi, %r12
leaq 0x459b2(%rip), %rdi     # 0xa9789
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8c841
movq %rax, (%rsp)
testq %rax, %rax
je 0x6405a
movq %rax, %rdi
callq 0x87bdd
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x64064
movq %rbx, 0x8(%rsp)
movl 0xc0(%r12), %ebp
movl 0xc4(%r12), %r13d
movslq %r13d, %r15
movq (%rsp), %rdi
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rax, %rsi
movl %ebp, %edx
movl %r15d, %ecx
movl $0x10, %r8d
xorl %r9d, %r9d
pushq %r10
pushq %r10
pushq %r10
callq 0x88b68
addq $0x20, %rsp
movslq 0xcc(%r12), %rbx
leaq (%rbx,%rbx), %rdi
callq 0xe030
movq %rax, %r14
testq %rbx, %rbx
jle 0x63eb2
movq 0xb8(%r12), %rax
xorl %ecx, %ecx
movsd 0x3f142(%rip), %xmm0    # 0xa2fb8
movsd 0x454aa(%rip), %xmm1    # 0xa9328
movsd (%rax,%rcx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x63e94
jp 0x63e94
movw $0x0, (%r14,%rcx,2)
jmp 0x63eaa
movapd %xmm1, %xmm3
minsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdx
rolw $0x8, %dx
movw %dx, (%r14,%rcx,2)
incq %rcx
cmpq %rcx, %rbx
jne 0x63e7e
movq (%rsp), %rdi
shlq $0x3, %r15
movq %r15, %rsi
callq 0x92cb5
movq %rax, %r15
testl %r13d, %r13d
movq 0x8(%rsp), %rsi
jle 0x63ffe
addl %ebp, %ebp
leal 0x1(%r13), %eax
leaq -0x1(%r13), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movd %r13d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movd %ebp, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
shrl %eax
shlq $0x4, %rax
movq 0x45478(%rip), %xmm3    # 0xa9380
movdqa 0x3e370(%rip), %xmm4    # 0xa2280
xorl %ecx, %ecx
movdqa 0x3e376(%rip), %xmm5    # 0xa2290
pxor %xmm5, %xmm0
pcmpeqd %xmm6, %xmm6
pshufd $0xf5, %xmm2, %xmm7     # xmm7 = xmm2[1,1,3,3]
movdqa 0x3e370(%rip), %xmm8    # 0xa22a0
movdqa 0x45457(%rip), %xmm9    # 0xa9390
movdqa %xmm4, %xmm10
pxor %xmm5, %xmm10
movdqa %xmm10, %xmm11
pcmpgtd %xmm0, %xmm11
pcmpeqd %xmm0, %xmm10
pshufd $0xf5, %xmm10, %xmm12   # xmm12 = xmm10[1,1,3,3]
pand %xmm11, %xmm12
pshufd $0xf5, %xmm11, %xmm10   # xmm10 = xmm11[1,1,3,3]
por %xmm12, %xmm10
movdqa %xmm3, %xmm12
pxor %xmm6, %xmm12
paddd %xmm1, %xmm12
movdqa %xmm2, %xmm11
pmuludq %xmm12, %xmm11
pshufd $0xe8, %xmm11, %xmm11   # xmm11 = xmm11[0,2,2,3]
pshufd $0xf5, %xmm12, %xmm12   # xmm12 = xmm12[1,1,3,3]
pmuludq %xmm7, %xmm12
pshufd $0xe8, %xmm12, %xmm12   # xmm12 = xmm12[0,2,2,3]
punpckldq %xmm12, %xmm11  # xmm11 = xmm11[0],xmm12[0],xmm11[1],xmm12[1]
pxor %xmm12, %xmm12
pcmpgtd %xmm11, %xmm12
punpckldq %xmm12, %xmm11  # xmm11 = xmm11[0],xmm12[0],xmm11[1],xmm12[1]
movd %xmm10, %edx
notl %edx
testb $0x1, %dl
je 0x63fc4
movq %xmm11, %rdx
addq %r14, %rdx
movq %rdx, (%r15,%rcx)
pxor %xmm6, %xmm10
pextrw $0x4, %xmm10, %edx
testb $0x1, %dl
je 0x63fe7
pshufd $0xee, %xmm11, %xmm10   # xmm10 = xmm11[2,3,2,3]
movq %xmm10, %rdx
addq %r14, %rdx
movq %rdx, 0x8(%r15,%rcx)
paddq %xmm8, %xmm4
paddd %xmm9, %xmm3
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x63f39
movq %rsp, %r12
movq (%r12), %rdi
callq 0x88160
movq (%r12), %rdi
leaq 0x10(%rsp), %rbx
movq (%rbx), %rsi
callq 0x8c378
movq (%r12), %rdi
movq %r15, %rsi
callq 0x8cfc2
movq (%r12), %rdi
movq (%rbx), %rsi
callq 0x8c659
movq (%r12), %rdi
movq %r15, %rsi
callq 0x92d32
movq %r12, %rdi
movq %rbx, %rsi
callq 0x8d10f
movq %r14, %rdi
callq 0xe5b0
movl $0x1, %r14d
jmp 0x64079
movq %rbx, %rdi
callq 0xe430
jmp 0x64079
movq %rbx, %rdi
callq 0xe430
xorl %r14d, %r14d
movq %rsp, %rdi
xorl %esi, %esi
callq 0x8d10f
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop