// File: /workspace/github_c_makefile_star2_O3/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, %rbx
cmpl $0x1, %edi
jg 0xdac9
movq (%rbx), %rsi
leaq 0x2293(%rip), %rdi      # 0xfd57
jmp 0xddef
movq 0x8(%rbx), %rdi
leaq 0x1f68(%rip), %rsi      # 0xfa3c
callq 0x1140
movq 0x8(%rbx), %rsi
testq %rax, %rax
je 0xdde8
movq %rax, %r15
movq %rsi, %rdi
callq 0x1090
leaq 0x15(%rax), %rdi
callq 0x1100
movq %rax, 0x60(%rsp)
leaq 0x80(%rsp), %r14
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1060
movl 0x4(%r14), %eax
xorl %ebp, %ebp
movl %eax, 0x48(%rsp)
cmpl $0x100, %eax            # imm = 0x100
setae %bpl
leaq 0x227c(%rip), %rax      # 0xfdb2
leaq 0x2269(%rip), %rsi      # 0xfda6
cmovaeq %rax, %rsi
leaq 0x2248(%rip), %rdi      # 0xfd90
xorl %eax, %eax
callq 0x10a0
movq %r14, %rdi
movl %ebp, %esi
callq 0xcd1f
movq %rbx, 0x78(%rsp)
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 0xce5a
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 %r12, %rsi
movl %ebp, %edx
callq 0xd11e
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 %r12, %rsi
callq 0xd655
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, 0x68(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, 0x50(%rsp)
movq %r14, %rdx
callq 0xd897
movq %rbx, 0x70(%rsp)
movl 0xc(%rbx), %eax
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0xdd38
decl %eax
movl %eax, 0x4c(%rsp)
movq 0x50(%rsp), %r13
xorl %ebp, %ebp
movl 0x14(%r13), %r14d
leaq 0x216f(%rip), %rdi      # 0xfdbe
movl %ebp, %esi
movl 0x4c(%rsp), %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10a0
movq 0x637a(%rip), %rax      # 0x13fe0
movq (%rax), %rdi
movq %rax, %rbx
callq 0x1110
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0xd9d4
movq %rax, %r15
leaq 0x214e(%rip), %rdi      # 0xfdee
xorl %eax, %eax
callq 0x10a0
movq (%rbx), %rdi
callq 0x1110
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rdx
movq %r12, %rbx
movq 0x60(%rsp), %r12
movq %r12, %rdi
leaq 0x212b(%rip), %rsi      # 0xfdf5
movl %r14d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1150
movl 0x4(%r13), %edx
movl 0x8(%r13), %ecx
movq %r12, %rdi
movq %rbx, %r12
movq %r15, %rsi
callq 0xa760
leaq 0x211a(%rip), %rdi      # 0xfe0e
xorl %eax, %eax
callq 0x10a0
movq 0x62de(%rip), %rbx      # 0x13fe0
movq (%rbx), %rdi
callq 0x1110
movq %r15, %rdi
callq 0x1030
leaq 0x2432(%rip), %rdi      # 0x1014b
callq 0x1050
movq (%rbx), %rdi
callq 0x1110
incq %rbp
addq $0x30, %r13
cmpq %rbp, 0x58(%rsp)
jne 0xdc44
movq 0x68(%rsp), %rdi
callq 0x1070
movq 0x70(%rsp), %r13
movl 0x4(%r13), %eax
testq %rax, %rax
movq 0x60(%rsp), %r15
movq 0x50(%rsp), %rbp
je 0xdd87
shlq $0x3, %rax
leaq (%rax,%rax,4), %rbx
xorl %r14d, %r14d
movq 0x18(%r12,%r14), %rdi
callq 0x1030
movq 0x20(%r12,%r14), %rdi
testq %rdi, %rdi
je 0xdd7e
callq 0x1030
addq $0x28, %r14
cmpq %r14, %rbx
jne 0xdd65
cmpl $0x100, 0x48(%rsp)      # imm = 0x100
movq 0x58(%rsp), %rax
jb 0xddb8
testl %eax, %eax
je 0xddb8
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq 0x28(%rbp,%r14), %rdi
callq 0x1030
addq $0x30, %r14
cmpq %r14, %rbx
jne 0xdda5
movq %r13, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
leaq 0x2366(%rip), %rdi      # 0x10145
callq 0x1050
xorl %eax, %eax
jmp 0xddfb
leaq 0x1f8b(%rip), %rdi      # 0xfd7a
xorl %eax, %eax
callq 0x10a0
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq