// File: /workspace/github_c_makefile_star2_O1/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 %ebp, %ebp
xorl %esi, %esi
callq 0xef1ca
movq %rax, %r12
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xef1ca
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xf23f0
testl %eax, %eax
je 0x69f65
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
movq %r15, %rcx
imulq %r12, %rcx
leaq 0x4(,%rcx,2), %rdx
cmpq %rdx, %rax
sete %dl
leaq 0x4(,%rcx,4), %rcx
xorl %r13d, %r13d
cmpq %rcx, %rax
sete %al
orb %al, %dl
testb $0x1, %dl
je 0x69e01
movb %al, %r13b
jmp 0x69e18
leaq 0xbb65a(%rip), %rsi     # 0x125462
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movl $0x1, %r13d
xorl %r14d, %r14d
testl %r13d, %r13d
sete %r14b
leal 0x2(,%r13,2), %edx
leaq 0xd655b(%rip), %rsi     # 0x14038c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl $0x2, %ecx
subq %r14, %rcx
movq %r12, %rax
movq %rcx, 0x28(%rsp)
shlq %cl, %rax
movq %rax, 0x10(%rsp)
movl $0x4, %ecx
subl %r14d, %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xf2cab
movq %rax, %rbp
testq %r15, %r15
jle 0x69f49
movq $0x0, 0x8(%rsp)
movq %r15, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
testq %r12, %r12
jle 0x69f2e
movq 0x8(%rsp), %rax
imulq 0x10(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq %r14, %r15
movq 0x28(%rsp), %rcx
shlq %cl, %r15
addq 0x30(%rsp), %r15
movq 0x18(%rbx), %rdi
testl %r13d, %r13d
je 0x69f01
movq %r15, %rsi
xorl %edx, %edx
callq 0xefa60
movq %r13, %rbp
movq %r12, %r13
movl %eax, %r12d
movq 0x18(%rbx), %rdi
addq $0x3, %r15
movq %r15, %rsi
callq 0xef0d5
andl $0xffffff, %r12d        # imm = 0xFFFFFF
movzbl %al, %ecx
shll $0x18, %ecx
orl %r12d, %ecx
movq %r13, %r12
movq %rbp, %r13
movq 0x20(%rsp), %rbp
jmp 0x69f12
movq %r15, %rsi
callq 0xef1ca
movl %eax, %edi
callq 0xecf84
movl %eax, %ecx
movq %rbp, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
callq 0xf2b02
incq %r14
cmpq %r14, %r12
jne 0x69ea8
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %r15
movq %rcx, 0x8(%rsp)
cmpq %r15, %rcx
jne 0x69e88
movq %rbp, %rdi
movl $0x3, %esi
callq 0xf3f11
movq %rbp, %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xf2923
movq %rbp, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf28d4