// File: /workspace/github_c_makefile_star2_O3/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 $0xd8, %rsp
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0xcfce
movq (%rbx), %rsi
leaq 0x2b07(%rip), %rdi      # 0xfad0
jmp 0xd56c
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1120
movq %rax, -0xe0(%rbp)
movq 0x8(%rbx), %rdi
leaq 0x2a88(%rip), %rsi      # 0xfa78
callq 0x1110
movq 0x8(%rbx), %rsi
testq %rax, %rax
je 0xd565
movq %rax, %r12
movq %rbx, -0xb0(%rbp)
movq %rsi, %rdi
callq 0x1070
leaq 0x16(%rax), %rdi
callq 0x10e0
movq %rax, -0x48(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10f0
movq %r12, %rdi
callq 0x10c0
movq %rax, -0xd0(%rbp)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
leaq -0x100(%rbp), %r14
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x1050
movq %rsp, -0xb8(%rbp)
movzwl (%r14), %ebx
movq %rsp, %rcx
movl %ebx, %eax
shll $0x4, %eax
subq %rax, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movl 0x8(%r14), %eax
movq %rsp, %rcx
movq %rax, -0x40(%rbp)
shlq $0x5, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
leal 0xf(,%rbx,4), %eax
andl $-0x10, %eax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
testl %ebx, %ebx
je 0xd0e5
xorl %r15d, %r15d
movq -0x70(%rbp), %r14
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x1050
movl $0x0, (%r13,%r15,4)
incq %r15
addq $0x10, %r14
cmpq %r15, %rbx
jne 0xd0bb
movl -0xf4(%rbp), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x10f0
cmpq $0x0, -0x40(%rbp)
je 0xd130
movq -0x40(%rbp), %r15
movq -0x50(%rbp), %r14
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x1050
movl 0x8(%r14), %eax
shrl $0x4, %eax
decl %eax
incl (%r13,%rax,4)
addq $0x20, %r14
decq %r15
jne 0xd104
leaq 0x29fb(%rip), %rdi      # 0xfb32
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x2a0c(%rip), %rdi      # 0xfb53
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
testq %rbx, %rbx
movq %rbx, %rcx
movq -0x70(%rbp), %rbx
je 0xd544
movq -0x50(%rbp), %rax
addq $0xc, %rax
movq %rax, -0xc8(%rbp)
movl %ecx, %eax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
movq %r12, -0x88(%rbp)
movq %r13, -0x80(%rbp)
leaq 0x29df(%rip), %rdi      # 0xfb76
movq -0x30(%rbp), %r15
movl %r15d, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rax
shlq $0x4, %rax
movq %rax, %r15
movzwl 0x8(%rbx,%rax), %eax
movl %eax, %ecx
andl $-0x8, %ecx
addl $0x8, %ecx
testb $0x7, %al
movq %rax, -0x78(%rbp)
cmovel %eax, %ecx
movq %rcx, -0x98(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movl %ecx, %r13d
cmpl $0x1, -0x40(%rbp)
movq %rbx, %r12
je 0xd205
leal 0x1(%r14), %eax
shlq $0x5, %rax
movq -0x50(%rbp), %rcx
movl 0xc(%rcx,%rax), %r13d
movl %r14d, %eax
shlq $0x5, %rax
addq %rcx, %rax
addq $0xc, %rax
movzwl 0xa(%r12,%r15), %edx
movq -0x98(%rbp), %rcx
movl %ecx, %ebx
subl (%rax), %r13d
movl %edx, -0x34(%rbp)
imull %edx, %ebx
movl %r13d, %eax
shrl $0x2, %eax
cmpl $0x100, %eax            # imm = 0x100
movq %rax, -0xf0(%rbp)
je 0xd262
cmpl $0x10, %eax
jne 0xd295
leaq 0x29fb(%rip), %rdi      # 0xfc38
callq 0x1040
movl $0x80, %edi
callq 0x10e0
movq %rax, -0x60(%rbp)
andl $-0x4, %r13d
movq %r13, %rdi
callq 0x10e0
movq %rax, -0x58(%rbp)
jmp 0xd2c5
leaq 0x29bb(%rip), %rdi      # 0xfc24
callq 0x1040
movl $0x800, %edi            # imm = 0x800
callq 0x10e0
movq %rax, -0x60(%rbp)
andl $-0x4, %r13d
movq %r13, %rdi
callq 0x10e0
movq %rax, -0x58(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0xd2d4
leal (%rax,%rax), %edi
shlq $0x2, %rdi
callq 0x10e0
movq %rax, -0x60(%rbp)
andl $-0x4, %r13d
movq %r13, %rdi
callq 0x10e0
movq %rax, -0x58(%rbp)
movl -0x38(%rbp), %eax
cmpl $0x1, %eax
je 0xd2d4
testl %eax, %eax
jne 0xd58c
shrl %ebx
movq %rbx, -0x68(%rbp)
movl $0x0, -0x38(%rbp)
jmp 0xd2d8
movq %rbx, -0x68(%rbp)
movq %r15, %rbx
addq %r12, %rbx
leaq 0x28a6(%rip), %rdi      # 0xfb8b
movq -0x78(%rbp), %r15
movl %r15d, %esi
movq -0x98(%rbp), %rdx
movl -0x34(%rbp), %ecx
xorl %eax, %eax
callq 0x1080
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %r12d
leaq 0x28b3(%rip), %rdi      # 0xfbc3
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
movl -0x68(%rbp), %edi
movq %rdi, -0xa8(%rbp)
callq 0x10e0
movq %rax, -0x90(%rbp)
imull -0x34(%rbp), %r15d
shll $0x2, %r15d
movq %r15, -0x78(%rbp)
movq %r15, %rdi
callq 0x10e0
movq %rax, -0xa0(%rbp)
movq %rbx, -0xe8(%rbp)
movl 0xc(%rbx), %esi
movq -0x88(%rbp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f0
movl $0x1, %edx
movq -0x90(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq %rbx, %rcx
callq 0x1050
testl %r12d, %r12d
je 0xd4f7
decl %r12d
movq %r13, -0xd8(%rbp)
movl %r12d, -0xa8(%rbp)
movl %r14d, %eax
shlq $0x5, %rax
movq -0x50(%rbp), %rcx
movl 0xc(%rcx,%rax), %esi
movq -0x88(%rbp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f0
movl $0x1, %edx
movq -0x58(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0x1050
movq -0xf0(%rbp), %rdi
movq %r15, %rsi
movq -0x60(%rbp), %r15
movq %r15, %rdx
movl -0x38(%rbp), %r13d
movl %r13d, %ecx
callq 0xcd27
movq -0xe8(%rbp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq -0x90(%rbp), %rcx
movq -0xa0(%rbp), %r12
movq %r12, %r8
movl %r13d, %r9d
pushq -0x98(%rbp)
pushq -0x68(%rbp)
callq 0xce20
addq $0x10, %rsp
cmpl $0x0, -0xe0(%rbp)
je 0xd46e
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x48(%rbp), %r15
movq %r15, %rdi
leaq 0x27a2(%rip), %rsi      # 0xfbeb
movq -0x30(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1130
movzwl %bx, %edx
movq %r15, %rdi
movq -0x80(%rbp), %r15
movq %r12, %rsi
movl -0x34(%rbp), %ecx
callq 0xa69d
jmp 0xd4d4
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
leaq 0x277d(%rip), %rsi      # 0xfc04
movq -0x30(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
leaq 0x16b6(%rip), %rsi      # 0xeb55
callq 0x1110
testq %rax, %rax
movq -0x80(%rbp), %r15
je 0xd57a
movq %rax, %rbx
movl $0x1, %edx
movq -0xa0(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq %rax, %rcx
callq 0x1140
movq %rbx, %rdi
callq 0x1060
incl %r14d
movq -0x30(%rbp), %rax
movl -0xa8(%rbp), %r12d
movl %r12d, (%r15,%rax,4)
addl $-0x1, %r12d
movq -0xd8(%rbp), %r13
jb 0xd395
movq -0xa0(%rbp), %rdi
callq 0x1030
movq -0x58(%rbp), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
movq -0x90(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rcx
incq %rcx
movq %rcx, -0x30(%rbp)
cmpq -0xc0(%rbp), %rcx
movq -0x88(%rbp), %r12
movq -0x70(%rbp), %rbx
jne 0xd190
movq %r12, %rdi
callq 0x1060
movq -0x48(%rbp), %rdi
callq 0x1030
leaq 0x26c2(%rip), %rdi      # 0xfc1e
callq 0x1040
xorl %eax, %eax
jmp 0xd591
leaq 0x25b0(%rip), %rdi      # 0xfb1c
xorl %eax, %eax
callq 0x1080
movl $0x1, %eax
jmp 0xd598
leaq 0x259b(%rip), %rdi      # 0xfb1c
movq -0x48(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
movl $0x1, %eax
movq -0xb8(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq