// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, -0x88(%rbp)
cmpl $0x2, %edi
jg 0xcdcd
movq -0x88(%rbp), %rax
movq (%rax), %rsi
leaq 0x2c08(%rip), %rdi      # 0xf9d0
jmp 0xd364
movq -0x88(%rbp), %r14
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1110
movq %rax, -0xe8(%rbp)
movq 0x8(%r14), %rdi
leaq 0x2b82(%rip), %rsi      # 0xf978
callq 0x1100
movq 0x8(%r14), %rsi
testq %rax, %rax
je 0xd35d
movq %rax, %rbx
movq %rsi, %rdi
callq 0x1070
leaq 0x16(%rax), %rdi
callq 0x10d0
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10e0
movq %rbx, %rdi
callq 0x10b0
movq %rax, -0xd8(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10e0
leaq -0x108(%rbp), %r14
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movq %rsp, -0xc0(%rbp)
movzwl (%r14), %r12d
movq %rsp, %rcx
movl %r12d, %eax
shll $0x4, %eax
subq %rax, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movl 0x8(%r14), %eax
movq %rsp, %rcx
movq %rax, -0x50(%rbp)
shlq $0x5, %rax
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
leal 0xf(,%r12,4), %eax
andl $-0x10, %eax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
testl %r12d, %r12d
je 0xcee7
xorl %r15d, %r15d
movq -0x70(%rbp), %r14
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl $0x0, (%r13,%r15,4)
incq %r15
addq $0x10, %r14
cmpq %r15, %r12
jne 0xcebd
movl -0xfc(%rbp), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10e0
cmpq $0x0, -0x50(%rbp)
je 0xcf32
movq -0x50(%rbp), %r15
movq -0x68(%rbp), %r14
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl 0x8(%r14), %eax
shrl $0x4, %eax
decl %eax
incl (%r13,%rax,4)
addq $0x20, %r14
decq %r15
jne 0xcf06
movq %r13, -0x58(%rbp)
leaq 0x2af5(%rip), %rdi      # 0xfa32
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x2b05(%rip), %rdi      # 0xfa53
movq -0x50(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
testq %r12, %r12
movq %rbx, %r15
je 0xd33c
movq -0x68(%rbp), %rax
addq $0xc, %rax
movq %rax, -0xd0(%rbp)
movl %r12d, %eax
movq %rax, -0xc8(%rbp)
xorl %ebx, %ebx
movl $0x0, -0x3c(%rbp)
xorl %r14d, %r14d
movq %r15, -0x80(%rbp)
leaq 0x2ae1(%rip), %rdi      # 0xfa76
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movq %rbx, -0x38(%rbp)
shlq $0x4, %rbx
movq -0x70(%rbp), %r12
movzwl 0x8(%r12,%rbx), %eax
movl %eax, %ecx
andl $-0x8, %ecx
addl $0x8, %ecx
testb $0x7, %al
movq %rax, -0x78(%rbp)
cmovel %eax, %ecx
movq %rcx, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movq -0xd8(%rbp), %rcx
movl %ecx, %esi
cmpl $0x1, -0x50(%rbp)
je 0xcffc
leal 0x1(%r14), %eax
shlq $0x5, %rax
movq -0x68(%rbp), %rcx
movl 0xc(%rcx,%rax), %esi
movl %r14d, %eax
shlq $0x5, %rax
addq %rcx, %rax
addq $0xc, %rax
subl (%rax), %esi
movq %rbx, -0x48(%rbp)
movzwl 0xa(%r12,%rbx), %eax
movl %eax, -0x40(%rbp)
movq %rsi, -0x98(%rbp)
movl %esi, %ebx
shrl $0x2, %ebx
cmpl $0x10, %ebx
je 0xd033
cmpl $0x100, %ebx            # imm = 0x100
jne 0xd042
movl $0x1, %r13d
leaq 0x2af3(%rip), %rdi      # 0xfb24
jmp 0xd03d
xorl %r13d, %r13d
leaq 0x2afb(%rip), %rdi      # 0xfb38
callq 0x1040
movq -0x90(%rbp), %rax
movl %eax, %r12d
imull -0x40(%rbp), %r12d
movq %rbx, -0xf8(%rbp)
leal (%rbx,%rbx), %edi
shlq $0x2, %rdi
callq 0x10d0
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rdi
andl $-0x4, %edi
movq %rdi, -0x98(%rbp)
callq 0x10d0
movq %rax, -0xb0(%rbp)
cmpl $0x1, %r13d
movl %r13d, -0x2c(%rbp)
je 0xd0a7
testl %r13d, %r13d
movq -0x48(%rbp), %rax
jne 0xd2aa
movq %r12, %r13
shrl %r13d
jmp 0xd0ae
movq -0x48(%rbp), %rax
movq %r12, %r13
addq -0x70(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x29ce(%rip), %rdi      # 0xfa8b
movq -0x78(%rbp), %r12
movl %r12d, %esi
movq -0x90(%rbp), %rdx
movl -0x40(%rbp), %ebx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1080
movq -0x38(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx,%rax,4), %esi
leaq 0x29da(%rip), %rdi      # 0xfac3
xorl %eax, %eax
callq 0x1080
movq %r13, -0xf0(%rbp)
movl %r13d, %edi
movq %rdi, -0xe0(%rbp)
callq 0x10d0
movq %rax, %r13
imull %ebx, %r12d
shll $0x2, %r12d
movq %r12, -0x78(%rbp)
movq %r12, %rdi
callq 0x10d0
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10e0
movl $0x1, %edx
movq %r13, -0xa8(%rbp)
movq %r13, %rdi
movq -0xe0(%rbp), %rsi
movq %r15, %rcx
callq 0x1050
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %ebx
testl %ebx, %ebx
je 0xd2b5
movl -0x2c(%rbp), %r13d
movl %r14d, %eax
shlq $0x5, %rax
movq -0x68(%rbp), %rcx
movl 0xc(%rcx,%rax), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10e0
movl $0x1, %edx
movq -0xb0(%rbp), %r12
movq %r12, %rdi
movq -0x98(%rbp), %rsi
movq %r15, %rcx
callq 0x1050
movq -0xf8(%rbp), %rdi
movq %r12, %rsi
movq -0xb8(%rbp), %r15
movq %r15, %rdx
movl %r13d, %ecx
callq 0xcb1b
movq -0x48(%rbp), %r12
movq 0x8(%r12), %rsi
movq %r15, %rdx
movq -0xa8(%rbp), %rcx
movq -0xa0(%rbp), %r15
movq %r15, %r8
movl %r13d, %r9d
pushq -0x90(%rbp)
pushq -0xf0(%rbp)
callq 0xcc14
addq $0x10, %rsp
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdx
cmpl $0x0, -0xe8(%rbp)
je 0xd238
movq -0x60(%rbp), %r13
movq %r13, %rdi
leaq 0x28d7(%rip), %rsi      # 0xfaeb
movq -0x38(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1120
movzwl 0x8(%r12), %edx
movq %r13, %rdi
movq %r15, %rsi
movl -0x40(%rbp), %ecx
callq 0xa1fa
jmp 0xd28f
movq -0x60(%rbp), %r15
movq %r15, %rdi
leaq 0x28be(%rip), %rsi      # 0xfb04
movq -0x38(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1120
movq %r15, %rdi
leaq 0x17f7(%rip), %rsi      # 0xea55
callq 0x1100
testq %rax, %rax
je 0xd30c
movq %rax, %r12
movl $0x1, %edx
movq -0xa0(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq %rax, %rcx
callq 0x1130
movq %r12, %rdi
callq 0x1060
movq -0x80(%rbp), %r15
incl %r14d
decl %ebx
movl -0x2c(%rbp), %r13d
jne 0xd16a
xorl %ebx, %ebx
movq -0x58(%rbp), %rax
jmp 0xd2b9
xorl %ebx, %ebx
movl $0x1, -0x3c(%rbp)
jmp 0xd2f2
movl -0x2c(%rbp), %r13d
movq -0x38(%rbp), %rcx
movl %ebx, (%rax,%rcx,4)
movq -0xa0(%rbp), %rdi
callq 0x1030
movq -0xb0(%rbp), %rdi
callq 0x1030
movq -0xb8(%rbp), %rdi
callq 0x1030
movq -0xa8(%rbp), %rdi
callq 0x1030
movb $0x1, %bl
testb %bl, %bl
je 0xd372
movq -0x38(%rbp), %rbx
incq %rbx
cmpq -0xc8(%rbp), %rbx
jne 0xcf8e
jmp 0xd33c
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %ebx, (%rax,%rcx,4)
xorl %ebx, %ebx
leaq 0x26fc(%rip), %rdi      # 0xfa1c
movq -0x60(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
movl $0x1, -0x3c(%rbp)
movq -0x80(%rbp), %r15
movl -0x2c(%rbp), %r13d
jmp 0xd2f2
movq %r15, %rdi
callq 0x1060
movq -0x60(%rbp), %rdi
callq 0x1030
leaq 0x27ca(%rip), %rdi      # 0xfb1e
callq 0x1040
xorl %eax, %eax
jmp 0xd375
leaq 0x26b8(%rip), %rdi      # 0xfa1c
xorl %eax, %eax
callq 0x1080
movl $0x1, %eax
jmp 0xd37c
movl -0x3c(%rbp), %eax
movq -0xc0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq