// File: /workspace/github_c_makefile_star2_O1/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 0xd55e
movq %rsi, %rbx
movq %rdi, %r14
xorl %r13d, %r13d
leaq 0x289c(%rip), %rdi      # 0xfbf7
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 0x10f0
movq %rax, %rbp
movl 0x10(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1110
cmpl $0x0, 0x4(%rbx)
je 0xd3fe
leaq 0x2869(%rip), %rdi      # 0xfc05
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 0x10f0
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 0xd513
movq %r12, %rdi
callq 0x1030
jmp 0xd411
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x1060
leaq 0x2812(%rip), %rdi      # 0xfc2a
xorl %eax, %eax
callq 0x10a0
movl (%rbx), %ecx
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
movq %rbp, %rdi
callq 0xd1c0
movq %rax, 0x18(%rbx)
movq %rbp, %rdi
callq 0x1030
cmpl $0x0, 0x14(%rbx)
je 0xd4f5
leaq 0x27ec(%rip), %rdi      # 0xfc38
xorl %eax, %eax
callq 0x10a0
movl 0x14(%rbx), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1110
movl (%rbx), %eax
cmpl $0x6, %eax
je 0xd483
cmpl $0x5, %eax
je 0xd47c
cmpl $0x4, %eax
movl 0x4(%rsp), %ebp
jne 0xd4ba
movl $0x40, %ebp
jmp 0xd4ba
movl $0x400, %ebp            # imm = 0x400
jmp 0xd4ba
movl $0x14, %esi
movq %r14, %rdi
movl $0x1, %edx
callq 0x1110
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 0x1110
movl %ebp, 0x4(%rsp)
movl %ebp, %r15d
movq %r15, %rdi
callq 0x10f0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x1060
movl (%rbx), %esi
movq %r12, %rdi
callq 0xd2d5
movq %rax, 0x20(%rbx)
movq %r12, %rdi
callq 0x1030
movl $0xa, %edi
callq 0x1040
incq %r13
addq $0x28, %rbx
cmpq %r13, 0x8(%rsp)
jne 0xd354
jmp 0xd55e
movl %eax, %r15d
leaq 0x2a89(%rip), %rdi      # 0xffa6
callq 0x1050
addl $0x5, %r15d
cmpl $0x7, %r15d
ja 0xd53f
movl %r15d, %eax
leaq 0x2b12(%rip), %rcx      # 0x10048
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmp 0xd546
leaq 0x2ae5(%rip), %rdi      # 0x1002b
callq 0x1050
movq 0x10(%rsp), %rsi
leaq 0x26bf(%rip), %rdi      # 0xfc16
xorl %eax, %eax
callq 0x10a0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq