// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0x60, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq $0x2, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %edi
callq 0x156f00
movq -0x50(%rbp), %rdi
movl -0x44(%rbp), %edx
movq %rax, %rcx
leaq 0x7ac81(%rip), %rsi     # 0x1d16df
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x7ac62(%rip), %rsi     # 0x1d16f4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165210
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0x7ac40(%rip), %rsi     # 0x1d1705
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x7ac1e(%rip), %rsi     # 0x1d1719
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x7abf4(%rip), %rsi     # 0x1d1725
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0x7abc8(%rip), %rsi     # 0x1d1731
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
leaq 0x7ab7f(%rip), %rsi     # 0x1d173c
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x18(%rbp), %rax
movl (%rax), %r9d
movl $0x50, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x172650
movq -0x40(%rbp), %rdi
movq %rax, %rdx
leaq 0x5759e(%rip), %rsi     # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %edi
callq 0x156f70
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %edx
movq %rax, %rcx
leaq 0x7aac3(%rip), %rsi     # 0x1d1751
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jle 0x156cae
movl $0x1, -0x28(%rbp)
jmp 0x156db8
movq -0x18(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x156cc4
movl $0x1, -0x28(%rbp)
jmp 0x156db8
movq -0x18(%rbp), %rax
cmpl $0x1, 0x8(%rax)
je 0x156ce4
movq -0x18(%rbp), %rax
cmpl $0x2, 0x8(%rax)
je 0x156ce4
movl $0x1, -0x28(%rbp)
jmp 0x156db8
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x156cfb
movq -0x18(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x156d12
movq -0x18(%rbp), %rax
movq $0x1, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x156d29
movq -0x18(%rbp), %rax
movq $0x1, 0x30(%rax)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x30(%rax)
jle 0x156d3d
movl $0x3, -0x24(%rbp)
jmp 0x156d5a
movq -0x18(%rbp), %rax
cmpq $0x1, 0x28(%rax)
jle 0x156d51
movl $0x2, -0x24(%rbp)
jmp 0x156d58
movl $0x1, -0x24(%rbp)
jmp 0x156d5a
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
cmpl -0x24(%rbp), %eax
jb 0x156d70
movq -0x18(%rbp), %rax
cmpl $0x3, 0xc(%rax)
jbe 0x156d8c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
movl -0x24(%rbp), %ecx
leaq 0x7a9e3(%rip), %rsi     # 0x1d1768
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rax
cmpq $0x1, 0x30(%rax)
jl 0x156da2
movq -0x18(%rbp), %rax
cmpq $0x4, 0x30(%rax)
jle 0x156dab
movl $0x1, -0x28(%rbp)
jmp 0x156db8
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x157000
cmpl $0x0, -0x28(%rbp)
je 0x156dd0
movq -0x8(%rbp), %rdi
leaq 0x7a9dc(%rip), %rsi     # 0x1d17a5
movb $0x0, %al
callq 0x15bfb0
cmpq $0x0, -0x18(%rbp)
je 0x156df1
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)