// File: /workspace/github_c_makefile_star2_O3/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 0xeeb67
movq %rax, %r14
leaq 0xcb4a9(%rip), %rsi     # 0x1347c9
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x4, %r14
je 0x69347
leaq 0xbc23f(%rip), %rsi     # 0x125579
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xeeb67
movq %rax, %r14
testq %rax, %rax
jne 0x6937f
leaq 0xbc249(%rip), %rsi     # 0x1255ad
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movq 0x18(%rbx), %rdi
movl $0x1c, %esi
callq 0xeed0d
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeeb67
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeeb67
movq %rax, %r12
leaq 0xa800d(%rip), %rsi     # 0x1113c3
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeec17
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xeec17
leaq 0xbc1d9(%rip), %rsi     # 0x1255c2
movq %rbx, %rdi
movl %r13d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x14, %esi
callq 0xeeb67
movq %rax, %r13
leaq 0xb86a1(%rip), %rsi     # 0x121ab1
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x16, %esi
callq 0xeeb67
movq %rax, %rbp
leaq 0xc9235(%rip), %rsi     # 0x13266a
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x101, %r12            # imm = 0x101
setl %al
orq %r13, %rbp
sete %cl
testb %cl, %al
jne 0x6946e
leaq 0xbc176(%rip), %rsi     # 0x1255d2
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x6956a
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x69526
movq %rsp, %rdi
xorl %ebp, %ebp
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
testq %r12, %r12
je 0x6952b
movq 0x18(%rbx), %rsi
subq $0x8, %rsp
leaq 0x8(%rsp), %r13
movl $0x20, %edx
movl $0x3, %r8d
movq %rbx, %rdi
movq %r12, %rcx
movq %r13, %r9
pushq %rbp
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xf2cb1
addq $0x20, %rsp
leaq (%r12,%r12,2), %rbp
addq $0x20, %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0xf1ed8
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(%rcx,%rcx), %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf266a
movq %rax, %r15
movq 0x18(%rbx), %rdi
subq $0x8, %rsp
movl $0x8, %edx
movq %rbp, %rsi
movq %r14, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %rax
callq 0xf2f79
addq $0x10, %rsp
jmp 0x6955e
xorl %r15d, %r15d
jmp 0x6956a
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r15
movq 0x18(%rbx), %rdi
leaq (%r14,%r14,2), %rdx
movl $0x20, %esi
movl $0x3, %ecx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf3279
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq %r15, %rdi
callq 0xf228d
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq