// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sunras.c
// Function: do_image
static void do_image(deark *c, lctx *d, dbuf *unc_pixels)
{
	de_bitmap *img = NULL;
	u8 nonport_warn_flag = 0;
	i64 dst_bypp;

	if(d->depth!=1 && d->depth!=4 && d->depth!=8 && d->depth!=16 &&
		d->depth!=24 && d->depth!=32)
	{
		de_err(c, "Bit depth %u not supported", d->depth);
		goto done;
	}
	if(d->depth==16) {
		nonport_warn_flag = 1;
	}
	if(d->depth==32 && !d->user_set_fmt32) {
		// Some apps think the extra channel comes first (e.g. xBGR); others
		// think it comes last (BGRx).
		// Some apps think the extra channel is for alpha; others think it is
		// unused.
		// Some apps think the color channels are always in BGR order; others
		// think the order is RGB for RT_FORMAT_RGB format.
		//
		// By default we use ARGB for RT_FORMAT_RGB, and ABGR otherwise, with
		// alpha autodetected (alpha channel is ignored if all values are 0).

		de_warn(c, "32-bit Sun Raster files are not portable. You may have to use "
			"\"-opt sunras:fmt32=...\".");

		d->color32desc.channel_shift[0] = 24; // A or x
		d->color32desc.channel_shift[2] = 8;  // G
		if(d->is_rgb_order) { // xrgb or argb
			d->color32desc.channel_shift[1] = 16; // R
			d->color32desc.channel_shift[3] = 0;  // B
		}
		else { // xbgr or abgr
			d->color32desc.channel_shift[1] = 0;  // B
			d->color32desc.channel_shift[3] = 16; // R
		}

		d->color32desc.alpha_code = 2;
	}
	if(nonport_warn_flag) {
		de_warn(c, "This image type might not be portable");
	}

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

	d->src_bypp = (i64)d->depth/8;

	if(d->is_paletted) {
		dst_bypp = 3;
	}
	else if(d->is_grayscale) {
		dst_bypp = 1;
	}
	else if(d->depth==32) {
		if(d->color32desc.alpha_code==0) {
			dst_bypp = 3;
		}
		else {
			dst_bypp = 4;
		}
	}
	else {
		dst_bypp = 3;
	}

	if(d->is_rgb_order) {
		d->getrgbflags = 0;
	}
	else {
		d->getrgbflags = DE_GETRGBFLAG_BGR;
	}

	img = de_bitmap_create2(c, d->npwidth, d->pdwidth, d->height, (int)dst_bypp);

	if(d->depth==16) {
		decode_image_16(c, d, unc_pixels, img);
	}
	else if(d->is_paletted || d->is_grayscale) {
		de_convert_image_paletted(unc_pixels, 0, d->depth, d->rowspan, d->pal, img, 0);
	}
	else {
		decode_image_24_32(c, d, unc_pixels, img);
	}

	if(d->depth==32 && d->color32desc.alpha_code==2) { // autodetect alpha
		de_bitmap_optimize_alpha(img, 0x1);
	}

	de_bitmap_write_to_file(img, NULL, 0);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x18(%rax)
je 0x127fea
movq -0x10(%rbp), %rax
cmpl $0x4, 0x18(%rax)
je 0x127fea
movq -0x10(%rbp), %rax
cmpl $0x8, 0x18(%rax)
je 0x127fea
movq -0x10(%rbp), %rax
cmpl $0x10, 0x18(%rax)
je 0x127fea
movq -0x10(%rbp), %rax
cmpl $0x18, 0x18(%rax)
je 0x127fea
movq -0x10(%rbp), %rax
cmpl $0x20, 0x18(%rax)
je 0x127fea
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edx
leaq 0x9f155(%rip), %rsi     # 0x1c7133
movb $0x0, %al
callq 0x15bfb0
jmp 0x128228
movq -0x10(%rbp), %rax
cmpl $0x10, 0x18(%rax)
jne 0x127ff8
movb $0x1, -0x21(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x20, 0x18(%rax)
jne 0x128062
movq -0x10(%rbp), %rax
cmpb $0x0, 0x22(%rax)
jne 0x128062
movq -0x8(%rbp), %rdi
leaq 0x9f137(%rip), %rsi     # 0x1c714e
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movb $0x18, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x8, 0x2a(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x21(%rax)
je 0x12804a
movq -0x10(%rbp), %rax
movb $0x10, 0x29(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x2b(%rax)
jmp 0x12805a
movq -0x10(%rbp), %rax
movb $0x0, 0x29(%rax)
movq -0x10(%rbp), %rax
movb $0x10, 0x2b(%rax)
movq -0x10(%rbp), %rax
movb $0x2, 0x2c(%rax)
cmpb $0x0, -0x21(%rbp)
je 0x12807a
movq -0x8(%rbp), %rdi
leaq 0x9f132(%rip), %rsi     # 0x1c71a5
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x12809c
jmp 0x128228
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x468(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x60(%rax)
je 0x1280cf
movq $0x3, -0x30(%rbp)
jmp 0x12811a
movq -0x10(%rbp), %rax
cmpb $0x0, 0x61(%rax)
je 0x1280e3
movq $0x1, -0x30(%rbp)
jmp 0x128118
movq -0x10(%rbp), %rax
cmpl $0x20, 0x18(%rax)
jne 0x12810e
movq -0x10(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpl $0x0, %eax
jne 0x128104
movq $0x3, -0x30(%rbp)
jmp 0x12810c
movq $0x4, -0x30(%rbp)
jmp 0x128116
movq $0x3, -0x30(%rbp)
jmp 0x128118
jmp 0x12811a
movq -0x10(%rbp), %rax
cmpb $0x0, 0x21(%rax)
je 0x128134
movq -0x10(%rbp), %rax
movl $0x0, 0x470(%rax)
jmp 0x128142
movq -0x10(%rbp), %rax
movl $0x1, 0x470(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movl %eax, %r8d
callq 0x16b7f0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x10, 0x18(%rax)
jne 0x12818e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x128360
jmp 0x1281f4
movq -0x10(%rbp), %rax
movzbl 0x60(%rax), %eax
cmpl $0x0, %eax
jne 0x1281a8
movq -0x10(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0x0, %eax
je 0x1281dd
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x10(%rbp), %r8
addq $0x64, %r8
movq -0x20(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
jmp 0x1281f2
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x128420
jmp 0x1281f4
movq -0x10(%rbp), %rax
cmpl $0x20, 0x18(%rax)
jne 0x128219
movq -0x10(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpl $0x2, %eax
jne 0x128219
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x16d190
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x20(%rbp), %rdi
callq 0x16af60
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)