// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/jovianvi.c
// Function: de_run_jovianvi
static void de_run_jovianvi(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	de_bitmap *img = NULL;
	int has_palette = 0;
	int is_grayscale = 0;
	const char *imgtypename;
	UI createflags = 0;

	// Warning: This decoder is based on reverse engineering, and may be
	// incorrect or incomplete.

	d = de_malloc(c, sizeof(lctx));

	d->imgtype = de_getbyte(2);
	de_dbg(c, "image type: 0x%02x", (unsigned int)d->imgtype);

	switch(d->imgtype) {
	case 0x10:
		d->bitdepth = 16;
		break;
	case 0x11:
		d->bitdepth = 24;
		break;
	case 0x20:
		d->bitdepth = 4;
		is_grayscale = 1;
		break;
	case 0x21:
		d->bitdepth = 6;
		is_grayscale = 1;
		break;
	case 0x22:
		d->bitdepth = 8;
		is_grayscale = 1;
		break;
	case 0x23:
		d->bitdepth = 1;
		is_grayscale = 1;
		break;
	case 0x30:
		d->bitdepth = 8;
		has_palette = 1;
		break;
	case 0x31:
		d->bitdepth = 4;
		has_palette = 1;
		break;
	default:
		de_err(c, "Unknown VI image type: 0x%02x", (unsigned int)d->imgtype);
		goto done;
	}

	if(d->bitdepth==6)
		d->bits_alloc = 8;
	else
		d->bits_alloc = d->bitdepth;

	de_dbg_indent(c, 1);

	if(is_grayscale) imgtypename="grayscale";
	else if(has_palette) imgtypename="palette color";
	else imgtypename="RGB";
	de_dbg(c, "%d bits/pixel, %s", (int)d->bitdepth, imgtypename);
	de_dbg_indent(c, -1);
	if(is_grayscale && (d->bitdepth!=1 && d->bitdepth!=4 && d->bitdepth!=6 && d->bitdepth!=8)) {
		de_err(c, "This type of VI image is not supported");
		goto done;
	}

	d->npwidth = de_getu16le(3);
	d->h = de_getu16le(5);
	de_dbg_dimensions(c, d->npwidth, d->h);
	if(!de_good_image_dimensions(c, d->npwidth, d->h)) goto done;

	if(has_palette) {
		d->pal_code = de_getbyte(9);
		de_dbg(c, "palette code: 0x%02x", (unsigned int)d->pal_code);

		d->pal_first_entry_idx = (i64)de_getbyte(10);
		d->num_pal_colors =  (i64)de_getbyte(11);
		if(d->num_pal_colors==0)
			d->num_pal_colors = 256;
		de_dbg(c, "index of first palette color: %d", (int)d->pal_first_entry_idx);
		de_dbg(c, "number of palette colors: %d", (int)d->num_pal_colors);
	}

	d->palpos = de_getu16le(12);
	d->bitspos = de_getu16le(14);

	// Read palette, if applicable
	if(has_palette) {
		do_read_palette(c, d);
	}

	// Convert the image
	de_dbg(c, "bitmap at %d", (int)d->bitspos);
	d->rowspan = (d->npwidth*d->bits_alloc + 7)/8;
	// Note that a partial pixel's worth of padding is possible in some cases, but
	// it's not worth the trouble to make -padpix support it.
	d->pdwidth = (d->rowspan*8)/(d->bits_alloc);

	img = de_bitmap_create2(c, d->npwidth, d->pdwidth, d->h, is_grayscale?1:3);
	if(has_palette) {
		de_convert_image_paletted(c->infile, d->bitspos, d->bitdepth, d->rowspan, d->pal, img, 0);
	}
	else if(is_grayscale && d->bitdepth==1) {
		createflags |= DE_CREATEFLAG_IS_BWIMG;
		de_convert_image_bilevel(c->infile, d->bitspos, d->rowspan, img, 0);
	}
	else if(is_grayscale) {
		de_make_grayscale_palette(d->pal, 1ULL<<d->bitdepth, 0);
		de_convert_image_paletted(c->infile, d->bitspos, d->bits_alloc, d->rowspan,
			d->pal, img, 0);
	}
	else {
		do_convert_rgb(c, d, img);
	}
	de_bitmap_write_to_file(img, NULL, createflags);

done:
	de_bitmap_destroy(img);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
movl $0x458, %esi            # imm = 0x458
callq 0xe969b
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xef0d5
movb %al, (%r14)
movzbl %al, %edx
leaq 0xaa1ef(%rip), %rsi     # 0x12a5ac
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl (%r14), %edx
movb $0x1, %r12b
cmpl $0x1f, %edx
jle 0x803fa
leal -0x20(%rdx), %eax
cmpl $0x3, %eax
ja 0x80410
leaq 0xaa196(%rip), %rcx     # 0x12a578
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %edx
xorl %r12d, %r12d
movl $0x4, %eax
jmp 0x80476
cmpl $0x10, %edx
je 0x80446
cmpl $0x11, %edx
jne 0x8042d
movl $0x18, %edx
movl $0x18, %eax
jmp 0x80476
cmpl $0x30, %edx
je 0x8091c
cmpl $0x31, %edx
jne 0x8042d
movl $0x4, %edx
xorl %r15d, %r15d
movl $0x4, %eax
jmp 0x80479
leaq 0xaa16d(%rip), %rsi     # 0x12a5a1
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x808cd
movq %rdx, %rax
jmp 0x80476
movl $0x8, %edx
xorl %r12d, %r12d
movl $0x8, %eax
jmp 0x80476
movl $0x1, %edx
xorl %r12d, %r12d
movl $0x1, %eax
jmp 0x80476
movl $0x6, %eax
movl $0x8, %edx
xorl %r12d, %r12d
movb $0x1, %r15b
movq %rax, 0x20(%r14)
movq %rdx, 0x28(%r14)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0xbf10b(%rip), %rax     # 0x13f5a0
leaq 0xc9c22(%rip), %rdx     # 0x14a0be
testb %r15b, %r15b
cmovneq %rax, %rdx
leaq 0x95b26(%rip), %rcx     # 0x115fd0
testb %r12b, %r12b
cmovneq %rdx, %rcx
movl 0x20(%r14), %edx
leaq 0xaa103(%rip), %rsi     # 0x12a5bf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
testb %r12b, %r12b
jne 0x804f5
movq 0x20(%r14), %rax
cmpq $0x8, %rax
ja 0x8092e
movl $0x152, %ecx            # imm = 0x152
btq %rax, %rcx
jae 0x8092e
movq 0x18(%rbx), %rdi
movl $0x3, %esi
callq 0xef1ca
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xef1ca
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x80891
testb %r15b, %r15b
jne 0x805ce
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xef0d5
movb %al, 0x1(%r14)
movzbl %al, %edx
leaq 0xaa093(%rip), %rsi     # 0x12a5f8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef0d5
movzbl %al, %eax
movq %rax, 0x48(%r14)
movq 0x18(%rbx), %rdi
movl $0xb, %esi
callq 0xef0d5
movzbl %al, %eax
testb %al, %al
movl $0x100, %ecx            # imm = 0x100
cmovneq %rax, %rcx
movq %rcx, 0x50(%r14)
movl 0x48(%r14), %edx
leaq 0xaa05e(%rip), %rsi     # 0x12a60d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x50(%r14), %edx
leaq 0x90df0(%rip), %rsi     # 0x1113b4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xef1ca
movq %rax, 0x38(%r14)
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef1ca
movq %rax, 0x40(%r14)
testb %r15b, %r15b
movq %rbx, 0x18(%rsp)
jne 0x8073c
movl %r15d, 0x28(%rsp)
movl 0x38(%r14), %edx
leaq 0xafb60(%rip), %rsi     # 0x130170
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq $0x0, 0x50(%r14)
jle 0x80725
leaq 0x15(%rsp), %r15
xorl %ebp, %ebp
movq 0x48(%r14), %rbx
addq %rbp, %rbx
cmpq $0xff, %rbx
jg 0x80725
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
addq 0x38(%r14), %rdx
movl $0x3, %ecx
movq %r15, %rsi
callq 0xeeee8
xorl %r13d, %r13d
cmpb $0x0, 0x1(%r14)
movb 0x15(%rsp,%r13), %al
jne 0x80689
movzbl %al, %edi
callq 0xecde3
movb %al, 0x25(%rsp,%r13)
incq %r13
cmpq $0x3, %r13
jne 0x80675
movzbl 0x25(%rsp), %eax
shll $0x10, %eax
movzbl 0x26(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x27(%rsp), %edx
orl %ecx, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x58(%r14,%rbx,4)
cmpb $0x0, 0x1(%r14)
je 0x806d1
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0xe9d68
jmp 0x80718
movzbl 0x15(%rsp), %ecx
movzbl 0x16(%rsp), %r8d
movzbl 0x17(%rsp), %r9d
movl $0x40, %esi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
leaq 0x90893(%rip), %rdx     # 0x110f89
xorl %eax, %eax
callq 0xe8d67
movl 0x58(%r14,%rbx,4), %edx
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
incq %rbp
cmpq 0x50(%r14), %rbp
jl 0x80639
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl 0x28(%rsp), %r15d
movl 0x40(%r14), %edx
leaq 0xb5d95(%rip), %rsi     # 0x1364dc
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x28(%r14), %rdi
movq 0x8(%r14), %rsi
movq %rdi, %rax
imulq %rsi, %rax
leaq 0x7(%rax), %rcx
addq $0xe, %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
movq 0x10(%r14), %rcx
movq %rax, %rdx
sarq $0x3, %rdx
movq %rdx, 0x30(%r14)
andq $-0x8, %rax
cqto
idivq %rdi
movq %rax, 0x18(%r14)
movzbl %r12b, %edx
leal 0x1(,%rdx,2), %r8d
movq %rbx, %rdi
movq %rax, %rdx
callq 0xf2ce6
movq %rax, %r13
testb %r15b, %r15b
je 0x80896
testb %r12b, %r12b
je 0x808ef
cmpq $0x0, 0x10(%r14)
jle 0x808bc
movq %r13, 0x28(%rsp)
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
cmpq $0x0, 0x18(%r14)
jle 0x8087b
movl $0x1, %ebx
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x30(%r14), %rax
cmpq $0x10, 0x20(%r14)
movq 0x18(%rbp), %rdi
movq 0x40(%r14), %rsi
jne 0x80843
imulq %r12, %rax
addq %rax, %rsi
addq %rbx, %rsi
decq %rsi
callq 0xef0d5
movl %eax, %ebp
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x30(%r14), %rsi
imulq %r12, %rsi
addq 0x40(%r14), %rsi
addq %rbx, %rsi
callq 0xef0d5
movzbl %al, %eax
shll $0x8, %eax
movzbl %bpl, %edi
movq 0x18(%rsp), %rbp
orl %eax, %edi
callq 0xece9d
jmp 0x80854
imulq %r12, %rax
addq %r15, %rsi
addq %rax, %rsi
xorl %edx, %edx
callq 0xefa60
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl %eax, %ecx
callq 0xf2afd
incq %r13
addq $0x2, %rbx
addq $0x3, %r15
cmpq 0x18(%r14), %r13
jl 0x807e7
incq %r12
cmpq 0x10(%r14), %r12
jl 0x807d1
xorl %ebp, %ebp
movq 0x28(%rsp), %r13
jmp 0x808bc
xorl %r13d, %r13d
jmp 0x808cd
movq 0x18(%rbx), %rdi
movq 0x40(%r14), %rsi
movq 0x20(%r14), %rdx
movq 0x30(%r14), %rcx
movq %r14, %r8
addq $0x58, %r8
movl $0x0, (%rsp)
movq %r13, %r9
callq 0xf35b8
movq %r13, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0xf2923
movq 0x18(%rsp), %rbx
movq %r13, %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x80944
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xf3081
movl $0x8, %ebp
jmp 0x808bc
movl $0x8, %edx
xorl %r15d, %r15d
movl $0x8, %eax
jmp 0x80479
leaq 0xa9c9c(%rip), %rsi     # 0x12a5d1
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x808cd
testb %r12b, %r12b
jne 0x807b9
movq %r14, %r12
addq $0x58, %r12
movl $0x1, %esi
shlq %cl, %rsi
xorl %ebp, %ebp
movq %r12, %rdi
xorl %edx, %edx
callq 0xf41f4
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rcx
movl $0x0, (%rsp)
movq %r12, %r8
jmp 0x808b4