// File: /workspace/github_c_makefile_star2_O3/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 0xe8a1c
leaq 0xda9ab(%rip), %rdx     # 0x119832
movl $0x4, %ecx
movq %r14, %rdi
movq %r15, %rsi
callq 0xefc85
testl %eax, %eax
jne 0x3f17b
leaq 0xdb1c3(%rip), %rsi     # 0x11a069
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0xc(%r15), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %r14, %rdi
callq 0xeed83
cmpq $0x20, %rax
jne 0x3f17b
leaq 0x20(%r15), %r12
movq %r12, 0x8(%rsp)
leaq 0xebd32(%rip), %rdx     # 0x12ac23
movl $0x2, %ecx
movq %r14, %rdi
movq %r12, %rsi
callq 0xefc85
testl %eax, %eax
jne 0x3f17b
leaq 0x8(%rsp), %rsi
addq $0x2, (%rsi)
movq %r14, %rdi
callq 0xeed83
addq %r12, %rax
cmpq 0x18(%r14), %rax
jg 0x3f17b
leaq 0x8(%rsp), %r13
addq $0x10, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
movq %rbx, %rdi
movq %r12, 0x28(%rsp)
movq %r12, %rsi
movq %rax, 0x20(%rsp)
movq %rax, %rdx
callq 0xe8f88
addq $0x4, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, 0x30(%rsp)
leaq 0xdb105(%rip), %rsi     # 0x11a083
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
leaq 0xdb0f7(%rip), %rsi     # 0x11a093
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
leaq 0xdb0e3(%rip), %rsi     # 0x11a0a2
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0xc, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
movq %rax, %r12
leaq 0xdb0cd(%rip), %rsi     # 0x11a0b3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movq %r13, %rsi
callq 0xeed83
leaq 0xdb0bc(%rip), %rsi     # 0x11a0c1
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0xc, (%r13)
movq 0x30(%rsp), %rax
andl $-0x11, %eax
cmpl $0x8, %eax
jne 0x3f17b
leaq 0x40(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0xf3bdb
testq %r12, %r12
je 0x3f0de
leaq (%r15,%r12), %rdx
addq $0x2e, %rdx
leaq 0x8(%rsp), %r13
movq %rdx, (%r13)
leaq 0xdb070(%rip), %rsi     # 0x11a0cf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
addq $0x2, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0xeebeb
movq %rax, %r13
leaq 0xdb050(%rip), %rsi     # 0x11a0e0
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x100, %r13            # imm = 0x100
jg 0x3f17b
movq 0x8(%rsp), %rsi
movl $0x1, (%rsp)
leaq 0x40(%rsp), %r8
movl $0x3, %ecx
movl $0x100, %r9d            # imm = 0x100
movq %r14, %rdi
movq %r13, %rdx
callq 0xf2c4e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xf1db1
testl %eax, %eax
je 0x3f17b
xorl %eax, %eax
testq %r12, %r12
setne %al
movq 0x30(%rsp), %r13
cmpl $0x18, %r13d
leal 0x1(%rax,%rax), %eax
movl $0x3, %ecx
cmovbl %eax, %ecx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xf266a
movq %rax, %r12
movq 0x38(%rsp), %rax
leaq (%r15,%rax), %rdx
addq $0x2e, %rdx
movq %rdx, 0x8(%rsp)
leaq 0xdafb1(%rip), %rsi     # 0x11a0f4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rsi
cmpl $0x17, %r13d
ja 0x3f1b5
movl $0x0, (%rsp)
leaq 0x40(%rsp), %r8
movl $0x8, %edx
movq %r14, %rdi
movq 0x18(%rsp), %rcx
movq %r12, %r9
callq 0xf2f79
jmp 0x3f1d0
leaq 0xdaf82(%rip), %rsi     # 0x11a104
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
callq 0xf228d
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3, %ecx
movq %r14, %rdi
movq 0x18(%rsp), %rdx
movq %r12, %r8
movl $0x1, %r9d
callq 0xf3279
xorl %esi, %esi
testl %ebp, %ebp
leaq 0xebf3b(%rip), %rax     # 0x12b116
cmovneq %rax, %rsi
movq %r12, %rdi
movl $0x4, %edx
callq 0xf22dc
jmp 0x3f18f
nop