// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2260
leaq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x20a0
leaq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
callq 0x21c0
cmpl $0x0, %eax
je 0x2cd1
movq 0x9341(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x53c6(%rip), %rsi      # 0x8087
movb $0x0, %al
callq 0x21d0
movb $0x0, -0x1(%rbp)
jmp 0x2f92
leaq 0x53a8(%rip), %rdi      # 0x8080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x2360
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x2d13
movq 0x92ff(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5398(%rip), %rsi      # 0x809b
movb $0x0, %al
callq 0x21d0
movb $0x0, -0x1(%rbp)
jmp 0x2f92
movq -0x28(%rbp), %rdi
callq 0x2200
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x2d40
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2240
movb $0x0, -0x1(%rbp)
jmp 0x2f92
movq -0x28(%rbp), %rdi
callq 0x2200
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x2d6e
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2240
movb $0x0, -0x1(%rbp)
jmp 0x2f92
movq -0x28(%rbp), %rdi
movq 0x9267(%rip), %rsi      # 0xbfe0
movl $0xc8, %edx
callq 0x2100
movq %rax, %rdi
callq 0x2190
cmpl $0x0, %eax
je 0x2daa
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x2240
movb $0x0, -0x1(%rbp)
jmp 0x2f92
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2280
movq -0x28(%rbp), %rdi
movl $0x8, %esi
callq 0x22f0
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x4, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x20c0
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2290
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2340
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2050
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2070
movzbl %al, %eax
movl %eax, -0x48(%rbp)
cmpl $0x3, -0x48(%rbp)
je 0x2e41
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x2240
movb $0x0, -0x1(%rbp)
jmp 0x2f92
movl -0x3c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
imull -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x4c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movslq -0x54(%rbp), %rdi
movl $0x40, %esi
callq 0x21a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2370
movq %rax, -0x60(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x2f8e
movl $0x0, -0x78(%rbp)
cmpl $0x8, -0x78(%rbp)
jge 0x2f7e
movq -0x60(%rbp), %rax
movl -0x74(%rbp), %ecx
shll $0x3, %ecx
addl -0x78(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x2f6e
movl $0x0, -0x80(%rbp)
cmpl $0x8, -0x80(%rbp)
jge 0x2f61
movq -0x68(%rbp), %rax
movl -0x7c(%rbp), %ecx
shll $0x3, %ecx
addl -0x80(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x81(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x74(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movb -0x81(%rbp), %dl
movq -0x70(%rbp), %rax
movl -0x78(%rbp), %ecx
shll $0x3, %ecx
addl -0x80(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2efe
jmp 0x2f63
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2eef
jmp 0x2f70
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x2ec6
jmp 0x2f80
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x2eb3
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq