// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadPNGStream(_IO_FILE*)
int R2Grid::
ReadPNGStream(FILE *fp)
{
#ifdef RN_USE_PNG
  // Create and initialize the png_struct 
  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png_ptr == NULL) {
    fclose(fp);
    return 0;
  }

  // Allocate/initialize the memory for image information. 
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == NULL) {
    fclose(fp);
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    return 0;
  }

  // Set up the input control if you are using standard C streams 
  png_init_io(png_ptr, fp);

  // Read the png info 
  png_read_info(png_ptr, info_ptr);

  // Extract image info 
  png_byte color_type = png_get_color_type(png_ptr, info_ptr);
  int width = png_get_image_width(png_ptr, info_ptr);
  int height = png_get_image_height(png_ptr, info_ptr);
  int rowsize = png_get_rowbytes(png_ptr, info_ptr);
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;
  png_byte depth = png_get_bit_depth(png_ptr, info_ptr);
  int bytes_per_pixel = depth / 8;
  assert(rowsize >= bytes_per_pixel * width);

  // Set ncomponents
  int ncomponents = 0;
  if (color_type == PNG_COLOR_TYPE_GRAY) ncomponents = 1;
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ncomponents = 2;
  else if (color_type == PNG_COLOR_TYPE_RGB) ncomponents = 3;
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) ncomponents = 4;
  else { 
    fclose(fp);
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    return 0;
  }

  // Allocate the pixels and row pointers 
  unsigned char *pixels = new unsigned char [ height * rowsize ]; 
  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 ];

  // Read the pixels 
  png_read_image(png_ptr, row_pointers);

  // Finish reading 
  png_read_end(png_ptr, info_ptr);

  // Fill in grid info
  grid_resolution[0] = width;
  grid_resolution[1] = height;
  grid_row_size = width;
  grid_size = width * height;
  world_to_grid_scale_factor = 1;
  world_to_grid_transform = R2identity_affine;
  grid_to_world_transform = R2identity_affine;
  if (grid_values) delete [] grid_values;
  grid_values = new RNScalar [ grid_size ];
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      int rgba[4];
      for (int k = 0; k < ncomponents; k++) {
        rgba[k] = 0;
        for (int d = 0; d < bytes_per_pixel; d++) {
          rgba[k] = rgba[k] << 8;
          rgba[k] += row_pointers[height - 1 - j][bytes_per_pixel*i+d];
        }
      }

      // Compute value
      RNScalar value = 0;
      if (ncomponents == 1) value = rgba[0];
      else if (ncomponents == 2) value = rgba[0];
      else if (ncomponents == 3) value = 0.3*rgba[0] + 0.59*rgba[1] + 0.11*rgba[2];
      else if (ncomponents == 4) value = 0.3*rgba[0] + 0.59*rgba[1] + 0.11*rgba[2];
      SetGridValue(i, j, value);
    }
  }

  // Free the row pointers 
  png_free(png_ptr, row_pointers);

  // Free the pixels
  delete [] pixels;

  // Clean up after the read, and free any memory allocated  
  png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

  // 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 $0x78, %rsp
movq %rsi, %r12
movq %rdi, %r13
leaq 0x46cf8(%rip), %rdi     # 0xa9789
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x89c28
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x62b6b
movq %rax, %r15
movq %rax, %rdi
callq 0x86dfd
movq %rax, 0x70(%rsp)
testq %rax, %rax
je 0x62b78
movq %rax, %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x87383
movq %r15, %rdi
movq %r14, %rsi
callq 0x89f96
movq %r15, %rdi
movq %r14, %rsi
callq 0x892df
movl %eax, %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0x8929d
movl %eax, 0x1c(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x892b2
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x8926a
movq %rax, %rbx
testb $0x3, %bl
je 0x62b2d
leal 0x3(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
andl $-0x4, %eax
addl $0x4, %eax
movl %eax, %ebx
movq %r15, %rdi
movq %r14, 0x30(%rsp)
movq %r14, %rsi
callq 0x892c8
movl %eax, %r14d
testb %bpl, %bpl
je 0x62b95
cmpb $0x6, %bpl
je 0x62bbf
movzbl %bpl, %eax
cmpl $0x4, %eax
je 0x62ba7
cmpl $0x2, %eax
jne 0x62b78
movb %bpl, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl $0x3, %r15d
jmp 0x62bcf
movq %r12, %rdi
callq 0xe430
jmp 0x62f66
movq %r12, %rdi
callq 0xe430
xorl %ebx, %ebx
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8ad0d
jmp 0x62f66
movb %bpl, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl $0x1, %r15d
jmp 0x62bcf
movb %bpl, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl $0x2, %r15d
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x62bd7
movb %bpl, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl $0x4, %r15d
movl $0x0, 0xc(%rsp)
movl %ebx, %eax
movq 0x10(%rsp), %r12
imull %r12d, %eax
movslq %eax, %rdi
callq 0xe030
movq %rax, %rbp
movslq %r12d, %r12
leaq (,%r12,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x91fa8
movl %r12d, %ecx
movq %rcx, 0x48(%rsp)
testl %r12d, %r12d
jle 0x62d2e
movslq %ebx, %rcx
movq 0x10(%rsp), %rdx
leal 0x1(%rdx), %esi
movq 0x48(%rsp), %rdx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq %r12, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
movq %rcx, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
shrl %esi
shlq $0x4, %rsi
movdqa 0x3f62e(%rip), %xmm3    # 0xa2280
xorl %ecx, %ecx
movdqa 0x3f634(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x3f62b(%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, %edx
notl %edx
testb $0x1, %dl
je 0x62cfa
movq %xmm8, %rdx
addq %rbp, %rdx
movq %rdx, (%rax,%rcx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %edx
testb $0x1, %dl
je 0x62d1d
pshufd $0xee, %xmm8, %xmm8     # xmm8 = xmm8[2,3,2,3]
movq %xmm8, %rdx
addq %rbp, %rdx
movq %rdx, 0x8(%rax,%rcx)
paddq %xmm7, %xmm3
addq $0x10, %rcx
cmpq %rcx, %rsi
jne 0x62c75
movq %r12, 0x60(%rsp)
movq %rbp, 0x68(%rsp)
movq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rax, 0x40(%rsp)
movq %rax, %rsi
callq 0x8a952
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x8a9b9
movl 0x1c(%rsp), %ecx
movl %ecx, 0xc0(%r13)
movq 0x10(%rsp), %rax
movl %eax, 0xc4(%r13)
movl %ecx, 0xc8(%r13)
imull %ecx, %eax
movl %eax, 0xcc(%r13)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%r13)
leaq 0x60(%r13), %rdi
leaq 0x4851ba(%rip), %r12    # 0x4e7f58
leaq 0x8(%r12), %rbx
movq %rbx, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0xa8(%r13)
leaq 0x8(%r13), %rdi
movq %rbx, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0x50(%r13)
movq 0xb8(%r13), %rdi
testq %rdi, %rdi
je 0x62ddd
callq 0xe5b0
movslq 0xcc(%r13), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0x30(%rsp)
movq %rax, 0xb8(%r13)
cmpl $0x0, 0x10(%rsp)
movb 0x28(%rsp), %cl
movq 0x60(%rsp), %rdx
jle 0x62f37
movl %r14d, %eax
shrb $0x3, %al
testb %cl, %cl
sete %cl
movslq 0xc8(%r13), %rsi
movq %rsi, 0x28(%rsp)
movzbl %al, %esi
movq 0x40(%rsp), %rax
leaq (%rax,%rdx,8), %rdi
movl 0x1c(%rsp), %r8d
orb 0xc(%rsp), %cl
movq $0x0, 0x10(%rsp)
movsd 0x46494(%rip), %xmm0    # 0xa92f0
movsd 0x46494(%rip), %xmm1    # 0xa92f8
movsd 0x46494(%rip), %xmm2    # 0xa9300
cmpl $0x0, 0x1c(%rsp)
jle 0x62f1f
movq 0x10(%rsp), %rdx
movq %rdx, %r10
notq %r10
imulq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rax
leaq (%rax,%rdx,8), %r11
xorl %ebx, %ebx
xorl %r12d, %r12d
movl %ebx, %ebx
xorl %ebp, %ebp
movl $0x0, 0x50(%rsp,%rbp,4)
cmpb $0x8, %r14b
jb 0x62ed0
movq (%rdi,%r10,8), %r13
addq %rbx, %r13
xorl %edx, %edx
xorl %r9d, %r9d
movl %r9d, %eax
shll $0x8, %eax
movzbl (%r13,%rdx), %r9d
orl %eax, %r9d
incq %rdx
cmpq %rdx, %rsi
jne 0x62eb4
movl %r9d, 0x50(%rsp,%rbp,4)
incq %rbp
cmpq %r15, %rbp
jne 0x62e9a
xorps %xmm3, %xmm3
cvtsi2sdl 0x50(%rsp), %xmm3
testb %cl, %cl
jne 0x62f0b
xorps %xmm4, %xmm4
cvtsi2sdl 0x54(%rsp), %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
xorps %xmm4, %xmm4
cvtsi2sdl 0x58(%rsp), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm4, %xmm3
movsd %xmm3, (%r11,%r12,8)
incq %r12
addl %esi, %ebx
cmpq %r8, %r12
jne 0x62e96
movq 0x10(%rsp), %rdx
incq %rdx
movq %rdx, 0x10(%rsp)
cmpq 0x48(%rsp), %rdx
jne 0x62e6c
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x92016
movq 0x68(%rsp), %rdi
callq 0xe5b0
leaq 0x38(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
callq 0x8ad0d
movl $0x1, %ebx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop