// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/txp-convert.c
// Function: getFrameInfos
frameHeader *getFrameInfos(FILE *f, txpHeader header, textureHeader *textures, unsigned int game) {
	unsigned int i;
	frameHeaderPW *pwframe = NULL;
	frameHeaderPO *poframe = NULL;
	frameHeader *rethead = malloc(header.numframes * sizeof(frameHeader));
	fseek(f, header.framelist, SEEK_SET);
	
	printf("Raw frame values:\n");
	
	switch(game) {
		case mgspo: {
			printf(" index ~ unknown1 -   hash   - unknown2 - wdth - hght - unknown3 - texture  - unknown4 -    fx    -    fy    -  fwidth  - fheight  - unknown5\n");
			poframe = malloc(header.numframes * sizeof(frameHeaderPO));
			fread(poframe, header.numframes * sizeof(frameHeaderPO), 1, f);
			for(i = 0; i < header.numframes; i++) {
				printf("idx %02d ~ %08x - %08x - %08x - %04x - %04x - %08x - %08x - %08x - %f - %f - %f - %f - %08x\n", i,
					poframe[i].unknown1, poframe[i].hash, poframe[i].unknown2, poframe[i].width, poframe[i].height,
					poframe[i].unknown3, poframe[i].texture, poframe[i].unknown4,
					poframe[i].fx, poframe[i].fy, poframe[i].fwidth, poframe[i].fheight,
					poframe[i].unknown5);
				
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].hash = poframe[i].hash;
				rethead[i].width = poframe[i].width;
				rethead[i].height = poframe[i].height;
				/* this is just the offset to the matching texture header */
				rethead[i].sourcetexture = (poframe[i].texture - header.texturelist) / 16;
				/* portableops doesnt store the source x and y directly, but as relative floats
				   so we need the sourcetexture first */
				rethead[i].sourcex = (unsigned int)(textures[rethead[i].sourcetexture].width * poframe[i].fx);
				rethead[i].sourcey = (unsigned int)(textures[rethead[i].sourcetexture].height * poframe[i].fy);
				
				/* portableops has one palette per texture */
				rethead[i].palette = textures[rethead[i].sourcetexture].palette;
			}
			free(poframe);
			break;
		}
		case mgspw: {
			pwframe = malloc(header.numframes * sizeof(frameHeaderPW));
			fread(pwframe, header.numframes * sizeof(frameHeaderPW), 1, f);
			for(i = 0; i < header.numframes; i++) {
				printf("idx %02d ~ %08x - %08x - %08x - %08x - %f - %f - %f - %f - %04x - %04x - %04x - %04x\n", i,
					pwframe[i].unknown1, pwframe[i].hash, pwframe[i].texture, pwframe[i].palette,
					pwframe[i].fx, pwframe[i].fy, pwframe[i].fwidth, pwframe[i].fheight,
					pwframe[i].width, pwframe[i].height, pwframe[i].x, pwframe[i].y);
			
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].hash = pwframe[i].hash;
				rethead[i].width = pwframe[i].width;
				rethead[i].height = pwframe[i].height;
				/* this is just the offset to the matching texture header */
				rethead[i].sourcetexture = (pwframe[i].texture - header.texturelist) / 20;
				/* peacewalker has an absolute source x and y stored as well as relative ones */
				rethead[i].sourcex = pwframe[i].x;
				rethead[i].sourcey = pwframe[i].y;
				
				/* peacewalker has one palette per frame */
				rethead[i].palette = pwframe[i].palette;
			}
			free(pwframe);
			break;
		}
		default: {
			printf("TRIPLE FAULT YAY\n");
			break;
		}
	}
	
	printf("Processed frame values:\n");
	printf(" index ~   hash   - wdth - hgth - tx - srcx - srcy - palette\n");
	for(i = 0; i < header.numframes; i++) printf("idx %02d ~ %08x - %04d - %04d - %02d - %04d - %04d - %08x\n", i,
				rethead[i].hash, rethead[i].width, rethead[i].height, rethead[i].sourcetexture,
				rethead[i].sourcex, rethead[i].sourcey, rethead[i].palette);
	
	return rethead;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x90(%rsp), %r15
movl 0xc(%r15), %eax
movq %rax, (%rsp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
movq %r13, %rdi
callq 0x1100
movq %rax, %r14
movl 0x10(%r15), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1120
leaq 0x2e40(%rip), %rdi      # 0xffac
callq 0x1050
cmpl $0x1, %ebp
movq %r14, 0x8(%rsp)
je 0xd32e
testl %ebp, %ebp
jne 0xd46e
movq %r15, %r14
leaq 0x2e2d(%rip), %rdi      # 0xffbe
callq 0x1050
movq %r13, %rdi
callq 0x1100
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x1060
movq (%rsp), %r13
testq %r13, %r13
je 0xd326
movq %rbx, %r10
movl 0x8(%r14), %eax
movl %eax, 0x38(%rsp)
movl $0x20, %r14d
xorl %r12d, %r12d
movl -0x20(%r15,%r14), %edx
movl -0x1c(%r15,%r14), %ecx
movl %ecx, 0x10(%rsp)
movl -0x18(%r15,%r14), %r8d
movzwl -0x14(%r15,%r14), %r9d
movl %r9d, 0x3c(%rsp)
movzwl -0x12(%r15,%r14), %eax
movq %rax, 0x30(%rsp)
movl -0x10(%r15,%r14), %eax
movq %rax, 0x20(%rsp)
movl -0xc(%r15,%r14), %r11d
movl -0x8(%r15,%r14), %eax
movq %rax, 0x18(%rsp)
xorps %xmm2, %xmm2
cvtss2sd 0x4(%r15,%r14), %xmm2
cvtss2sd 0x8(%r15,%r14), %xmm3
movl 0xc(%r15,%r14), %ebx
movl %r11d, %ebp
subl 0x38(%rsp), %ebp
shrl $0x4, %ebp
leaq (,%rbp,4), %rax
addq %rbp, %rax
movq %rax, 0x28(%rsp)
movsd -0x4(%r15,%r14), %xmm1
movaps %xmm1, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
cvtss2sd %xmm1, %xmm1
subq $0x8, %rsp
leaq 0x29a8(%rip), %rdi      # 0xfc15
movl %r12d, %esi
movq %r10, %r13
movb $0x4, %al
pushq %rbx
pushq 0x28(%rsp)
pushq %r11
pushq 0x40(%rsp)
movq 0x58(%rsp), %rbx
pushq %rbx
callq 0x10a0
movq 0x38(%rsp), %rcx
movq %r13, %r10
addq $0x30, %rsp
xorps %xmm1, %xmm1
movups %xmm1, (%rcx,%r14)
movl 0x10(%rsp), %eax
movl %eax, -0x20(%rcx,%r14)
movl 0x3c(%rsp), %eax
movl %eax, -0x1c(%rcx,%r14)
movq (%rsp), %r13
movl %ebx, -0x18(%rcx,%r14)
movl %ebp, -0xc(%rcx,%r14)
movq 0x28(%rsp), %rax
movsd 0x8(%r10,%rax,8), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps 0x27cb(%rip), %xmm1     # 0xfaa0
orps %xmm1, %xmm0
subpd %xmm1, %xmm0
cvtpd2ps %xmm0, %xmm0
mulps 0x40(%rsp), %xmm0
cvttps2dq %xmm0, %xmm1
movdqa %xmm1, %xmm2
psrad $0x1f, %xmm2
subps 0x27b7(%rip), %xmm0     # 0xfab0
cvttps2dq %xmm0, %xmm0
pand %xmm2, %xmm0
por %xmm1, %xmm0
movq %xmm0, -0x14(%rcx,%r14)
movl 0x14(%r10,%rax,8), %eax
movl %eax, -0x8(%rcx,%r14)
incq %r12
addq $0x30, %r14
cmpq %r12, %r13
jne 0xd1d5
movq %r15, %rdi
jmp 0xd467
movq (%rsp), %r13
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %r14
movq %r14, %rdi
callq 0x1100
movl $0x1, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x1060
testq %r13, %r13
je 0xd462
movl 0x8(%r15), %eax
movl %eax, 0x18(%rsp)
movq 0x10(%rsp), %r12
addq $0x26, %r12
movq 0x8(%rsp), %rax
leaq 0x20(%rax), %rbx
xorl %r15d, %r15d
movl -0x26(%r12), %edx
movl -0x22(%r12), %ecx
movl %ecx, 0x20(%rsp)
movl -0x1e(%r12), %r13d
movl -0x1a(%r12), %r9d
movl %r9d, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtss2sd -0x16(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x12(%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0xe(%r12), %xmm2
xorps %xmm3, %xmm3
cvtss2sd -0xa(%r12), %xmm3
movzwl -0x6(%r12), %r14d
movzwl -0x4(%r12), %ebp
movzwl -0x2(%r12), %r11d
movq %r11, 0x28(%rsp)
movzwl (%r12), %r10d
movq %r10, 0x30(%rsp)
leaq 0x2881(%rip), %rdi      # 0xfc72
movl %r15d, %esi
movl %r13d, %r8d
movb $0x4, %al
pushq %r10
pushq %r11
pushq %rbp
pushq %r14
callq 0x10a0
addq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movl 0x20(%rsp), %eax
movl %eax, -0x20(%rbx)
movl %r14d, -0x1c(%rbx)
movl %ebp, -0x18(%rbx)
subl 0x18(%rsp), %r13d
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rax, %r13
shrq $0x24, %r13
movl %r13d, -0xc(%rbx)
movq (%rsp), %r13
movq 0x28(%rsp), %rax
movl %eax, -0x14(%rbx)
movq 0x30(%rsp), %rax
movl %eax, -0x10(%rbx)
movl 0x40(%rsp), %eax
movl %eax, -0x8(%rbx)
incq %r15
addq $0x28, %r12
addq $0x30, %rbx
cmpq %r15, %r13
jne 0xd384
movq 0x10(%rsp), %rdi
callq 0x1030
jmp 0xd47e
leaq 0x2bd7(%rip), %rdi      # 0x1004c
callq 0x1050
movq (%rsp), %r13
leaq 0x2bd8(%rip), %rdi      # 0x1005d
callq 0x1050
leaq 0x2be4(%rip), %rdi      # 0x10075
callq 0x1050
testq %r13, %r13
je 0xd4ee
movq 0x8(%rsp), %rbx
addq $0x18, %rbx
leaq 0x281d(%rip), %r14      # 0xfcc8
xorl %r15d, %r15d
movl -0x18(%rbx), %edx
movl -0x14(%rbx), %ecx
movl -0x10(%rbx), %r8d
movl -0x4(%rbx), %r9d
movl -0xc(%rbx), %r10d
movl -0x8(%rbx), %r11d
movl (%rbx), %r12d
subq $0x8, %rsp
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
pushq %r12
pushq %r11
pushq %r10
callq 0x10a0
addq $0x20, %rsp
incq %r15
addq $0x30, %rbx
cmpq %r15, %r13
jne 0xd4ae
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq