// File: /workspace/github_c_makefile_star2_O2/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 $0x448, %rsp            # imm = 0x448
movl %r8d, %ebp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, 0x8(%rsp)
leaq 0x14(%rsp), %rsi
callq 0xdebae
leaq 0xcf4dc(%rip), %rdx     # 0x10c742
pushq $0x4
popq %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0xe4d48
testl %eax, %eax
jne 0x3d543
leaq 0xcfcf6(%rip), %rsi     # 0x10cf79
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0xc(%r15), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %r14, %rdi
callq 0xe40bf
cmpq $0x20, %rax
jne 0x3d543
leaq 0x20(%r15), %r12
movq %r12, 0x8(%rsp)
leaq 0xe06e7(%rip), %rdx     # 0x11d9b3
pushq $0x2
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
callq 0xe4d48
testl %eax, %eax
jne 0x3d543
leaq 0x8(%rsp), %rsi
addq $0x2, (%rsi)
movq %r14, %rdi
callq 0xe40bf
addq %r12, %rax
cmpq 0x18(%r14), %rax
jg 0x3d543
leaq 0x8(%rsp), %r13
addq $0x10, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
movq %rbx, %rdi
movq %r12, 0x28(%rsp)
movq %r12, %rsi
movq %rax, 0x20(%rsp)
movq %rax, %rdx
callq 0xdf038
addq $0x4, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
movq %rax, 0x30(%rsp)
leaq 0xcfc3c(%rip), %rsi     # 0x10cf93
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
leaq 0xcfc2e(%rip), %rsi     # 0x10cfa3
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
leaq 0xcfc1a(%rip), %rsi     # 0x10cfb2
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0xc, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
movq %rax, %r12
leaq 0xcfc04(%rip), %rsi     # 0x10cfc3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
movq %r13, %rsi
callq 0xe40bf
leaq 0xcfbf3(%rip), %rsi     # 0x10cfd1
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0xc, (%r13)
movq 0x30(%rsp), %rax
andl $-0x11, %eax
cmpl $0x8, %eax
jne 0x3d543
leaq 0x40(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0xe87a8
testq %r12, %r12
je 0x3d4b1
leaq (%r15,%r12), %rdx
addq $0x2e, %rdx
leaq 0x8(%rsp), %r13
movq %rdx, (%r13)
leaq 0xcfba7(%rip), %rsi     # 0x10cfdf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq $0x2, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xe3fc4
movq %rax, %r13
leaq 0xcfb89(%rip), %rsi     # 0x10cff0
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x100, %r13            # imm = 0x100
jg 0x3d543
movq 0x8(%rsp), %rsi
movl $0x1, (%rsp)
pushq $0x3
popq %rcx
leaq 0x40(%rsp), %r8
movl $0x100, %r9d            # imm = 0x100
movq %r14, %rdi
movq %r13, %rdx
callq 0xe7963
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xe6be1
testl %eax, %eax
je 0x3d543
xorl %eax, %eax
testq %r12, %r12
setne %al
movq 0x30(%rsp), %r13
cmpl $0x18, %r13d
leal 0x1(%rax,%rax), %eax
pushq $0x3
popq %rcx
cmovbl %eax, %ecx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xe7437
movq %rax, %r12
movq 0x38(%rsp), %rax
leaq (%r15,%rax), %rdx
addq $0x2e, %rdx
movq %rdx, 0x8(%rsp)
leaq 0xcfaf4(%rip), %rsi     # 0x10d004
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rsi
cmpl $0x17, %r13d
ja 0x3d57d
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
leaq 0x40(%rsp), %r8
movq %r14, %rdi
movq 0x18(%rsp), %rcx
movq %r12, %r9
callq 0xe7cbc
jmp 0x3d594
leaq 0xcfaca(%rip), %rsi     # 0x10d014
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
callq 0xe7084
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
popq %rcx
pushq $0x1
popq %r9
movq %r14, %rdi
movq 0x18(%rsp), %rdx
movq %r12, %r8
callq 0xe7f67
xorl %esi, %esi
testl %ebp, %ebp
leaq 0xe0907(%rip), %rax     # 0x11dea6
cmovneq %rax, %rsi
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0xe70b7
jmp 0x3d557