// File: /workspace/github_c_makefile_star2_O1/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 0x1c(%rsp), %rsi
callq 0xe958c
leaq 0xbb9cf(%rip), %rsi     # 0x1253d5
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef32a
movq %rax, %r14
leaq 0xbb9eb(%rip), %rsi     # 0x125416
movq %rbx, %rdi
movl $0x4, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe9226
leaq 0x4(%r14), %rbp
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rbp
jge 0x69c4a
movq %rbp, %rsi
callq 0xef32a
movq %rax, %r15
leaq 0xbb9cc(%rip), %rsi     # 0x12542d
movq %rbx, %rdi
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
leaq 0x28(%r14), %rsi
callq 0xef32a
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0x2c(%r14), %rsi
callq 0xef32a
movq %rax, %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe9b05
movq %rbx, %rdi
movq %r12, 0x8(%rsp)
movq %r12, %rsi
movq %r13, 0x10(%rsp)
movq %r13, %rdx
callq 0xf241d
testl %eax, %eax
je 0x69c2e
movq 0x18(%rbx), %rdi
addq $0x3c, %r14
movq %r14, %rsi
callq 0xef32a
movq %rax, %r13
leaq 0xbb96b(%rip), %rsi     # 0x125450
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r13, %rax
andq $-0x3, %rax
cmpq $0x1, %rax
jne 0x69c33
addq %rbp, %r15
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r15
jge 0x69c2e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0xcc9b4(%rip), %rsi     # 0x1364dc
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x8, %esi
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xea80b
movq %rax, %rbp
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq 0x10(%rsp), %rcx
movl %r13d, %r8d
callq 0xf2ce6
movq %r13, %rcx
movq %rax, %r13
testq %rcx, %rcx
jle 0x69c18
movq %rcx, %r14
movq 0x10(%rsp), %rax
imulq %rbp, %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, 0x20(%rsp)
cmpq $0x0, 0x10(%rsp)
jle 0x69bf8
movq 0x20(%rsp), %r15
xorl %r12d, %r12d
testq %rbp, %rbp
jle 0x69beb
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (%r15,%rbx), %rsi
callq 0xef0d5
movzbl %al, %r8d
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
cmpq $0x3, %r14
jne 0x69bdb
movq 0x8(%rsp), %rcx
callq 0xf299c
jmp 0x69be3
movl %r8d, %ecx
callq 0xf2a4a
incq %rbx
cmpq %rbx, %rbp
jne 0x69baa
incq %r12
addq %rbp, %r15
cmpq 0x10(%rsp), %r12
jne 0x69ba3
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r15
addq 0x30(%rsp), %r15
movq %rcx, 0x8(%rsp)
cmpq %r14, %rcx
jne 0x69b8e
movq %r13, %r15
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
movq 0x28(%rsp), %rbx
jmp 0x69c4a
xorl %r15d, %r15d
jmp 0x69c4a
leaq 0xbb80a(%rip), %rsi     # 0x125444
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9df7
movq %r15, %rdi
callq 0xf28d4
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq