// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/wmf.c
// Function: wmf_handler_BITBLT_STRETCHBLT_DIBBITBLT
static int wmf_handler_BITBLT_STRETCHBLT_DIBBITBLT(deark *c, lctx *d, struct decoder_params *dp)
{
	i64 pos = dp->dpos;
	int has_src_bitmap;
	unsigned int RasterOperation;
	i64 XSrc, YSrc;
	i64 Width, Height;
	i64 YDest, XDest;

	has_src_bitmap = (dp->recsize_words != ((i64)(dp->recfunc>>8)+3));
	de_dbg(c, "has src bitmap: %d", has_src_bitmap);
	if(!has_src_bitmap) goto done;

	RasterOperation = (unsigned int)de_getu32le(pos);
	de_dbg(c, "RasterOperation: 0x%08x", RasterOperation);
	pos += 4;

	if(dp->rectype==0x23) { // STRETCHBLT
		i64 SrcWidth, SrcHeight;
		SrcHeight = de_geti16le_p(&pos);
		SrcWidth = de_geti16le_p(&pos);
		de_dbg(c, "SrcWidth, SrcHeight: %d"DE_CHAR_TIMES"%d",
			(int)SrcWidth, (int)SrcHeight);
	}

	YSrc = de_geti16le_p(&pos);
	XSrc = de_geti16le_p(&pos);
	de_dbg(c, "XSrc, YSrc: (%d, %d)", (int)XSrc, (int)YSrc);

	Height = de_geti16le_p(&pos);
	Width = de_geti16le_p(&pos);
	de_dbg_dimensions(c, Width, Height);

	YDest = de_geti16le_p(&pos);
	XDest = de_geti16le_p(&pos);
	de_dbg(c, "XDest, YDest: (%d, %d)", (int)XDest, (int)YDest);

	// TODO: Bitmap16 object (if BITBLT or STRETCHBLT)
	if(dp->rectype==0x40) { // DIBBITBLT
		i64 dib_pos, dib_len;

		// TODO: Merge this with the DIBSTRETCHBLT, STRETCHDIB code.
		dib_pos = pos;
		dib_len = dp->dpos + dp->dlen - dib_pos;

		if(dib_len<12) goto done;
		de_dbg(c, "DIB at %d, size=%d", (int)dib_pos, (int)dib_len);

		de_dbg_indent(c, 1);
		de_run_module_by_id_on_slice(c, "dib", NULL, c->infile, dib_pos, dib_len);
		de_dbg_indent(c, -1);
	}

done:
	return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rdi, %rbx
movq 0x20(%rdx), %rax
movq %rax, (%rsp)
movq 0x10(%rdx), %r15
movzbl 0x1(%rdx), %r12d
addq $0x3, %r12
xorl %edx, %edx
cmpq %r12, %r15
setne %dl
leaq 0x6ce5d(%rip), %rsi     # 0x140713
xorl %eax, %eax
callq 0xe9226
cmpq %r12, %r15
je 0xd3a1c
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
callq 0xef37e
leaq 0x6ce4c(%rip), %rsi     # 0x140726
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
addq $0x4, (%rsp)
cmpb $0x23, 0x2(%r14)
jne 0xd3926
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xef2dd
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
leaq 0x6ce27(%rip), %rsi     # 0x14073e
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xef2dd
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
leaq 0x6ce0f(%rip), %rsi     # 0x14075a
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xe9b05
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
leaq 0x6cdca(%rip), %rsi     # 0x14076f
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
cmpb $0x40, 0x2(%r14)
jne 0xd3a1c
movq (%rsp), %r15
movq 0x20(%r14), %r12
subq %r15, %r12
addq 0x28(%r14), %r12
cmpq $0xc, %r12
jl 0xd3a1c
leaq 0x6cdaf(%rip), %rsi     # 0x140786
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rcx
leaq 0x4f830(%rip), %rsi     # 0x12322f
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
movq %r12, %r9
callq 0xea5fa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq