// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rsi, %r12
movq %rdi, %r14
leaq 0x3fdde(%rip), %rdi     # 0x9a719
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7e7b7
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x5aacb
movq %rax, %rbx
movq %rax, %rdi
callq 0x7a260
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x5aad5
movq %r12, 0x38(%rsp)
movl 0xc0(%r14), %edx
movl 0xc4(%r14), %ecx
movslq %ecx, %r13
andl $0x0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0x10
popq %r8
xorl %ebp, %ebp
movq %rbx, %rdi
movq %rax, 0x30(%rsp)
movq %rax, %rsi
movq %rdx, 0x28(%rsp)
movl %r13d, %ecx
xorl %r9d, %r9d
callq 0x7b0d1
movslq 0xcc(%r14), %r15
movq %r15, %rdi
addq %r15, %rdi
callq 0xe030
movq %rax, %r12
movq 0xb8(%r14), %rax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movsd 0x39583(%rip), %xmm0    # 0x93f68
movsd 0x3f8eb(%rip), %xmm1    # 0x9a2d8
cmpq %rbp, %rcx
je 0x5aa25
movsd (%rax,%rbp,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5aa05
jp 0x5aa05
xorl %edx, %edx
xorl %esi, %esi
jmp 0x5aa17
movapd %xmm1, %xmm3
minsd %xmm2, %xmm3
cvttsd2si %xmm3, %rsi
movl %esi, %edx
shrl $0x8, %edx
movb %dl, (%r12,%rbp,2)
movb %sil, 0x1(%r12,%rbp,2)
incq %rbp
jmp 0x5a9ed
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x848bb
movq %rax, %r14
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movq 0x28(%rsp), %rsi
leal (%rsi,%rsi), %edx
decl %r13d
imull %esi, %r13d
addl %r13d, %r13d
cmpq %rax, %rcx
je 0x5aa70
movslq %r13d, %r13
leaq (%r12,%r13), %rsi
movq %rsi, (%r14,%rax,8)
incq %rax
subl %edx, %r13d
jmp 0x5aa58
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x7a79c
movq %rbx, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x7e303
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7ef09
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7e5f1
movq %rbx, %rdi
movq %r14, %rsi
callq 0x84923
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x7f05b
movq %r12, %rdi
callq 0xe5d0
pushq $0x1
popq %r15
jmp 0x5aaec
movq %r12, %rdi
callq 0xe440
jmp 0x5aaec
movq %r12, %rdi
callq 0xe440
xorl %r15d, %r15d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x7f05b
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq