// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rsi, %r12
movq %rdi, %r14
leaq 0x467f6(%rip), %rdi     # 0xa9789
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8ba9f
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6321b
movq %rax, %rbx
movq %rax, %rdi
callq 0x86dfd
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x63225
movq %r12, 0x18(%rsp)
movl 0xc0(%r14), %edx
movl 0xc4(%r14), %ebp
movslq %ebp, %r13
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movl %edx, 0xc(%rsp)
movl %r13d, %ecx
movl $0x10, %r8d
xorl %r9d, %r9d
pushq %r10
pushq %r10
pushq %r10
callq 0x87d78
addq $0x20, %rsp
movslq 0xcc(%r14), %r15
leaq (%r15,%r15), %rdi
callq 0xe030
movq %rax, %r12
testq %r15, %r15
jle 0x63076
movq 0xb8(%r14), %rax
xorl %ecx, %ecx
movsd 0x3ff80(%rip), %xmm0    # 0xa2fb8
movsd 0x462e8(%rip), %xmm1    # 0xa9328
movsd (%rax,%rcx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x63053
jp 0x63053
xorl %edx, %edx
xorl %esi, %esi
jmp 0x63065
movapd %xmm1, %xmm3
minsd %xmm2, %xmm3
cvttsd2si %xmm3, %rsi
movl %esi, %edx
shrl $0x8, %edx
movb %dl, (%r12,%rcx,2)
movb %sil, 0x1(%r12,%rcx,2)
incq %rcx
cmpq %rcx, %r15
jne 0x63040
shlq $0x3, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x91fa8
movq %rax, %r13
testl %ebp, %ebp
jle 0x631be
movl 0x4(%rsp), %edx
addl %edx, %edx
leal 0x1(%rbp), %eax
leaq -0x1(%rbp), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movd %ebp, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movd %edx, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
shrl %eax
shlq $0x4, %rax
movq 0x462b9(%rip), %xmm3    # 0xa9380
movdqa 0x3f1b1(%rip), %xmm4    # 0xa2280
xorl %ecx, %ecx
movdqa 0x3f1b7(%rip), %xmm5    # 0xa2290
pxor %xmm5, %xmm0
pcmpeqd %xmm6, %xmm6
pshufd $0xf5, %xmm2, %xmm7     # xmm7 = xmm2[1,1,3,3]
movdqa 0x3f1b1(%rip), %xmm8    # 0xa22a0
movdqa 0x46298(%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 0x63184
movq %xmm11, %rdx
addq %r12, %rdx
movq %rdx, (%r13,%rcx)
pxor %xmm6, %xmm10
pextrw $0x4, %xmm10, %edx
testb $0x1, %dl
je 0x631a7
pshufd $0xee, %xmm11, %xmm10   # xmm10 = xmm11[2,3,2,3]
movq %xmm10, %rdx
addq %r12, %rdx
movq %rdx, 0x8(%r13,%rcx)
paddq %xmm8, %xmm4
paddd %xmm9, %xmm3
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x630f8
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x87383
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x8b5d3
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8c1ff
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8b8bf
movq %rbx, %rdi
movq %r13, %rsi
callq 0x92016
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x8c355
movq %r12, %rdi
callq 0xe5b0
movl $0x1, %r15d
jmp 0x6323c
movq %r12, %rdi
callq 0xe430
jmp 0x6323c
movq %r12, %rdi
callq 0xe430
xorl %r15d, %r15d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x8c355
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq