// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessImage()
int CCapsImageStd::ProcessImage()
{
	// check whether the image is in a supported format
	int res=CheckEncoder(dii.ci.encoder, dii.ci.encrev);
	if (res != imgeOk)
		return res;

	PDISKTRACKINFO pti=di.pdt;

	// check density type
	if (pti->ci.dentype<=cpdenNA || pti->ci.dentype>=cpdenLast)
		return imgeIncompatible;

	// check signal/cell type
	if (pti->ci.sigtype<=cpsigNA || pti->ci.sigtype>=cpsigLast)
		return imgeIncompatible;

	// check process
	if (pti->ci.process)
		return imgeIncompatible;

	// clear track data
	FreeTrack(pti, 1);

	// generate comparable image if supported
	if (di.flag & DI_LOCK_COMP)
		return CompareImage();

	// forced indexing not allowed for varied cell density
	switch (pti->ci.dentype) {
		case cpdenCLAmiga:
		case cpdenCLAmiga2:
		case cpdenCLST:
		case cpdenSLAmiga:
		case cpdenSLAmiga2:
		case cpdenABAmiga:
		case cpdenABAmiga2:
			di.flag&=~DI_LOCK_INDEX;
			break;
	}

	// original track size calculated
	uint32_t trackbits=di.dsctrackbc;

	// generate 1 revolution of data as default, 5 for analyser
	int trackcnt=(di.flag & DI_LOCK_ANA) ? 5 : 1;

	// set unformatted track parameters
	if (pti->ci.dentype == cpdenNoise) {
		// 2 revolutions if multiple revolutions defined
		if (di.flag & DI_LOCK_NOISEREV)
			trackcnt=2;

		// don't generate unformatted data unless requested, set default size unless already defined
		if (!(di.flag & DI_LOCK_NOISE))
			trackcnt=0;
		else
			if (!trackbits)
				trackbits=100000;
	}

	// 5 revolutions for weak data, if track update is not used
	if ((pti->ci.flag & CAPS_IF_FLAKEY) && !(di.flag & DI_LOCK_UPDATEFD))
		trackcnt=5;

	// 16 bit aligned track size if requested
	if (di.flag & DI_LOCK_ALIGN) {
		if (trackbits & 15)
			trackbits+=16-(trackbits & 15);
	}

	// re-align track to 8 bit
	if (!(di.flag & DI_LOCK_TRKBIT)) {
		if (trackbits & 7)
			trackbits+=8-(trackbits & 7);
	}

	// size for all tracks
	uint32_t alltrackbits=trackbits*trackcnt;

	// get number of bytes required for track buffer
	int bufsize=trackbuf.CalculateByteSize(alltrackbits);
	uint8_t *tbuf;

	// allocate and clear track buffer
	if (bufsize) {
		tbuf=new uint8_t[bufsize];
		memset(tbuf, 0, bufsize);
	} else
		tbuf=NULL;

	// encoding start position, always on first track
	uint32_t startbit=di.dscstartbit;
	if (trackbits)
		startbit=startbit % trackbits;

	// start position is 0/index if indexing is forced
	if (di.flag & DI_LOCK_INDEX)
		startbit=0;

	// write track info
	pti->trackcnt=trackcnt;
	pti->trackbuf=tbuf;
	pti->tracklen=bufsize;
	pti->sdpos=startbit >> 3;
	pti->overlap=-1;
	pti->overlapbit=-1;
	pti->trackbc=alltrackbits;
	pti->singletrackbc=trackbits;
	pti->startbit=startbit;

	// reset random seed
	pti->wseed=0x87654321;

	// initialize sector information
	AllocTrackSI(pti);

	// set track buffer information
	di.track=tbuf;
	di.trackbc=alltrackbits;
	di.singletrackbc=trackbits;
	trackbuf.InitBitSize(di.track, di.trackbc);

	// additional track size
	uint32_t trackdiff=trackbits-di.dsctrackbc;

	// track gap split position unknown
	int gsvalid=0;
	uint32_t gspos=0;

	// process all revolutions
	uint32_t trksize=0, actpos=startbit;
	for (int trk=0; trk < trackcnt; trk++) {
		// create track size and pointer for each revolution
		uint32_t ofsact=trksize >> 3;
		pti->trackdata[trk]=tbuf+ofsact;
		pti->trackstart[trk]=ofsact;
		trksize+=trackbits;
		uint32_t ofsnext=trksize >> 3;
		pti->tracksize[trk]=ofsnext-ofsact;

		// decode/encode all blocks
		for (int blk=0; blk < di.blockcount; blk++) {
			uint32_t datasize=di.block[blk].blockbits;
			uint32_t gapsize=di.block[blk].gapbits;

			// add additional size to last, track gap
			if (blk == di.blockcount-1) {
				// trying to grow an empty gap area is not supported
				if (!gapsize && trackdiff)
					return imgeGeneric;

				gapsize+=trackdiff;
			}

			// write block
			res=ProcessBlock(blk, actpos, datasize, gapsize);
			if (res != imgeOk)
				return res;

			// if track gap has known split position, save it
			if (!trk && (blk == di.blockcount-1) && di.encgsvalid) {
				gsvalid=1;
				gspos=di.encgapsplit;
			}

			// next write position, handle track buffer wrap around (disk rotation)
			actpos+=datasize+gapsize;
			if (actpos >= di.trackbc)
				actpos-=di.trackbc;
		}

		// fix MFM encoding
		MFMFixup();
	}

	// sanity check; writing must end where it started
	if (actpos != startbit)
		return imgeGeneric;

	// set track overlap position
	if (gsvalid) {
		uint32_t ofs=gspos % trackbits;
		pti->overlapbit=ofs;

		if (di.flag & DI_LOCK_OVLBIT)
			pti->overlap=ofs;
		else
			pti->overlap=ofs >> 3;
	}

	// create noise track
	if (pti->ci.dentype==cpdenNoise && (di.flag & DI_LOCK_NOISE))
		GenerateNoiseTrack(pti);

	// create density map
	if ((res=DecodeDensity(pti, di.data, di.flag)) != imgeOk)
		return res;

	// update bits at the overlap position
	UpdateOverlap();

	// first image update to ensure weak bits etc are in place
	res=UpdateImage(0);

	return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl 0x50(%rdi), %esi
movl 0x54(%rdi), %edx
callq 0x8336
testl %eax, %eax
je 0x881e
movl %eax, %ebp
jmp 0x884b
movq 0x210(%rbx), %r14
movl 0x108(%r14), %eax
addl $-0xa, %eax
pushq $0xe
popq %rbp
cmpl $-0x9, %eax
jb 0x884b
cmpl $0x1, 0x10c(%r14)
jne 0x884b
cmpl $0x0, 0x12c(%r14)
je 0x885c
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xc720
movl 0x208(%rbx), %ebp
testl %ebp, %ebp
js 0x88b3
movl 0x108(%r14), %edx
leal -0x3(%rdx), %eax
cmpl $0x6, %eax
ja 0x888c
andl $0x7ffffffe, %ebp       # imm = 0x7FFFFFFE
movl %ebp, 0x208(%rbx)
movl 0x1c8(%rbx), %eax
xorl %ecx, %ecx
cmpl $0x40000000, %ebp       # imm = 0x40000000
setae %cl
leal 0x1(,%rcx,4), %ecx
cmpl $0x1, %edx
jne 0x88de
testb $0x20, %bpl
jne 0x88ca
xorl %ecx, %ecx
jmp 0x88de
movq (%rbx), %rax
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x30(%rax)
testb $0x40, %bpl
pushq $0x2
popq %rdx
cmovnel %edx, %ecx
testl %eax, %eax
movl $0x186a0, %edx          # imm = 0x186A0
cmovel %edx, %eax
btl $0x8, %ebp
pushq $0x5
popq %rdx
cmovbl %ecx, %edx
movq %r14, 0x28(%rsp)
testb $0x1, 0x130(%r14)
cmovel %ecx, %edx
movl %eax, %ecx
andl $-0x10, %ecx
addl $0x10, %ecx
testb $0xf, %al
cmovel %eax, %ecx
testb $0x2, %bpl
cmovel %eax, %ecx
movl %ecx, %r13d
andl $-0x8, %r13d
addl $0x8, %r13d
testb $0x7, %cl
cmovel %ecx, %r13d
testw $0x1000, %bp            # imm = 0x1000
cmovnel %ecx, %r13d
movl %r13d, %r12d
movl %edx, 0x18(%rsp)
imull %edx, %r12d
leal 0x7(%r12), %eax
movl %eax, %r14d
shrl $0x3, %r14d
cmpl $0x8, %eax
jae 0x894e
movq $0x0, 0x20(%rsp)
jmp 0x896b
movl %r14d, %r15d
movq %r15, %rdi
callq 0x3040
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3110
movl 0x1d4(%rbx), %eax
testl %r13d, %r13d
je 0x897d
xorl %edx, %edx
divl %r13d
movl %edx, %eax
xorl %ecx, %ecx
testb $0x1, %bpl
cmovnel %ecx, %eax
movl %eax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movl 0x18(%rsp), %r15d
movl %r15d, 0x28(%rdi)
movq 0x20(%rsp), %rbp
movq %rbp, 0x48(%rdi)
movl %r14d, 0x50(%rdi)
movl 0x8(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0xc0(%rdi)
orq $-0x1, 0xe0(%rdi)
movl %r12d, 0xe8(%rdi)
movl %r13d, 0xec(%rdi)
movl 0x8(%rsp), %eax
movl %eax, 0xf0(%rdi)
movl $0x87654321, 0xc4(%rdi) # imm = 0x87654321
callq 0xcf58
movq %rbp, 0x1b8(%rbx)
movl %r12d, 0x1c0(%rbx)
movl %r13d, 0x1c4(%rbx)
leaq 0xb8(%rbx), %rdi
movq %rbp, %rsi
movl %r12d, %edx
callq 0x392a
movl 0x8(%rsp), %eax
movl 0x1c8(%rbx), %ecx
movl %r13d, 0xc(%rsp)
movl %ecx, 0x38(%rsp)
subl %ecx, %r13d
movl %r13d, 0x3c(%rsp)
movl %r15d, %ecx
pushq $0x4
popq %r12
xorl %esi, %esi
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
xorl %edi, %edi
movl %eax, %edx
movq %rcx, 0x40(%rsp)
cmpq %rcx, %rsi
je 0x8b75
movl %edi, %eax
shrl $0x3, %eax
movq 0x20(%rsp), %rcx
addq %rax, %rcx
movq 0x28(%rsp), %r8
movq %rcx, 0x58(%r8,%rsi,8)
movl %eax, 0x94(%r8,%rsi,4)
addl 0xc(%rsp), %edi
movl %edi, 0x34(%rsp)
movl %edi, %ecx
shrl $0x3, %ecx
subl %eax, %ecx
movl %ecx, 0x80(%r8,%rsi,4)
xorl %ebp, %ebp
movq %rsi, 0x18(%rsp)
movslq 0x204(%rbx), %rax
cmpq %rax, %rbp
jge 0x8b43
movq 0x1f8(%rbx), %rcx
movl -0x4(%rcx,%r12), %r13d
movl (%rcx,%r12), %r14d
decl %eax
cmpq %rax, %rbp
jne 0x8aca
testl %r14d, %r14d
jne 0x8ac5
movl 0x38(%rsp), %eax
cmpl %eax, 0xc(%rsp)
jne 0x8b6d
addl 0x3c(%rsp), %r14d
movq %rbx, %rdi
movl %ebp, %esi
movl %edx, %r15d
movl %r13d, %ecx
movl %r14d, %r8d
callq 0x8c04
testl %eax, %eax
jne 0x881a
cmpq $0x0, 0x18(%rsp)
jne 0x8b1a
movl 0x204(%rbx), %eax
decl %eax
cmpq %rax, %rbp
setne %al
cmpl $0x0, 0x1e0(%rbx)
sete %cl
orb %al, %cl
jne 0x8b1a
movl 0x1e4(%rbx), %eax
movl %eax, 0x10(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
addl %r15d, %r13d
addl %r14d, %r13d
movl 0x1c0(%rbx), %eax
cmpl %eax, %r13d
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %r13d
incq %rbp
addq $0x28, %r12
movl %r13d, %edx
jmp 0x8a8b
movq %rbx, %rdi
movl %edx, %ebp
callq 0x8cd2
movl %ebp, %edx
movq 0x18(%rsp), %rsi
incq %rsi
movl 0x8(%rsp), %eax
movq 0x40(%rsp), %rcx
pushq $0x4
popq %r12
movl 0x34(%rsp), %edi
jmp 0x8a45
pushq $0x2
popq %rbp
jmp 0x884b
cmpl %eax, %edx
movq 0x28(%rsp), %r14
pushq $0x2
popq %rbp
jne 0x884b
movl 0x208(%rbx), %ecx
cmpl $0x0, 0x14(%rsp)
je 0x8bb6
movl 0x10(%rsp), %eax
xorl %edx, %edx
divl 0xc(%rsp)
movl %edx, 0xe4(%r14)
movl %edx, %eax
shrl $0x3, %eax
btl $0xb, %ecx
cmovbl %edx, %eax
movl %eax, 0xe0(%r14)
cmpl $0x1, 0x108(%r14)
setne %al
testb $0x20, %cl
sete %dl
orb %al, %dl
jne 0x8bd9
movq %r14, %rsi
callq 0x8d54
movl 0x208(%rbx), %ecx
movq 0x1e8(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8d76
movq %rbx, %rdi
callq 0x8670
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8472
jmp 0x884b