// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x98, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x20(%rbp), %rax
movq $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
leaq 0xa0734(%rip), %rsi     # 0x1a2f05
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jge 0x1027e8
jmp 0x102a51
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
leaq -0x60(%rbp), %rdi
callq 0x160150
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x102a22
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
subq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movl $0x14, %edx
callq 0x15c450
movq %rax, %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x102a0f
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
imulq 0x20(%rax), %rsi
movq -0x30(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x165060
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
imulq 0x20(%rax), %rsi
movq -0x30(%rbp), %rax
shlq %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x3a(%rbp)
movzbl -0x3a(%rbp), %ecx
andl $0xf, %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movzbl -0x3a(%rbp), %ecx
andl $0xf0, %ecx
sarl $0x4, %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
je 0x102975
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imulq $0x14, -0x30(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
imulq $0x14, -0x30(%rbp), %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jne 0x1029b4
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x1029b4
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imulq $0x14, -0x30(%rbp), %rcx
addq %rcx, %rax
movl $0x20, (%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imulq $0x14, -0x30(%rbp), %rcx
addq %rcx, %rax
movl $0x20, 0x4(%rax)
jmp 0x1029fc
movzbl -0x39(%rbp), %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movzbl -0x39(%rbp), %edi
leaq -0x60(%rbp), %rsi
callq 0x160a30
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0x1029fe
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x102877
jmp 0x102a11
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x102821
movq -0x20(%rbp), %rcx
addq $0x20, %rcx
movl $0x14, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
movl %r9d, %esi
callq 0x161320
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16da40
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16d960
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)