// File: /workspace/github_c_makefile_star2_O2/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
imulq $0x28, %r13, %rdi
callq 0x10f0
movq %rax, (%rsp)
movl 0x78(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1110
leaq 0x2e71(%rip), %rdi      # 0xce02
callq 0x1050
leaq 0x2e79(%rip), %rdi      # 0xce16
callq 0x1050
cmpl $0x1, %ebp
je 0xa0da
testl %ebp, %ebp
jne 0xa168
movq %r13, %r12
shlq $0x4, %r12
movq %r12, %rdi
callq 0x10f0
movq %rax, %rbx
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1060
movq %rbx, 0x18(%rsp)
movq %rbx, %rcx
addq $0xc, %rcx
movq (%rsp), %rax
leaq 0x18(%rax), %r15
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
cmpq %r14, %r13
je 0xa0d0
movq -0xc(%rcx), %r10
movl %r10d, %edx
movl $0xfff, %eax            # imm = 0xFFF
andl %eax, %edx
movl %edx, 0x14(%rsp)
movq %rcx, 0x8(%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, %r12
shrq $0x30, %r12
andl $0xf, %r12d
shrq $0x34, %r10
movq 0x8(%rsp), %rax
movl -0x4(%rax), %eax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl (%rax), %ebp
movq %rbp, 0x30(%rsp)
leaq 0x2a08(%rip), %rdi      # 0xca77
movl %r14d, %esi
movl %r13d, %r8d
xorl %eax, %eax
pushq %rbp
movq 0x30(%rsp), %rbp
pushq %rbp
pushq %r10
pushq %r12
pushq %r11
pushq %rbx
callq 0x10a0
movq 0x38(%rsp), %rcx
addq $0x30, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl %ebx, -0x18(%r15)
movl %r12d, -0x14(%r15)
movl 0x14(%rsp), %eax
movl %eax, -0x10(%r15)
movl %r13d, -0xc(%r15)
movq 0x20(%rsp), %r13
movl %ebp, -0x8(%r15)
movq 0x30(%rsp), %rax
movl %eax, -0x4(%r15)
incq %r14
addq $0x10, %rcx
addq $0x28, %r15
jmp 0x9ff2
movq 0x18(%rsp), %rdi
jmp 0xa179
imulq $0x14, %r13, %r12
movq %r12, %rdi
callq 0x10f0
movq %rax, %r14
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1060
testq %r13, %r13
je 0xa176
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 0x298f(%rip), %rdi      # 0xcac7
xorl %esi, %esi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x10a0
addq $0x10, %rsp
leaq 0x29ab(%rip), %rbx      # 0xcafb
xorl %ebp, %ebp
movl %ebp, %eax
movzbl 0x6(%r14,%rax), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incl %ebp
jmp 0xa152
leaq 0x2cfb(%rip), %rdi      # 0xce6a
callq 0x1050
jmp 0xa17e
movq %r14, %rdi
callq 0x1030
leaq 0x2cf7(%rip), %rdi      # 0xce7c
callq 0x1050
leaq 0x2d03(%rip), %rdi      # 0xce94
callq 0x1050
movq (%rsp), %rbx
addq $0x14, %rbx
leaq 0x295c(%rip), %r14      # 0xcb01
xorl %r15d, %r15d
cmpq %r15, %r13
je 0xa1de
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
popq %rcx
popq %rdx
incq %r15
addq $0x28, %rbx
jmp 0xa1a8
movq (%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq