// File: /workspace/github_c_makefile_star2_O2/sadistech[P]nesromtool2/./src/chr2png_main.c
// Function: png2chr
int png2chr(options *opts, int argc, char **argv) {
  char *filename = argv[0];
  char output_path[PATH_MAX];

  validate_outdir(opts->outdir);

  while (filename) {
    validate_pngfile(filename);
    char *base_filename = filename_no_ext(basename(filename));

    // first let's figure out the to and from stuff.
    if (opts->outdir[0] == '\0') {
      // if the outdir is not specified, it's gonna be outpu to the same place as the original file
      // so to get the output path for each file,
      // we need to get the filename sans extension
      // and the dirname
      // and then concat dirname, basename, new extension
      sprintf(output_path, "%s/%s.chr", dirname(filename), base_filename);
    } else {
      // the output path is set to somethign
      sprintf(output_path, "%s/%s.chr", opts->outdir, base_filename);
    }

    printf("Will convert %s -> %s\n", filename, output_path);

    // read in png to bitmap
    // convert to chr
    // write out to file

    FILE *pngfile = fopen(filename, "r");

    if (!pngfile) {
      perror(filename);
      exit(EXIT_FAILURE);
    }

    nrt_tile_bitmap_lockup *lockup = (nrt_tile_bitmap_lockup*)calloc(opts->size, sizeof(nrt_tile_bitmap_lockup));

    if (!nrt_png_to_tiles(pngfile, lockup)) {
      fprintf(stderr, "Failed to read PNG file: %s\n", filename);
      fclose(pngfile);
      exit(EXIT_FAILURE);
    }

    fclose(pngfile);

    if (lockup->count > opts->size) {
      fprintf(stderr, "Incoming image is larger than the expected size (%d > %d tiles)\n", lockup->count, opts->size);
      free(lockup->bitmaps);
      free(lockup);
      exit(EXIT_FAILURE);
    }

    nrt_chrbank *chr_bank = NRT_CHR_ALLOC;
    nrt_tile *raw_tile = NRT_TILE_ALLOC;

    // convert the bitmap into CHR stuff
    int i;
    for (i = 0; i < lockup->count; i++) {
      nrt_bitmap_to_tile(&lockup->bitmaps[i], raw_tile);
      memcpy(&chr_bank->tile[i], raw_tile, NRT_TILE_SIZE);
    }

    free(raw_tile);
    free(lockup->bitmaps);
    free(lockup);

    // write this out.
    FILE *outfile = fopen(output_path, "w");

    if (!outfile) {
      free(chr_bank);
      perror(output_path);
      exit(EXIT_FAILURE);
    }

    if (fwrite(chr_bank, NRT_TILE_SIZE, opts->size, outfile) != opts->size) {
      free(chr_bank);
      fclose(outfile);
      perror(output_path);
      exit(EXIT_FAILURE);
    }

    fclose(outfile);
    free(chr_bank);

    // get next and continue loop.
    filename = *(++argv);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rdx, %rbx
movq %rdi, %r14
movq (%rdx), %r12
leaq 0x4(%rdi), %r13
movq %r13, %rdi
callq 0x5ae3
leaq 0x20(%rsp), %rbp
movq %r14, 0x10(%rsp)
movq %r13, 0x8(%rsp)
testq %r12, %r12
je 0x5a4b
movq %r12, %rdi
callq 0x5bcb
movq %r12, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x3242
movq %rax, %r15
cmpb $0x0, (%r13)
je 0x58cf
movq %rbp, %rdi
leaq 0x2122(%rip), %rsi      # 0x79ec
movq %r13, %rdx
jmp 0x58e4
movq %r12, %rdi
callq 0x2170
movq %rbp, %rdi
leaq 0x210b(%rip), %rsi      # 0x79ec
movq %rax, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2320
leaq 0x20ae(%rip), %rdi      # 0x79a3
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2140
movq %r12, %rdi
leaq 0xe38(%rip), %rsi       # 0x6744
callq 0x22c0
testq %rax, %rax
je 0x5a84
movq %rax, %r15
movslq 0x1008(%r14), %rdi
pushq $0x10
popq %rsi
callq 0x21c0
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x29c1
testb %al, %al
je 0x5a5f
movq %rbx, 0x18(%rsp)
movq %r15, %rdi
callq 0x2100
movl (%r13), %ebp
movl 0x1008(%r14), %ecx
cmpl %ecx, %ebp
jg 0x5a89
movl $0x2000, %esi           # imm = 0x2000
pushq $0x1
popq %rbx
movq %rbx, %rdi
callq 0x21c0
movq %rax, %r12
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x21c0
movq %rax, %r15
xorl %ebx, %ebx
movq %r12, (%rsp)
xorl %r14d, %r14d
movslq %ebp, %rax
cmpq %rax, %r14
jge 0x59bd
movq 0x8(%r13), %rdi
addq %rbx, %rdi
movq %r15, %rsi
callq 0x2734
movups (%r15), %xmm0
movups %xmm0, (%r12)
incq %r14
movl (%r13), %ebp
addq $0x10, %r12
addq $0x40, %rbx
jmp 0x598c
movq %r15, %rdi
callq 0x2040
movq 0x8(%r13), %rdi
callq 0x2040
movq %r13, %rdi
callq 0x2040
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1c2f(%rip), %rsi      # 0x7614
callq 0x22c0
testq %rax, %rax
je 0x5ab6
movq %rax, %r15
movq 0x10(%rsp), %r14
movslq 0x1008(%r14), %rdx
movq (%rsp), %r12
movq %r12, %rdi
pushq $0x10
popq %rsi
movq %rax, %rcx
callq 0x2340
movslq 0x1008(%r14), %rcx
cmpq %rcx, %rax
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r13
jne 0x5ac1
movq %r15, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x2040
movq 0x8(%rbx), %r12
addq $0x8, %rbx
jmp 0x5895
xorl %eax, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5592(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x1f86(%rip), %rsi      # 0x79f6
movq %r12, %rdx
xorl %eax, %eax
callq 0x21f0
movq %r15, %rdi
callq 0x2100
jmp 0x5adb
movq %r12, %rdi
jmp 0x5ad6
movq 0x5568(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x1f79(%rip), %rsi      # 0x7a13
movl %ebp, %edx
xorl %eax, %eax
callq 0x21f0
movq 0x8(%r13), %rdi
callq 0x2040
movq %r13, %rdi
callq 0x2040
jmp 0x5adb
movq (%rsp), %rdi
callq 0x2040
jmp 0x5ad1
movq %r12, %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2100
leaq 0x20(%rsp), %rdi
callq 0x22d0
pushq $0x1
popq %rdi
callq 0x2330