// File: /workspace/github_c_makefile_star2_O2/sadistech[P]nesromtool2/./src/nrt/tile.c
// Function: nrt_png_to_tiles
bool nrt_png_to_tiles(FILE *png_file, nrt_tile_bitmap_lockup *lockup) {
  char header[NRT_PNG_SIG_SIZE];

  fseek(png_file, 0, SEEK_SET);
  fread(header, 1, NRT_PNG_SIG_SIZE, png_file);

  if (png_sig_cmp((png_const_bytep)header, 0, 8)) {
    fprintf(stderr, "this is not a png!\n");
    return false;
  }

  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (!png_ptr) {
    fprintf(stderr, "Failed to allocate png_ptr\n");
    return false;
  }

  png_infop info_ptr = png_create_info_struct(png_ptr);

  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
    return false;
  }

  png_infop end_info = png_create_info_struct(png_ptr);
  if (!end_info) {
    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    return false;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    return false;
  }

  png_init_io(png_ptr, png_file);
  png_set_sig_bytes(png_ptr, NRT_PNG_SIG_SIZE);
  png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING, NULL);

  //png_read_info(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 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  int color_type = png_get_color_type(png_ptr, info_ptr);

  if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    return false;
  }

  int tiles_width = width / NRT_TILE_WIDTH_PX;
  int tiles_height = height / NRT_TILE_HEIGHT_PX;
  int tiles_count = tiles_width * tiles_height;

  // configure the lockup and allocate the memory
  lockup->count = tiles_count;
  lockup->width = tiles_width;
  lockup->bitmaps = (nrt_tile_bitmap*)calloc(tiles_count, sizeof(nrt_tile_bitmap));

  /* png_bytep row = (png_bytep)malloc(tiles_width * NRT_TILE_WIDTH_PX * bit_depth / CHAR_BIT * sizeof(png_byte)); */
  png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
  png_bytep row;

  nrt_tile_bitmap *current_tile = NULL;

  // iterate over each row of tiles
  // each row of pixels
  // each tile in the row
  // each pixel in the tile
  int tile_row; // which row of tiles we're on
  int px_row;   // which row of pixels we're on for the row of tiles
  int tile;     // which column of tiles we're on
  int tile_px;  // which pixel we're on in the row of pixels

  png_byte px; // our scratch pad for the value of the current pixel

  for (tile_row = 0; tile_row < tiles_height; tile_row++) {
    for (px_row = 0; px_row < NRT_TILE_HEIGHT_PX; px_row++) {
      row = row_pointers[tile_row * NRT_TILE_HEIGHT_PX + px_row];

      for (tile = 0; tile < tiles_width; tile++) {
        for (tile_px = 0; tile_px < NRT_TILE_WIDTH_PX; tile_px++) {
          // get the current byte from the png row
          px = row[tile * NRT_TILE_WIDTH_PX + tile_px];

          current_tile = &lockup->bitmaps[tile_row * tiles_width + tile];
          current_tile->pixels[px_row * NRT_TILE_WIDTH_PX + tile_px] = (char)px;
        }
      }
    }
  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorl %edx, %edx
movl %edx, %r15d
movq %r15, %rsi
callq 0x2280
leaq 0x30(%rsp), %r12
pushq $0x1
popq %rsi
pushq $0x8
popq %r13
movq %r12, %rdi
movq %r13, %rdx
movq %r14, %rcx
callq 0x20c0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x21e0
testl %eax, %eax
jne 0x2c1c
leaq 0x3667(%rip), %rdi      # 0x6080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x2380
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x2c31
movq %rax, %r15
movq %rax, %rdi
callq 0x2220
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2bdd
movq %r15, %rdi
callq 0x2220
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2c01
movq 0x8(%rsp), %rdi
movq 0x856f(%rip), %rsi      # 0xafe0
movl $0xc8, %edx
callq 0x2120
movq %rax, %rdi
callq 0x21b0
testl %eax, %eax
jne 0x2beb
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x22a0
movq 0x8(%rsp), %rdi
pushq $0x8
popq %rbp
movl %ebp, %esi
callq 0x2310
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %r12d, %r12d
movl %r12d, %ecx
pushq $0x4
popq %rdx
callq 0x20e0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x22b0
movl %eax, %r14d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2360
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2060
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2080
cmpb $0x3, %al
jne 0x2beb
movl %r14d, %eax
cltd
idivl %ebp
movl %eax, %r14d
movl %r15d, %eax
cltd
idivl %ebp
movl %eax, %ebp
imull %r14d, %eax
movl %eax, (%rbx)
movl %r14d, 0x4(%rbx)
movslq %eax, %rdi
pushq $0x40
popq %rsi
callq 0x21c0
movq %rax, 0x8(%rbx)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2390
testl %r14d, %r14d
movl %r12d, %edx
cmovgl %r14d, %edx
movslq %r14d, %rsi
testl %ebp, %ebp
cmovlel %r12d, %ebp
movl %ebp, %ecx
movq %rcx, 0x28(%rsp)
shlq $0x6, %rsi
movq %rsi, 0x20(%rsp)
movl %r12d, %edi
movq %rdi, %r8
cmpq 0x28(%rsp), %r8
je 0x2c18
leaq (,%r8,8), %rsi
movl %r12d, %r11d
movq %rdi, %r10
cmpq $0x8, %r11
je 0x2bd3
movq %r11, %r14
orq %rsi, %r14
movq (%rax,%r14,8), %r14
movl %r12d, %r13d
movq %r10, %r15
cmpq %rdx, %r13
je 0x2bca
movl %r12d, %ebp
cmpq $0x8, %rbp
je 0x2bbd
movb (%r14,%rbp), %cl
movq 0x8(%rbx), %r9
addq %r15, %r9
movb %cl, (%rbp,%r9)
incq %rbp
jmp 0x2ba2
incq %r13
addq $0x40, %r15
addq $0x8, %r14
jmp 0x2b9a
incq %r11
addq $0x8, %r10
jmp 0x2b84
incq %r8
addq 0x20(%rsp), %rdi
jmp 0x2b6b
xorl %ebx, %ebx
movl %ebx, %edx
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
jmp 0x2c0f
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x2260
jmp 0x2c4d
xorl %ebx, %ebx
movl %ebx, %edx
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2260
movl %ebx, %eax
jmp 0x2c4f
movb $0x1, %al
jmp 0x2c4f
movq 0x83d5(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x345a(%rip), %rdi      # 0x6087
pushq $0x13
jmp 0x2c44
movq 0x83c0(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x3459(%rip), %rdi      # 0x609b
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq