// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movl 0x54(%rsp), %eax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xd888
movq %rsi, %rbx
movq %rdi, %r14
xorl %r13d, %r13d
leaq 0x267e(%rip), %rdi      # 0xfd03
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
movl 0xc(%rbx), %r12d
imull 0x8(%rbx), %r12d
cmpl $0x4, (%rbx)
sete %cl
shrl %cl, %r12d
movq %r12, %rdi
callq 0x1100
movq %rax, %rbp
movl 0x10(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1120
cmpl $0x0, 0x4(%rbx)
je 0xd728
leaq 0x264b(%rip), %rdi      # 0xfd11
xorl %eax, %eax
callq 0x10a0
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
xorl %ecx, %ecx
callq 0x1060
movl (%rsp), %r15d
movq %r15, %rdi
callq 0x1100
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x1060
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x1080
testl %eax, %eax
jne 0xd83d
movq %r12, %rdi
callq 0x1030
jmp 0xd73b
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x1060
leaq 0x25f4(%rip), %rdi      # 0xfd36
xorl %eax, %eax
callq 0x10a0
movl (%rbx), %ecx
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
movq %rbp, %rdi
callq 0xd502
movq %rax, 0x18(%rbx)
movq %rbp, %rdi
callq 0x1030
cmpl $0x0, 0x14(%rbx)
je 0xd81f
leaq 0x25ce(%rip), %rdi      # 0xfd44
xorl %eax, %eax
callq 0x10a0
movl 0x14(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1120
movl (%rbx), %eax
cmpl $0x6, %eax
je 0xd7ad
cmpl $0x5, %eax
je 0xd7a6
cmpl $0x4, %eax
movl 0x4(%rsp), %ebp
jne 0xd7e4
movl $0x40, %ebp
jmp 0xd7e4
movl $0x400, %ebp            # imm = 0x400
jmp 0xd7e4
movl $0x14, %esi
movq %r14, %rdi
movl $0x1, %edx
callq 0x1120
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
movq %r14, %rcx
callq 0x1060
movl (%rsp), %ebp
movl 0x14(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1120
movl %ebp, 0x4(%rsp)
movl %ebp, %r15d
movq %r15, %rdi
callq 0x1100
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x1060
movl (%rbx), %esi
movq %r12, %rdi
callq 0xd617
movq %rax, 0x20(%rbx)
movq %r12, %rdi
callq 0x1030
movl $0xa, %edi
callq 0x1040
incq %r13
addq $0x28, %rbx
cmpq %r13, 0x8(%rsp)
jne 0xd67e
jmp 0xd888
movl %eax, %r15d
leaq 0x286b(%rip), %rdi      # 0x100b2
callq 0x1050
addl $0x5, %r15d
cmpl $0x7, %r15d
ja 0xd869
movl %r15d, %eax
leaq 0x28f4(%rip), %rcx      # 0x10154
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmp 0xd870
leaq 0x28c7(%rip), %rdi      # 0x10137
callq 0x1050
movq 0x10(%rsp), %rsi
leaq 0x24a1(%rip), %rdi      # 0xfd22
xorl %eax, %eax
callq 0x10a0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq