// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
movq %rdi, %rbx
movq 0x10(%rdx), %r12
movq 0x20(%rdx), %r15
movzbl 0x1(%rdx), %r13d
addq $0x3, %r13
xorl %edx, %edx
cmpq %r13, %r12
setne %dl
leaq 0x69859(%rip), %rsi     # 0x1337a3
xorl %eax, %eax
callq 0xde896
cmpq %r13, %r12
je 0xca0b4
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe409f
leaq 0x69849(%rip), %rsi     # 0x1337b6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
addq $0x4, %r15
movq %r15, 0x8(%rsp)
cmpb $0x23, 0x2(%r14)
jne 0xc9fbf
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xe4038
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
leaq 0x6981e(%rip), %rsi     # 0x1337ce
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xe4038
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
leaq 0x69804(%rip), %rsi     # 0x1337ea
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xdf038
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe4038
leaq 0x697bf(%rip), %rsi     # 0x1337ff
movq %rbx, %rdi
movl %eax, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
cmpb $0x40, 0x2(%r14)
jne 0xca0b4
movq 0x8(%rsp), %r15
movq 0x20(%r14), %r12
subq %r15, %r12
addq 0x28(%r14), %r12
cmpq $0xc, %r12
jl 0xca0b4
leaq 0x697a3(%rip), %rsi     # 0x133816
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rcx
leaq 0x4c1b6(%rip), %rsi     # 0x11624f
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %r8
movq %r12, %r9
callq 0xdf9b8
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x1
popq %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq