// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x78(%rbp)
movl 0x50(%rdi), %esi
movl 0x54(%rdi), %edx
callq 0xebb0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0xf486
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfa6d
movq -0x78(%rbp), %rax
movq 0x210(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x108(%rax)
jbe 0xf4af
movq -0x20(%rbp), %rax
cmpl $0xa, 0x108(%rax)
jb 0xf4bb
movl $0xe, -0x4(%rbp)
jmp 0xfa6d
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10c(%rax)
jbe 0xf4d5
movq -0x20(%rbp), %rax
cmpl $0x2, 0x10c(%rax)
jb 0xf4e1
movl $0xe, -0x4(%rbp)
jmp 0xfa6d
movq -0x20(%rbp), %rax
cmpl $0x0, 0x12c(%rax)
je 0xf4fa
movl $0xe, -0x4(%rbp)
jmp 0xfa6d
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x17de0
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
movabsq $0x80000000, %rcx       # imm = 0x80000000
andq %rcx, %rax
cmpq $0x0, %rax
je 0xf537
movq -0x78(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
movl %eax, -0x4(%rbp)
jmp 0xfa6d
movq -0x20(%rbp), %rax
movl 0x108(%rax), %eax
addl $-0x3, %eax
subl $0x6, %eax
ja 0xf55f
jmp 0xf54b
movq -0x78(%rbp), %rax
movl 0x208(%rax), %ecx
andq $-0x2, %rcx
movl %ecx, 0x208(%rax)
movq -0x78(%rbp), %rax
movl 0x1c8(%rax), %ecx
movl %ecx, -0x24(%rbp)
movl 0x208(%rax), %eax
movl %eax, %edx
andq $0x40000000, %rdx       # imm = 0x40000000
movl $0x1, %eax
movl $0x5, %ecx
cmpq $0x0, %rdx
cmovnel %ecx, %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x1, 0x108(%rax)
jne 0xf5e5
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x40, %rax
cmpq $0x0, %rax
je 0xf5b7
movl $0x2, -0x28(%rbp)
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x20, %rax
cmpq $0x0, %rax
jne 0xf5d4
movl $0x0, -0x28(%rbp)
jmp 0xf5e3
cmpl $0x0, -0x24(%rbp)
jne 0xf5e1
movl $0x186a0, -0x24(%rbp)   # imm = 0x186A0
jmp 0xf5e3
jmp 0xf5e5
movq -0x20(%rbp), %rax
movl 0x130(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
je 0xf616
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x100, %rax            # imm = 0x100
cmpq $0x0, %rax
jne 0xf616
movl $0x5, -0x28(%rbp)
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xf64a
movl -0x24(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0xf648
movl -0x24(%rbp), %ecx
andl $0xf, %ecx
movl $0x10, %eax
subl %ecx, %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0xf64a
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x1000, %rax           # imm = 0x1000
cmpq $0x0, %rax
jne 0xf680
movl -0x24(%rbp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
je 0xf67e
movl -0x24(%rbp), %ecx
andl $0x7, %ecx
movl $0x8, %eax
subl %ecx, %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0xf680
movl -0x24(%rbp), %eax
imull -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x5510
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0xf6b9
movslq -0x30(%rbp), %rdi
callq 0x4040
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x30(%rbp), %rdx
xorl %esi, %esi
callq 0x4140
jmp 0xf6c1
movq $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rax
movl 0x1d4(%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0xf6df
movl -0x3c(%rbp), %eax
xorl %edx, %edx
divl -0x24(%rbp)
movl %edx, -0x3c(%rbp)
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
je 0xf6fa
movl $0x0, -0x3c(%rbp)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x50(%rax)
movl -0x3c(%rbp), %ecx
shrl $0x3, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc0(%rax)
movq -0x20(%rbp), %rax
movl $0xffffffff, 0xe0(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movl $0xffffffff, 0xe4(%rax) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xe8(%rax)
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xec(%rax)
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xf0(%rax)
movq -0x20(%rbp), %rax
movl $0x87654321, 0xc4(%rax) # imm = 0x87654321
movq -0x20(%rbp), %rdi
callq 0x18e30
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, 0x1b8(%rax)
movl -0x2c(%rbp), %ecx
movl %ecx, 0x1c0(%rax)
movl -0x24(%rbp), %ecx
movl %ecx, 0x1c4(%rax)
movq %rax, %rdi
addq $0xb8, %rdi
movq 0x1b8(%rax), %rsi
movl 0x1c0(%rax), %edx
callq 0x4ce0
movq -0x78(%rbp), %rcx
movl -0x24(%rbp), %eax
subl 0x1c8(%rcx), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0xf98f
movl -0x4c(%rbp), %eax
shrl $0x3, %eax
movl %eax, -0x58(%rbp)
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %eax
addq %rax, %rdx
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq %rdx, 0x58(%rax,%rcx,8)
movl -0x58(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, 0x94(%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
shrl $0x3, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %edx
subl -0x58(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, 0x80(%rax,%rcx,4)
movl $0x0, -0x60(%rbp)
movq -0x78(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x204(%rcx), %eax
jge 0xf978
movq -0x78(%rbp), %rcx
movq 0x1f8(%rcx), %rax
movslq -0x60(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movq 0x1f8(%rcx), %rax
movslq -0x60(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x60(%rbp), %eax
movl 0x204(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xf8d6
cmpl $0x0, -0x68(%rbp)
jne 0xf8cd
cmpl $0x0, -0x40(%rbp)
je 0xf8cd
movl $0x2, -0x4(%rbp)
jmp 0xfa6d
movl -0x40(%rbp), %eax
addl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x78(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x50(%rbp), %edx
movl -0x64(%rbp), %ecx
movl -0x68(%rbp), %r8d
callq 0xfa80
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0xf900
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfa6d
cmpl $0x0, -0x54(%rbp)
jne 0xf93b
movq -0x78(%rbp), %rcx
movl -0x60(%rbp), %eax
movl 0x204(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xf93b
movq -0x78(%rbp), %rax
cmpl $0x0, 0x1e0(%rax)
je 0xf93b
movq -0x78(%rbp), %rax
movl $0x1, -0x44(%rbp)
movl 0x1e4(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x78(%rbp), %rcx
movl -0x64(%rbp), %eax
addl -0x68(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl 0x1c0(%rcx), %eax
jb 0xf968
movq -0x78(%rbp), %rax
movl 0x1c0(%rax), %ecx
movl -0x50(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x50(%rbp)
jmp 0xf96a
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xf85f
movq -0x78(%rbp), %rdi
callq 0xfc60
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xf7f3
movl -0x50(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0xf9a3
movl $0x2, -0x4(%rbp)
jmp 0xfa6d
cmpl $0x0, -0x44(%rbp)
je 0xf9f8
movl -0x48(%rbp), %eax
xorl %edx, %edx
divl -0x24(%rbp)
movq -0x78(%rbp), %rax
movl %edx, -0x6c(%rbp)
movl -0x6c(%rbp), %edx
movq -0x20(%rbp), %rcx
movl %edx, 0xe4(%rcx)
movl 0x208(%rax), %eax
andq $0x800, %rax            # imm = 0x800
cmpq $0x0, %rax
je 0xf9e6
movl -0x6c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xe0(%rax)
jmp 0xf9f6
movl -0x6c(%rbp), %ecx
shrl $0x3, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xe0(%rax)
jmp 0xf9f8
movq -0x20(%rbp), %rax
cmpl $0x1, 0x108(%rax)
jne 0xfa26
movq -0x78(%rbp), %rax
movl 0x208(%rax), %eax
andq $0x20, %rax
cmpq $0x0, %rax
je 0xfa26
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xfd30
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq 0x1e8(%rdi), %rdx
movl 0x208(%rdi), %ecx
callq 0xfd90
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
je 0xfa50
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfa6d
movq -0x78(%rbp), %rdi
callq 0xf1a0
movq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0xee20
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)