// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/tex-to_image.c
// Function: main
int main( int argc, char **argv ) {
	textureHeader header;
	unsigned char *paletteData, *pixelData, *rgbaPixelData;
	
	FILE *f, *o;
	unsigned int i, fileSize, paletteSize, textureSize, textureSizeAligned, pixelDataSize, numPaletteEntries, bpp, widthAligned = 0, mode;
	unsigned int curPal = 0;
	
	if( argc < 3 ) {
		printf("Not enough args!\nUse: %s TEX-file mode\nuse mode 0 for rgba, mode 1 for png\n", argv[0]);
		return 1;
	}
	
	mode = strtoul(argv[2], NULL, 10);
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	
	char *outputname = malloc( strlen( argv[1] ) + 9 + 7 + 5 + 1 ); // will contain: infilename + "-frame###" + "-pal###" + ".png" + NULL or ".rgba"
	
	fseek( f, 0, SEEK_END );
	fileSize = ftell(f);
	fseek( f, 0, SEEK_SET );
	
	fread( &header, sizeof(textureHeader), 1, f );
	
	textureAttributes attributes[header.textureNumFrames];
	paletteHeader palettes[header.textureNumPalettes];
	unsigned int paletteStats[header.textureNumFrames];
	
	for( i = 0; i < header.textureNumFrames; i++ ) {
		fread( &attributes[i], sizeof(textureAttributes), 1, f);
		paletteStats[i] = 0;
	}
	
	fseek( f, header.paletteTableOffset, SEEK_SET );
	for( i = 0; i < header.textureNumPalettes; i++ ) {
		fread( &palettes[i], sizeof(paletteHeader), 1, f );
		paletteStats[(palettes[i].frameIndexMult/0x10)-1] += 1;
	}
	
	printf("Number of frames in texture: %d\n", header.textureNumFrames);
	printf("Number of palettes in texture: %d\n", header.textureNumPalettes);
	
	
	for( i = 0; i < header.textureNumFrames; i++ ) {
		printf( "Processing frame %d\n", i );
		
		
		if( attributes[i].textureWidth % 8 ) widthAligned = attributes[i].textureWidth + (8-(attributes[i].textureWidth%8));
		else widthAligned = attributes[i].textureWidth;
		textureSize = attributes[i].textureWidth * attributes[i].textureHeight;
		textureSizeAligned = widthAligned * attributes[i].textureHeight;
		
		if( header.textureNumPalettes == 1 ) numPaletteEntries = (fileSize - palettes[0].paletteOffset) / 4;
		else numPaletteEntries = (palettes[curPal+1].paletteOffset - palettes[curPal].paletteOffset) / 4;
		
		if( numPaletteEntries == 16 ) {
			bpp = palMode4bpp;
			printf("Frame mode is 4bpp.\n");
		}
		else if( numPaletteEntries == 256 ) {
			bpp = palMode8bpp;
			printf("Frame mode is 8bpp.\n");
		}
		
		paletteEntry *framePalette = malloc(2*numPaletteEntries*sizeof(paletteEntry));
		paletteSize = numPaletteEntries*4;
		paletteData = malloc(paletteSize);
		
		if(bpp == palMode4bpp) pixelDataSize = textureSizeAligned/2;
		else if(bpp == palMode8bpp) pixelDataSize = textureSizeAligned;
		else return 1;
		
		printf("Frame width: %d - Aligned width: %d - Frame height: %d\n", attributes[i].textureWidth, widthAligned, attributes[i].textureHeight);
		printf("Number of palettes found for frame: %d\n", paletteStats[i]);
		
		pixelData = malloc(pixelDataSize);
		rgbaPixelData = malloc(textureSize * 4);
		
		fseek( f, attributes[i].texturePixelOffset, SEEK_SET );
		
		fread( pixelData, pixelDataSize, 1, f );
		
		while(paletteStats[i]) {
			fseek(f, palettes[curPal].paletteOffset, SEEK_SET);
			fread(paletteData, paletteSize, 1, f);
			generatePalettePairs( numPaletteEntries, paletteData, framePalette, bpp );
			
			texToRgba( attributes[i], framePalette, pixelData, rgbaPixelData, bpp, pixelDataSize, widthAligned );
			
			if(mode) {
				sprintf( outputname, "%s-frame%03d-pal%03d.png", argv[1], i, curPal );
				
				lodepng_encode32_file( outputname, rgbaPixelData, attributes[i].textureWidth, attributes[i].textureHeight );
			}
			else {
				sprintf( outputname, "%s-frame%03d-pal%03d.rgba", argv[1], i, curPal );
				
				if( !(o = fopen( outputname, "wb" ))) {
					printf("Couldnt open file %s\n", outputname);
					return 1;
				}
				fwrite( rgbaPixelData, textureSize * 4, 1, o );
				fclose(o);
			}
			curPal++;
			paletteStats[i] -= 1;
		}
		free(rgbaPixelData);
		free(paletteData);
		free(framePalette);
		free(pixelData);
	}
	
	fclose(f);
	free( outputname );
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x11efe
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x25a2(%rip), %rdi      # 0x1448d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x125ea
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x1100
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2532(%rip), %rsi      # 0x14458
callq 0x10f0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x11f57
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2595(%rip), %rdi      # 0x144d9
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x125ea
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1060
movq %rax, %rdi
addq $0x16, %rdi
callq 0x10c0
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, -0xcc(%rbp)
movl %eax, %esi
movq %rsi, -0xd8(%rbp)
movl $0x2, %edx
callq 0x10d0
movq -0x40(%rbp), %rdi
callq 0x10a0
movq -0xd8(%rbp), %rsi
movl -0xcc(%rbp), %edx
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x10d0
movq -0x40(%rbp), %rcx
leaq -0x20(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1040
movzwl -0x20(%rbp), %ecx
movl %ecx, %eax
movq %rsp, %rdx
movq %rdx, -0x88(%rbp)
shll $0x4, %ecx
movl %ecx, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movl -0x18(%rbp), %eax
movq %rax, %rdx
shlq $0x5, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x98(%rbp)
movzwl -0x20(%rbp), %edx
movl %edx, %eax
movl %edx, %ecx
leal 0xf(,%rcx,4), %ecx
andl $-0x10, %ecx
movl %ecx, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0xa0(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movzwl -0x20(%rbp), %ecx
cmpl %ecx, %eax
jae 0x12096
movq -0xc8(%rbp), %rdi
movl -0x4c(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x1040
movq -0xb8(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1204b
movq -0x40(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x12110
movq -0xc0(%rbp), %rdi
movl -0x4c(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rcx
movl $0x20, %esi
movl $0x1, %edx
callq 0x1040
movq -0xc0(%rbp), %rcx
movq -0xb8(%rbp), %rax
movl -0x4c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl $0x4, %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x120ad
movzwl -0x20(%rbp), %esi
leaq 0x23d4(%rip), %rdi      # 0x144ef
movb $0x0, %al
callq 0x1070
movl -0x18(%rbp), %esi
leaq 0x23e4(%rip), %rdi      # 0x14510
movb $0x0, %al
callq 0x1070
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movzwl -0x20(%rbp), %ecx
cmpl %ecx, %eax
jae 0x125af
movl -0x4c(%rbp), %esi
leaq 0x23e0(%rip), %rdi      # 0x14533
movb $0x0, %al
callq 0x1070
movq -0xc8(%rbp), %rax
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movzwl 0x8(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x121c6
movq -0xc8(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movzwl 0x8(%rcx), %ecx
movl %ecx, -0xdc(%rbp)
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movzwl 0x8(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0xdc(%rbp), %eax
movl $0x8, %ecx
subl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x6c(%rbp)
jmp 0x121de
movq -0xc8(%rbp), %rax
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movzwl 0x8(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x4c(%rbp), %eax
movl %eax, %edx
shlq $0x4, %rdx
movq %rcx, %rax
addq %rdx, %rax
movzwl 0x8(%rax), %eax
movl -0x4c(%rbp), %edx
movl %edx, %esi
shlq $0x4, %rsi
movq %rcx, %rdx
addq %rsi, %rdx
movzwl 0xa(%rdx), %edx
imull %edx, %eax
movl %eax, -0x58(%rbp)
movl -0x6c(%rbp), %eax
movl -0x4c(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movzwl 0xa(%rcx), %ecx
imull %ecx, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x1, -0x18(%rbp)
jne 0x12243
movq -0xc0(%rbp), %rcx
movl -0x50(%rbp), %eax
subl 0xc(%rcx), %eax
shrl $0x2, %eax
movl %eax, -0x64(%rbp)
jmp 0x12274
movq -0xc0(%rbp), %rcx
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
shlq $0x5, %rdx
movq %rcx, %rax
addq %rdx, %rax
movl 0xc(%rax), %eax
movl -0x74(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
subl 0xc(%rcx), %eax
shrl $0x2, %eax
movl %eax, -0x64(%rbp)
cmpl $0x10, -0x64(%rbp)
jne 0x12291
movl $0x0, -0x68(%rbp)
leaq 0x22c0(%rip), %rdi      # 0x14548
movb $0x0, %al
callq 0x1070
jmp 0x122b1
cmpl $0x100, -0x64(%rbp)     # imm = 0x100
jne 0x122af
movl $0x1, -0x68(%rbp)
leaq 0x22b5(%rip), %rdi      # 0x1455d
movb $0x0, %al
callq 0x1070
jmp 0x122b1
movl -0x64(%rbp), %eax
shll %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x10c0
movq %rax, -0xa8(%rbp)
movl -0x64(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, %edi
callq 0x10c0
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x68(%rbp)
jne 0x122f1
movl -0x5c(%rbp), %eax
shrl %eax
movl %eax, -0x60(%rbp)
jmp 0x12317
cmpl $0x1, -0x68(%rbp)
jne 0x122ff
movl -0x5c(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x12315
movl $0x1, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
jmp 0x125e0
jmp 0x12317
movq -0xc8(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movzwl 0x8(%rcx), %esi
movl -0x6c(%rbp), %edx
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movzwl 0xa(%rax), %ecx
leaq 0x2229(%rip), %rdi      # 0x14572
movb $0x0, %al
callq 0x1070
movq -0xb8(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
leaq 0x2246(%rip), %rdi      # 0x145aa
movb $0x0, %al
callq 0x1070
movl -0x60(%rbp), %eax
movl %eax, %edi
callq 0x10c0
movq %rax, -0x30(%rbp)
movl -0x58(%rbp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x10c0
movq %rax, %rcx
movq -0xc8(%rbp), %rax
movq %rcx, -0x38(%rbp)
movq -0x40(%rbp), %rdi
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x30(%rbp), %rdi
movl -0x60(%rbp), %eax
movl %eax, %esi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0xb8(%rbp), %rax
movl -0x4c(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x1257a
movq -0xc0(%rbp), %rax
movq -0x40(%rbp), %rdi
movl -0x74(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x28(%rbp), %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movl -0x64(%rbp), %edi
movq -0x28(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movl -0x68(%rbp), %ecx
callq 0x119b0
movq -0xc8(%rbp), %rsi
movl -0x4c(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rsi
movq -0xa8(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x68(%rbp), %r9d
movl -0x60(%rbp), %r10d
movl -0x6c(%rbp), %eax
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
subq $0x10, %rsp
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x11be0
addq $0x10, %rsp
cmpl $0x0, -0x70(%rbp)
je 0x124d1
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movl -0x4c(%rbp), %ecx
movl -0x74(%rbp), %r8d
leaq 0x2142(%rip), %rsi      # 0x145d2
movb $0x0, %al
callq 0x1110
movq -0xc8(%rbp), %rax
movq -0x80(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movzwl 0x8(%rcx), %edx
movl -0x4c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movzwl 0xa(%rax), %ecx
callq 0x9ef0
jmp 0x12559
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movl -0x4c(%rbp), %ecx
movl -0x74(%rbp), %r8d
leaq 0x2100(%rip), %rsi      # 0x145eb
movb $0x0, %al
callq 0x1110
movq -0x80(%rbp), %rdi
leaq 0x1038(%rip), %rsi      # 0x13535
callq 0x10f0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x12534
movq -0x80(%rbp), %rsi
leaq 0x1fc2(%rip), %rdi      # 0x144d9
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
jmp 0x125e0
movq -0x38(%rbp), %rdi
movl -0x58(%rbp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x1120
movq -0x48(%rbp), %rdi
callq 0x1050
movq -0xb8(%rbp), %rax
movl -0x74(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x74(%rbp)
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %edx
subl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x123c7
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rdi
callq 0x1030
movq -0xa8(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1213a
movq -0x40(%rbp), %rdi
callq 0x1050
movq -0x80(%rbp), %rdi
callq 0x1030
leaq 0x203d(%rip), %rdi      # 0x14605
movb $0x0, %al
callq 0x1070
movl $0x0, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
movq -0x88(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq