// File: /workspace/github_c_makefile_star2_O2/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, %rbx
pushq $0x60
popq %rsi
callq 0xdecaf
movq %rax, %r14
pushq $0x1
popq %rbp
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0xe08d2
movl %eax, (%r14)
leaq 0x50(%rsp), %r12
movq $0x2, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movb %al, 0x4(%r14)
movzbl %al, %edx
cmpb $0x1, %dl
leaq 0x3c1ea(%rip), %rax     # 0x1180a2
leaq 0x43022(%rip), %r15     # 0x11eee1
cmovneq %r15, %rax
testb %dl, %dl
leaq 0x27845(%rip), %rcx     # 0x103711
cmovneq %rax, %rcx
leaq 0x5c8eb(%rip), %rsi     # 0x1387c2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movzbl %al, %edx
movl %edx, 0x8(%r14)
leaq 0x5c8dc(%rip), %rsi     # 0x1387d7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f6e
movl %eax, 0xc(%r14)
leaq 0x5c8cc(%rip), %rsi     # 0x1387e8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f6e
movq %rax, 0x20(%r14)
leaq 0x5c8bd(%rip), %rsi     # 0x1387fc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f6e
movq %rax, 0x28(%r14)
leaq 0x5c8a5(%rip), %rsi     # 0x138808
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f6e
movq %rax, 0x30(%r14)
leaq 0x5c88d(%rip), %rsi     # 0x138814
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4071
movl %eax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4071
movl %eax, 0x14(%r14)
movl 0x10(%r14), %edx
leaq 0x5c861(%rip), %rsi     # 0x13881f
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
addq $0x4, (%r12)
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x58(%r14)
movq 0x18(%rbx), %rdi
movq (%r12), %rsi
movl (%r14), %r9d
pushq $0x50
popq %rdx
movq %rax, %rcx
movl %ebp, %r8d
callq 0xe4c9a
movq 0x58(%r14), %rdi
callq 0xeb877
leaq 0x395b7(%rip), %rsi     # 0x1155bb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x50, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4071
movl %eax, 0x18(%r14)
cmpl $0x3, %eax
ja 0xdc03e
movl %eax, %ecx
andl $0x3, %ecx
leaq 0x5c961(%rip), %rdx     # 0x138998
movslq (%rdx,%rcx,4), %r15
addq %rdx, %r15
leaq 0x5c7ef(%rip), %rsi     # 0x138834
movq %rbx, %rdi
movl %eax, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xde896
cmpb $0x1, 0x4(%r14)
ja 0xdc0fb
cmpl $0x0, 0x18(%r14)
jne 0xdc0fb
movl 0x8(%r14), %eax
decl %eax
cmpl $0x1, %eax
ja 0xdc0fb
cmpq $0x0, 0x20(%r14)
jne 0xdc088
movq $0x1, 0x20(%r14)
movq 0x28(%r14), %rdx
testq %rdx, %rdx
jne 0xdc09c
movq $0x1, 0x28(%r14)
pushq $0x1
popq %rdx
movq 0x30(%r14), %rax
testq %rax, %rax
je 0xdc0b0
cmpq $0x1, %rax
jle 0xdc0bb
pushq $0x3
popq %rcx
jmp 0xdc0c6
pushq $0x1
popq %rax
movq $0x1, 0x30(%r14)
xorl %ecx, %ecx
cmpq $0x2, %rdx
setge %cl
incl %ecx
movl 0xc(%r14), %edx
cmpl %ecx, %edx
setae %sil
cmpl $0x4, %edx
setb %dil
testb %dil, %sil
jne 0xdc0f1
leaq 0x5c768(%rip), %rsi     # 0x13884b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x30(%r14), %rax
leaq -0x5(%rax), %rcx
cmpq $-0x4, %rcx
jae 0xdc12f
leaq 0x5c786(%rip), %rsi     # 0x138888
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x58(%r14), %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x3, %rax
setb 0x38(%r14)
addq $-0x2, %rax
testq $-0x3, %rax
sete 0x39(%r14)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0xdc1d6
movl 0x14(%r14), %eax
testl %eax, %eax
je 0xdc19c
movl 0x8(%r14), %ecx
cmpl $0x1, %ecx
sete %dl
cmpl $0x18, %eax
setb %sil
testb %dl, %sil
jne 0xdc18b
cmpl $0x2, %ecx
setne %cl
cmpl $0x1800, %eax           # imm = 0x1800
setae %al
orb %cl, %al
jne 0xdc19c
leaq 0x5c748(%rip), %rsi     # 0x1388da
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %r15
movl 0x8(%r14), %ecx
cmpl $0x2, %ecx
jne 0xdc1e4
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbx, %rdi
callq 0xe7437
movl 0x8(%r14), %ecx
jmp 0xdc1e6
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0xdc53c
xorl %eax, %eax
movl %ecx, %ecx
imulq 0x20(%r14), %rcx
movq %rcx, 0x40(%r14)
movq 0x30(%r14), %rsi
imulq 0x28(%r14), %rsi
movq %rsi, 0x48(%r14)
imulq %rcx, %rsi
movq %rsi, 0x50(%r14)
cmpb $0x0, 0x4(%r14)
je 0xdc35a
movq %rax, 0x38(%rsp)
movq %r15, 0x40(%rsp)
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r13
movq %rax, %rdi
callq 0xe589c
movq 0x48(%r14), %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %r15
movq $0x200, (%r15)          # imm = 0x200
leaq 0x5c6ba(%rip), %rsi     # 0x138914
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x5c6af(%rip), %rsi     # 0x13892a
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpq 0x48(%r14), %r12
jge 0xdc2aa
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4071
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%r12,8)
incq %r12
jmp 0xdc28a
leaq 0x30(%rsp), %r15
movq (%r15), %rdx
leaq 0x5c680(%rip), %rsi     # 0x138939
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpq 0x48(%r14), %r12
jge 0xdc2e7
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4071
movq 0x8(%rsp), %rcx
movl %eax, 0x4(%rcx,%r12,8)
incq %r12
jmp 0xdc2c6
movq 0x30(%rsp), %rdx
leaq 0x5c655(%rip), %rsi     # 0x138948
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpl $0x2, (%rbx)
jl 0xdc335
leaq 0x5c64e(%rip), %r15     # 0x138957
xorl %r12d, %r12d
cmpq 0x48(%r14), %r12
jge 0xdc335
movq 0x8(%rsp), %rax
movl (%rax,%r12,8), %ecx
movl 0x4(%rax,%r12,8), %r8d
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde9f1
incq %r12
jmp 0xdc30c
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x39(%r14)
movq %rbx, 0x18(%rsp)
je 0xdc37c
movq 0x30(%r14), %rax
decq %rax
imulq 0x28(%r14), %rax
jmp 0xdc380
movq 0x18(%rbx), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %r15, %rcx
movq %rax, %r12
movq %rax, %r8
callq 0xdc5d4
xorl %r13d, %r13d
jmp 0xdc51c
movq 0x48(%r14), %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
cmpq 0x48(%r14), %r15
jge 0xdc4e5
movq 0x8(%rsp), %rax
movl (%rax,%r15,8), %ecx
movq %r15, 0x28(%rsp)
movl 0x4(%rax,%r15,8), %eax
movq %rcx, 0x10(%rsp)
cmpq $0x14, %rcx
movq 0x18(%rsp), %r15
jae 0xdc41b
cmpb $0x0, 0x3a(%r14)
jne 0xdc3d7
movq %r15, %rdi
leaq 0x5c5af(%rip), %rsi     # 0x138975
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0xdf05a
movb $0x1, 0x3a(%r14)
movq 0x28(%rsp), %rbx
leaq 0x1(%rbx), %r15
movq 0x40(%r14), %rbp
imulq %r15, %rbp
movq %r13, %rdi
callq 0xe651f
movq %rbp, %rdx
subq %rax, %rdx
je 0xdc388
jle 0xdc4d5
cmpq 0x20(%rsp), %rbx
jl 0xdc4d5
movq %r13, %rdi
movl $0xff, %esi
callq 0xe5e40
jmp 0xdc388
addq %rcx, %rax
xorl %r12d, %r12d
movq %rax, 0x48(%rsp)
cmpq %rax, %rcx
jge 0xdc3d7
cmpq 0x40(%r14), %r12
jge 0xdc3d7
movq 0x18(%r15), %rsi
movq %r14, %rdi
leaq 0x10(%rsp), %rdx
callq 0xdc6da
movl %eax, %ebx
andl $0x7f, %ebx
je 0xdc3d7
movq 0x18(%r15), %rcx
testb %al, %al
js 0xdc481
movq %r14, %rdi
movq %rcx, %rsi
leaq 0x10(%rsp), %rdx
callq 0xdc6da
cmpl $0x2, 0x8(%r14)
jne 0xdc4a8
movl %eax, %r15d
movq %rbx, %rbp
subq $0x1, %rbp
jb 0xdc4b6
movq %r13, %rdi
movq %r15, %rsi
callq 0xe5f1e
jmp 0xdc46e
movq 0x10(%rsp), %rsi
movl 0x8(%r14), %edx
imulq %rbx, %rdx
movq %rcx, %rdi
movq %r13, %rcx
callq 0xe45f4
movl 0x8(%r14), %eax
imulq %rbx, %rax
addq %rax, 0x10(%rsp)
jmp 0xdc4b6
movzbl %al, %esi
movq %r13, %rdi
movq %rbx, %rdx
callq 0xe5e40
movl 0x8(%r14), %eax
imulq %rbx, %rax
addq %rax, %r12
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r15
movq 0x48(%rsp), %rax
jmp 0xdc426
movq %r13, %rdi
movq %rbp, %rsi
callq 0xe5eb2
jmp 0xdc388
movq %r13, %rdi
callq 0xe58e8
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xded32
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq 0x40(%rsp), %r15
movq %r15, %rcx
movq 0x38(%rsp), %r12
movq %r12, %r8
callq 0xdc5d4
testq %r12, %r12
jne 0xdc52c
pushq $0x6
popq %rsi
movq %r15, %rdi
callq 0xe84da
pushq $0x6
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xe6d4b
movq %r13, %rdi
callq 0xe5591
movq %r15, %rdi
callq 0xe7084
movq %r12, %rdi
callq 0xe7084
jmp 0xdc10c