// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/sgiimage.c
// Function: de_run_sgiimage
static void de_run_sgiimage(deark *c, de_module_params *mparams)
{
	struct sgiimage_ctx *d = NULL;
	i64 pos;
	UI min_dim_count_expected;
	int need_errmsg = 0;

	d = de_malloc(c, sizeof(struct sgiimage_ctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_ASCII);

	pos = 2;
	d->storage_fmt = de_getbyte_p(&pos);
	de_dbg(c, "compression: %u (%s)", (UI)d->storage_fmt,
		get_cmprtype_name(d->storage_fmt));

	d->bytes_per_sample = (UI)de_getbyte_p(&pos);
	de_dbg(c, "bytes/sample: %u", d->bytes_per_sample);

	d->dimension_count = (UI)de_getu16be_p(&pos);
	de_dbg(c, "dimension count: %u", d->dimension_count);

	d->width = de_getu16be_p(&pos);
	de_dbg(c, "x-size: %"I64_FMT, d->width);
	d->height = de_getu16be_p(&pos);
	de_dbg(c, "y-size: %"I64_FMT, d->height);
	d->num_channels = de_getu16be_p(&pos);
	de_dbg(c, "z-size: %u", (UI)d->num_channels);

	d->pix_min = (u32)de_getu32be_p(&pos);
	d->pix_max = (u32)de_getu32be_p(&pos);
	de_dbg(c, "pix min, max: %u, %u", (UI)d->pix_min, (UI)d->pix_max);
	// TODO?: Support normalizing the image brightness/contrast.
	// Unfortunately, the spec is ambiguous about the meaning of these fields,
	// and there's no simple logic that would always do the right thing.

	pos += 4; // unused

	d->name = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, 80, d->name, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(d->name));
	pos += 80;

	d->colormap_id = (u32)de_getu32be_p(&pos);
	de_dbg(c, "colormap code: %u (%s)", (UI)d->colormap_id,
		get_cmi_name(d->colormap_id));

	if(d->storage_fmt>1) {
		need_errmsg = 1;
		goto done;
	}

	if(d->colormap_id!=CMI_NORMAL) {
		// TODO: Support other image types?
		need_errmsg = 1;
		goto done;
	}

	if(d->bytes_per_sample!=1 && d->bytes_per_sample!=2) {
		need_errmsg = 1;
		goto done;
	}

	// We're hoping that unused fields will be set to 0 or 1.
	if(d->width==0) d->width = 1;
	if(d->height==0) d->height = 1;
	if(d->num_channels==0) d->num_channels = 1;

	if(d->num_channels>1) min_dim_count_expected = 3;
	else if(d->height>1) min_dim_count_expected = 2;
	else min_dim_count_expected = 1;

	if(d->dimension_count<min_dim_count_expected || d->dimension_count>3) {
		de_warn(c, "Likely bad dimension count (is %u, assuming it should be %u)",
			d->dimension_count, min_dim_count_expected);
	}
	// Note that, other than for the above warning, we ignore dimension_count.
	// The x-size, y-size, z-size fields seem to be more reliable than it is.

	if(d->num_channels<1 || d->num_channels>4) {
		need_errmsg = 1;
		goto done;
	}

	do_sgiimage_image(c, d);

done:
	if(need_errmsg) {
		de_err(c, "This type of SGI image is not supported");
	}
	if(d) {
		ucstring_destroy(d->name);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbp
movl $0x60, %esi
callq 0xe969b
movq %rax, %r14
movq %rbp, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xeb7d1
movl %eax, (%r14)
leaq 0x50(%rsp), %r12
movq $0x2, (%r12)
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef14d
movb %al, 0x4(%r14)
movzbl %al, %edx
cmpb $0x1, %dl
leaq 0x3eaa8(%rip), %rax     # 0x125072
leaq 0x45898(%rip), %r15     # 0x12be69
cmovneq %r15, %rax
testb %dl, %dl
leaq 0x2a147(%rip), %rcx     # 0x110725
cmovneq %rax, %rcx
leaq 0x5f149(%rip), %rsi     # 0x145732
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef14d
movzbl %al, %edx
movl %edx, 0x8(%r14)
leaq 0x5f13a(%rip), %rsi     # 0x145747
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef193
movl %eax, 0xc(%r14)
leaq 0x5f12a(%rip), %rsi     # 0x145758
movq %rbp, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef193
movq %rax, 0x20(%r14)
leaq 0x5f11b(%rip), %rsi     # 0x14576c
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef193
movq %rax, 0x28(%r14)
leaq 0x5f103(%rip), %rsi     # 0x145778
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef193
movq %rax, 0x30(%r14)
leaq 0x5f0eb(%rip), %rsi     # 0x145784
movq %rbp, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef34e
movl %eax, 0x10(%r14)
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef34e
movl %eax, 0x14(%r14)
movl 0x10(%r14), %edx
leaq 0x5f0bf(%rip), %rsi     # 0x14578f
movq %rbp, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
addq $0x4, (%r12)
movq %rbp, %rdi
callq 0xf6870
movq %rax, 0x58(%r14)
movq 0x18(%rbp), %rdi
movq (%r12), %rsi
movl (%r14), %r9d
movl $0x50, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xf01b0
movq 0x58(%r14), %rdi
callq 0xf7557
leaq 0x3be58(%rip), %rsi     # 0x122573
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x50, (%r12)
movq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0xef34e
movl %eax, 0x18(%r14)
cmpl $0x3, %eax
ja 0xe6755
movl %eax, %ecx
andl $0x3, %ecx
leaq 0x5f1ba(%rip), %rdx     # 0x145908
movslq (%rdx,%rcx,4), %r15
addq %rdx, %r15
leaq 0x5f048(%rip), %rsi     # 0x1457a4
movq %rbp, %rdi
movl %eax, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9226
cmpb $0x1, 0x4(%r14)
ja 0xe6805
cmpl $0x0, 0x18(%r14)
jne 0xe6805
movl 0x8(%r14), %eax
decl %eax
cmpl $0x1, %eax
ja 0xe6805
cmpq $0x0, 0x20(%r14)
jne 0xe679b
movq $0x1, 0x20(%r14)
cmpq $0x0, 0x28(%r14)
jne 0xe67aa
movq $0x1, 0x28(%r14)
cmpq $0x0, 0x30(%r14)
jne 0xe67b9
movq $0x1, 0x30(%r14)
movl $0x3, %ecx
cmpq $0x1, 0x30(%r14)
jg 0xe67d1
xorl %ecx, %ecx
cmpq $0x2, 0x28(%r14)
setge %cl
incl %ecx
movl 0xc(%r14), %edx
cmpl %ecx, %edx
setae %al
cmpl $0x4, %edx
setb %sil
testb %sil, %al
jne 0xe67f7
leaq 0x5efce(%rip), %rsi     # 0x1457bb
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x30(%r14), %rax
leaq -0x5(%rax), %rcx
cmpq $-0x4, %rcx
jae 0xe6839
leaq 0x5efec(%rip), %rsi     # 0x1457f8
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x58(%r14), %rdi
callq 0xf6bb0
movq %rbp, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x3, %rax
setl 0x38(%r14)
addq $-0x2, %rax
testq $-0x3, %rax
sete 0x39(%r14)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbp, %rdi
callq 0xf241d
testl %eax, %eax
movq %r14, (%rsp)
je 0xe68e1
movl 0x14(%r14), %eax
testl %eax, %eax
je 0xe68aa
movl 0x8(%r14), %ecx
cmpl $0x1, %ecx
sete %dl
cmpl $0x18, %eax
setb %sil
testb %dl, %sil
jne 0xe6899
cmpl $0x2, %ecx
setne %cl
cmpl $0x1800, %eax           # imm = 0x1800
setae %al
orb %cl, %al
jne 0xe68aa
leaq 0x5efaa(%rip), %rsi     # 0x14584a
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbp, %rdi
callq 0xf2cab
movq %rax, %rbx
cmpl $0x2, 0x8(%r14)
jne 0xe68ee
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbp, %rdi
callq 0xf2cab
movq %rax, %r15
jmp 0xe68f1
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0xe6c7f
xorl %r15d, %r15d
movl 0x8(%r14), %eax
imulq 0x20(%r14), %rax
movq %rax, 0x40(%r14)
movq 0x30(%r14), %rsi
imulq 0x28(%r14), %rsi
movq %rsi, 0x48(%r14)
imulq %rax, %rsi
movq %rsi, 0x50(%r14)
cmpb $0x0, 0x4(%r14)
je 0xe6a6f
movq %r15, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movq %rbp, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %r14, %rbx
movq %rax, %r14
movq %rax, %rdi
callq 0xf0e3f
movq 0x48(%rbx), %rsi
movl $0x8, %edx
movq %rbp, %rdi
callq 0xea259
movq %rax, %r13
movq $0x200, 0x8(%rsp)       # imm = 0x200
leaq 0x5ef20(%rip), %rsi     # 0x145884
movl $0x200, %edx            # imm = 0x200
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x8(%rsp), %rdx
leaq 0x5ef0e(%rip), %rsi     # 0x14589a
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x48(%rbx)
jle 0xe69bf
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x18(%rbp), %rdi
movq %r15, %rsi
callq 0xef34e
movl %eax, (%r13,%r12,8)
incq %r12
cmpq 0x48(%rbx), %r12
jl 0xe69a5
movq 0x8(%rsp), %rdx
leaq 0x5eede(%rip), %rsi     # 0x1458a9
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x48(%rbx)
jle 0xe69fe
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x18(%rbp), %rdi
movq %r15, %rsi
callq 0xef34e
movl %eax, 0x4(%r13,%r12,8)
incq %r12
cmpq 0x48(%rbx), %r12
jl 0xe69e4
movq 0x8(%rsp), %rdx
leaq 0x5eeae(%rip), %rsi     # 0x1458b8
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x2, (%rbp)
jl 0xe6a4e
cmpq $0x0, 0x48(%rbx)
jle 0xe6a4e
leaq 0x5ee9f(%rip), %r15     # 0x1458c7
xorl %r12d, %r12d
movl (%r13,%r12,8), %ecx
movl 0x4(%r13,%r12,8), %r8d
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe93cf
incq %r12
cmpq 0x48(%rbx), %r12
jl 0xe6a2b
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $0x0, 0x39(%rbx)
je 0xe6a8e
movq 0x30(%rbx), %rax
decq %rax
imulq 0x28(%rbx), %rax
jmp 0xe6a92
movq 0x18(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %rbx, %rcx
movq %r15, %r8
callq 0xe6d25
xorl %r14d, %r14d
jmp 0xe6c5b
movq 0x48(%rbx), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x48(%rbx)
jle 0xe6c2a
xorl %r12d, %r12d
movq %rbp, 0x48(%rsp)
movq %r14, 0x18(%rsp)
movq %r13, 0x40(%rsp)
movl (%r13,%r12,8), %ecx
movq %r12, 0x20(%rsp)
movl 0x4(%r13,%r12,8), %r12d
movq %rcx, 0x10(%rsp)
cmpq $0x14, %rcx
jae 0xe6b4b
movq (%rsp), %r14
cmpb $0x0, 0x3a(%r14)
jne 0xe6af4
movq %rbp, %rdi
leaq 0x5ee02(%rip), %rsi     # 0x1458e5
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0xe9b27
movb $0x1, 0x3a(%r14)
movq 0x20(%rsp), %rax
leaq 0x1(%rax), %r12
movq 0x40(%r14), %r15
imulq %r12, %r15
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0xf1d13
movq %r15, %rdx
subq %rax, %rdx
movq 0x40(%rsp), %r13
je 0xe6c1c
jle 0xe6c11
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
jl 0xe6c11
movq %r14, %rdi
movl $0xff, %esi
callq 0xf14bd
jmp 0xe6c1c
addq %rcx, %r12
xorl %r13d, %r13d
movq (%rsp), %r14
cmpq %r12, 0x10(%rsp)
jge 0xe6bb5
cmpq 0x40(%r14), %r13
jge 0xe6bb5
movq 0x18(%rbp), %rsi
movq %r14, %rdi
leaq 0x10(%rsp), %rdx
callq 0xe6e60
movl %eax, %ebx
andl $0x7f, %ebx
je 0xe6bb5
movq 0x18(%rbp), %rcx
testb %al, %al
js 0xe6bc0
movq %r14, %rdi
movq %rcx, %rsi
leaq 0x10(%rsp), %rdx
callq 0xe6e60
cmpl $0x2, 0x8(%r14)
jne 0xe6be9
movl %eax, %ebp
movq %rbx, %r15
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0xf15b0
decq %r15
jne 0xe6ba3
jmp 0xe6bf9
xorl %eax, %eax
testb %al, %al
jne 0xe6b55
jmp 0xe6af4
movq 0x10(%rsp), %rsi
movl 0x8(%r14), %edx
imulq %rbx, %rdx
movq %rcx, %rdi
movq 0x18(%rsp), %rcx
callq 0xefabc
movl 0x8(%r14), %eax
imulq %rbx, %rax
addq %rax, 0x10(%rsp)
jmp 0xe6bf9
movzbl %al, %esi
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
callq 0xf14bd
movq (%rsp), %r14
movl 0x8(%r14), %eax
imulq %rbx, %rax
addq %rax, %r13
movb $0x1, %al
movq 0x48(%rsp), %rbp
jmp 0xe6bb7
movq %r14, %rdi
movq %r15, %rsi
callq 0xf152a
movq (%rsp), %rax
cmpq 0x48(%rax), %r12
jl 0xe6ab4
movq %r14, %rdi
callq 0xf0ea4
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe9752
movq (%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq 0x38(%rsp), %rbx
movq %rbx, %rcx
movq 0x30(%rsp), %r15
movq %r15, %r8
callq 0xe6d25
testq %r15, %r15
jne 0xe6c6d
movq %rbx, %rdi
movl $0x6, %esi
callq 0xf3f11
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x6, %ecx
callq 0xf2586
movq %r14, %rdi
callq 0xf0b13
movq %rbx, %rdi
callq 0xf28d4
movq %r15, %rdi
callq 0xf28d4
movq (%rsp), %r14
jmp 0xe6816