// File: 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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x7d9f5(%rip), %rdi     # 0x136100
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1006b0
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0xb874a
movq 0x90(%rsp), %rdi
callq 0xf4b0
movl $0x0, 0xa4(%rsp)
jmp 0xb8ca7
movq 0x88(%rsp), %rdi
callq 0xfb820
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0xb879b
movq 0x90(%rsp), %rdi
callq 0xf4b0
leaq 0x88(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x102b40
movl $0x0, 0xa4(%rsp)
jmp 0xb8ca7
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0xfc210
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x100be0
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xff1f0
movb %al, 0x7f(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xff130
movl %eax, 0x78(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xff170
movl %eax, 0x74(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xff0b0
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xb8850
movl 0x70(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
addl $0x1, %eax
shll $0x2, %eax
movl %eax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xff1b0
movb %al, 0x6f(%rsp)
movzbl 0x6f(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movzbl 0x7f(%rsp), %eax
cmpl $0x0, %eax
jne 0xb8896
movl $0x1, 0x64(%rsp)
jmp 0xb8909
movzbl 0x7f(%rsp), %eax
cmpl $0x4, %eax
jne 0xb88aa
movl $0x2, 0x64(%rsp)
jmp 0xb8907
movzbl 0x7f(%rsp), %eax
cmpl $0x2, %eax
jne 0xb88be
movl $0x3, 0x64(%rsp)
jmp 0xb8905
movzbl 0x7f(%rsp), %eax
cmpl $0x6, %eax
jne 0xb88d2
movl $0x4, 0x64(%rsp)
jmp 0xb8903
movq 0x90(%rsp), %rdi
callq 0xf4b0
leaq 0x88(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x102b40
movl $0x0, 0xa4(%rsp)
jmp 0xb8ca7
jmp 0xb8905
jmp 0xb8907
jmp 0xb8909
movl 0x74(%rsp), %eax
imull 0x70(%rsp), %eax
movslq %eax, %rdi
callq 0xf030
movq %rax, 0x58(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
shlq $0x3, %rsi
callq 0x1135b0
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0xb8981
movq 0x58(%rsp), %rdx
movl 0x74(%rsp), %eax
subl 0x4c(%rsp), %eax
subl $0x1, %eax
imull 0x70(%rsp), %eax
cltq
addq %rax, %rdx
movq 0x50(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xb8942
movq 0x88(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x1022e0
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x1023a0
movq 0x18(%rsp), %rdi
movl 0x78(%rsp), %eax
movl %eax, 0xc0(%rdi)
movl 0x74(%rsp), %eax
movl %eax, 0xc4(%rdi)
movl 0x78(%rsp), %eax
movl %eax, 0xc8(%rdi)
movl 0x78(%rsp), %eax
imull 0x74(%rsp), %eax
movl %eax, 0xcc(%rdi)
movsd 0x7a626(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rdi)
addq $0x58, %rdi
leaq 0x4c2073(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x18(%rsp), %rdi
leaq 0x4c2062(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x18(%rsp), %rax
cmpq $0x0, 0xb8(%rax)
je 0xb8a3d
movq 0x18(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xb8a3b
movq 0x10(%rsp), %rdi
callq 0xf670
jmp 0xb8a3d
movq 0x18(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0xb8(%rax)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0xb8c57
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x78(%rsp), %eax
jge 0xb8c45
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0xb8b46
movslq 0x2c(%rsp), %rax
movl $0x0, 0x30(%rsp,%rax,4)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x68(%rsp), %eax
jge 0xb8b34
movslq 0x2c(%rsp), %rax
movl 0x30(%rsp,%rax,4), %ecx
shll $0x8, %ecx
movslq 0x2c(%rsp), %rax
movl %ecx, 0x30(%rsp,%rax,4)
movq 0x50(%rsp), %rax
movl 0x74(%rsp), %ecx
subl $0x1, %ecx
subl 0x48(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x68(%rsp), %ecx
imull 0x44(%rsp), %ecx
addl 0x28(%rsp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %ecx
movslq 0x2c(%rsp), %rax
addl 0x30(%rsp,%rax,4), %ecx
movl %ecx, 0x30(%rsp,%rax,4)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xb8ad0
jmp 0xb8b36
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xb8aad
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
cmpl $0x1, 0x64(%rsp)
jne 0xb8b67
cvtsi2sdl 0x30(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xb8c1d
cmpl $0x2, 0x64(%rsp)
jne 0xb8b7f
cvtsi2sdl 0x30(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xb8c1b
cmpl $0x3, 0x64(%rsp)
jne 0xb8bcc
cvtsi2sdl 0x30(%rsp), %xmm1
cvtsi2sdl 0x34(%rsp), %xmm2
movsd 0x7d0c6(%rip), %xmm0    # 0x135c60
mulsd %xmm2, %xmm0
movsd 0x7d0b2(%rip), %xmm2    # 0x135c58
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
cvtsi2sdl 0x38(%rsp), %xmm0
movsd 0x7d094(%rip), %xmm2    # 0x135c50
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xb8c19
cmpl $0x4, 0x64(%rsp)
jne 0xb8c17
cvtsi2sdl 0x30(%rsp), %xmm1
cvtsi2sdl 0x34(%rsp), %xmm2
movsd 0x7d079(%rip), %xmm0    # 0x135c60
mulsd %xmm2, %xmm0
movsd 0x7d065(%rip), %xmm2    # 0x135c58
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
cvtsi2sdl 0x38(%rsp), %xmm0
movsd 0x7d047(%rip), %xmm2    # 0x135c50
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xb8c19
jmp 0xb8c1b
jmp 0xb8c1d
movq 0x18(%rsp), %rdi
movl 0x44(%rsp), %esi
movl 0x48(%rsp), %edx
movsd 0x20(%rsp), %xmm0
callq 0x6bc00
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0xb8a97
jmp 0xb8c47
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xb8a81
movq 0x88(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x1136d0
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb8c83
movq 0x8(%rsp), %rdi
callq 0xf670
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x102b40
movl $0x1, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)