// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdi, %r12
leaq 0x90(%rsp), %rbx
movl 0xc(%rbx), %eax
movq %rax, 0x8(%rsp)
imulq $0x30, %rax, %r13
movq %r13, %rdi
callq 0x10f0
movq %rax, 0x10(%rsp)
movl 0x10(%rbx), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1110
leaq 0x2c8e(%rip), %rdi      # 0xcecc
callq 0x1050
cmpl $0x1, %ebp
je 0xa3d2
testl %ebp, %ebp
jne 0xa4f6
leaq 0x2c83(%rip), %rdi      # 0xcede
callq 0x1050
movq %r13, %rdi
callq 0x10f0
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x1060
movl 0x8(%rbx), %eax
movl %eax, 0x3c(%rsp)
pushq $0x20
popq %r14
xorl %esi, %esi
movq 0x8(%rsp), %rbp
cmpq %rsi, %rbp
je 0xa3ca
movl -0x20(%r15,%r14), %edx
movl -0x1c(%r15,%r14), %ecx
movl %ecx, 0x4(%rsp)
movl -0x18(%r15,%r14), %r8d
movzwl -0x14(%r15,%r14), %r12d
movzwl -0x12(%r15,%r14), %r13d
movl -0x10(%r15,%r14), %eax
movq %rax, 0x28(%rsp)
movl -0xc(%r15,%r14), %r11d
movl -0x8(%r15,%r14), %eax
movq %rax, 0x20(%rsp)
cvtss2sd 0x4(%r15,%r14), %xmm2
cvtss2sd 0x8(%r15,%r14), %xmm3
movl 0xc(%r15,%r14), %r10d
movl %r11d, %ebx
subl 0x3c(%rsp), %ebx
shrl $0x4, %ebx
imulq $0x28, %rbx, %rax
movq %rax, 0x30(%rsp)
movsd -0x4(%r15,%r14), %xmm1
movaps %xmm1, 0x40(%rsp)
cvtss2sd %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
cvtss2sd %xmm1, %xmm1
subq $0x8, %rsp
leaq 0x281d(%rip), %rdi      # 0xcb35
movq %rsi, %rbp
movl %r12d, %r9d
movb $0x4, %al
pushq %r10
pushq 0x30(%rsp)
pushq %r11
pushq 0x48(%rsp)
pushq %r13
callq 0x10a0
movq %rbp, %rsi
addq $0x30, %rsp
movq 0x10(%rsp), %rcx
xorps %xmm1, %xmm1
movups %xmm1, (%rcx,%r14)
movl 0x4(%rsp), %eax
movl %eax, -0x20(%rcx,%r14)
movq 0x8(%rsp), %rbp
movl %r12d, -0x1c(%rcx,%r14)
movl %r13d, -0x18(%rcx,%r14)
movl %ebx, -0xc(%rcx,%r14)
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rdx
movsd 0x8(%rax,%rdx), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps 0x2642(%rip), %xmm1     # 0xc9c0
orps %xmm1, %xmm0
subpd %xmm1, %xmm0
cvtpd2ps %xmm0, %xmm0
mulps 0x40(%rsp), %xmm0
cvttps2dq %xmm0, %xmm1
movdqa %xmm1, %xmm2
psrad $0x1f, %xmm2
subps 0x262e(%rip), %xmm0     # 0xc9d0
cvttps2dq %xmm0, %xmm0
pand %xmm2, %xmm0
por %xmm1, %xmm0
movq %xmm0, -0x14(%rcx,%r14)
movl 0x14(%rax,%rdx), %eax
movl %eax, -0x8(%rcx,%r14)
incq %rsi
addq $0x30, %r14
jmp 0xa28e
movq %r15, %rdi
jmp 0xa4ef
movq 0x8(%rsp), %rbp
imulq $0x28, %rbp, %r14
movq %r14, %rdi
callq 0x10f0
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x1060
movl 0x8(%rbx), %eax
movl %eax, 0x4(%rsp)
movq %r15, 0x18(%rsp)
movq %r15, %r12
addq $0x26, %r12
movq 0x10(%rsp), %rax
leaq 0x20(%rax), %rbx
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0xa4ea
movl -0x26(%r12), %edx
movl -0x22(%r12), %ecx
movl %ecx, 0x28(%rsp)
movl -0x1e(%r12), %r13d
movl -0x1a(%r12), %r9d
movl %r9d, 0x40(%rsp)
cvtss2sd -0x16(%r12), %xmm0
cvtss2sd -0x12(%r12), %xmm1
cvtss2sd -0xe(%r12), %xmm2
cvtss2sd -0xa(%r12), %xmm3
movzwl -0x6(%r12), %ebp
movzwl -0x4(%r12), %r15d
movzwl -0x2(%r12), %r11d
movq %r11, 0x20(%rsp)
movzwl (%r12), %r10d
movq %r10, 0x30(%rsp)
leaq 0x2712(%rip), %rdi      # 0xcb92
movl %r14d, %esi
movl %r13d, %r8d
movb $0x4, %al
pushq %r10
pushq %r11
pushq %r15
pushq %rbp
callq 0x10a0
addq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movl 0x28(%rsp), %eax
movl %eax, -0x20(%rbx)
movl %ebp, -0x1c(%rbx)
movq 0x8(%rsp), %rbp
movl %r15d, -0x18(%rbx)
subl 0x4(%rsp), %r13d
movl %r13d, %eax
xorl %edx, %edx
pushq $0x14
popq %rcx
divl %ecx
movl %eax, -0xc(%rbx)
movq 0x20(%rsp), %rax
movl %eax, -0x14(%rbx)
movq 0x30(%rsp), %rax
movl %eax, -0x10(%rbx)
movl 0x40(%rsp), %eax
movl %eax, -0x8(%rbx)
incq %r14
addq $0x28, %r12
addq $0x30, %rbx
jmp 0xa416
movq 0x18(%rsp), %rdi
callq 0x1030
jmp 0xa507
leaq 0x2a6f(%rip), %rdi      # 0xcf6c
callq 0x1050
movq 0x8(%rsp), %rbp
leaq 0x2a6f(%rip), %rdi      # 0xcf7d
callq 0x1050
leaq 0x2a7b(%rip), %rdi      # 0xcf95
callq 0x1050
movq 0x10(%rsp), %rbx
addq $0x18, %rbx
leaq 0x26b9(%rip), %r14      # 0xcbe8
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0xa574
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
jmp 0xa532
movq 0x10(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq