// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rcx
cqto
idivq %rcx
movq %rax, %r13
movl $0x98, %esi
callq 0xdecaf
movq %rax, %r14
movw $0x101, 0x2(%rax)       # imm = 0x101
movq $0x1, 0x10(%rax)
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x18(%r14)
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq 0x18(%r14), %rcx
movq %rax, (%rcx)
movq 0x18(%r14), %rax
movq (%rax), %r12
movq %r13, (%r12)
movq 0x28(%r15), %rcx
movq %rcx, 0x8(%r12)
leaq 0x61104(%rip), %rsi     # 0x10a0cf
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x8(%r12), %rsi
testq %rsi, %rsi
jle 0xa914e
movq %r14, 0x18(%rsp)
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x10(%r12)
movl 0x8(%r15), %esi
leaq 0x28(%rsp), %rdi
callq 0xe11fc
xorl %ebp, %ebp
movq %r12, %rsi
movq %rbx, 0x20(%rsp)
movq %r12, 0x8(%rsp)
movq 0x8(%rsi), %r14
cmpq %r14, %rbp
jge 0xa9124
movq %rbp, %rax
notq %rax
addq %rax, %r14
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
pushq $0x14
popq %rdx
callq 0xdf66f
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rcx
movq %r14, 0x10(%rsp)
movq %rax, (%rcx,%r14,8)
pushq $0x1
popq %rbx
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq (%rsi), %r12
jge 0xa9117
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rax
imulq %rbp, %rax
leaq (%rbx,%rax), %rsi
decq %rsi
callq 0xe3eb4
movl %eax, %r14d
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rsi
imulq %rbp, %rsi
addq %rbx, %rsi
callq 0xe3eb4
movq 0x8(%rsp), %rsi
movzbl %al, %eax
movl %eax, %edx
andl $0xf, %edx
movq 0x10(%rsi), %rcx
movq 0x10(%rsp), %rdi
movq (%rcx,%rdi,8), %rcx
movl %edx, 0x8(%rcx,%r13)
shrl $0x4, %eax
movl %eax, 0xc(%rcx,%r13)
testb %r14b, %r14b
je 0xa90f7
cmpl %eax, %edx
jne 0xa90d1
cmpl $0x0, 0xc(%r15)
je 0xa90f7
movzbl %r14b, %edi
movl %edi, (%rcx,%r13)
leaq 0x28(%rsp), %rsi
callq 0xe1679
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rcx
movq 0x10(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
jmp 0xa9102
movl $0x20, (%rcx,%r13)
pushq $0x20
popq %rax
movl %eax, 0x4(%rcx,%r13)
incq %r12
addq $0x14, %r13
addq $0x2, %rbx
jmp 0xa905c
incq %rbp
movq 0x20(%rsp), %rbx
jmp 0xa9018
movq 0x18(%rsp), %r14
movq %r14, %rcx
addq $0x20, %rcx
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8a63
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe89d2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq