// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movw $0x101, 0x2(%rax)       # imm = 0x101
movq $0x1, 0x10(%rax)
movl $0x1, %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x18(%r14)
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe969b
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 0x66ae5(%rip), %rsi     # 0x1170bf
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r13), %rsi
testq %rsi, %rsi
jle 0xb0772
movq %r14, 0x18(%rsp)
movl $0x8, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x10(%r13)
movl 0x8(%r15), %esi
leaq 0x28(%rsp), %rdi
callq 0xec207
movq 0x8(%r13), %rax
testq %rax, %rax
jle 0xb0744
xorl %r14d, %r14d
movq %rbx, 0x20(%rsp)
movq %r13, 0x8(%rsp)
movq %r14, %r12
notq %r12
addq %rax, %r12
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movl $0x14, %edx
movq %rbx, %rdi
callq 0xea259
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rcx
movq %r12, 0x10(%rsp)
movq %rax, (%rcx,%r12,8)
cmpq $0x0, (%rsi)
jle 0xb072f
movl $0x1, %r12d
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rax
imulq %r14, %rax
leaq (%r12,%rax), %rsi
decq %rsi
callq 0xef0d5
movl %eax, %ebp
movq 0x88(%r15), %rdi
movq 0x20(%r15), %rsi
imulq %r14, %rsi
addq %r12, %rsi
callq 0xef0d5
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,%rbx)
shrl $0x4, %eax
movl %eax, 0xc(%rcx,%rbx)
testb %bpl, %bpl
je 0xb070b
movl 0x8(%rcx,%rbx), %eax
cmpl 0xc(%rcx,%rbx), %eax
jne 0xb06e6
cmpl $0x0, 0xc(%r15)
je 0xb070b
movzbl %bpl, %edi
movl %edi, (%rcx,%rbx)
leaq 0x28(%rsp), %rsi
callq 0xec6fd
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rcx
movq 0x10(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
jmp 0xb0717
movl $0x20, (%rcx,%rbx)
movl $0x20, %eax
movl %eax, 0x4(%rcx,%rbx)
incq %r13
addq $0x14, %rbx
addq $0x2, %r12
cmpq (%rsi), %r13
jl 0xb0677
incq %r14
movq 0x8(%rsi), %rax
cmpq %rax, %r14
movq 0x20(%rsp), %rbx
jl 0xb0632
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 0xecbbc
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf455b
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf44c8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq