// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/cfb.c
// Function: do_Corel_UIformat
static void do_Corel_UIformat(deark *c, lctx *d, struct dir_entry_info *dei,
	dbuf *f, i64 pos1, i64 len, int is_thumb)
{
	i64 pos = pos1;
	i64 hdr_len;
	i64 ri_pos;
	i64 ri_len;
	i64 w, h;
	i64 pal_offs, img_offs;
	i64 rowspan;
	i64 pixels_size;
	i64 npalent;
	int bpp;
	int ok = 0;
	int saved_indent_level;
	de_bitmap *img = NULL;
	u32 pal[256];

	de_dbg_indent_save(c, &saved_indent_level);

	if(dbuf_memcmp(f, pos, "UI\x00\x00", 4)) goto done;

	de_dbg(c, "CorelUI at [%"I64_FMT"], len=%"I64_FMT, pos1, len);
	de_dbg_indent(c, 1);
	pos += 2; // "UI"
	pos += 2; // ?
	pos += 4; // The size of the "RI" segment? Redundant?
	pos += 4; // ?

	hdr_len = dbuf_getu32le_p(f, &pos);
	// Apparently the size of the "UI" segment
	if(hdr_len != 32) goto done;
	// TODO: More fields here

	ri_pos = pos1+hdr_len;
	pos = ri_pos;
	if(dbuf_memcmp(f, pos, "RI", 2)) goto done;
	pos += 2;
	ri_len = dbuf_getu32le_p(f, &pos);
	if(ri_pos + ri_len > f->len) goto done;

	pos += 16;
	w = dbuf_getu32le_p(f, &pos);
	h = dbuf_getu32le_p(f, &pos);
	de_dbg_dimensions(c, w, h);

	pos += 4; // ? (observed 1)

	bpp = (int)dbuf_getu32le_p(f, &pos);
	de_dbg(c, "bits/pixel?: %d", bpp);
	rowspan = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "bytes/row?: %d", (int)rowspan);
	pixels_size = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "pixels size: %"I64_FMT, pixels_size);
	pos += 4; // ?
	pos += 8; // ? (density?)

	pal_offs = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "pal offs: %"I64_FMT, pal_offs);

	img_offs = dbuf_getu32le_p(f, &pos);
	de_dbg(c, "img offs: %"I64_FMT, img_offs);

	pos += 12; // ?

	if(bpp!=8 && bpp!=24) goto done;

	// == palette ==
	de_make_grayscale_palette(pal, 256, 0);
	if(pal_offs!=0) {
		// This formula doesn't make sense to me, but seems to work.
		pos = ri_pos+14+pal_offs;
		de_dbg(c, "palette at [%"I64_FMT"]", pos);
		de_dbg_indent(c, 1);
		pos += 2; // ? (observed 4, 5)

		npalent = dbuf_getu16le_p(f, &pos);
		de_dbg(c, "num pal entries: %d", (int)npalent);
		if(npalent>256) goto done;

		de_read_palette_rgb(f, pos, npalent, 3, pal, 256, DE_GETRGBFLAG_BGR);
		de_dbg_indent(c, -1);
	}

	// == image ==
	if(!de_good_image_dimensions(c, w, h)) goto done;

	img = de_bitmap_create(c, w, h, (bpp<24 && pal_offs==0)?1:3);
	pos = ri_pos+14+img_offs;
	de_dbg(c, "bitmap at [%"I64_FMT"]", pos);
	if(bpp<24) {
		de_convert_image_paletted(f, pos, 8, rowspan, pal, img, 0);
	}
	else {
		de_convert_image_rgb(f, pos, rowspan, 3, img, DE_GETRGBFLAG_BGR);
	}

	de_bitmap_write_to_file(img, is_thumb?"thumb":NULL, DE_CREATEFLAG_FLIP_IMAGE);

	ok = 1;

done:
	if(!ok) {
		de_dbg(c, "[unsupported image type]");
	}
	de_bitmap_destroy(img);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %r8d, %r12d
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, 0x10(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0xe958c
movq 0x10(%rsp), %rsi
leaq 0xda87a(%rip), %rdx     # 0x1196fa
movl $0x4, %ecx
movq %r14, %rdi
callq 0xf025e
movb $0x1, %bpl
testl %eax, %eax
je 0x3eed3
xorl %r12d, %r12d
testb %bpl, %bpl
je 0x3eead
leaq 0xdb141(%rip), %rsi     # 0x119fe4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
callq 0xf28d4
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, 0x18(%rsp)
leaq 0xdb06a(%rip), %rsi     # 0x119f49
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x10(%rsp), %rsi
addq $0xc, (%rsi)
movq %r14, %rdi
callq 0xef3a0
cmpq $0x20, %rax
jne 0x3ee97
leaq 0x20(%r15), %r12
movq %r12, 0x10(%rsp)
leaq 0xeba2d(%rip), %rdx     # 0x12a953
movl $0x2, %ecx
movq %r14, %rdi
movq %r12, %rsi
callq 0xf025e
testl %eax, %eax
jne 0x3ee94
leaq 0x10(%rsp), %rsi
addq $0x2, (%rsi)
movq %r14, %rdi
callq 0xef3a0
addq %r12, %rax
cmpq 0x18(%r14), %rax
jg 0x3ee94
leaq 0x10(%rsp), %r13
addq $0x10, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rbx, %rdi
movq %r12, 0x48(%rsp)
movq %r12, %rsi
movq %rax, 0x40(%rsp)
movq %rax, %rdx
callq 0xe9b05
addq $0x4, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, 0x30(%rsp)
leaq 0xdafb0(%rip), %rsi     # 0x119f63
xorl %r12d, %r12d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
leaq 0xdaf9f(%rip), %rsi     # 0x119f73
movq %rbx, %rdi
movq %rax, 0x20(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
leaq 0xdaf8b(%rip), %rsi     # 0x119f82
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0xc, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
leaq 0xdaf78(%rip), %rsi     # 0x119f93
movq %rbx, %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movq %r13, %rsi
callq 0xef3a0
leaq 0xdaf62(%rip), %rsi     # 0x119fa1
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0xc, (%r13)
movq 0x30(%rsp), %rax
andl $-0x11, %eax
cmpl $0x8, %eax
jne 0x3ee97
leaq 0x50(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0xf41f4
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x3f120
leaq (%r15,%rax), %rdx
addq $0x2e, %rdx
leaq 0x10(%rsp), %r13
movq %rdx, (%r13)
leaq 0xdaf11(%rip), %rsi     # 0x119faf
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
addq $0x2, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xef1ed
movq %rax, %r13
leaq 0xdaeee(%rip), %rsi     # 0x119fc0
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x100, %r13            # imm = 0x100
jg 0x3ee97
movq 0x10(%rsp), %rsi
movl $0x1, (%rsp)
leaq 0x50(%rsp), %r8
movl $0x3, %ecx
movl $0x100, %r9d            # imm = 0x100
movq %r14, %rdi
movq %r13, %rdx
callq 0xf328f
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq %rbx, %rdi
movq 0x48(%rsp), %r12
movq %r12, %rsi
movq 0x40(%rsp), %r13
movq %r13, %rdx
callq 0xf241d
testl %eax, %eax
je 0x3ee94
xorl %eax, %eax
cmpq $0x0, 0x28(%rsp)
setne %al
movq %r13, %rdx
movq 0x30(%rsp), %r13
cmpl $0x18, %r13d
leal 0x1(%rax,%rax), %eax
movl $0x3, %ecx
cmovll %eax, %ecx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf2cab
movq %rax, %r12
movq 0x38(%rsp), %rax
leaq (%r15,%rax), %rdx
addq $0x2e, %rdx
movq %rdx, 0x10(%rsp)
leaq 0xdae4a(%rip), %rsi     # 0x119fd4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%rsp), %rsi
cmpl $0x17, %r13d
jg 0x3f1c2
movl $0x0, (%rsp)
leaq 0x50(%rsp), %r8
movl $0x8, %edx
movq %r14, %rdi
movq 0x20(%rsp), %rcx
movq %r12, %r9
callq 0xf35b8
jmp 0x3f1dd
movl $0x3, %ecx
movq %r14, %rdi
movq 0x20(%rsp), %rdx
movq %r12, %r8
movl $0x1, %r9d
callq 0xf38d9
xorl %ebp, %ebp
cmpl $0x0, 0x18(%rsp)
leaq 0xebc5b(%rip), %rax     # 0x12ae46
movl $0x0, %esi
cmovneq %rax, %rsi
movq %r12, %rdi
movl $0x4, %edx
callq 0xf2923
jmp 0x3ee97
nop