// File: /workspace/github_c_makefile_star2_O1/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
movl $0x8, %r13d
movl $0x1, %esi
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 0x33ca
leaq 0x3eee(%rip), %rdi      # 0x7080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x2380
movq %rax, (%rsp)
testq %rax, %rax
je 0x33d1
movq %rax, %rdi
callq 0x2220
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3386
movq (%rsp), %rdi
callq 0x2220
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x33a8
movq (%rsp), %rdi
movq 0x8dfa(%rip), %rsi      # 0xbfe0
movl $0xc8, %edx
callq 0x2120
movq %rax, %rdi
callq 0x21b0
testl %eax, %eax
jne 0x3392
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x22a0
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x2310
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movl $0x4, %edx
callq 0x20e0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x22b0
movl %eax, %ebp
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2360
movl %eax, %r14d
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2060
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2080
cmpb $0x3, %al
jne 0x3392
movl %ebp, %eax
leal 0x7(%rax), %r15d
testl %ebp, %ebp
cmovnsl %ebp, %r15d
movl %r15d, %r13d
sarl $0x3, %r13d
movl %r14d, %eax
leal 0x7(%rax), %r12d
testl %r14d, %r14d
cmovnsl %r14d, %r12d
sarl $0x3, %r12d
movl %r12d, %eax
imull %r13d, %eax
movl %eax, (%rbx)
movl %r13d, 0x4(%rbx)
movslq %eax, %rdi
movl $0x40, %esi
callq 0x21c0
movq %rax, 0x8(%rbx)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2390
movq %rax, 0x28(%rsp)
movb $0x1, %al
cmpl $0x8, %r14d
jl 0x33bb
movl %r12d, %r12d
movl %r12d, %eax
movq %rax, 0x20(%rsp)
movl %r13d, %ecx
movl %ecx, %esi
xorl %edi, %edi
movl %edi, %r9d
movl %edi, %r15d
movl %r13d, 0x14(%rsp)
movl %r15d, %ecx
shlq $0x6, %rcx
movq %r9, %r10
shlq $0x6, %r10
addq 0x28(%rsp), %r10
movl %edi, %r14d
movq %rcx, %r11
cmpl $0x8, %ebp
jl 0x3353
movq (%r10,%r14,8), %r13
movl %edi, %ecx
movq %r11, %r12
movl %edi, %edx
movb (%r13,%rdx), %al
movq 0x8(%rbx), %r8
addq %r12, %r8
movb %al, (%rdx,%r8)
movq %rdx, %rax
incq %rax
movq %rax, %rdx
cmpq $0x8, %rax
jne 0x331e
movq %rcx, %rax
incq %rax
addq $0x8, %r13
addq $0x40, %r12
movq %rax, %rcx
cmpq %rsi, %rax
jne 0x331c
movq %r14, %rax
incq %rax
addq $0x8, %r11
movq %rax, %r14
cmpq $0x8, %rax
jne 0x330e
movq %r9, %rax
incq %rax
movl 0x14(%rsp), %r13d
addl %r13d, %r15d
movq %rax, %r9
cmpq 0x20(%rsp), %rax
jne 0x32f5
movb $0x1, %al
jmp 0x33bb
xorl %ebx, %ebx
movl %ebx, %edx
movq %rsp, %rdi
movq %rdx, %rsi
jmp 0x33b4
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x2260
xorl %eax, %eax
jmp 0x33bb
xorl %ebx, %ebx
movl %ebx, %edx
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2260
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x23d5
jmp 0x33a4
callq 0x23f7
jmp 0x33a4