// File: /workspace/github_c_makefile_star2_O2/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, %rbx
cmpl $0x2, %edi
jg 0xa1f4
movq (%rbx), %rsi
leaq 0x2801(%rip), %rdi      # 0xc9f0
jmp 0xa732
movq 0x10(%rbx), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x1110
movq %rax, -0xe0(%rbp)
movq 0x8(%rbx), %rdi
leaq 0x2784(%rip), %rsi      # 0xc998
callq 0x1100
movq 0x8(%rbx), %rsi
testq %rax, %rax
je 0xa72b
movq %rax, %r13
movq %rbx, -0x98(%rbp)
movq %rsi, %rdi
callq 0x1070
leaq 0x16(%rax), %rdi
callq 0x10d0
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
pushq $0x2
popq %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r13, %rdi
callq 0x10b0
movq %rax, -0xc8(%rbp)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10e0
leaq -0x108(%rbp), %r15
pushq $0x10
popq %rbx
pushq $0x1
popq %r14
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1050
movq %rsp, -0xb8(%rbp)
movzwl (%r15), %ecx
movq %rsp, %rdx
movl %ecx, %eax
shll $0x4, %eax
subq %rax, %rdx
movq %rdx, %rsp
movl 0x8(%r15), %eax
movq %rsp, %rsi
movq %rax, -0x68(%rbp)
shlq $0x5, %rax
subq %rax, %rsi
movq %rsi, -0x48(%rbp)
movq %rsi, %rsp
movq %rcx, -0x78(%rbp)
leal 0xf(,%rcx,4), %eax
andl $-0x10, %eax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rdx, -0xd0(%rbp)
movq %rdx, %r15
cmpq %r12, -0x78(%rbp)
je 0xa30f
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1050
movq -0x60(%rbp), %rax
andl $0x0, (%rax,%r12,4)
incq %r12
addq $0x10, %r15
jmp 0xa2e6
movl -0xfc(%rbp), %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x10e0
pushq $0x20
popq %rbx
pushq $0x1
popq %r14
movq -0x48(%rbp), %r15
movq -0x68(%rbp), %r12
subq $0x1, %r12
jb 0xa35b
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1050
movl 0x8(%r15), %eax
shrl $0x4, %eax
decl %eax
movq -0x60(%rbp), %rcx
incl (%rcx,%rax,4)
addq $0x20, %r15
jmp 0xa32e
movq %r13, -0x80(%rbp)
leaq 0x26ec(%rip), %rdi      # 0xca52
movq -0x78(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x26fb(%rip), %rdi      # 0xca73
movq -0x68(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
movq -0x48(%rbp), %rax
addq $0xc, %rax
movq %rax, -0xc0(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq -0x78(%rbp), %r15
movq -0xd0(%rbp), %rbx
je 0xa74b
leaq 0x26e6(%rip), %rdi      # 0xca96
movl %r15d, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rax
shlq $0x4, %rax
movq %rax, -0x50(%rbp)
movzwl 0x8(%rbx,%rax), %eax
movl %eax, %ecx
andl $-0x8, %ecx
addl $0x8, %ecx
testb $0x7, %al
movq %rax, -0x70(%rbp)
cmovel %eax, %ecx
movq %rcx, -0x88(%rbp)
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
movl %ecx, %edx
cmpl $0x1, -0x68(%rbp)
je 0xa416
leal 0x1(%r14), %eax
shlq $0x5, %rax
movq -0x48(%rbp), %rcx
movl 0xc(%rcx,%rax), %edx
movl %r14d, %eax
shlq $0x5, %rax
addq %rcx, %rax
addq $0xc, %rax
subl (%rax), %edx
movq -0x50(%rbp), %rax
movzwl 0xa(%rbx,%rax), %eax
movl %eax, -0x2c(%rbp)
movq %rdx, %r13
shrl $0x2, %edx
cmpl $0x10, %edx
je 0xa449
cmpl $0x100, %edx            # imm = 0x100
jne 0xa462
movq %rdx, %r12
leaq 0x2703(%rip), %rdi      # 0xcb44
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
jmp 0xa45a
movq %rdx, %r12
movl $0x0, -0x30(%rbp)
leaq 0x26fe(%rip), %rdi      # 0xcb58
callq 0x1040
movq %r12, %rdx
movq -0x88(%rbp), %rax
movl %eax, %r12d
imull -0x2c(%rbp), %r12d
movq %rdx, -0xf0(%rbp)
leal (%rdx,%rdx), %edi
shlq $0x2, %rdi
callq 0x10d0
movq %rax, -0xb0(%rbp)
andl $-0x4, %r13d
movq %r13, -0xf8(%rbp)
movq %r13, %rdi
callq 0x10d0
movq %rax, -0xa8(%rbp)
movl -0x30(%rbp), %eax
cmpl $0x1, %eax
movq %r14, -0xd8(%rbp)
je 0xa4bf
testl %eax, %eax
jne 0xa71f
shrl %r12d
movq %r12, %r14
movq -0x50(%rbp), %r12
addq %rbx, %r12
leaq 0x25db(%rip), %rdi      # 0xcaab
movq -0x70(%rbp), %rbx
movl %ebx, %esi
movq -0x88(%rbp), %rdx
movl -0x2c(%rbp), %r13d
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1080
movq -0x60(%rbp), %rax
movl (%rax,%r15,4), %esi
movl %esi, -0x34(%rbp)
leaq 0x25e6(%rip), %rdi      # 0xcae3
xorl %eax, %eax
callq 0x1080
movq %r14, -0xe8(%rbp)
movl %r14d, %edi
movq %rdi, -0x58(%rbp)
callq 0x10d0
movq %rax, %r14
imull %r13d, %ebx
shll $0x2, %ebx
movq %rbx, -0x70(%rbp)
movq %rbx, %rdi
callq 0x10d0
movq %rax, %rbx
movq %r12, -0x50(%rbp)
movl 0xc(%r12), %esi
movq -0x80(%rbp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x10e0
movq %r14, -0xa0(%rbp)
movq %r14, %rdi
movq -0x58(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x1050
movl -0x34(%rbp), %ecx
decl %ecx
movq -0xd8(%rbp), %r14
movq %rbx, -0x58(%rbp)
movq %r15, -0x90(%rbp)
cmpl $-0x1, %ecx
je 0xa6d9
movl %ecx, -0x34(%rbp)
movl %r14d, %eax
shlq $0x5, %rax
movq -0x48(%rbp), %rcx
movl 0xc(%rcx,%rax), %esi
movq -0x80(%rbp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x10e0
movq -0xa8(%rbp), %r15
movq %r15, %rdi
movq -0xf8(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x1050
movq -0xf0(%rbp), %rdi
movq %r15, %rsi
movq -0xb0(%rbp), %r15
movq %r15, %rdx
movl -0x30(%rbp), %r13d
movl %r13d, %ecx
callq 0x9f83
movq -0x50(%rbp), %rax
movq 0x8(%rax), %r12
movq %r12, %rsi
movq %r15, %rdx
movq -0xa0(%rbp), %rcx
movq %rbx, %r8
movl %r13d, %r9d
pushq -0x88(%rbp)
pushq -0xe8(%rbp)
callq 0xa050
popq %rax
popq %rcx
cmpl $0x0, -0xe0(%rbp)
je 0xa654
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %r15
movq %r15, %rdi
leaq 0x24df(%rip), %rsi      # 0xcb0b
movq -0x90(%rbp), %r13
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1120
movzwl %r12w, %edx
movq %r15, %rdi
movq %rbx, %rsi
movl -0x2c(%rbp), %ecx
callq 0x81b1
jmp 0xa6bd
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %r15
movq %r15, %rdi
leaq 0x24b7(%rip), %rsi      # 0xcb24
movq -0x90(%rbp), %rcx
movq %r14, %rbx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1120
movq %r15, %rdi
leaq 0x13ea(%rip), %rsi      # 0xba75
callq 0x1100
testq %rax, %rax
je 0xa70d
movq %rax, %r12
movq -0x58(%rbp), %rdi
movq -0x70(%rbp), %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1130
movq %r12, %rdi
callq 0x1060
movq -0x90(%rbp), %r13
movq %rbx, %r14
incl %r14d
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, (%rax,%r13,4)
decl %ecx
movq %r13, %r15
movq -0x58(%rbp), %rbx
jmp 0xa577
movq %rbx, %rdi
callq 0x1030
movq -0xa8(%rbp), %rdi
callq 0x1030
movq -0xb0(%rbp), %rdi
callq 0x1030
movq -0xa0(%rbp), %rdi
callq 0x1030
incq %r15
jmp 0xa398
leaq 0x2328(%rip), %rdi      # 0xca3c
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %rax
movq -0xb8(%rbp), %rsp
jmp 0xa73c
leaq 0x230a(%rip), %rdi      # 0xca3c
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x1060
movq -0x40(%rbp), %rdi
callq 0x1030
leaq 0x23da(%rip), %rdi      # 0xcb3e
callq 0x1040
xorl %eax, %eax
jmp 0xa722