// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc2.c
// Function: de_run_pm_xv
static void de_run_pm_xv(deark *c, de_module_params *mparams)
{
	de_bitmap *img = NULL;
	int is_le;
	i64 width, height;
	i64 nplanes;
	i64 nbands;
	i64 pixelformat;
	i64 commentsize;
	i64 i, j;
	i64 plane;
	i64 rowspan;
	i64 planespan;
	i64 pos;
	u8 b;

	if(!dbuf_memcmp(c->infile, 0, "WEIV", 4))
		is_le = 1;
	else
		is_le = 0;

	nplanes = dbuf_geti32x(c->infile, 4, is_le);
	de_dbg(c, "planes: %d", (int)nplanes);

	height = dbuf_geti32x(c->infile, 8, is_le);
	width = dbuf_geti32x(c->infile, 12, is_le);
	de_dbg_dimensions(c, width, height);
	if(!de_good_image_dimensions(c, width, height)) goto done;

	nbands = dbuf_geti32x(c->infile, 16, is_le);
	de_dbg(c, "bands: %d", (int)nbands);

	pixelformat = dbuf_geti32x(c->infile, 20, is_le);
	de_dbg(c, "pixel format: 0x%04x", (unsigned int)pixelformat);

	commentsize = dbuf_geti32x(c->infile, 24, is_le);
	de_dbg(c, "comment size: %d", (int)commentsize);

	pos = 28;

	if((pixelformat==0x8001 && nplanes==3 && nbands==1) ||
		(pixelformat==0x8001 && nplanes==1 && nbands==1))
	{
		;
	}
	else {
		de_err(c, "Unsupported image type (pixel format=0x%04x, "
			"planes=%d, bands=%d)", (unsigned int)pixelformat,
			(int)nplanes, (int)nbands);
		goto done;
	}

	rowspan = width;
	planespan = rowspan*height;

	img = de_bitmap_create(c, width, height, (int)nplanes);

	for(plane=0; plane<nplanes; plane++) {
		for(j=0; j<height; j++) {
			for(i=0; i<width; i++) {
				b = de_getbyte(pos + plane*planespan + j*rowspan + i);
				if(nplanes==3) {
					de_bitmap_setsample(img, i, j, plane, b);
				}
				else {
					de_bitmap_setpixel_gray(img, i, j, b);
				}
			}
		}
	}
	de_bitmap_write_to_file(img, NULL, 0);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0xbc468(%rip), %rdx     # 0x125292
xorl %r13d, %r13d
movl $0x4, %ecx
xorl %esi, %esi
callq 0xf025e
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movq 0x18(%rbx), %rdi
movl $0x4, %esi
movl %ebp, %edx
callq 0xef654
leaq 0xd229f(%rip), %rsi     # 0x13b0f7
movq %rbx, %rdi
movq %rax, (%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x8, %esi
movl %ebp, %edx
callq 0xef654
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0xc, %esi
movl %ebp, %edx
callq 0xef654
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xe9b05
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, 0x8(%rsp)
movq %r14, %rdx
callq 0xf241d
testl %eax, %eax
je 0x69065
movq 0x18(%rbx), %rdi
movl $0x10, %esi
movl %ebp, %edx
callq 0xef654
movq %rax, %r14
leaq 0xbc3c6(%rip), %rsi     # 0x125297
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x14, %esi
movl %ebp, %edx
callq 0xef654
movq %rax, %r15
leaq 0xbc3a9(%rip), %rsi     # 0x1252a1
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x18, %esi
movl %ebp, %edx
callq 0xef654
leaq 0xbc39a(%rip), %rsi     # 0x1252b6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x1, %r14
jne 0x68f3e
cmpq $0x3, (%rsp)
jne 0x68f3e
cmpq $0x8001, %r15           # imm = 0x8001
je 0x68f60
cmpq $0x1, %r14
jne 0x69047
cmpq $0x1, (%rsp)
jne 0x69047
cmpq $0x8001, %r15           # imm = 0x8001
jne 0x69047
movq %rbx, %r13
movq %rbx, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq (%rsp), %rbx
movl %ebx, %ecx
callq 0xf2cab
movq %rax, %rbp
testq %rbx, %rbx
jle 0x69036
movq 0x8(%rsp), %rax
imulq %r12, %rax
movq %rax, 0x20(%rsp)
movl $0x1c, %eax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
cmpq $0x0, 0x8(%rsp)
jle 0x69010
movq 0x10(%rsp), %r14
xorl %r15d, %r15d
testq %r12, %r12
jle 0x69003
xorl %ebx, %ebx
movq 0x18(%r13), %rdi
leaq (%r14,%rbx), %rsi
callq 0xef0d5
movzbl %al, %r8d
cmpq $0x3, (%rsp)
jne 0x68fea
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
callq 0xf299c
jmp 0x68ffb
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r8d, %ecx
callq 0xf2a4a
incq %rbx
cmpq %rbx, %r12
jne 0x68fbd
incq %r15
addq %r12, %r14
cmpq 0x8(%rsp), %r15
jne 0x68fb6
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
cmpq (%rsp), %rcx
jne 0x68fa6
movq %rbp, %r13
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
jmp 0x69065
leaq 0xbc279(%rip), %rsi     # 0x1252c7
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r15d, %edx
movq (%rsp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0xe9df7
movq %r13, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf28d4