// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl $0x458, %esi            # imm = 0x458
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
leaq 0xfb6f1(%rip), %rsi     # 0x1b6244
xorl %eax, %eax
callq 0x15b0d0
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
addl $-0x10, %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x21, %eax
ja 0xbac2a
movq -0x40(%rbp), %rax
leaq 0xfb61a(%rip), %rcx     # 0x1b6198
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movq $0x10, 0x20(%rax)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x18, 0x20(%rax)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x4, 0x20(%rax)
movl $0x1, -0x28(%rbp)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x6, 0x20(%rax)
movl $0x1, -0x28(%rbp)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x8, 0x20(%rax)
movl $0x1, -0x28(%rbp)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x1, 0x20(%rax)
movl $0x1, -0x28(%rbp)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x8, 0x20(%rax)
movl $0x1, -0x24(%rbp)
jmp 0xbac48
movq -0x18(%rbp), %rax
movq $0x4, 0x20(%rax)
movl $0x1, -0x24(%rbp)
jmp 0xbac48
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
leaq 0xfb5fd(%rip), %rsi     # 0x1b6239
movb $0x0, %al
callq 0x15bfb0
jmp 0xbb066
movq -0x18(%rbp), %rax
cmpq $0x6, 0x20(%rax)
jne 0xbac61
movq -0x18(%rbp), %rax
movq $0x8, 0x28(%rax)
jmp 0xbac71
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpl $0x0, -0x28(%rbp)
je 0xbac92
leaq 0xe723c(%rip), %rax     # 0x1a1ec8
movq %rax, -0x30(%rbp)
jmp 0xbacb2
cmpl $0x0, -0x24(%rbp)
je 0xbaca5
leaq 0x11b47f(%rip), %rax    # 0x1d611e
movq %rax, -0x30(%rbp)
jmp 0xbacb0
leaq 0x110969(%rip), %rax    # 0x1cb615
movq %rax, -0x30(%rbp)
jmp 0xbacb2
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
movq -0x30(%rbp), %rcx
leaq 0xfb58c(%rip), %rsi     # 0x1b6257
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x0, -0x28(%rbp)
je 0xbad29
movq -0x18(%rbp), %rax
cmpq $0x1, 0x20(%rax)
je 0xbad29
movq -0x18(%rbp), %rax
cmpq $0x4, 0x20(%rax)
je 0xbad29
movq -0x18(%rbp), %rax
cmpq $0x6, 0x20(%rax)
je 0xbad29
movq -0x18(%rbp), %rax
cmpq $0x8, 0x20(%rax)
je 0xbad29
movq -0x8(%rbp), %rdi
leaq 0xfb54c(%rip), %rsi     # 0x1b6269
movb $0x0, %al
callq 0x15bfb0
jmp 0xbb066
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x3, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xbad9f
jmp 0xbb066
cmpl $0x0, -0x24(%rbp)
je 0xbae6b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x9, %esi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0xfb4b9(%rip), %rsi     # 0x1b6290
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xb, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0xbae33
movq -0x18(%rbp), %rax
movq $0x100, 0x50(%rax)      # imm = 0x100
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0xfb45d(%rip), %rsi     # 0x1b62a5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0xe2538(%rip), %rsi     # 0x19d39c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xe, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
cmpl $0x0, -0x24(%rbp)
je 0xbaeb8
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xbb130
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
leaq 0x1072db(%rip), %rsi    # 0x1c21a8
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
imulq 0x28(%rcx), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
shlq $0x3, %rax
movq -0x18(%rbp), %rcx
cqto
idivq 0x28(%rcx)
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x28(%rbp), %r9d
movl $0x3, %r8d
movl $0x1, %eax
cmpl $0x0, %r9d
cmovnel %eax, %r8d
callq 0x16b7f0
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0xbafa0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x58, %r8
movq -0x20(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
jmp 0xbb056
cmpl $0x0, -0x28(%rbp)
je 0xbafe0
movq -0x18(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jne 0xbafe0
movl -0x34(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x20(%rbp), %rcx
xorl %r8d, %r8d
callq 0x16bee0
jmp 0xbb054
cmpl $0x0, -0x28(%rbp)
je 0xbb041
movq -0x18(%rbp), %rdi
addq $0x58, %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0x16d580
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x58, %r8
movq -0x20(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
jmp 0xbb052
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0xbb2e0
jmp 0xbb054
jmp 0xbb056
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x16afd0
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)