// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/insetpix.c
// Function: do_char_graphics
static void do_char_graphics(deark *c, lctx *d)
{
	struct insetpix_item_data itd;
	dbuf *unc_pixels = NULL;
	struct de_char_context *charctx = NULL;
	struct fmtutil_char_simplectx *csctx = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "reading char graphics");
	de_dbg_indent(c, 1);
	if(d->num_tiles_found!=1) {
		de_err(c, "Multi-tile char. graphics not supported");
		goto done;
	}

	insetpix_read_item(c, 4 + 8*d->idx_of_1st_tile, &itd);
	insetpix_dbg_item(c, &itd, d->idx_of_1st_tile);

	unc_pixels = dbuf_create_membuf(c, 4096, 0);
	csctx = de_malloc(c, sizeof(struct fmtutil_char_simplectx));
	charctx = de_create_charctx(c, 0);
	charctx->screen_image_flag = 1;

	csctx->width_in_chars = d->page_cols;
	csctx->height_in_chars = d->page_rows;

	if(csctx->width_in_chars>80 || csctx->height_in_chars>25) {
		charctx->no_density = 1;
	}

	// Unless I'm missing something, this format is just wacky.
	// Assume the screen is 80x25. The original data is then 80x25 bytes of
	// character data, followed immediately by 80x25 bytes of attribute data.
	// So its sort of 80x50. Good so far. But then it's compressed as if it were
	// 160x25. Which is strange.
	// The first 12 rows are character data.
	// The 13th row is half character data, half attribute data.
	// The last 12 rows are attribute data.
	// The compression is only effective when a character is the same as the one
	// *two* rows above it.
	// (The format documentation does hint at this, but doesn't adequately
	// explain it.)

	d->gfore = 1; // hack (gfore is used as # of planes)
	d->rowspan = csctx->width_in_chars*2;
	d->compression_bytes_per_row = (d->rowspan+7)/8;
	do_decompress_tile(c, d, &itd, unc_pixels, csctx->height_in_chars);

	csctx->input_encoding = d->input_encoding;
	csctx->inf = unc_pixels;
	csctx->inf_pos = 0;
	csctx->inf_len = csctx->width_in_chars * csctx->height_in_chars * 2;
	csctx->fg_stride = 1;
	csctx->attr_offset = csctx->width_in_chars*csctx->height_in_chars;
	de_memcpy(&charctx->pal, &d->pal, sizeof(de_color)*16);

	fmtutil_char_simple_run(c, csctx, charctx);

done:
	de_free_charctx(c, charctx);
	de_free(c, csctx);
	dbuf_close(unc_pixels);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0xfce9f(%rip), %rsi     # 0x1b55af
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
je 0xb8747
movq -0x8(%rbp), %rdi
leaq 0xfce8a(%rip), %rsi     # 0x1b55c5
movb $0x0, %al
callq 0x15bfb0
jmp 0xb88f6
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x3, %rsi
addq $0x4, %rsi
leaq -0x30(%rbp), %rdx
callq 0xb7ad0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
leaq -0x30(%rbp), %rsi
callq 0xb7bc0
movq -0x8(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
xorl %edx, %edx
callq 0x168020
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x16d7b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x48(%rbp), %rax
cmpq $0x50, 0x20(%rax)
jg 0xb87f2
movq -0x48(%rbp), %rax
cmpq $0x19, 0x28(%rax)
jle 0xb87fa
movq -0x40(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
movq $0x1, 0x80(%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
shlq %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq 0x28(%rax), %r8
leaq -0x30(%rbp), %rdx
callq 0xb8d10
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x48(%rbp), %rax
imulq 0x28(%rax), %rcx
shlq %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rax
movq $0x1, 0x30(%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x48(%rbp), %rax
imulq 0x28(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rdi
addq $0x20, %rdi
movq -0x10(%rbp), %rsi
addq $0xf0, %rsi
movl $0x40, %edx
callq 0x191e0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x176610
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x16d960
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15b820
movq -0x38(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)