// File: /workspace/github_c_makefile_star2_O2/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 0x14(%rsp), %rsi
callq 0xdebae
leaq 0xb266a(%rip), %rsi     # 0x118405
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe404f
movq %rax, %r13
leaq 0xb2688(%rip), %rsi     # 0x118446
pushq $0x4
popq %rdx
movq %rbx, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0xde896
leaq 0x4(%r13), %r12
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r12
jge 0x65fe3
movq %r12, %rsi
callq 0xe404f
movq %rax, %r14
leaq 0xb266b(%rip), %rsi     # 0x11845d
movq %rbx, %rdi
movl %r12d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
leaq 0x28(%r13), %rsi
callq 0xe404f
movq %rax, %r15
movq 0x18(%rbx), %rdi
leaq 0x2c(%r13), %rsi
callq 0xe404f
movq %rax, %rbp
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xdf038
movq %rbx, %rdi
movq %r15, (%rsp)
movq %r15, %rsi
movq %rbp, 0x8(%rsp)
movq %rbp, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x65fb2
movq 0x18(%rbx), %rdi
addq $0x3c, %r13
movq %r13, %rsi
callq 0xe404f
movq %rax, %rbp
leaq 0xb260c(%rip), %rsi     # 0x118480
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq %rbp, %rax
andq $-0x3, %rax
cmpq $0x1, %rax
jne 0x65fb7
addq %r12, %r14
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r14
jge 0x65fb2
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0xc36f2(%rip), %rsi     # 0x1295a6
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x8
popq %rsi
movq (%rsp), %r12
movq %r12, %rdi
callq 0xdfbb6
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %rax, %rbx
movq 0x8(%rsp), %r12
movq %r12, %rcx
movl %ebp, %r8d
callq 0xe746f
movq %rax, %r13
movq %rbx, %rcx
testq %rbx, %rbx
movl $0x0, %ebx
cmovgq %rcx, %rbx
testq %r12, %r12
movl $0x0, %eax
cmovgq %r12, %rax
movq %rax, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
imulq %rcx, %r12
movq %r12, 0x8(%rsp)
cmpq %rbp, %r15
je 0x65fcf
movq %r15, (%rsp)
movq %rbp, %r15
movq %r14, 0x20(%rsp)
movq %r14, %rbp
xorl %r14d, %r14d
cmpq 0x30(%rsp), %r14
je 0x65f96
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x65f8c
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
leaq (%r12,%rbp), %rsi
callq 0xe3eb4
movzbl %al, %r8d
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
cmpq $0x3, %r15
jne 0x65f7f
movq (%rsp), %rcx
callq 0xe712a
jmp 0x65f87
movl %r8d, %ecx
callq 0xe71da
incq %r12
jmp 0x65f4a
incq %r14
addq 0x28(%rsp), %rbp
jmp 0x65f40
movq (%rsp), %rax
incq %rax
movq 0x20(%rsp), %r14
addq 0x8(%rsp), %r14
movq %r15, %rbp
movq %rax, %r15
jmp 0x65f25
xorl %r14d, %r14d
jmp 0x65fe3
leaq 0xb24b6(%rip), %rsi     # 0x118474
xorl %r14d, %r14d
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x65fe3
movq %r13, %r14
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq 0x18(%rsp), %rbx
movq %r14, %rdi
callq 0xe7084
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq