// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x6
popq %rsi
callq 0xe3f46
movq %rax, %r14
leaq 0xc1bb4(%rip), %rsi     # 0x1274f1
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x4, %r14
je 0x65964
leaq 0xb2a22(%rip), %rsi     # 0x118379
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0xe3f46
movq %rax, %r14
testq %rax, %rax
jne 0x65998
leaq 0xb2a2e(%rip), %rsi     # 0x1183ad
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
movq 0x18(%rbx), %rdi
pushq $0x1c
popq %rsi
callq 0xe404f
movq %rax, %r14
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0xe3f46
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xdf038
movq 0x18(%rbx), %rdi
pushq $0xc
popq %rsi
callq 0xe3f46
movq %rax, %r12
leaq 0x9e9d5(%rip), %rsi     # 0x1043a0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0xe
popq %rsi
callq 0xe3fee
movq %rax, %r13
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe3fee
leaq 0xb29c8(%rip), %rsi     # 0x1183c2
movq %rbx, %rdi
movl %r13d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x14
popq %rsi
callq 0xe3f46
movq %rax, %r13
leaq 0xaefa2(%rip), %rsi     # 0x1149c1
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x16
popq %rsi
callq 0xe3f46
movq %rax, %rbp
leaq 0xbf928(%rip), %rsi     # 0x12536a
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x101, %r12            # imm = 0x101
setl %al
orq %r13, %rbp
sete %cl
testb %cl, %al
jne 0x65a7b
leaq 0xb2969(%rip), %rsi     # 0x1183d2
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x65b5a
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x65af3
movq %rsp, %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
testq %r12, %r12
je 0x65af8
movq 0x18(%rbx), %rsi
subq $0x8, %rsp
pushq $0x20
popq %rdx
pushq $0x3
popq %r8
leaq 0x8(%rsp), %rbp
movq %rbx, %rdi
movq %r12, %rcx
movq %rbp, %r9
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xe79ca
addq $0x20, %rsp
leaq (%r12,%r12,2), %r13
addq $0x20, %r13
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe6d06
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
jmp 0x65aff
xorl %r15d, %r15d
jmp 0x65b5a
pushq $0x3
popq %rcx
pushq $0x20
popq %r13
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xe7437
movq %rax, %r15
movq 0x18(%rbx), %rdi
testq %r12, %r12
je 0x65b39
subq $0x8, %rsp
pushq $0x8
popq %rdx
leaq 0x8(%rsp), %r8
movq %r13, %rsi
movq %r14, %rcx
movq %r15, %r9
pushq $0x0
callq 0xe7cbc
popq %rax
popq %rcx
jmp 0x65b4e
leaq (%r14,%r14,2), %rdx
pushq $0x3
popq %rcx
movq %r13, %rsi
movq %r15, %r8
xorl %r9d, %r9d
callq 0xe7f67
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq %r15, %rdi
callq 0xe7084
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq