// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/txp-convert.c
// Function: main
int main( int argc, char **argv ) {
	
	unsigned char headerdata[32], *readyframe = NULL;
	uint32_t *gamecheck = NULL;
	
	unsigned int i, game;
	
	txpHeader *header = NULL;
	textureHeader *textures = NULL;
	frameHeader *frames = NULL;
	
	char *outputname = NULL;
	
	FILE *f;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s TXP-file\n", argv[0]);
		return 1;
	}
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	
	outputname = malloc( strlen( argv[1] ) + 7 + 9 + 4 + 1 ); // will contain: infilename + "-tex###" + "-frame###" + ".png" + NULL
	
	fread( headerdata, 32, 1, f );
	gamecheck = (uint32_t *)(headerdata+4);
	
	game = (*gamecheck < 256) ? mgspo : mgspw;
	printf("TXP is %s compatible\n", (*gamecheck < 256) ? "portableops" : "peacewalker");
	
	header = getTxpHeader(headerdata, game);
	textures = getTextureInfos(f, *header, game);
	frames = getFrameInfos(f, *header, textures, game);
	
	getTexturePixels(f, *header, textures);
	getFramePixels(f, *header, textures, frames);
	
	for(i = 0; i < header->numframes; i++) {
	//~ for(i = 0; i < 1; i++) {
		printf("Processing frame %02d of %02d (texture %02d)...", i, header->numframes-1, frames[i].sourcetexture);
		fflush(stdout);
		readyframe = buildSingleFrameRgba(textures, frames[i]);
		printf("built ");
		fflush(stdout);
		sprintf(outputname, "%s-tex%03d-frame%03d.png", argv[1], frames[i].sourcetexture, i);
		lodepng_encode32_file(outputname, readyframe, frames[i].width, frames[i].height);
		printf("encoded ");
		fflush(stdout);
		free(readyframe);
		printf("managed\n");
		fflush(stdout);
	}
	
	fclose(f);
	
	for(i = 0; i < header->numtextures; i++) {
		free(textures[i].texpixels);
		if(textures[i].palpixels) free(textures[i].palpixels);
	}
	if(game == mgspw) for(i = 0; i < header->numframes; i++) free(frames[i].palpixels);
	
	free(header);
	free(textures);
	free(frames);
	free(outputname);
	
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r13
cmpl $0x1, %edi
jg 0xab12
movq (%r13), %rsi
leaq 0x216a(%rip), %rdi      # 0xcc77
jmp 0xae1c
movq 0x8(%r13), %rdi
leaq 0x1e3f(%rip), %rsi      # 0xc95c
callq 0x1130
movq 0x8(%r13), %rsi
testq %rax, %rax
je 0xae15
movq %rax, %r15
movq %rsi, %rdi
callq 0x1090
leaq 0x15(%rax), %rdi
callq 0x10f0
movq %rax, 0x58(%rsp)
leaq 0x80(%rsp), %r14
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1060
movl 0x4(%r14), %eax
xorl %ebp, %ebp
movl %eax, 0x50(%rsp)
cmpl $0x100, %eax            # imm = 0x100
setae %bpl
leaq 0x2157(%rip), %rax      # 0xccd2
leaq 0x2144(%rip), %rsi      # 0xccc6
cmovaeq %rax, %rsi
leaq 0x2123(%rip), %rdi      # 0xccb0
xorl %eax, %eax
callq 0x10a0
movq %r14, %rdi
movl %ebp, %esi
callq 0x9e17
movq %rax, %rbx
movl 0x10(%rax), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq 0x9f57
movq %rax, %r14
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xa1f1
movq %rax, %r12
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xa6ca
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, 0x70(%rsp)
movq %r15, %rdi
movq %r14, 0x48(%rsp)
movq %r14, %rsi
movq %r12, %rdx
callq 0xa906
movq %rbx, 0x78(%rsp)
movl 0xc(%rbx), %eax
movq %rax, 0x60(%rsp)
decl %eax
movl %eax, 0x54(%rsp)
movq %r12, 0x68(%rsp)
xorl %ebp, %ebp
cmpq %rbp, 0x60(%rsp)
je 0xad76
movl 0x14(%r12), %r15d
leaq 0x204c(%rip), %rdi      # 0xccde
movl %ebp, %esi
movl 0x54(%rsp), %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10a0
movq 0x6337(%rip), %rax      # 0x10fe0
movq (%rax), %rdi
movq %rax, %rbx
callq 0x1100
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq 0x48(%rsp), %rdi
callq 0xaa39
movq %rax, %r14
leaq 0x2027(%rip), %rdi      # 0xcd0e
xorl %eax, %eax
callq 0x10a0
movq (%rbx), %rdi
callq 0x1100
movq 0x8(%r13), %rdx
movq %r13, %rbx
movq 0x58(%rsp), %r13
movq %r13, %rdi
leaq 0x2009(%rip), %rsi      # 0xcd15
movl %r15d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1140
movl 0x4(%r12), %edx
movl 0x8(%r12), %ecx
movq %r13, %rdi
movq %rbx, %r13
movq %r14, %rsi
callq 0x8048
leaq 0x1ff6(%rip), %rdi      # 0xcd2e
xorl %eax, %eax
callq 0x10a0
movq 0x629a(%rip), %rbx      # 0x10fe0
movq (%rbx), %rdi
callq 0x1100
movq %r14, %rdi
callq 0x1030
leaq 0x230e(%rip), %rdi      # 0xd06b
callq 0x1050
movq (%rbx), %rdi
callq 0x1100
incq %rbp
addq $0x30, %r12
jmp 0xac7b
movq 0x70(%rsp), %rdi
callq 0x1070
movq 0x78(%rsp), %r15
movl 0x4(%r15), %eax
imulq $0x28, %rax, %rbx
xorl %r14d, %r14d
movq 0x48(%rsp), %r12
cmpq %r14, %rbx
je 0xadb4
movq 0x18(%r12,%r14), %rdi
callq 0x1030
movq 0x20(%r12,%r14), %rdi
callq 0x1030
addq $0x28, %r14
jmp 0xad95
cmpl $0x100, 0x50(%rsp)      # imm = 0x100
movq 0x68(%rsp), %r12
jb 0xade1
imulq $0x30, 0x60(%rsp), %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0xade1
movq 0x28(%r12,%r14), %rdi
callq 0x1030
addq $0x30, %r14
jmp 0xadcc
movq %r15, %rdi
callq 0x1030
movq 0x48(%rsp), %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq 0x58(%rsp), %rdi
callq 0x1030
leaq 0x2259(%rip), %rdi      # 0xd065
callq 0x1050
xorl %eax, %eax
jmp 0xae26
leaq 0x1e7e(%rip), %rdi      # 0xcc9a
xorl %eax, %eax
callq 0x10a0
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq