// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sgiimage.c
// Function: do_sgiimage_image
static void do_sgiimage_image(deark *c, struct sgiimage_ctx *d)
{
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_bitmap *imglo = NULL;

	d->is_grayscale = (d->num_channels<=2);
	d->has_alpha = (d->num_channels==2 || d->num_channels==4);

	if(!de_good_image_dimensions(c, d->width, d->height)) goto done;

	if(d->pix_max>0 && (
		(d->bytes_per_sample==1 && d->pix_max<24) ||
		(d->bytes_per_sample==2 && d->pix_max<24*256)))
	{
		// If pix_max is to be believed, this image likely needs its brightness
		// adjusted.
		de_warn(c, "This image might need special processing (not supported).");
	}

	img = de_bitmap_create(c, d->width, d->height, (int)d->num_channels);
	if(d->bytes_per_sample==2) {
		imglo = de_bitmap_create(c, d->width, d->height, (int)d->num_channels);
	}

	d->rowspan = d->width * d->bytes_per_sample;
	d->num_scanlines = d->height * d->num_channels;
	d->total_unc_size = d->num_scanlines * d->rowspan;

	if(d->storage_fmt==0) { // Uncompressed
		sgiimage_decode_image(c, d, c->infile, 512, img, imglo);
	}
	else {
		unc_pixels = dbuf_create_membuf(c, d->total_unc_size, 0);
		dbuf_enable_wbuffer(unc_pixels);
		sgiimage_decompress_rle(c, d, unc_pixels);
		sgiimage_decode_image(c, d, unc_pixels, 0, img, imglo);
	}

	// Remove the alpha channel if it seems bad
	if(!imglo) {
		de_bitmap_optimize_alpha(img, 0x4 | 0x2);
	}

	de_bitmap16_write_to_file_finfo(img, imglo, NULL, DE_CREATEFLAG_FLIP_IMAGE |
		DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
	de_bitmap_destroy(imglo);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x2, 0x30(%rax)
setle %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x38(%rax)
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpq $0x2, 0x30(%rcx)
movb %al, -0x29(%rbp)
je 0x157061
movq -0x10(%rbp), %rax
cmpq $0x4, 0x30(%rax)
sete %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x39(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x157095
jmp 0x15722b
movq -0x10(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jbe 0x1570dc
movq -0x10(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x1570b3
movq -0x10(%rbp), %rax
cmpl $0x18, 0x14(%rax)
jb 0x1570ca
movq -0x10(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x1570dc
movq -0x10(%rbp), %rax
cmpl $0x1800, 0x14(%rax)     # imm = 0x1800
jae 0x1570dc
movq -0x8(%rbp), %rdi
leaq 0x7a722(%rip), %rsi     # 0x1d17f7
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
callq 0x16b750
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x157134
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
callq 0x16b750
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
imulq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x40(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x1571b2
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
movl $0x200, %ecx            # imm = 0x200
callq 0x157250
jmp 0x157200
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1573c0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
callq 0x157250
cmpq $0x0, -0x28(%rbp)
jne 0x157215
movq -0x20(%rbp), %rdi
movl $0x6, %esi
callq 0x16d190
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x6, %ecx
callq 0x16aac0
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x28(%rbp), %rdi
callq 0x16af60
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)