// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x48(%rdi)
je 0xa3a5
movl 0x54(%rbx), %eax
movl 0x50(%rbx), %ecx
cmpl $0x2, %ecx
je 0xa3a9
movl $0xe, %ebp
cmpl $0x1, %ecx
jne 0xa3b6
xorl %ecx, %ecx
cmpl $0x1, %eax
jmp 0xa3b3
xorl %ebp, %ebp
jmp 0xa3b6
xorl %ecx, %ecx
cmpl $0x1, %eax
movl $0xe, %ebp
cmovel %ecx, %ebp
testl %ebp, %ebp
jne 0xa3e9
movq 0x210(%rbx), %r14
movl 0x108(%r14), %eax
addl $-0xa, %eax
movl $0xe, %ebp
cmpl $-0x9, %eax
jb 0xa3e9
cmpl $0x1, 0x10c(%r14)
jne 0xa3e9
cmpl $0x0, 0x12c(%r14)
je 0xa3fa
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8c0
movl 0x208(%rbx), %eax
testl %eax, %eax
js 0xa452
movl 0x108(%r14), %edx
leal -0x3(%rdx), %ecx
cmpl $0x6, %ecx
ja 0xa42b
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
movl %eax, 0x208(%rbx)
movl 0x1c8(%rbx), %eax
movl 0x208(%rbx), %r13d
movl %r13d, %ecx
shrl $0x1c, %ecx
andl $0x4, %ecx
incl %ecx
cmpl $0x1, %edx
jne 0xa47f
testb $0x20, %r13b
jne 0xa469
xorl %ecx, %ecx
jmp 0xa47f
movq (%rbx), %rax
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x30(%rax)
testb $0x40, %r13b
movl $0x2, %edx
cmovnel %edx, %ecx
testl %eax, %eax
movl $0x186a0, %edx          # imm = 0x186A0
cmovel %edx, %eax
btl $0x8, %r13d
movl $0x5, %ebp
cmovbl %ecx, %ebp
movq %r14, 0x40(%rsp)
testb $0x1, 0x130(%r14)
cmovel %ecx, %ebp
movl %eax, %ecx
andl $-0x10, %ecx
addl $0x10, %ecx
testb $0xf, %al
cmovel %eax, %ecx
testb $0x2, %r13b
cmovel %eax, %ecx
movl %ecx, %eax
andl $-0x8, %eax
addl $0x8, %eax
testb $0x7, %cl
cmovel %ecx, %eax
testl $0x1000, %r13d          # imm = 0x1000
cmovnel %ecx, %eax
movl %eax, 0x1c(%rsp)
movl %eax, %r12d
imull %ebp, %r12d
leal 0x7(%r12), %eax
movl %eax, %ecx
shrl $0x3, %ecx
cmpl $0x8, %eax
jae 0xa4ed
movq $0x0, 0x28(%rsp)
jmp 0xa510
movl %ecx, %r14d
movq %r14, %rdi
movl %ecx, %r15d
callq 0x4040
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4140
movl %r15d, %ecx
movl 0x1d4(%rbx), %edx
movl 0x1c(%rsp), %r15d
testl %r15d, %r15d
je 0xa527
movl %edx, %eax
xorl %edx, %edx
divl %r15d
xorl %eax, %eax
testb $0x1, %r13b
cmovnel %eax, %edx
movl %edx, 0x14(%rsp)
movq 0x40(%rsp), %r14
movl %ebp, 0x28(%r14)
movq 0x28(%rsp), %r13
movq %r13, 0x48(%r14)
movl %ecx, 0x50(%r14)
movl %edx, %eax
shrl $0x3, %eax
movl %eax, 0xc0(%r14)
movq $-0x1, 0xe0(%r14)
movl %r12d, 0xe8(%r14)
movl %r15d, 0xec(%r14)
movl %edx, 0xf0(%r14)
movl $0x87654321, 0xc4(%r14) # imm = 0x87654321
movq %r14, %rdi
callq 0xf2bc
movq %r13, 0x1b8(%rbx)
movl %r12d, 0x1c0(%rbx)
movl %r15d, 0x1c4(%rbx)
leaq 0xb8(%rbx), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x4994
testl %ebp, %ebp
setg %al
jle 0xa75c
movb %al, 0xf(%rsp)
movl 0x1c8(%rbx), %eax
movl %r15d, %ecx
movl %eax, 0x3c(%rsp)
subl %eax, %ecx
movl %ecx, 0x38(%rsp)
movl %ebp, %eax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl %ebp, %eax
shrl $0x3, %eax
movq 0x28(%rsp), %rcx
addq %rax, %rcx
movq %rcx, 0x58(%r14,%r12,8)
movl %eax, 0x94(%r14,%r12,4)
addl %r15d, %ebp
movl %ebp, 0x34(%rsp)
movl %ebp, %ecx
shrl $0x3, %ecx
subl %eax, %ecx
movq %r12, 0x48(%rsp)
movl %ecx, 0x80(%r14,%r12,4)
movl 0x204(%rbx), %eax
testl %eax, %eax
setg %r15b
jle 0xa711
movl $0x4, %r13d
xorl %r12d, %r12d
movq 0x1f8(%rbx), %rcx
movl -0x4(%rcx,%r13), %ebp
movl (%rcx,%r13), %r14d
decl %eax
cmpq %rax, %r12
jne 0xa67d
testl %r14d, %r14d
jne 0xa678
movl 0x3c(%rsp), %eax
cmpl %eax, 0x1c(%rsp)
je 0xa678
xorl %ecx, %ecx
movl $0x2, 0x24(%rsp)
jmp 0xa6f2
addl 0x38(%rsp), %r14d
movq %rbx, %rdi
movl %r12d, %esi
movl 0x10(%rsp), %edx
movl %ebp, %ecx
movl %r14d, %r8d
callq 0xa81c
testl %eax, %eax
je 0xa69d
xorl %ecx, %ecx
movl %eax, 0x24(%rsp)
jmp 0xa6f2
cmpq $0x0, 0x48(%rsp)
jne 0xa6d3
movl 0x204(%rbx), %eax
decl %eax
cmpq %rax, %r12
setne %al
cmpl $0x0, 0x1e0(%rbx)
sete %cl
orb %al, %cl
jne 0xa6d3
movl $0x1, 0x20(%rsp)
movl 0x1e4(%rbx), %eax
movl %eax, 0x18(%rsp)
addl 0x10(%rsp), %ebp
addl %r14d, %ebp
movl 0x1c0(%rbx), %eax
cmpl %eax, %ebp
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %ebp
movb $0x1, %cl
movl %ebp, 0x10(%rsp)
testb %cl, %cl
je 0xa711
incq %r12
movslq 0x204(%rbx), %rax
addq $0x28, %r13
cmpq %rax, %r12
setl %r15b
jl 0xa646
testb $0x1, %r15b
movq 0x40(%rsp), %r14
movl 0x1c(%rsp), %r15d
movq 0x48(%rsp), %r12
movl 0x34(%rsp), %ebp
jne 0xa745
movq %rbx, %rdi
callq 0xa910
incq %r12
cmpq 0x50(%rsp), %r12
setb 0xf(%rsp)
jne 0xa5f6
cmpl $0x0, 0x20(%rsp)
sete %cl
movl 0x14(%rsp), %r13d
movl 0x24(%rsp), %ebp
movb 0xf(%rsp), %al
jmp 0xa770
movl $0x0, 0x18(%rsp)
movb $0x1, %cl
movl 0x14(%rsp), %r13d
movl %r13d, 0x10(%rsp)
testb $0x1, %al
movl 0x18(%rsp), %eax
jne 0xa3e9
movl $0x2, %ebp
cmpl %r13d, 0x10(%rsp)
jne 0xa3e9
testb %cl, %cl
jne 0xa7b3
xorl %edx, %edx
divl %r15d
movl %edx, 0xe4(%r14)
movl %edx, %eax
shrl $0x3, %eax
btl $0xb, 0x208(%rbx)
cmovbl %edx, %eax
movl %eax, 0xe0(%r14)
cmpl $0x1, 0x108(%r14)
jne 0xa7ea
testb $0x20, 0x208(%rbx)
je 0xa7ea
cmpl $0x0, 0x50(%r14)
jle 0xa7ea
movl $0x10030f01, %eax       # imm = 0x10030F01
xorl %ecx, %ecx
movq 0x48(%r14), %rdx
movb %al, (%rdx,%rcx)
roll $0x8, %eax
incq %rcx
movslq 0x50(%r14), %rdx
cmpq %rdx, %rcx
jl 0xa7d4
movq 0x1e8(%rbx), %rdx
movl 0x208(%rbx), %ecx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa9b2
movq %rbx, %rdi
callq 0xa1bc
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9f94
jmp 0xa3e9
nop