// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pkfont.c
// Function: do_read_raster
static void do_read_raster(deark *c, lctx *d, struct page_ctx *pg)
{
	i64 char_idx;
	struct de_bitmap_font_char *ch;
	u8 v, v1;
	i64 nybble_pos;
	i64 expected_num_pixels;
	i64 j;
	i64 k;
	int parity;
	int next_num_is_repeat_count;
	i64 number;
	i64 run_count;
	i64 repeat_count;

	de_dbg(c, "%scompressed character raster at %d, len=%d", pg->dyn_f==14?"un":"",
		(int)pg->raster_pos, (int)pg->raster_len);
	de_dbg_indent(c, 1);

	expected_num_pixels = (i64)pg->w * (i64)pg->h;
	if(expected_num_pixels<1) {
		de_dbg(c, "ignoring zero-size character (cc=%d) at %d",
			(int)pg->cc, (int)pg->raster_pos);
		goto done;
	}

	// Make sure we have room for the new character
	if(d->font->num_chars+1 > d->char_array_alloc) {
		i64 new_numalloc;
		new_numalloc = d->char_array_alloc*2;
		if(new_numalloc<d->font->num_chars+1) new_numalloc=d->font->num_chars+1;
		if(new_numalloc<37) new_numalloc=37;
		d->font->char_array = de_reallocarray(c, d->font->char_array,
			d->char_array_alloc, sizeof(struct de_bitmap_font_char),
			new_numalloc);
		d->char_array_alloc = new_numalloc;
	}

	// Create the new character
	char_idx = d->font->num_chars++;

	ch = &d->font->char_array[char_idx];
	ch->width = pg->w;
	ch->height = pg->h;
	if(pg->hoff<0 && (pg->hoff >= -128)) {
		// Not sure if this is the right way to handle horizontal spacing, but
		// it looks about right. (At least in a relative way. With some fonts,
		// the glyphs definitely would have to be rendered closer together than
		// our presentation would imply.)
		// Some characters have positive ->hoff values, but we don't have any
		// way to deal with that.
		ch->extraspace_l = (i16)-pg->hoff;
	}

	// The vertical offset will be normalized later, once we know the offsets
	// of all the characters.
	ch->v_offset = (int)-pg->voff;

	ch->rowspan = (ch->width+7)/8;
	ch->bitmap = de_malloc(c, ch->rowspan * ch->height);
	ch->codepoint_nonunicode = pg->cc;

	if(pg->dyn_f==14) {
		u8 *srcbitmap;
		i64 srcbitmap_size;

		srcbitmap_size = (pg->w*pg->h+7)/8;
		srcbitmap = de_malloc(c, srcbitmap_size);
		de_read(srcbitmap, pg->raster_pos, srcbitmap_size);
		for(j=0; j<pg->h; j++) {
			de_copy_bits(srcbitmap, j*ch->width, ch->bitmap, j*ch->rowspan*8, ch->width);
		}

		de_free(c, srcbitmap);
		goto done;
	}

	nybble_pos = 0;
	number = 0;
	parity = pg->start_with_black;
	repeat_count = 0;
	next_num_is_repeat_count = 0;
	pg->curpos_x = 0;
	pg->curpos_y = 0;
	pg->pixelcount = 0;

	while(1) {
		double initial_abs_nybble_pos = (double)pg->raster_pos + (double)nybble_pos/2.0;

		if(nybble_pos >= pg->raster_len*2) break; // out of source data
		if(pg->curpos_y>=pg->h) break; // reached end of image

		v = get_nybble(c->infile, pg->raster_pos, nybble_pos++);

		// The compressed data is a sequence of tokens.
		// A token consists of one or more nybbles.
		// A token beginning with nybble value 0 through 13 represents a number.
		// A number is either a "run count" or a "repeat count".
		// 14 and 15 are special one-nybble tokens.
		// 14 indicates that the next number is a repeat count (instead of a run count).
		// 15 means to set the current repeat count to 1.

		if(v==14) {
			next_num_is_repeat_count = 1;
			if(c->debug_level>=3) {
				de_dbg3(c, "[%.1f] n=%d; repeat_count=...", initial_abs_nybble_pos, (int)v);
			}
			continue;
		}
		else if(v==15) { // v==15: repeat count = 1
			if(c->debug_level>=3) {
				de_dbg3(c, "[%.1f] n=%d; repeat_count=1", initial_abs_nybble_pos, (int)v);
			}
			repeat_count = 1;
			continue;
		}

		// If we get here, then this nybble represents a number, or the start of a number.

		if(v==0) { // large run count
			if(!get_packed_int(c->infile, pg->raster_pos, &nybble_pos, 1, &number)) goto done;
			number = number - 15 + (13-pg->dyn_f)*16 + pg->dyn_f;
		}
		else if(v<=pg->dyn_f) { // one-nybble run count
			number = (i64)v;
		}
		else if(v<=13) { // two-nybble run count
			v1 = get_nybble(c->infile, pg->raster_pos, nybble_pos++);
			number = ((i64)v-pg->dyn_f-1)*16 + v1 + pg->dyn_f + 1;
		}

		if(next_num_is_repeat_count) {
			if(c->debug_level>=3) {
				de_dbg3(c, "[%.1f] ...%d", initial_abs_nybble_pos, (int)number);
			}
			repeat_count = number;
			next_num_is_repeat_count = 0;
			continue;
		}

		// If we get here, we have a number that represents a run count (not a
		// repeat count).
		// Apply it to the character bitmap.

		run_count = number;

		if(c->debug_level>=3) {
			de_dbg3(c, "[%.1f] n=%d; run_count=%d %s", initial_abs_nybble_pos,
				(int)v, (int)run_count, parity?"B":"W");
		}

		for(k=0; k<run_count; k++) {
			pg->pixelcount++;
			if(parity) {
				set_bit_at_cur_pos(ch, pg);
			}
			pg->curpos_x++;

			if(pg->curpos_x>=pg->w) {
				pg->curpos_y++;
				pg->curpos_x = 0;

				// A repeat count applies to the "row on which the first pixel of
				// the next run count will lie".
				// This means that repeats should be applied immediately after the
				// last pixel of a row has been emitted (as opposed to immediately
				// before the first pixel of a row is emitted).
				repeat_row_as_needed(ch, pg, repeat_count);
				repeat_count = 0;
			}
		}
		parity = !parity;
	}

	if(pg->pixelcount != expected_num_pixels) {
		de_warn(c, "Expected %d pixels, got %d (codepoint %d)", (int)expected_num_pixels,
			(int)pg->pixelcount, (int)pg->cc);
	}

done:
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0xc6f16(%rip), %rdx     # 0x1d0bd0
leaq 0x9593e(%rip), %rax     # 0x19f5ff
cmpq $0xe, %rcx
cmoveq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %r8d
leaq 0xb9000(%rip), %rsi     # 0x1c2ce5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movslq 0x4(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
imulq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x1, -0x40(%rbp)
jge 0x109d40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %ecx
leaq 0xb8fdd(%rip), %rsi     # 0x1c2d11
movb $0x0, %al
callq 0x15b0d0
jmp 0x10a33f
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jle 0x109de5
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq 0x18(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x109d97
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
cmpq $0x25, -0x78(%rbp)
jge 0x109da6
movq $0x25, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x78(%rbp), %r8
movl $0x28, %ecx
callq 0x15c4d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq 0x18(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x18(%rcx)
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jge 0x109e5d
movq -0x18(%rbp), %rax
cmpq $-0x80, 0x20(%rax)
jl 0x109e5d
movq -0x18(%rbp), %rcx
xorl %eax, %eax
subq 0x20(%rcx), %rax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x18(%rbp), %rcx
xorl %eax, %eax
subq 0x28(%rcx), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
movslq 0xc(%rax), %rax
imulq %rax, %rsi
callq 0x15b780
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpq $0xe, 0x30(%rax)
jne 0x109f9a
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
imull 0x8(%rcx), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
cltq
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x15b780
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x80(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x88(%rbp), %rcx
callq 0x164d10
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jge 0x109f88
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x28(%rbp), %rax
movslq 0x8(%rax), %rax
imulq %rax, %rsi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
imulq 0x18(%rax), %rcx
shlq $0x3, %rcx
movq -0x28(%rbp), %rax
movslq 0x8(%rax), %r8
callq 0x15e500
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x109f2f
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x15b820
jmp 0x10a33f
movq $0x0, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x54(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x18(%rbp), %rax
cvtsi2sdq 0x40(%rax), %xmm0
cvtsi2sdq -0x38(%rbp), %xmm1
movsd 0xa6429(%rip), %xmm2    # 0x1b0428
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jl 0x10a028
jmp 0x10a306
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jl 0x10a042
jmp 0x10a306
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x38(%rbp), %rdx
movq %rdx, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x10a360
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0xe, %eax
jne 0x10a0a5
movl $0x1, -0x58(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x10a0a0
movq -0x8(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
movzbl -0x29(%rbp), %edx
leaq 0xb8ca3(%rip), %rsi     # 0x1c2d3c
movb $0x1, %al
callq 0x15b400
jmp 0x109fe7
movzbl -0x29(%rbp), %eax
cmpl $0xf, %eax
jne 0x10a0e2
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x10a0d5
movq -0x8(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
movzbl -0x29(%rbp), %edx
leaq 0xb8c8c(%rip), %rsi     # 0x1c2d5a
movb $0x1, %al
callq 0x15b400
movq $0x1, -0x70(%rbp)
jmp 0x109fe7
jmp 0x10a0e4
movzbl -0x29(%rbp), %eax
cmpl $0x0, %eax
jne 0x10a143
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
leaq -0x38(%rbp), %rdx
movl $0x1, %ecx
leaq -0x60(%rbp), %r8
callq 0x10a3d0
cmpl $0x0, %eax
jne 0x10a119
jmp 0x10a33f
movq -0x60(%rbp), %rax
subq $0xf, %rax
movq -0x18(%rbp), %rdx
movl $0xd, %ecx
subq 0x30(%rdx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x60(%rbp)
jmp 0x10a1ba
movzbl -0x29(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jg 0x10a15b
movzbl -0x29(%rbp), %eax
movq %rax, -0x60(%rbp)
jmp 0x10a1b8
movzbl -0x29(%rbp), %eax
cmpl $0xd, %eax
jg 0x10a1b6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x38(%rbp), %rdx
movq %rdx, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x10a360
movb %al, -0x2a(%rbp)
movzbl -0x29(%rbp), %eax
movq -0x18(%rbp), %rcx
subq 0x30(%rcx), %rax
subq $0x1, %rax
shlq $0x4, %rax
movzbl -0x2a(%rbp), %ecx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
addq 0x30(%rcx), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x10a1b8
jmp 0x10a1ba
cmpl $0x0, -0x58(%rbp)
je 0x10a1fd
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x10a1e9
movq -0x8(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
movq -0x60(%rbp), %rax
movl %eax, %edx
leaq 0xb8b94(%rip), %rsi     # 0x1c2d76
movb $0x1, %al
callq 0x15b400
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x58(%rbp)
jmp 0x109fe7
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x10a24a
movq -0x8(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
movzbl -0x29(%rbp), %edx
movq -0x68(%rbp), %rax
movl %eax, %ecx
movl -0x54(%rbp), %esi
leaq 0xa5aa1(%rip), %r8      # 0x1afccf
leaq 0xc0d31(%rip), %rax     # 0x1caf66
cmpl $0x0, %esi
cmovneq %rax, %r8
leaq 0xb8b40(%rip), %rsi     # 0x1c2d83
movb $0x1, %al
callq 0x15b400
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x10a2f0
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x60(%rax)
cmpl $0x0, -0x54(%rbp)
je 0x10a283
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x10a4f0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jl 0x10a2dd
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x10a5c0
movq $0x0, -0x70(%rbp)
jmp 0x10a2df
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x10a252
cmpl $0x0, -0x54(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x54(%rbp)
jmp 0x109fe7
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
cmpq -0x40(%rbp), %rax
je 0x10a33d
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl (%rax), %r8d
leaq 0xb8a6a(%rip), %rsi     # 0x1c2da0
movb $0x0, %al
callq 0x15bc10
jmp 0x10a33f
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)