// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc2.c
// Function: de_run_hsiraw
static void de_run_hsiraw(deark *c, de_module_params *mparams)
{
	i64 w, h;
	i64 num_pal_colors;
	i64 pos;
	i64 ver;
	i64 hdpi, vdpi;
	i64 cmpr;
	i64 alpha_info;
	de_bitmap *img = NULL;
	de_color pal[256];
	int is_grayscale;

	ver = de_getu16be(6);
	de_dbg(c, "version: %d", (int)ver);
	if(ver!=4) {
		de_warn(c, "HSI Raw version %d might not be supported correctly", (int)ver);
	}

	w = de_getu16be(8);
	if(w==0) {
		// MPlayer extension?
		de_dbg2(c, "reading 32-bit width");
		w = de_getu32be(28);
	}
	h = de_getu16be(10);
	de_dbg_dimensions(c, w, h);
	num_pal_colors = de_getu16be(12);
	de_dbg(c, "number of palette colors: %d", (int)num_pal_colors);

	hdpi = de_geti16be(14);
	vdpi = de_geti16be(16);
	de_dbg(c, "density: %d"DE_CHAR_TIMES"%d", (int)hdpi, (int)vdpi);
	// [18: Gamma]
	cmpr = de_getu16be(20);
	de_dbg(c, "compression: %d", (int)cmpr);
	alpha_info = de_getu16be(22);
	de_dbg(c, "alpha: %d", (int)alpha_info);

	if(num_pal_colors>256 || cmpr!=0 || alpha_info!=0) {
		de_err(c, "This type of HSI Raw image is not supported");
		goto done;
	}
	if(!de_good_image_dimensions(c, w, h)) goto done;

	pos = 32;
	de_zeromem(pal, sizeof(pal));
	if(num_pal_colors==0) { // 24-bit RGB
		is_grayscale = 0;
	}
	else { // 8-bit paletted
		de_read_simple_palette(c, c->infile, pos, num_pal_colors, 3, pal, 256,
			DE_RDPALTYPE_24BIT, 0);
		pos += 3*num_pal_colors;
		is_grayscale = de_is_grayscale_palette(pal, num_pal_colors);
	}

	img = de_bitmap_create(c, w, h, is_grayscale?1:3);

	if(num_pal_colors==0) {
		de_convert_image_rgb(c->infile, pos, 3*w, 3, img, 0);
	}
	else {
		de_convert_image_paletted(c->infile, pos, 8, w, pal, img, 0);
	}

	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 $0x408, %rsp            # imm = 0x408
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movl $0x6, %esi
callq 0xef169
movq %rax, %r14
leaq 0xcae61(%rip), %rsi     # 0x1343d1
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x4, %r14
je 0x69597
leaq 0xbbdbf(%rip), %rsi     # 0x125349
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xef169
movq %rax, %r14
testq %rax, %rax
jne 0x695cf
leaq 0xbbdc9(%rip), %rsi     # 0x12537d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
movq 0x18(%rbx), %rdi
movl $0x1c, %esi
callq 0xef32a
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef169
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xef169
movq %rax, %r12
leaq 0xa7dae(%rip), %rsi     # 0x1113b4
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef219
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef219
leaq 0xbbd59(%rip), %rsi     # 0x125392
movq %rbx, %rdi
movl %r13d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x14, %esi
callq 0xef169
movq %rax, %r13
leaq 0xb8345(%rip), %rsi     # 0x1219a5
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x16, %esi
callq 0xef169
movq %rax, %rbp
leaq 0xc8c4d(%rip), %rsi     # 0x1322d2
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x101, %r12            # imm = 0x101
setl %al
orq %r13, %rbp
sete %cl
testb %cl, %al
jne 0x696be
leaq 0xbbcf6(%rip), %rsi     # 0x1253a2
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x697ab
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf241d
testl %eax, %eax
je 0x6973a
movq %rsp, %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
testq %r12, %r12
je 0x6973f
movq 0x18(%rbx), %rsi
subq $0x8, %rsp
leaq 0x8(%rsp), %rbp
movl $0x20, %edx
movl $0x3, %r8d
movq %rbx, %rdi
movq %r12, %rcx
movq %rbp, %r9
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xf32f2
addq $0x20, %rsp
leaq (%r12,%r12,2), %r13
addq $0x20, %r13
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf2544
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
jmp 0x6974a
xorl %r15d, %r15d
jmp 0x697ab
movl $0x3, %ecx
movl $0x20, %r13d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf2cab
movq %rax, %r15
movq 0x18(%rbx), %rdi
testq %r12, %r12
je 0x69788
subq $0x8, %rsp
leaq 0x8(%rsp), %r8
movl $0x8, %edx
movq %r13, %rsi
movq %r14, %rcx
movq %r15, %r9
pushq $0x0
callq 0xf35b8
addq $0x10, %rsp
jmp 0x6979f
leaq (%r14,%r14,2), %rdx
movl $0x3, %ecx
movq %r13, %rsi
movq %r15, %r8
xorl %r9d, %r9d
callq 0xf38d9
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
movq %r15, %rdi
callq 0xf28d4
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq