// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq (%rdx), %r12
leaq 0x4(%rdi), %r14
movq %r14, %rdi
callq 0x64ab
testq %r12, %r12
je 0x645d
leaq 0x20(%rsp), %rbp
movq %r14, 0x10(%rsp)
movq %r12, %rdi
callq 0x6541
movq %r12, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x3b04
movq %rax, %r13
cmpb $0x0, (%r14)
je 0x62d8
movq %rbp, %rdi
leaq 0x2719(%rip), %rsi      # 0x89ec
movq %r14, %rdx
jmp 0x62ed
movq %r12, %rdi
callq 0x2170
movq %rbp, %rdi
leaq 0x2702(%rip), %rsi      # 0x89ec
movq %rax, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2320
leaq 0x26a5(%rip), %rdi      # 0x89a3
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2140
movq %r12, %rdi
leaq 0x142f(%rip), %rsi      # 0x7744
callq 0x22c0
testq %rax, %rax
je 0x6471
movq %rax, %r13
movslq 0x1008(%r15), %rdi
movl $0x10, %esi
callq 0x21c0
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0x3136
testb %al, %al
je 0x64a0
movq %rbx, 0x18(%rsp)
movq %r13, %rdi
callq 0x2100
movl (%rbp), %edi
movl 0x1008(%r15), %esi
cmpl %esi, %edi
jg 0x6479
movq %r15, %r14
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21c0
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x21c0
movq %rax, %r13
cmpl $0x0, (%rbp)
jle 0x63d2
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
xorl %ebx, %ebx
movq 0x8(%rbp), %rdi
addq %r15, %rdi
movq %r13, %rsi
callq 0x2e7b
movups (%r13), %xmm0
movups %xmm0, (%r12)
incq %rbx
movslq (%rbp), %rax
addq $0x10, %r12
addq $0x40, %r15
cmpq %rax, %rbx
jl 0x63a5
movq %r13, %rdi
callq 0x2040
movq 0x8(%rbp), %rdi
callq 0x2040
movq %rbp, %rdi
callq 0x2040
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0x221a(%rip), %rsi      # 0x8614
callq 0x22c0
testq %rax, %rax
je 0x6481
movq %rax, %r13
movq %r14, %r15
movslq 0x1008(%r14), %rdx
movl $0x10, %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2340
movslq 0x1008(%r14), %rcx
cmpq %rcx, %rax
jne 0x6490
movq %r13, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2040
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %r12
addq $0x8, %rbx
testq %r12, %r12
movq 0x10(%rsp), %r14
jne 0x62a8
xorl %eax, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x29fe
movq %rbp, %rdx
callq 0x29c5
leaq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x29ac
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2985
movq %r12, %rdi
movq %r13, %rsi
callq 0x2956