// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0xe3fa3
movq %rax, %r14
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rsi
movq %rsi, %r12
callq 0xe3fa3
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xe6bb4
testl %eax, %eax
je 0x662db
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
movq %r13, %rcx
imulq %r14, %rcx
leaq 0x4(,%rcx,2), %rdx
cmpq %rdx, %rax
sete %dl
leaq 0x4(,%rcx,4), %rcx
xorl %esi, %esi
cmpq %rcx, %rax
sete %al
orb %al, %dl
movq %r14, %r15
testb $0x1, %dl
je 0x6617e
movb %al, %sil
jmp 0x66192
leaq 0xb230d(%rip), %rsi     # 0x118492
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rsi
xorl %r14d, %r14d
testl %esi, %esi
sete %r14b
movq %rsi, 0x8(%rsp)
leal 0x2(,%rsi,2), %edx
leaq 0xcd26e(%rip), %rsi     # 0x13341c
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %r12, %rcx
subq %r14, %rcx
movq %r15, %rax
movq %rcx, 0x20(%rsp)
shlq %cl, %rax
movq %rax, 0x10(%rsp)
pushq $0x4
popq %rcx
subl %r14d, %ecx
movq %r15, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xe7437
testq %r15, %r15
cmovleq %rbp, %r14
movq %rax, %r12
testq %r13, %r13
cmovleq %rbp, %r13
movq %r13, 0x18(%rsp)
cmpq %r13, %rbp
je 0x662c0
movq %rbp, %rax
imulq 0x10(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %r13
movq %rbp, 0x30(%rsp)
cmpq %r15, %r14
je 0x662b3
movq %r12, %rbp
movq %r15, %r12
movq 0x20(%rsp), %rcx
shlq %cl, %r12
addq 0x28(%rsp), %r12
movq 0x18(%rbx), %rdi
testl %r13d, %r13d
je 0x66284
movq %r12, %rsi
xorl %edx, %edx
callq 0xe45a4
movq %r14, %r13
movl %eax, %r14d
movq 0x18(%rbx), %rdi
addq $0x3, %r12
movq %r12, %rsi
callq 0xe3eb4
andl $0xffffff, %r14d        # imm = 0xFFFFFF
movzbl %al, %ecx
shll $0x18, %ecx
orl %r14d, %ecx
movq %r13, %r14
movq 0x8(%rsp), %r13
jmp 0x66295
movq %r12, %rsi
callq 0xe3fa3
movl %eax, %edi
callq 0xe1ea3
movl %eax, %ecx
movq %rbp, %r12
movq %rbp, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
callq 0xe728c
incq %r15
jmp 0x66225
incq %rbp
movq 0x18(%rsp), %r13
jmp 0x661fd
pushq $0x3
popq %rsi
movq %r12, %rdi
callq 0xe84da
pushq $0x4
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0xe70b7
movq %r12, %r15
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe7084