// File: /workspace/github_c_makefile_star2_O3/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
movl $0x60, %esi
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xeb220
movl %eax, (%r14)
leaq 0x50(%rsp), %r12
movq $0x2, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movb %al, 0x4(%r14)
movzbl %al, %edx
cmpb $0x1, %dl
leaq 0x3f818(%rip), %rax     # 0x1252a2
leaq 0x466c0(%rip), %r15     # 0x12c151
cmovneq %r15, %rax
testb %dl, %dl
leaq 0x2ac73(%rip), %rcx     # 0x110711
cmovneq %rax, %rcx
leaq 0x60169(%rip), %rsi     # 0x145c12
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movzbl %al, %edx
movl %edx, 0x8(%r14)
leaq 0x6015a(%rip), %rsi     # 0x145c27
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb91
movl %eax, 0xc(%r14)
leaq 0x6014a(%rip), %rsi     # 0x145c38
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb91
movq %rax, 0x20(%r14)
leaq 0x6013b(%rip), %rsi     # 0x145c4c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb91
movq %rax, 0x28(%r14)
leaq 0x60123(%rip), %rsi     # 0x145c58
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb91
movq %rax, 0x30(%r14)
leaq 0x6010b(%rip), %rsi     # 0x145c64
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movl %eax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movl %eax, 0x14(%r14)
movl 0x10(%r14), %edx
leaq 0x600df(%rip), %rsi     # 0x145c6f
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
addq $0x4, (%r12)
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x58(%r14)
movq 0x18(%rbx), %rdi
movq (%r12), %rsi
movl (%r14), %r9d
movl $0x50, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
movq 0x58(%r14), %rdi
callq 0xf70f6
leaq 0x3cba0(%rip), %rsi     # 0x12277b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0x50, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movl %eax, 0x18(%r14)
cmpl $0x3, %eax
ja 0xe5c15
movl %eax, %ecx
andl $0x3, %ecx
leaq 0x601da(%rip), %rdx     # 0x145de8
movslq (%rdx,%rcx,4), %r15
addq %rdx, %r15
leaq 0x60068(%rip), %rsi     # 0x145c84
movq %rbx, %rdi
movl %eax, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
cmpb $0x1, 0x4(%r14)
ja 0xe5cd8
cmpl $0x0, 0x18(%r14)
jne 0xe5cd8
movl 0x8(%r14), %eax
decl %eax
cmpl $0x1, %eax
ja 0xe5cd8
cmpq $0x0, 0x20(%r14)
jne 0xe5c5f
movq $0x1, 0x20(%r14)
movq 0x28(%r14), %rdx
testq %rdx, %rdx
jne 0xe5c75
movq $0x1, 0x28(%r14)
movl $0x1, %edx
movq 0x30(%r14), %rax
testq %rax, %rax
je 0xe5c8b
movl $0x3, %ecx
cmpq $0x1, %rax
jle 0xe5c98
jmp 0xe5ca3
movq $0x1, 0x30(%r14)
movl $0x1, %eax
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 0xe5cce
leaq 0x5ffdb(%rip), %rsi     # 0x145c9b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x30(%r14), %rax
leaq -0x5(%rax), %rcx
cmpq $-0x4, %rcx
jae 0xe5d0c
leaq 0x5fff9(%rip), %rsi     # 0x145cd8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x58(%r14), %rdi
callq 0xf6753
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
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 0xf1db1
testl %eax, %eax
je 0xe5db6
movl 0x14(%r14), %eax
testl %eax, %eax
je 0xe5d79
movl 0x8(%r14), %ecx
cmpl $0x1, %ecx
sete %dl
cmpl $0x18, %eax
setb %sil
testb %dl, %sil
jne 0xe5d68
cmpl $0x2, %ecx
setne %cl
cmpl $0x1800, %eax           # imm = 0x1800
setae %al
orb %cl, %al
jne 0xe5d79
leaq 0x5ffbb(%rip), %rsi     # 0x145d2a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbx, %rdi
callq 0xf266a
movq %rax, %r15
movl 0x8(%r14), %eax
cmpl $0x2, %eax
jne 0xe5dc3
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movl 0x30(%r14), %ecx
movq %rbx, %rdi
callq 0xf266a
movq %rax, %r12
movl 0x8(%r14), %eax
jmp 0xe5dc6
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0xe6152
xorl %r12d, %r12d
movl %eax, %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 0xe5f51
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %rbp
movq %rax, %rdi
callq 0xf0855
movq 0x48(%r14), %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, %r13
movq $0x200, 0x10(%rsp)      # imm = 0x200
leaq 0x5ff30(%rip), %rsi     # 0x145d64
movl $0x200, %r15d           # imm = 0x200
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x5ff1d(%rip), %rsi     # 0x145d7a
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x48(%r14)
jle 0xe5e9a
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeed31
movl %eax, (%r13,%r12,8)
incq %r12
cmpq 0x48(%r14), %r12
jl 0xe5e7b
movq 0x10(%rsp), %r15
leaq 0x5fee8(%rip), %rsi     # 0x145d89
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x48(%r14)
jle 0xe5ed7
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeed31
movl %eax, 0x4(%r13,%r12,8)
incq %r12
cmpq 0x48(%r14), %r12
jl 0xe5ebd
movq 0x10(%rsp), %rdx
leaq 0x5feb5(%rip), %rsi     # 0x145d98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpl $0x2, (%rbx)
jl 0xe5f26
cmpq $0x0, 0x48(%r14)
jle 0xe5f26
leaq 0x5fea7(%rip), %r15     # 0x145da7
xorl %r12d, %r12d
movl (%r13,%r12,8), %ecx
movl 0x4(%r13,%r12,8), %r8d
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe87e4
incq %r12
cmpq 0x48(%r14), %r12
jl 0xe5f03
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0x39(%r14)
je 0xe5f6f
movq 0x30(%r14), %rcx
movq 0x48(%r14), %rax
decq %rcx
imulq 0x28(%r14), %rcx
movq %rcx, 0x20(%rsp)
jmp 0xe5f78
movq 0x18(%rbx), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0xe61f4
xorl %ebp, %ebp
jmp 0xe612e
movq 0x48(%r14), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
jle 0xe60fe
xorl %r12d, %r12d
movq %r14, 0x40(%rsp)
movq %r13, 0x38(%rsp)
movl (%r13,%r12,8), %ecx
movq %r12, 0x18(%rsp)
movl 0x4(%r13,%r12,8), %eax
movq %rcx, 0x8(%rsp)
cmpq $0x14, %rcx
jae 0xe5fd3
cmpb $0x0, 0x3a(%r14)
jne 0xe60a6
movq %rbx, %rdi
leaq 0x5fe08(%rip), %rsi     # 0x145dc5
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0xe8faa
movb $0x1, 0x3a(%r14)
jmp 0xe60a6
testl %eax, %eax
je 0xe60a6
addq %rcx, %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
cmpq 0x40(%r14), %r12
jge 0xe60a6
movq 0x18(%rbx), %rsi
movl 0x8(%r14), %edi
leaq 0x8(%rsp), %rdx
callq 0xe6341
movl %eax, %r13d
andl $0x7f, %r13d
je 0xe60a6
movq 0x18(%rbx), %rcx
testb %al, %al
js 0xe6049
movl 0x8(%r14), %edi
movq %rcx, %rsi
leaq 0x8(%rsp), %rdx
callq 0xe6341
cmpl $0x2, 0x8(%r14)
jne 0xe6070
movq %rbp, %r14
movl %eax, %ebp
movq %r13, %r15
movq %r14, %rdi
movq %rbp, %rsi
callq 0xf0fad
decq %r15
jne 0xe6037
jmp 0xe6081
movq 0x8(%rsp), %rsi
movl 0x8(%r14), %edx
imulq %r13, %rdx
movq %rcx, %rdi
movq %rbp, %rcx
callq 0xef481
movl 0x8(%r14), %eax
imulq %r13, %rax
addq %rax, 0x8(%rsp)
jmp 0xe607e
movzbl %al, %esi
movq %rbp, %rdi
movq %r13, %rdx
callq 0xf0eba
movq %rbp, %r14
movq 0x40(%rsp), %rcx
movl 0x8(%rcx), %eax
imulq %r13, %rax
addq %rax, %r12
movq 0x48(%rsp), %rax
cmpq %rax, 0x8(%rsp)
movq %r14, %rbp
movq %rcx, %r14
jl 0xe5fe6
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %r12
movq 0x40(%r14), %r15
imulq %r12, %r15
movq %rbp, %rdi
callq 0xf1701
movq %r15, %rdx
subq %rax, %rdx
movq 0x38(%rsp), %r13
je 0xe60f4
jle 0xe60e9
movq 0x18(%rsp), %rax
cmpq 0x20(%rsp), %rax
jl 0xe60e9
movq %rbp, %rdi
movl $0xff, %esi
callq 0xf0eba
jmp 0xe60f4
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf0f27
cmpq 0x48(%r14), %r12
jl 0xe5f8e
movq %rbp, %rdi
callq 0xf08ba
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq 0x30(%rsp), %r15
movq %r15, %rcx
movq 0x28(%rsp), %r12
movq %r12, %r8
callq 0xe61f4
testq %r12, %r12
jne 0xe6140
movq %r15, %rdi
movl $0x6, %esi
callq 0xf38c4
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x6, %ecx
callq 0xf1f1a
movq %rbp, %rdi
callq 0xf052b
movq %r15, %rdi
callq 0xf228d
movq %r12, %rdi
callq 0xf228d
jmp 0xe5ce9