// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: de_run_lumena_cel
static void de_run_lumena_cel(deark *c, de_module_params *mparams)
{
	i64 width, height;
	i64 rowspan;
	i64 i, j;
	u32 clr;
	u8 a;
	int is_16bit = 0;
	int is_32bit = 0;
	de_bitmap *img = NULL;
	const i64 headersize = 4;
	i64 bypp;

	width = de_getu16le(0);
	height = de_getu16le(2);
	if(!de_good_image_dimensions_noerr(c, width, height)) goto done;

	// TODO: Support multi-image files
	is_16bit = (c->infile->len == headersize + width*height*2);
	is_32bit = (c->infile->len == headersize + width*height*4);
	if(!is_16bit && !is_32bit) {
		de_warn(c, "Cannot detect bits/pixel, assuming 32");
		is_32bit = 1;
	}

	bypp = (is_32bit) ? 4 : 2;
	de_dbg(c, "bytes/pixel: %d", (int)bypp);
	rowspan = width * bypp;

	img = de_bitmap_create(c, width, height, is_32bit?4:3);

	for(j=0; j<height; j++) {
		for(i=0; i<width; i++) {
			i64 pos = headersize + j*rowspan + i*bypp;
			if(is_32bit) {
				clr = dbuf_getRGB(c->infile, pos, 0);
				a = de_getbyte(pos + 3);
				clr = DE_SET_ALPHA(clr, a);
			}
			else {
				clr = (u32)de_getu16le(pos);
				clr = de_rgb555_to_888(clr);
			}
			de_bitmap_setpixel_rgba(img, i, j, clr);
		}
	}

	de_bitmap_optimize_alpha(img, 0x3);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_FLIP_IMAGE);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x4, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x16a7f0
cmpl $0x0, %eax
jne 0x98084
jmp 0x98270
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
imulq -0x20(%rbp), %rcx
shlq %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
imulq -0x20(%rbp), %rcx
shlq $0x2, %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x980fe
cmpl $0x0, -0x48(%rbp)
jne 0x980fe
movq -0x8(%rbp), %rdi
leaq 0x118a12(%rip), %rsi    # 0x1b0b02
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0x48(%rbp)
movl -0x48(%rbp), %edx
movl $0x2, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
leaq 0x1342b4(%rip), %rsi    # 0x1cc3dc
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
imulq -0x60(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x48(%rbp), %r8d
movl $0x3, %ecx
movl $0x4, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x98250
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x9823d
movq -0x38(%rbp), %rax
imulq -0x28(%rbp), %rax
addq $0x4, %rax
movq -0x30(%rbp), %rcx
imulq -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x981f9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
addq $0x3, %rsi
callq 0x165060
movb %al, -0x3d(%rbp)
movl -0x3c(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movzbl -0x3d(%rbp), %ecx
shll $0x18, %ecx
orl %ecx, %eax
movl %eax, -0x3c(%rbp)
jmp 0x98218
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0x165280
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %edi
callq 0x1619e0
movl %eax, -0x3c(%rbp)
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x3c(%rbp), %ecx
callq 0x16b350
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x98184
jmp 0x9823f
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x9816e
movq -0x50(%rbp), %rdi
movl $0x3, %esi
callq 0x16d190
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x4, %edx
callq 0x16afd0
movq -0x50(%rbp), %rdi
callq 0x16af60
addq $0x70, %rsp
popq %rbp
retq
nop