// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xeead3
movb %al, (%r14)
movzbl %al, %edx
leaq 0xaaaf3(%rip), %rsi     # 0x12a884
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl (%r14), %edx
cmpl $0x1f, %edx
jle 0x7fdd2
leal -0x20(%rdx), %eax
cmpl $0x3, %eax
ja 0x7fde5
leaq 0x962ca(%rip), %r15     # 0x116080
movb $0x1, %r13b
leaq 0xaaa90(%rip), %rcx     # 0x12a850
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movaps 0xaaa30(%rip), %xmm0    # 0x12a800
jmp 0x7fe37
cmpl $0x10, %edx
je 0x7fe11
cmpl $0x11, %edx
jne 0x7fdf8
movaps 0xaaa4d(%rip), %xmm0    # 0x12a830
jmp 0x7fe18
cmpl $0x30, %edx
je 0x7fe45
cmpl $0x31, %edx
jne 0x7fdf8
movaps 0xaaa0a(%rip), %xmm0    # 0x12a800
jmp 0x7fe4c
leaq 0xaaa7a(%rip), %rsi     # 0x12a879
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x802a5
movaps 0xaaa28(%rip), %xmm0    # 0x12a840
leaq 0xbfb81(%rip), %r15     # 0x13f9a0
movb $0x1, %r12b
movb $0x1, %r13b
jmp 0x7fe59
movaps 0xaa9e2(%rip), %xmm0    # 0x12a810
jmp 0x7fe37
movaps 0xaa9e9(%rip), %xmm0    # 0x12a820
xorl %r12d, %r12d
jmp 0x7fe59
movaps 0xaa9ad(%rip), %xmm0    # 0x12a7f0
jmp 0x7fe59
movaps 0xaa9c4(%rip), %xmm0    # 0x12a810
leaq 0xca81b(%rip), %r15     # 0x14a66e
movb $0x1, %r12b
xorl %r13d, %r13d
movups %xmm0, 0x20(%r14)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl 0x20(%r14), %edx
leaq 0xaaa21(%rip), %rsi     # 0x12a897
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testb %r12b, %r12b
jne 0x7feb2
movq 0x20(%r14), %rax
cmpq $0x8, %rax
ja 0x802f9
movl $0x152, %ecx            # imm = 0x152
btq %rax, %rcx
jae 0x802f9
movq 0x18(%rbx), %rdi
movl $0x3, %esi
callq 0xeebc8
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xeebc8
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x80269
testb %r13b, %r13b
jne 0x7ff8b
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xeead3
movb %al, 0x1(%r14)
movzbl %al, %edx
leaq 0xaa9ae(%rip), %rsi     # 0x12a8d0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeead3
movzbl %al, %eax
movq %rax, 0x48(%r14)
movq 0x18(%rbx), %rdi
movl $0xb, %esi
callq 0xeead3
movzbl %al, %eax
testb %al, %al
movl $0x100, %ecx            # imm = 0x100
cmovneq %rax, %rcx
movq %rcx, 0x50(%r14)
movl 0x48(%r14), %edx
leaq 0xaa979(%rip), %rsi     # 0x12a8e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x50(%r14), %edx
leaq 0x91442(%rip), %rsi     # 0x1113c3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeebc8
movq %rax, 0x38(%r14)
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeebc8
movq %rax, 0x40(%r14)
testb %r13b, %r13b
movq %rbx, 0x10(%rsp)
jne 0x80107
movl %r13d, 0x2c(%rsp)
movl %r12d, 0x20(%rsp)
movl 0x38(%r14), %edx
leaq 0xb0497(%rip), %rsi     # 0x130469
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x0, 0x50(%r14)
jle 0x800e7
leaq 0xd(%rsp), %r15
leaq 0x30(%rsp), %r13
xorl %ebp, %ebp
movq 0x48(%r14), %rbx
addq %rbp, %rbx
cmpq $0xff, %rbx
jg 0x800e7
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
addq 0x38(%r14), %rdx
movl $0x3, %ecx
movq %r15, %rsi
callq 0xee8d4
xorl %r12d, %r12d
cmpb $0x0, 0x1(%r14)
movb 0xd(%rsp,%r12), %al
jne 0x80050
movzbl %al, %edi
callq 0xec78a
movb %al, 0x1d(%rsp,%r12)
incq %r12
cmpq $0x3, %r12
jne 0x8003c
movzbl 0x1d(%rsp), %eax
shll $0x10, %eax
movzbl 0x1e(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x1f(%rsp), %edx
orl %ecx, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x58(%r14,%rbx,4)
cmpb $0x0, 0x1(%r14)
je 0x80098
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xe91fc
jmp 0x800da
movzbl 0xd(%rsp), %ecx
movzbl 0xe(%rsp), %r8d
movzbl 0xf(%rsp), %r9d
movl $0x40, %esi
movq %r13, %rdi
leaq 0x90ee0(%rip), %rdx     # 0x110f98
xorl %eax, %eax
callq 0xe8193
movl 0x58(%r14,%rbx,4), %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
incq %rbp
cmpq 0x50(%r14), %rbp
jl 0x80000
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x40(%r14), %rax
movl 0x20(%rsp), %r12d
movl 0x2c(%rsp), %r13d
leaq 0xb6798(%rip), %rsi     # 0x1368a6
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
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 0xf26a5
movq %rax, %r15
testb %r13b, %r13b
je 0x8026e
movq %r15, 0x20(%rsp)
testb %r12b, %r12b
je 0x802c7
movq 0x10(%r14), %rcx
testq %rcx, %rcx
movq 0x20(%rsp), %r15
jle 0x80294
movq 0x18(%r14), %rax
xorl %r12d, %r12d
movq 0x10(%rsp), %rbp
testq %rax, %rax
jle 0x80254
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 0x80215
imulq %r12, %rax
addq %rax, %rsi
addq %rbx, %rsi
decq %rsi
callq 0xeead3
movl %eax, %ebp
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x30(%r14), %rsi
imulq %r12, %rsi
addq 0x40(%r14), %rsi
addq %rbx, %rsi
callq 0xeead3
movzbl %al, %eax
shll $0x8, %eax
movzbl %bpl, %edi
movq 0x10(%rsp), %rbp
orl %eax, %edi
callq 0xec844
jmp 0x80226
imulq %r12, %rax
addq %r15, %rsi
addq %rax, %rsi
xorl %edx, %edx
callq 0xef42f
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl %eax, %ecx
callq 0xf24ba
incq %r13
movq 0x18(%r14), %rax
addq $0x2, %rbx
addq $0x3, %r15
cmpq %rax, %r13
jl 0x801b9
movq 0x10(%r14), %rcx
incq %r12
cmpq %rcx, %r12
jl 0x801a5
xorl %ebp, %ebp
movq 0x20(%rsp), %r15
jmp 0x80294
xorl %r15d, %r15d
jmp 0x802a5
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 %r15, %r9
callq 0xf2f79
movq %r15, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0xf22dc
movq 0x10(%rsp), %rbx
movq %r15, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x8030f
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rsi
movq 0x20(%rsp), %r15
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xf2a40
movl $0x8, %ebp
jmp 0x80294
leaq 0xaa5a9(%rip), %rsi     # 0x12a8a9
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x802a5
movq %r14, %r12
addq $0x58, %r12
movl $0x1, %esi
shlq %cl, %rsi
xorl %ebp, %ebp
movq %r12, %rdi
xorl %edx, %edx
callq 0xf3bdb
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rcx
movl $0x0, (%rsp)
movq %r12, %r8
movq 0x20(%rsp), %r15
jmp 0x8028c