// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/grob.c
// Function: grob_text_1_image
static void grob_text_1_image(deark *c, lctx *d, i64 pos1)
{
	i64 data_start;
	u8 x;
	u8 b0, b1;
	i64 pos;
	dbuf *bin_bmp = NULL; // Binary version of the bitmap

	pos = pos1;

	d->w = 0;
	d->h_phys = 0;

	// We assume the GROB text format starts with
	// "GROB" <zero or more spaces> <width> <one or more spaces>
	// <height> <one or more spaces> <data>.

	// TODO: This parser is pretty clumsy.

	pos += 4; // Skip over "GROB"

	while(de_getbyte(pos)==' ')
		pos++;
	while((x=de_getbyte(pos))!=' ') {
		d->w = d->w*10 + (x-'0');
		pos++;
	}

	while(de_getbyte(pos)==' ')
		pos++;
	while((x=de_getbyte(pos))!=' ') {
		d->h_phys = d->h_phys*10 + (x-'0');
		pos++;
	}

	while(de_getbyte(pos)==' ')
		pos++;
	data_start = pos;

	de_dbg(c, "%sdimensions: %d"DE_CHAR_TIMES"%d", (d->num_planes==1)?"":"physical ",
		(int)d->w, (int)d->h_phys);

	// FIXME: This should really be testing the logical height, not the
	// physical height.
	if(!de_good_image_dimensions(c, d->w, d->h_phys))
		goto done;

	// Decode the quasi-hex-encoded data into a memory buffer, then use the
	// same decoder as for binary format.

	bin_bmp = dbuf_create_membuf(c, d->h_phys * (d->w+7)/8, 0);

	pos = data_start;
	while(pos < c->infile->len) {
		b0 = de_getbyte(pos);
		b1 = de_getbyte(pos+1);
		if(b0<48 || b1<48) {
			// Apparently, we've reached the end of the bitmap data.
			break;
		}

		pos+=2;

		x = de_decode_hex_digit(b0,NULL) | (de_decode_hex_digit(b1,NULL)<<4);
		dbuf_writebyte(bin_bmp, x);
	}

	d->bytes_consumed = pos - pos1;

	grob_read_binary_bitmap(c, d, bin_bmp, 0);

done:
	dbuf_close(bin_bmp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x20, %eax
jne 0x76fce
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x76fa7
jmp 0x76fd0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, -0x21(%rbp)
movzbl %al, %eax
cmpl $0x20, %eax
je 0x77015
movq -0x10(%rbp), %rax
imulq $0xa, (%rax), %rcx
movzbl -0x21(%rbp), %eax
subl $0x30, %eax
cltq
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x76fd0
jmp 0x77017
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x20, %eax
jne 0x7703e
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x77017
jmp 0x77040
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, -0x21(%rbp)
movzbl %al, %eax
cmpl $0x20, %eax
je 0x77087
movq -0x10(%rbp), %rax
imulq $0xa, 0x8(%rax), %rcx
movzbl -0x21(%rbp), %eax
subl $0x30, %eax
cltq
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x77040
jmp 0x77089
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x20, %eax
jne 0x770b0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x77089
movq -0x30(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x134f98(%rip), %rdx    # 0x1ac063
leaq 0x159afe(%rip), %rax    # 0x1d0bd0
cmpq $0x1, %rcx
cmoveq %rax, %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %r8d
leaq 0x134f59(%rip), %rsi    # 0x1ac04e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x7711e
jmp 0x77222
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
addq $0x7, %rcx
imulq %rcx, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x771fd
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, -0x22(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x23(%rbp)
movzbl -0x22(%rbp), %eax
cmpl $0x30, %eax
jl 0x771ad
movzbl -0x23(%rbp), %eax
cmpl $0x30, %eax
jge 0x771af
jmp 0x771fd
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %esi
movzbl -0x22(%rbp), %edi
callq 0x1600b0
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
movl %eax, %esi
movzbl -0x23(%rbp), %edi
callq 0x1600b0
movb %al, %cl
movl -0x3c(%rbp), %eax
movzbl %cl, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movb %al, -0x21(%rbp)
movq -0x38(%rbp), %rdi
movzbl -0x21(%rbp), %esi
callq 0x168380
jmp 0x77159
movq -0x30(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x76d50
movq -0x38(%rbp), %rdi
callq 0x1676a0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)