// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/misc2.c
// Function: de_run_vitec
static void de_run_vitec(deark *c, de_module_params *mparams)
{
	i64 npwidth, pdwidth, h;
	i64 i, j, plane;
	de_bitmap *img = NULL;
	i64 samplesperpixel;
	i64 rowspan, planespan;
	i64 pos;
	u8 b;
	i64 h1size, h2size;
	int saved_indent_level;

	// This code is based on reverse engineering, and may be incorrect.

	de_dbg_indent_save(c, &saved_indent_level);
	de_warn(c, "VITec image support is experimental, and may not work correctly.");

	pos = 4;
	h1size = de_getu32be(pos);
	de_dbg(c, "header 1 at %d, len=%d", (int)pos, (int)h1size);
	// Don't know what's in this part of the header. Just ignore it.
	pos += h1size;
	if(pos>=c->infile->len) goto done;

	h2size = de_getu32be(pos);
	de_dbg(c, "header 2 at %d, len=%d", (int)pos, (int)h2size);
	de_dbg_indent(c, 1);

	// pos+4: Bits size?
	// pos+24: Unknown field, usually 7

	npwidth = de_getu32be(pos+36);
	h = de_getu32be(pos+40);
	de_dbg_dimensions(c, npwidth, h);
	if(!de_good_image_dimensions(c, npwidth, h)) goto done;

	// pos+52: Unknown field, 1 in grayscale images

	samplesperpixel = de_getu32be(pos+56);
	de_dbg(c, "samples/pixel: %d", (int)samplesperpixel);
	if(samplesperpixel!=1 && samplesperpixel!=3) {
		de_err(c, "Unsupported samples/pixel: %d", (int)samplesperpixel);
		goto done;
	}

	pos += h2size;
	if(pos>=c->infile->len) goto done;
	de_dbg_indent(c, -1);

	de_dbg(c, "bitmap at %d", (int)pos);
	pdwidth = de_pad_to_n(npwidth, 8);
	img = de_bitmap_create2(c, npwidth, pdwidth, h, (int)samplesperpixel);
	rowspan = pdwidth;
	planespan = rowspan*h;

	for(plane=0; plane<samplesperpixel; plane++) {
		for(j=0; j<h; j++) {
			for(i=0; i<pdwidth; i++) {
				b = de_getbyte(pos + plane*planespan + j*rowspan + i);
				if(samplesperpixel==3) {
					de_bitmap_setsample(img, i, j, plane, b);
				}
				else {
					de_bitmap_setpixel_gray(img, i, j, b);
				}
			}
		}
	}

	de_bitmap_write_to_file(img, NULL, 0);

done:
	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 $0x38, %rsp
movq %rdi, %rbx
leaq 0x24(%rsp), %rsi
callq 0xe8a1c
leaq 0xbbe40(%rip), %rsi     # 0x125605
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeed0d
movq %rax, %r13
leaq 0xbbe5c(%rip), %rsi     # 0x125646
movq %rbx, %rdi
movl $0x4, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe8617
leaq 0x4(%r13), %r14
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r14
jge 0x699fc
movq %r14, %rsi
callq 0xeed0d
movq %rax, %r12
leaq 0xbbe3d(%rip), %rsi     # 0x12565d
movq %rbx, %rdi
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq 0x28(%r13), %rsi
callq 0xeed0d
movq %rax, %rbp
movq 0x18(%rbx), %rdi
leaq 0x2c(%r13), %rsi
callq 0xeed0d
movq %rax, %r15
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0xe8f88
movq %rbx, %rdi
movq %rbp, 0x8(%rsp)
movq %rbp, %rsi
movq %r15, 0x10(%rsp)
movq %r15, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x699e0
movq 0x18(%rbx), %rdi
addq $0x3c, %r13
movq %r13, %rsi
callq 0xeed0d
movq %rax, %r15
leaq 0xbbddb(%rip), %rsi     # 0x125680
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rax
andq $-0x3, %rax
cmpq $0x1, %rax
jne 0x699e5
addq %r14, %r12
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r12
jge 0x699e0
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0xccfbe(%rip), %rsi     # 0x1368a6
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x8, %esi
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0xe9cb4
movq %rax, %r14
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
movq %r15, %r13
movl %r15d, %r8d
callq 0xf26a5
movq %rax, %r15
imulq %r14, %rbx
movq %rbx, 0x30(%rsp)
movq %r12, 0x8(%rsp)
cmpq $0x0, 0x10(%rsp)
jle 0x699aa
movq 0x8(%rsp), %rbp
xorl %r12d, %r12d
testq %r14, %r14
jle 0x6999d
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (%rbx,%rbp), %rsi
callq 0xeead3
movzbl %al, %r8d
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
cmpq $0x3, %r13
jne 0x6998d
movq 0x18(%rsp), %rcx
callq 0xf2355
jmp 0x69995
movl %r8d, %ecx
callq 0xf2405
incq %rbx
cmpq %rbx, %r14
jne 0x6995c
incq %r12
addq %r14, %rbp
cmpq 0x10(%rsp), %r12
jne 0x69955
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r12
addq 0x30(%rsp), %r12
movq %rcx, 0x18(%rsp)
cmpq %r13, %rcx
jne 0x69940
movq %r15, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq 0x28(%rsp), %rbx
jmp 0x699fc
xorl %r12d, %r12d
jmp 0x699fc
leaq 0xbbc88(%rip), %rsi     # 0x125674
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9295
movq %r12, %rdi
callq 0xf228d
movl 0x24(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq