// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl $0x458, %esi            # imm = 0x458
callq 0xdecaf
movq %rax, %r14
movq 0x18(%r12), %rdi
pushq $0x2
popq %rsi
callq 0xe3eb4
movb %al, (%r14)
movzbl %al, %edx
leaq 0xa234c(%rip), %rsi     # 0x11d634
xorl %ebx, %ebx
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
movzbl (%r14), %edx
leal -0x20(%rdx), %eax
cmpl $0x3, %eax
jbe 0x7b321
cmpl $0x10, %edx
je 0x7b33d
cmpl $0x31, %edx
je 0x7b371
cmpl $0x30, %edx
je 0x7b354
cmpl $0x11, %edx
jne 0x7b7ee
movaps 0xa22c1(%rip), %xmm0    # 0x11d5e0
jmp 0x7b344
movb $0x1, %bpl
leaq 0xa22d5(%rip), %rcx     # 0x11d600
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movaps 0xa2275(%rip), %xmm0    # 0x11d5b0
jmp 0x7b364
movaps 0xa22ac(%rip), %xmm0    # 0x11d5f0
movb $0x1, %bl
movb $0x1, %bpl
jmp 0x7b37c
movaps 0xa226e(%rip), %xmm0    # 0x11d5c0
jmp 0x7b364
movaps 0xa2265(%rip), %xmm0    # 0x11d5c0
jmp 0x7b378
movaps 0xa226c(%rip), %xmm0    # 0x11d5d0
xorl %ebx, %ebx
jmp 0x7b37c
movaps 0xa2231(%rip), %xmm0    # 0x11d5a0
jmp 0x7b37c
movaps 0xa2238(%rip), %xmm0    # 0x11d5b0
movb $0x1, %bl
xorl %ebp, %ebp
movups %xmm0, 0x20(%r14)
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
leaq 0xb72dd(%rip), %rax     # 0x132670
leaq 0xc1e04(%rip), %rdx     # 0x13d19e
testb %bpl, %bpl
cmovneq %rax, %rdx
leaq 0x8dc48(%rip), %rcx     # 0x108ff0
testb %bl, %bl
cmovneq %rdx, %rcx
movl 0x20(%r14), %edx
leaq 0xa228e(%rip), %rsi     # 0x11d647
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
testb %bl, %bl
jne 0x7b3ef
movq 0x20(%r14), %rax
cmpq $0x8, %rax
ja 0x7b804
movl $0x152, %ecx            # imm = 0x152
btq %rax, %rcx
jae 0x7b804
movq 0x18(%r12), %rdi
pushq $0x3
popq %rsi
callq 0xe3fa3
movq %rax, 0x8(%r14)
movq 0x18(%r12), %rdi
pushq $0x5
popq %rsi
callq 0xe3fa3
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %r12, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %r12, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x7b613
testb %bpl, %bpl
jne 0x7b4c3
movq 0x18(%r12), %rdi
pushq $0x9
popq %rsi
callq 0xe3eb4
movb %al, 0x1(%r14)
movzbl %al, %edx
leaq 0xa2224(%rip), %rsi     # 0x11d680
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
pushq $0xa
popq %rsi
callq 0xe3eb4
movzbl %al, %eax
movq %rax, 0x48(%r14)
movq 0x18(%r12), %rdi
pushq $0xb
popq %rsi
callq 0xe3eb4
movzbl %al, %eax
testb %al, %al
movl $0x100, %ecx            # imm = 0x100
cmovneq %rax, %rcx
movq %rcx, 0x50(%r14)
movl 0x48(%r14), %edx
leaq 0xa21f1(%rip), %rsi     # 0x11d695
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x50(%r14), %edx
leaq 0x88ee7(%rip), %rsi     # 0x1043a0
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
pushq $0xc
popq %rsi
callq 0xe3fa3
movq %rax, 0x38(%r14)
movq 0x18(%r12), %rdi
pushq $0xe
popq %rsi
callq 0xe3fa3
movq %rax, 0x40(%r14)
testb %bpl, %bpl
movq %r12, 0x18(%rsp)
jne 0x7b637
movl %ebp, 0x2c(%rsp)
movl %ebx, 0x20(%rsp)
movl 0x38(%r14), %edx
leaq 0xa7cc2(%rip), %rsi     # 0x1231c8
movq %r12, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
leaq 0x12(%rsp), %r15
pushq $0x3
popq %r12
xorl %r13d, %r13d
cmpq 0x50(%r14), %r13
jge 0x7b61b
movq 0x48(%r14), %rbx
addq %r13, %rbx
cmpq $0xff, %rbx
jg 0x7b61b
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (,%r13,2), %rdx
addq %r13, %rdx
addq 0x38(%r14), %rdx
movq %r15, %rsi
movq %r12, %rcx
callq 0xe3cb7
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x7b58c
cmpb $0x0, 0x1(%r14)
movb 0x12(%rsp,%rbp), %al
jne 0x7b583
movzbl %al, %edi
callq 0xe1cec
movb %al, 0x15(%rsp,%rbp)
incq %rbp
jmp 0x7b56a
movzbl 0x15(%rsp), %eax
shll $0x10, %eax
movzbl 0x16(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x17(%rsp), %edx
orl %ecx, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x58(%r14,%rbx,4)
cmpb $0x0, 0x1(%r14)
je 0x7b5c6
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xdf23b
jmp 0x7b60b
movzbl 0x12(%rsp), %ecx
movzbl 0x13(%rsp), %r8d
movzbl 0x14(%rsp), %r9d
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
pushq $0x40
popq %rsi
leaq 0x8898c(%rip), %rdx     # 0x103f75
xorl %eax, %eax
callq 0xde40c
movl 0x58(%r14,%rbx,4), %edx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
incq %r13
jmp 0x7b527
xorl %r15d, %r15d
jmp 0x7b7cc
pushq $-0x1
popq %rsi
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0xdeba7
movq 0x40(%r14), %rax
movl 0x20(%rsp), %ebx
movl 0x2c(%rsp), %ebp
leaq 0xadf68(%rip), %rsi     # 0x1295a6
xorl %r13d, %r13d
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x28(%r14), %rdi
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rcx
movq %rdi, %rax
imulq %rsi, %rax
addq $0x7, %rax
pushq $0x8
popq %r8
cqto
idivq %r8
movq %rax, 0x30(%r14)
shlq $0x3, %rax
cqto
idivq %rdi
movq %rax, 0x18(%r14)
movzbl %bl, %edx
leal 0x1(,%rdx,2), %r8d
movq %r12, %rdi
movq %rax, %rdx
callq 0xe746f
movq %rax, %r15
testb %bpl, %bpl
je 0x7b765
testb %bl, %bl
je 0x7b78b
movq %r15, 0x20(%rsp)
xorl %r13d, %r13d
cmpq 0x10(%r14), %r13
jge 0x7b7b7
xorl %ebx, %ebx
pushq $0x1
popq %r15
xorl %ebp, %ebp
cmpq 0x18(%r14), %rbp
jge 0x7b75d
movq 0x30(%r14), %rax
cmpq $0x10, 0x20(%r14)
movq 0x18(%r12), %rdi
movq 0x40(%r14), %rsi
jne 0x7b72a
imulq %r13, %rax
addq %rax, %rsi
addq %r15, %rsi
decq %rsi
callq 0xe3eb4
movl %eax, %r12d
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x30(%r14), %rsi
imulq %r13, %rsi
addq 0x40(%r14), %rsi
addq %r15, %rsi
callq 0xe3eb4
movzbl %al, %eax
shll $0x8, %eax
movzbl %r12b, %edi
movq 0x18(%rsp), %r12
orl %eax, %edi
callq 0xe1da6
jmp 0x7b73b
imulq %r13, %rax
addq %rbx, %rsi
addq %rax, %rsi
xorl %edx, %edx
callq 0xe45a4
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl %eax, %ecx
callq 0xe7287
incq %rbp
addq $0x2, %r15
addq $0x3, %rbx
jmp 0x7b6c2
incq %r13
jmp 0x7b6b0
movq 0x18(%r12), %rdi
movq 0x40(%r14), %rsi
movq 0x20(%r14), %rdx
movq 0x30(%r14), %rcx
movq %r14, %r8
addq $0x58, %r8
andl $0x0, (%rsp)
movq %r15, %r9
callq 0xe7cbc
jmp 0x7b7bf
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x7b81a
movq 0x18(%r12), %rdi
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xe77b7
pushq $0x8
popq %r13
jmp 0x7b7bf
movq 0x20(%rsp), %r15
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0xe70b7
movq %r15, %rdi
callq 0xe7084
movq %r12, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa1e34(%rip), %rsi     # 0x11d629
xorl %r15d, %r15d
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x7b7cc
leaq 0xa1e4e(%rip), %rsi     # 0x11d659
xorl %r15d, %r15d
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x7b7cc
movq %r14, %r13
addq $0x58, %r13
pushq $0x1
popq %rsi
shlq %cl, %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0xe87a8
movq 0x18(%r12), %rdi
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rcx
andl $0x0, (%rsp)
movq %r13, %r8
xorl %r13d, %r13d
jmp 0x7b781