// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/txp-convert.c
// Function: getTexturePixels
void getTexturePixels(FILE *f, txpHeader header, textureHeader *textures) {
	unsigned int i, j, texsize, palsize;
	long unsigned int decompressed;
	uint32_t compressedsize;
	unsigned char *compressedpixels = NULL, *uncompressedpixels = NULL/*, *processedpixels = NULL*/, *palette = NULL;
	for(i = 0; i < header.numtextures; i++) {
		printf("Texture %02d ", i);
		texsize = textures[i].width * textures[i].height;
		if(textures[i].bpp == 4) texsize /= 2;
		uncompressedpixels = malloc(texsize);
		fseek(f, textures[i].offset, SEEK_SET);
		if(textures[i].compressed) {
			printf("Decompressing...");
			fread(&compressedsize, 4, 1, 0);
			compressedpixels = malloc(compressedsize);
			fread(compressedpixels, compressedsize, 1, f);
			if((j = uncompress(uncompressedpixels, &decompressed, compressedpixels, compressedsize)) != Z_OK) {
			//~ if((j = inflate(&strm, Z_FINISH)) != Z_OK) {
				printf("=====================================couldnt decompress data\n");
				switch(j) {
					case Z_MEM_ERROR: {
						printf("memory error\n");
						break;
					}
					case Z_NEED_DICT: {
						printf("need dictionary\n");
						break;
					}
					case Z_DATA_ERROR: {
						printf("input corrupted\n");
						//~ printf("%s\n", strm.msg);
						break;
					}
					case Z_STREAM_ERROR: {
						printf("broken stream\n");
						break;
					}
					case Z_BUF_ERROR: {
						printf("buffer error\n");
						break;
					}
					default: {
						printf("unknown error\n");
						break;
					}
				}
				printf("decompressed %08lx\n", decompressed);
				return;
			}
			free(compressedpixels);
		}
		else fread(uncompressedpixels, texsize, 1, f);
		
		printf("Unfiddling...");
		textures[i].texpixels = unfiddle(uncompressedpixels, textures[i].width, textures[i].height, textures[i].bpp);
		free(uncompressedpixels);
		
		if(textures[i].palette) {
			printf("Getting palette...");
			fseek(f, textures[i].palette, SEEK_SET);
			switch(textures[i].bpp) {
				case 4: {
					palsize = 16*4;
					break;
				}
				case 5: {
					palsize = 256*4;
					break;
				}
				case 6: {
					fseek(f, 20, SEEK_CUR);
					fread(&compressedsize, 4, 1, f);
					palsize = compressedsize;
					fseek(f, textures[i].palette, SEEK_SET);
					break;
				}
			}
			palette = malloc(palsize);
			fread(palette, palsize, 1, f);
			textures[i].palpixels = buildPalette(palette, textures[i].bpp);
			free(palette);
		}
		printf("\n");
	}
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x64(%rsp), %eax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq %rdi, 0x10(%rsp)
cmpq %r13, 0x18(%rsp)
je 0xa8f7
leaq 0x2522(%rip), %rdi      # 0xcc23
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
movl 0xc(%rbx), %r15d
imull 0x8(%rbx), %r15d
cmpl $0x4, (%rbx)
sete %cl
shrl %cl, %r15d
movq %r15, %rdi
callq 0x10f0
movq %rax, %rbp
movl 0x10(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1110
cmpl $0x0, 0x4(%rbx)
je 0xa7aa
leaq 0x24ef(%rip), %rdi      # 0xcc31
xorl %eax, %eax
callq 0x10a0
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x1060
movl 0x8(%rsp), %r12d
movq %r12, %rdi
callq 0x10f0
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq 0x10(%rsp), %r14
movq %r14, %rcx
callq 0x1060
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1080
testl %eax, %eax
jne 0xa8ac
movq %r15, %rdi
callq 0x1030
jmp 0xa7bb
movq %rbp, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1060
leaq 0x2494(%rip), %rdi      # 0xcc56
xorl %eax, %eax
callq 0x10a0
movl (%rbx), %ecx
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
movq %rbp, %rdi
callq 0xa588
movq %rax, 0x18(%rbx)
movq %rbp, %rdi
callq 0x1030
cmpl $0x0, 0x14(%rbx)
je 0xa898
leaq 0x246e(%rip), %rdi      # 0xcc64
xorl %eax, %eax
callq 0x10a0
movl 0x14(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1110
movl (%rbx), %eax
cmpl $0x6, %eax
je 0xa82b
cmpl $0x5, %eax
je 0xa824
cmpl $0x4, %eax
movl 0xc(%rsp), %eax
jne 0xa85f
pushq $0x40
popq %rax
jmp 0xa85f
movl $0x400, %eax            # imm = 0x400
jmp 0xa85f
movq %r14, %rdi
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1060
movl 0x8(%rsp), %ebp
movl 0x14(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1110
movl %ebp, %eax
movl %eax, 0xc(%rsp)
movl %eax, %r15d
movq %r15, %rdi
callq 0x10f0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1060
movl (%rbx), %esi
movq %r12, %rdi
callq 0xa68c
movq %rax, 0x20(%rbx)
movq %r12, %rdi
callq 0x1030
pushq $0xa
popq %rdi
callq 0x1040
incq %r13
addq $0x28, %rbx
jmp 0xa6ef
movl %eax, %r12d
leaq 0x271c(%rip), %rdi      # 0xcfd2
callq 0x1050
addl $0x5, %r12d
cmpl $0x7, %r12d
ja 0xa8d8
movl %r12d, %eax
leaq 0x27a5(%rip), %rcx      # 0xd074
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmp 0xa8df
leaq 0x2778(%rip), %rdi      # 0xd057
callq 0x1050
movq 0x20(%rsp), %rsi
leaq 0x2352(%rip), %rdi      # 0xcc42
xorl %eax, %eax
callq 0x10a0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq