// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r15
movq %rdi, %r12
leaq 0x45ebc(%rip), %rdi     # 0xa9789
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8aa30
movq %rax, (%rsp)
testq %rax, %rax
je 0x639b7
movq %rax, %rdi
callq 0x87bdd
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x639c4
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x88160
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8ad8e
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8a0e3
movl %eax, %ebx
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8a0a1
movl %eax, 0x24(%rsp)
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8a0b6
movq %rax, 0x10(%rsp)
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8a06e
movq %rax, %r13
testb $0x3, %r13b
je 0x63977
leal 0x3(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
andl $-0x4, %eax
addl $0x4, %eax
movl %eax, %r13d
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8a0cc
movl %eax, %ebp
testb %bl, %bl
je 0x639df
cmpb $0x6, %bl
je 0x639fd
movzbl %bl, %eax
cmpl $0x4, %eax
je 0x639eb
cmpl $0x2, %eax
jne 0x639c4
movb %bl, 0x28(%rsp)
movl $0x3, %r14d
movb $0x1, %al
movl %eax, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0x63a17
movq %r15, %rdi
callq 0xe430
jmp 0x63dab
movq %r15, %rdi
callq 0xe430
xorl %ebx, %ebx
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8bada
jmp 0x63dab
movb %bl, 0x28(%rsp)
movl $0x1, %r14d
jmp 0x63a07
movb %bl, 0x28(%rsp)
movl $0x2, %r14d
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x63a0f
movb %bl, 0x28(%rsp)
movl $0x4, %r14d
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl %r13d, %eax
movq 0x10(%rsp), %rbx
imull %ebx, %eax
movslq %eax, %rdi
callq 0xe030
movq %rax, %r15
movq (%rsp), %rdi
movslq %ebx, %rbx
leaq (,%rbx,8), %rsi
callq 0x92cb5
movl %ebx, %ecx
movq %rcx, 0x48(%rsp)
testl %ebx, %ebx
jle 0x63b6c
movslq %r13d, %rcx
movq 0x10(%rsp), %rdx
leal 0x1(%rdx), %esi
movq 0x48(%rsp), %rdi
leaq -0x1(%rdi), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq %rdi, %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 0x3e7f0(%rip), %xmm3    # 0xa2280
xorl %ecx, %ecx
movdqa 0x3e7f6(%rip), %xmm4    # 0xa2290
pxor %xmm4, %xmm0
pcmpeqd %xmm5, %xmm5
movdqa %xmm2, %xmm6
psrlq $0x20, %xmm6
movdqa 0x3e7ed(%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 0x63b38
movq %xmm8, %rdx
addq %r15, %rdx
movq %rdx, (%rax,%rcx)
pxor %xmm5, %xmm9
pextrw $0x4, %xmm9, %edx
testb $0x1, %dl
je 0x63b5b
pshufd $0xee, %xmm8, %xmm8     # xmm8 = xmm8[2,3,2,3]
movq %xmm8, %rdx
addq %r15, %rdx
movq %rdx, 0x8(%rax,%rcx)
paddq %xmm7, %xmm3
addq $0x10, %rcx
cmpq %rcx, %rsi
jne 0x63ab3
movq %r15, 0x58(%rsp)
movq (%rsp), %rdi
movq %rax, 0x50(%rsp)
movq %rax, %rsi
callq 0x8b71f
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x8b786
movl 0x24(%rsp), %ecx
movl %ecx, 0xc0(%r12)
movq 0x10(%rsp), %rax
movl %eax, 0xc4(%r12)
movl %ecx, 0xc8(%r12)
imull %ecx, %eax
movl %eax, 0xcc(%r12)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%r12)
leaq 0x60(%r12), %rdi
leaq 0x48437e(%rip), %rbx    # 0x4e7f58
leaq 0x8(%rbx), %r13
movq %r13, %rsi
callq 0x67944
movl 0x50(%rbx), %eax
movl %eax, 0xa8(%r12)
leaq 0x8(%r12), %rdi
movq %r13, %rsi
callq 0x67944
movl 0x50(%rbx), %eax
movl %eax, 0x50(%r12)
movq 0xb8(%r12), %rdi
testq %rdi, %rdi
je 0x63c18
callq 0xe5b0
movslq 0xcc(%r12), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0x60(%rsp)
movq %rax, 0xb8(%r12)
cmpl $0x0, 0x10(%rsp)
jle 0x63d7d
movl %ebp, %eax
shrb $0x3, %al
cmpb $0x0, 0x28(%rsp)
sete %cl
movslq 0xc8(%r12), %rdx
movq %rdx, 0x28(%rsp)
movzbl %al, %esi
movq 0x50(%rsp), %rax
movq 0x48(%rsp), %rdx
leaq (%rax,%rdx,8), %rdi
movl 0x24(%rsp), %r8d
orb 0x8(%rsp), %cl
movq $0x0, 0x10(%rsp)
movsd 0x45658(%rip), %xmm0    # 0xa92f0
movsd 0x45658(%rip), %xmm1    # 0xa92f8
movsd 0x45658(%rip), %xmm2    # 0xa9300
cmpl $0x0, 0x24(%rsp)
jle 0x63d65
movq 0x10(%rsp), %rdx
movq %rdx, %r10
notq %r10
imulq 0x28(%rsp), %rdx
movq 0x60(%rsp), %rax
leaq (%rax,%rdx,8), %r11
xorl %r13d, %r13d
xorl %r15d, %r15d
movl %r13d, %r13d
xorl %r12d, %r12d
movl $0x0, 0x30(%rsp,%r12,4)
cmpb $0x8, %bpl
jb 0x63d10
movl 0x30(%rsp,%r12,4), %edx
movq (%rdi,%r10,8), %rbx
addq %r13, %rbx
xorl %r9d, %r9d
movl %edx, %eax
shll $0x8, %eax
movzbl (%rbx,%r9), %edx
orl %eax, %edx
incq %r9
cmpq %r9, %rsi
jne 0x63cf7
movl %edx, 0x30(%rsp,%r12,4)
incq %r12
cmpq %r14, %r12
jne 0x63cd9
xorps %xmm3, %xmm3
cvtsi2sdl 0x30(%rsp), %xmm3
testb %cl, %cl
jne 0x63d50
cmpb $0x0, 0xc(%rsp)
xorps %xmm4, %xmm4
cvtsi2sdl 0x34(%rsp), %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
xorps %xmm4, %xmm4
cvtsi2sdl 0x38(%rsp), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm4, %xmm3
movsd %xmm3, (%r11,%r15,8)
incq %r15
addl %esi, %r13d
cmpq %r8, %r15
jne 0x63cd3
movq 0x10(%rsp), %rdx
incq %rdx
movq %rdx, 0x10(%rsp)
cmpq 0x48(%rsp), %rdx
jne 0x63ca8
movq %rsp, %rbx
movq (%rbx), %rdi
movq 0x50(%rsp), %rsi
callq 0x92d32
movq 0x58(%rsp), %rdi
callq 0xe5b0
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8bada
movl $0x1, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq