// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/txp-convert.c
// Function: getTextureInfos
textureHeader *getTextureInfos(FILE *f, txpHeader header, unsigned int game) {
	unsigned int i, j;
	textureHeaderPW *pwtex = NULL;
	textureHeaderPO *potex = NULL;
	textureHeader *rethead = malloc(header.numtextures * sizeof(textureHeader));
	fseek(f, header.texturelist, SEEK_SET);
	
	printf("Raw texture values:\n");
	printf(" index ~ wdth - unk1 - hght - unk2 - bpp  - unk3 - comp - unk4 -  offset  - palette\n");
	
	switch(game) {
		case mgspo: {
			potex = malloc(header.numtextures * sizeof(textureHeaderPO));
			fread(potex, header.numtextures * sizeof(textureHeaderPO), 1, f);
			for(i = 0; i < header.numtextures; i++) {
				printf("idx %02d ~ %04x - %04x - %04x - %04x - %04x - %04x - %04x - %04x - %08x - %08x\n", i,
					potex[i].width, potex[i].unknown1, potex[i].height, potex[i].unknown2,
					potex[i].bpp, potex[i].unknown3, potex[i].compressed, potex[i].unknown4,
					potex[i].offset, potex[i].palette);
				
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].bpp = potex[i].bpp;
				rethead[i].compressed = potex[i].compressed;
				rethead[i].width = potex[i].width;
				rethead[i].height = potex[i].height;
				
				rethead[i].offset = potex[i].offset;
				/* portableops has one per-texture palette */
				rethead[i].palette = potex[i].palette;
			}
			free(potex);
			break;
		}
		case mgspw: {
			pwtex = malloc(header.numtextures * sizeof(textureHeaderPW));
			fread(pwtex, header.numtextures * sizeof(textureHeaderPW), 1, f);
			for(i = 0; i < header.numtextures; i++) {
				printf("idx %02d ~ %04x - %04x - %04x - %04x - %04x - %04x\n", i,
					pwtex[i].bpp, pwtex[i].compressed, pwtex[i].width, pwtex[i].unknown1,
					pwtex[i].height, pwtex[i].unknown2);
				for(j = 0; i < 6; j++) printf("%02x ", pwtex[i].unknown3[j]);
				printf("\n%08x - %08x\n", pwtex[i].offset, pwtex[i].zoffset);
				
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].bpp = pwtex[i].bpp;
				rethead[i].compressed = pwtex[i].compressed;
				rethead[i].width = pwtex[i].width;
				rethead[i].height = pwtex[i].height;
				/* peacewalker has two different offsets for compressed and uncompressed */
				rethead[i].offset = pwtex[i].compressed ? pwtex[i].zoffset : pwtex[i].offset;
				/* peacewalker has per-frame palettes, not per-texture palettes */
				rethead[i].palette = 0;
			}
			free(pwtex);
			break;
		}
		default: {
			printf("i am asplod again\n");
			break;
		}
	}
	
	printf("Processed texture data:\n");
	printf(" index ~ bpp  - comp - wdth - hght -  offset  - palette\n");
	for(i = 0; i < header.numtextures; i++) printf("idx %02d ~ %04x - %04x - %04d - %04d - %08x - %08x\n", i,
				rethead[i].bpp, rethead[i].compressed, rethead[i].width, rethead[i].height,
				rethead[i].offset, rethead[i].palette);
	
	return rethead;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebp
movq %rdi, %r15
movl 0x74(%rsp), %r13d
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %rdi
callq 0x10f0
movq %rax, %r12
movl 0x78(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1110
leaq 0x323d(%rip), %rdi      # 0xfdd6
callq 0x1050
leaq 0x3245(%rip), %rdi      # 0xfdea
callq 0x1050
cmpl $0x1, %ebp
je 0xccf2
testl %ebp, %ebp
jne 0xcd8d
movq %r12, 0x10(%rsp)
movq %r13, %r12
shlq $0x4, %r12
movq %r12, %rdi
callq 0x10f0
movl $0x1, %edx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1060
testq %r13, %r13
je 0xccde
movq 0x8(%rsp), %r14
addq $0xc, %r14
movq 0x10(%rsp), %rax
leaq 0x18(%rax), %r15
xorl %eax, %eax
movq %r13, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq -0xc(%r14), %r10
movl %r10d, %ecx
movl $0xfff, %eax            # imm = 0xFFF
andl %eax, %ecx
movl %ecx, %edx
movl %ecx, 0x4(%rsp)
movl %r10d, %ecx
shrl $0xc, %ecx
andl $0xf, %ecx
movl %r10d, %r13d
shrl $0x10, %r13d
andl %eax, %r13d
movl %r10d, %r9d
shrl $0x1c, %r9d
movq %r10, %rbx
shrq $0x20, %rbx
andl $0xf, %ebx
movq %r10, %r11
shrq $0x24, %r11
andl %eax, %r11d
movq %r10, %rbp
shrq $0x30, %rbp
andl $0xf, %ebp
shrq $0x34, %r10
movl -0x4(%r14), %eax
movq %rax, 0x28(%rsp)
movl (%r14), %r12d
movq %r12, 0x30(%rsp)
leaq 0x2dd5(%rip), %rdi      # 0xfa4b
movq 0x18(%rsp), %rsi
movl %r13d, %r8d
xorl %eax, %eax
pushq %r12
movq 0x30(%rsp), %r12
pushq %r12
pushq %r10
pushq %rbp
pushq %r11
pushq %rbx
callq 0x10a0
movq 0x48(%rsp), %rax
addq $0x30, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl %ebx, -0x18(%r15)
movl %ebp, -0x14(%r15)
movl 0x4(%rsp), %ecx
movl %ecx, -0x10(%r15)
movl %r13d, -0xc(%r15)
movq 0x20(%rsp), %r13
movl %r12d, -0x8(%r15)
movq 0x30(%rsp), %rcx
movl %ecx, -0x4(%r15)
incq %rax
addq $0x10, %r14
addq $0x28, %r15
cmpq %rax, %r13
jne 0xcc09
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %r12
jmp 0xcda6
movq %r12, %rbx
leaq (,%r13,4), %rax
leaq (%rax,%rax,4), %r12
movq %r12, %rdi
callq 0x10f0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1060
testq %r13, %r13
je 0xcd9b
movl (%r14), %r9d
movl %r9d, %edx
andl $0xf, %edx
movl %r9d, %ecx
shrl $0x4, %ecx
movl $0xfff, %r10d           # imm = 0xFFF
andl %r10d, %ecx
movl %r9d, %r8d
shrl $0x10, %r8d
andl %r10d, %r8d
shrl $0x1c, %r9d
movzwl 0x4(%r14), %r11d
andl %r11d, %r10d
shrl $0xc, %r11d
leaq 0x2d3e(%rip), %rdi      # 0xfa9b
xorl %esi, %esi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x10a0
addq $0x10, %rsp
leaq 0x2d5a(%rip), %rbx      # 0xfacf
xorl %ebp, %ebp
movl %ebp, %eax
movzbl 0x6(%r14,%rax), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incl %ebp
jmp 0xcd77
leaq 0x30aa(%rip), %rdi      # 0xfe3e
callq 0x1050
jmp 0xcda6
movq %r14, %rdi
callq 0x1030
movq %rbx, %r12
leaq 0x30a3(%rip), %rdi      # 0xfe50
callq 0x1050
leaq 0x30af(%rip), %rdi      # 0xfe68
callq 0x1050
testq %r13, %r13
je 0xce0a
movq %r12, %rbx
addq $0x14, %rbx
leaq 0x2d04(%rip), %r14      # 0xfad5
xorl %r15d, %r15d
movl -0x14(%rbx), %edx
movl -0x10(%rbx), %ecx
movl -0xc(%rbx), %r8d
movl -0x8(%rbx), %r9d
movl -0x4(%rbx), %r10d
movl (%rbx), %r11d
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x10a0
addq $0x10, %rsp
incq %r15
addq $0x28, %rbx
cmpq %r15, %r13
jne 0xcdd4
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq