// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pcx.c
// Function: do_read_header
static int do_read_header(deark *c, lctx *d)
{
	u8 initialbyte;
	int retval = 0;
	i64 hres, vres;
	i64 pos = 0;
	const char *imgtypename = "";

	de_dbg(c, "header at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	initialbyte = de_getbyte_p(&pos);
	d->version = de_getbyte_p(&pos);
	if(!d->is_mswordscr) {
		if(initialbyte==0xeb && d->version==0x0e) {
			d->is_pcxsfx = 1;
			d->version = 5;
		}
	}
	de_dbg(c, "format version: %u", (UI)d->version);

	if(d->is_mswordscr) {
		de_declare_fmt(c, "Word for DOS screen capture");
	}
	else if(d->is_pcxsfx) {
		de_declare_fmt(c, "VGAPaint 386 PCX-SFX");
	}
	else {
		de_declare_fmt(c, "PCX");
	}

	d->encoding = de_getbyte_p(&pos);
	de_dbg(c, "encoding: %u", (UI)d->encoding);

	d->bits = (i64)de_getbyte_p(&pos); // Bits per pixel per plane
	de_dbg(c, "bits: %d", (int)d->bits);
	if(d->bits<1) d->bits = 1;

	d->margin_L = de_getu16le_p(&pos);
	d->margin_T = de_getu16le_p(&pos);
	d->margin_R = de_getu16le_p(&pos);
	d->margin_B = de_getu16le_p(&pos);
	de_dbg(c, "margins: %d, %d, %d, %d", (int)d->margin_L, (int)d->margin_T,
		(int)d->margin_R, (int)d->margin_B);
	d->reported_width = d->margin_R - d->margin_L +1;
	d->height = d->margin_B - d->margin_T +1;
	de_dbg_dimensions(c, d->reported_width, d->height);

	hres = de_getu16le_p(&pos);
	vres = de_getu16le_p(&pos);
	de_dbg(c, "resolution: %d"DE_CHAR_TIMES"%d", (int)hres, (int)vres);

	// The palette (offset 16-63) will be read later.

	pos = 64;
	// For older versions of PCX, this field might be useful to help identify
	// the intended video mode. Documentation is lacking, though.
	d->reserved1 = de_getbyte_p(&pos);
	de_dbg(c, "vmode: 0x%02x", (UI)d->reserved1);

	d->planes = (i64)de_getbyte_p(&pos);
	de_dbg(c, "planes: %d", (int)d->planes);
	d->rowspan_raw = de_getu16le_p(&pos);
	de_dbg(c, "bytes/plane/row: %d", (int)d->rowspan_raw);

	// TODO: Is this field (@68) 1 byte or 2?
	d->palette_info = (UI)de_getbyte_p(&pos);
	pos++;
	de_dbg(c, "palette info: %u", (UI)d->palette_info);

	if(d->version>=5) {
		d->hscreensize = de_getu16le_p(&pos);
		d->vscreensize = de_getu16le_p(&pos);
		if(!sane_screensize(d->hscreensize, d->vscreensize)) {
			d->hscreensize = 0;
			d->vscreensize = 0;
		}
	}
	if(d->hscreensize) {
		de_dbg(c, "screen size: %d" DE_CHAR_TIMES "%d", (int)d->hscreensize,
			(int)d->vscreensize);
	}

	//-----

	d->padded_width = (d->rowspan_raw*8) / d->bits;
	d->width_to_use = d->reported_width;
	if(c->padpix) {
		if(d->padded_width>d->reported_width) {
			d->width_to_use = d->padded_width;
		}
	}
	else {
		if(d->width_to_use<1 && d->padded_width>0) {
			de_warn(c, "Invalid width %"I64_FMT"; using %"I64_FMT" instead",
				d->width_to_use, d->padded_width);
			d->width_to_use = d->padded_width;
		}
	}

	if(!de_good_image_dimensions(c, d->width_to_use, d->height)) goto done;

	d->rowspan = d->rowspan_raw * d->planes;
	de_dbg(c, "calculated bytes/row: %d", (int)d->rowspan);

	d->bits_per_pixel = d->bits * d->planes;

	if(d->encoding!=0 && d->encoding!=1) {
		de_err(c, "Unsupported compression type: %d", (int)d->encoding);
		goto done;
	}

	// Enumerate the known PCX image types.
	if(d->planes==1 && d->bits==1) {
		imgtypename = "2-color";
		d->ncolors = 2;
	}
	//else if(d->planes==2 && d->bits==1) {
	//	d->ncolors = 4;
	//}
	else if(d->planes==1 && d->bits==2) {
		imgtypename = "4-color";
		d->ncolors = 4;
	}
	else if(d->planes==1 && d->bits==4) {
		imgtypename = "16-color nonplanar";
		d->ncolors = 16;
	}
	else if(d->planes==3 && d->bits==1) {
		imgtypename = "8-color";
		d->ncolors = 8;
	}
	else if(d->planes==4 && d->bits==1) {
		imgtypename = "16-color";
		d->ncolors = 16;
	}
	//else if(d->planes==1 && d->bits==4) {
	//	d->ncolors = 16;
	//}
	//else if(d->planes==4 && d->bits==2) {
	//	d->ncolors = 16; (?)
	//}
	else if(d->planes==1 && d->bits==8) {
		imgtypename = "256-color";
		d->ncolors = 256;
	}
	//else if(d->planes==4 && d->bits==4) {
	//	d->ncolors = 4096;
	//}
	else if(d->planes==3 && d->bits==8) {
		imgtypename = "truecolor";
		d->ncolors = 16777216;
	}
	else if(d->planes==4 && d->bits==8) {
		// I can't find a PCX spec that mentions 32-bit RGBA images, but
		// ImageMagick and Wikipedia act like they're perfectly normal.
		imgtypename = "truecolor+alpha";
		d->ncolors = 16777216;
		d->has_transparency = 1;
	}
	else {
		de_err(c, "Unsupported image type (bits=%d, planes=%d)",
			(int)d->bits, (int)d->planes);
		goto done;
	}

	de_dbg(c, "image type: %s", imgtypename);

	do_decode_resolution(c, d, hres, vres);

	retval = 1;
done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
leaq 0xcc5aa(%rip), %rax     # 0x1d0bd0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xaa72a(%rip), %rsi     # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x90(%rax)
jne 0x1046b3
movzbl -0x11(%rbp), %eax
cmpl $0xeb, %eax
jne 0x1046b1
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xe, %eax
jne 0x1046b1
movq -0x10(%rbp), %rax
movb $0x1, 0x91(%rax)
movq -0x10(%rbp), %rax
movb $0x5, (%rax)
jmp 0x1046b3
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl (%rax), %edx
leaq 0x9b82e(%rip), %rsi     # 0x19fef3
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x90(%rax)
je 0x1046eb
movq -0x8(%rbp), %rdi
leaq 0xbd4e7(%rip), %rsi     # 0x1c1bcb
callq 0x15e380
jmp 0x10471c
movq -0x10(%rbp), %rax
cmpb $0x0, 0x91(%rax)
je 0x10470a
movq -0x8(%rbp), %rdi
leaq 0xbd4e4(%rip), %rsi     # 0x1c1be7
callq 0x15e380
jmp 0x10471a
movq -0x8(%rbp), %rdi
leaq 0xb1a51(%rip), %rsi     # 0x1b6166
callq 0x15e380
jmp 0x10471c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0xbd4b3(%rip), %rsi     # 0x1c1bfc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0xd307a(%rip), %rsi     # 0x1d77fd
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jge 0x1047a1
movq -0x10(%rbp), %rax
movq $0x1, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %r8d
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %r9d
leaq 0xbd3c3(%rip), %rsi     # 0x1c1c09
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x18(%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x20(%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl %eax, %ecx
leaq 0xbd33c(%rip), %rsi     # 0x1c1c21
movb $0x0, %al
callq 0x15b0d0
movq $0x40, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x6c(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x6c(%rax), %edx
leaq 0xbd313(%rip), %rsi     # 0x1c1c34
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0xc23a4(%rip), %rsi     # 0x1c6cff
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0xbd2af(%rip), %rsi     # 0x1c1c42
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x68(%rax), %edx
leaq 0xbd283(%rip), %rsi     # 0x1c1c56
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5, %eax
jl 0x104a52
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x1053c0
cmpl $0x0, %eax
jne 0x104a50
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
jmp 0x104a52
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x104a83
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %ecx
leaq 0xbd1eb(%rip), %rsi     # 0x1c1c67
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
shlq $0x3, %rax
movq -0x10(%rbp), %rcx
cqto
idivq 0x8(%rcx)
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0xd5(%rax)
je 0x104aeb
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jle 0x104ae9
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
jmp 0x104b3e
movq -0x10(%rbp), %rax
cmpq $0x1, 0x80(%rax)
jge 0x104b3c
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jle 0x104b3c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
leaq 0xbd159(%rip), %rsi     # 0x1c1c7b
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
jmp 0x104b3e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x104b67
jmp 0x104dee
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x48(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edx
leaq 0xbd10b(%rip), %rsi     # 0x1c1ca0
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x48(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x104bee
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x1, %eax
je 0x104bee
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0xa33c5(%rip), %rsi     # 0x1a7fa7
movb $0x0, %al
callq 0x15bfb0
jmp 0x104dee
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x104c20
movq -0x10(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jne 0x104c20
leaq 0xbd0ae(%rip), %rax     # 0x1c1cb9
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x2, 0x60(%rax)
jmp 0x104dbc
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x104c52
movq -0x10(%rbp), %rax
cmpq $0x2, 0x8(%rax)
jne 0x104c52
leaq 0xbd084(%rip), %rax     # 0x1c1cc1
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x4, 0x60(%rax)
jmp 0x104dba
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x104c84
movq -0x10(%rbp), %rax
cmpq $0x4, 0x8(%rax)
jne 0x104c84
leaq 0xbd05a(%rip), %rax     # 0x1c1cc9
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x10, 0x60(%rax)
jmp 0x104db8
movq -0x10(%rbp), %rax
cmpq $0x3, 0x48(%rax)
jne 0x104cb6
movq -0x10(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jne 0x104cb6
leaq 0xbd03b(%rip), %rax     # 0x1c1cdc
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x8, 0x60(%rax)
jmp 0x104db6
movq -0x10(%rbp), %rax
cmpq $0x4, 0x48(%rax)
jne 0x104ce8
movq -0x10(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jne 0x104ce8
leaq 0xbd011(%rip), %rax     # 0x1c1ce4
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x10, 0x60(%rax)
jmp 0x104db4
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x104d1a
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jne 0x104d1a
leaq 0xbc9ea(%rip), %rax     # 0x1c16ef
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x100, 0x60(%rax)      # imm = 0x100
jmp 0x104db2
movq -0x10(%rbp), %rax
cmpq $0x3, 0x48(%rax)
jne 0x104d49
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jne 0x104d49
leaq 0xbcfb6(%rip), %rax     # 0x1c1ced
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x1000000, 0x60(%rax)  # imm = 0x1000000
jmp 0x104db0
movq -0x10(%rbp), %rax
cmpq $0x4, 0x48(%rax)
jne 0x104d86
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jne 0x104d86
leaq 0xbcf91(%rip), %rax     # 0x1c1cf7
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq $0x1000000, 0x60(%rax)  # imm = 0x1000000
movq -0x10(%rbp), %rax
movl $0x1, 0x98(%rax)
jmp 0x104dae
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %ecx
leaq 0xbcf62(%rip), %rsi     # 0x1c1d07
movb $0x0, %al
callq 0x15bfb0
jmp 0x104dee
jmp 0x104db0
jmp 0x104db2
jmp 0x104db4
jmp 0x104db6
jmp 0x104db8
jmp 0x104dba
jmp 0x104dbc
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xb72fc(%rip), %rsi     # 0x1bc0c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x105470
movl $0x1, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x18(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)