// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/pcpaint.c
// Function: decode_text
static void decode_text(deark *c, lctx *d)
{
	i64 width_in_chars;
	struct de_char_context *charctx = NULL;
	struct de_char_screen *screen;
	i64 i, j;
	u8 ch, attr;
	struct de_encconv_state es;

	// TODO: This might not work for monochrome text mode (d->video_mode==0x32).

	width_in_chars = d->npwidth / 2;

	charctx = de_malloc(c, sizeof(struct de_char_context));
	charctx->screen_image_flag = 1;
	charctx->no_density = 1;
	charctx->nscreens = 1;
	charctx->screens = de_mallocarray(c, charctx->nscreens, sizeof(struct de_char_screen*));
	charctx->screens[0] = de_malloc(c, sizeof(struct de_char_screen));
	screen = charctx->screens[0];

	screen->width = width_in_chars;
	screen->height = d->height;

	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d characters", (int)screen->width, (int)screen->height);

	if(screen->height<1) goto done;

	screen->cell_rows = de_mallocarray(c, screen->height, sizeof(struct de_char_cell*));
	de_encconv_init(&es, d->input_encoding);

	for(j=0; j<screen->height; j++) {
		i64 j2;

		j2 = screen->height-1-j;
		screen->cell_rows[j2] = de_mallocarray(c, screen->width, sizeof(struct de_char_cell));

		for(i=0; i<screen->width; i++) {
			ch = dbuf_getbyte(d->unc_pixels, j*d->npwidth + i*2);
			attr = dbuf_getbyte(d->unc_pixels, j*d->npwidth + i*2 + 1);

			screen->cell_rows[j2][i].fgcol = (de_color)(attr & 0x0f);
			screen->cell_rows[j2][i].bgcol = (de_color)((attr & 0xf0) >> 4);

			// In "blank" regions, some files have nonsense characters, with the fg
			// and bg colors the same. We turn them into spaces, so that copy/paste
			// works right with our HTML output.
			if(ch==0 ||
				(screen->cell_rows[j2][i].fgcol==screen->cell_rows[j2][i].bgcol &&
					!d->opt_keep_invis_chars))
			{
				screen->cell_rows[j2][i].codepoint = 32;
				screen->cell_rows[j2][i].codepoint_unicode = 32;
			}
			else {
				screen->cell_rows[j2][i].codepoint = (i32)ch;
				screen->cell_rows[j2][i].codepoint_unicode = de_char_to_unicode_ex((i32)ch, &es);
			}
		}
	}

	// TODO: Is this always the right palette? Maybe we can't ignore ->edesc
	de_copy_std_palette(DE_PALID_PC16, 0, 0, charctx->pal, 16, 0);

	de_char_output_to_file(c, charctx);

done:
	de_free_charctx(c, charctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x20(%rsi), %rax
movq %rax, %r12
shrq $0x3f, %r12
addq %rax, %r12
sarq %r12
movl $0x98, %esi
callq 0xe8b2b
movq %rax, %r14
movw $0x101, 0x2(%rax)       # imm = 0x101
movq $0x1, 0x10(%rax)
movl $0x1, %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x18(%r14)
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq 0x18(%r14), %rcx
movq %rax, (%rcx)
movq 0x18(%r14), %rax
movq (%rax), %r13
movq %r12, (%r13)
movq 0x28(%r15), %rcx
movq %rcx, 0x8(%r13)
leaq 0x676e0(%rip), %rsi     # 0x11715f
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r13), %rsi
testq %rsi, %rsi
jle 0xafc20
movq %r14, 0x18(%rsp)
movl $0x8, %edx
movq %r13, 0x8(%rsp)
movq %rbx, %rdi
callq 0xe96f7
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
movl 0x8(%r15), %esi
leaq 0x28(%rsp), %rdi
callq 0xebbf7
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
jle 0xafbf2
movq (%rcx), %rsi
xorl %r14d, %r14d
movq %rbx, 0x20(%rsp)
movq %r14, %r12
notq %r12
addq %rax, %r12
movl $0x14, %edx
movq %rbx, %rdi
callq 0xe96f7
movq 0x8(%rsp), %rdi
movq 0x10(%rdi), %rcx
movq %r12, 0x10(%rsp)
movq %rax, (%rcx,%r12,8)
movq (%rdi), %rsi
testq %rsi, %rsi
jle 0xafbdd
movl $0x1, %r12d
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rax
imulq %r14, %rax
leaq (%r12,%rax), %rsi
decq %rsi
callq 0xeead3
movl %eax, %ebp
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rsi
imulq %r14, %rsi
addq %r12, %rsi
callq 0xeead3
movq 0x8(%rsp), %rdi
movzbl %al, %eax
movl %eax, %edx
andl $0xf, %edx
movq 0x10(%rdi), %rcx
movq 0x10(%rsp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %edx, 0x8(%rcx,%r13)
shrl $0x4, %eax
movl %eax, 0xc(%rcx,%r13)
testb %bpl, %bpl
je 0xafbb4
cmpl %eax, %edx
jne 0xafb8e
cmpl $0x0, 0xc(%r15)
je 0xafbb4
movzbl %bpl, %edi
movl %edi, (%rcx,%r13)
leaq 0x28(%rsp), %rsi
callq 0xec0ad
movq 0x8(%rsp), %rdi
movq 0x10(%rdi), %rcx
movq 0x10(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
jmp 0xafbc1
movl $0x20, (%rcx,%r13)
movl $0x20, %eax
movl %eax, 0x4(%rcx,%r13)
incq %rbx
movq (%rdi), %rsi
addq $0x14, %r13
addq $0x2, %r12
cmpq %rsi, %rbx
jl 0xafb23
incq %r14
movq 0x8(%rdi), %rax
cmpq %rax, %r14
movq 0x20(%rsp), %rbx
jl 0xafae4
movq 0x18(%rsp), %r14
movq %r14, %rcx
addq $0x20, %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf404b
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf3fb8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq