// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x50(%rbp), %rcx
movl -0x14(%rbp), %eax
cmpl 0x4(%rcx), %eax
jae 0x12dc4
movl -0x14(%rbp), %esi
leaq 0x2f21(%rip), %rdi      # 0x15972
movb $0x0, %al
callq 0x1080
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
imull 0xc(%rcx), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x4, (%rax)
jne 0x12a99
movl -0x1c(%rbp), %eax
shrl %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
je 0x12bfc
leaq 0x2e9c(%rip), %rdi      # 0x15980
movb $0x0, %al
callq 0x1080
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1040
movl -0x2c(%rbp), %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movq -0x8(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq -0x28(%rbp), %rsi
callq 0x1060
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
je 0x12bf1
leaq 0x2e41(%rip), %rdi      # 0x15991
xorl %eax, %eax
callq 0x1080
movl -0x18(%rbp), %eax
addl $0x5, %eax
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x7, %eax
ja 0x12bcc
movq -0x58(%rbp), %rax
leaq 0x28e5(%rip), %rcx      # 0x15458
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2e4c(%rip), %rdi      # 0x159cf
movb $0x0, %al
callq 0x1080
jmp 0x12bda
leaq 0x2e4a(%rip), %rdi      # 0x159dd
movb $0x0, %al
callq 0x1080
jmp 0x12bda
leaq 0x2e4b(%rip), %rdi      # 0x159ee
movb $0x0, %al
callq 0x1080
jmp 0x12bda
leaq 0x2e4c(%rip), %rdi      # 0x159ff
movb $0x0, %al
callq 0x1080
jmp 0x12bda
leaq 0x2e4b(%rip), %rdi      # 0x15a0e
movb $0x0, %al
callq 0x1080
jmp 0x12bda
leaq 0x2e49(%rip), %rdi      # 0x15a1c
movb $0x0, %al
callq 0x1080
movq -0x28(%rbp), %rsi
leaq 0x2e46(%rip), %rdi      # 0x15a2b
movb $0x0, %al
callq 0x1080
jmp 0x12dc6
movq -0x38(%rbp), %rdi
callq 0x1030
jmp 0x12c13
movq -0x40(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
movq -0x8(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
leaq 0x2e25(%rip), %rdi      # 0x15a3f
movb $0x0, %al
callq 0x1080
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
callq 0x12770
movq %rax, %rcx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x14(%rax)
je 0x12da8
leaq 0x2db4(%rip), %rdi      # 0x15a4d
xorl %eax, %eax
movl %eax, -0x60(%rbp)
callq 0x1080
movl -0x60(%rbp), %edx
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x14(%rax,%rcx,8), %eax
movl %eax, %esi
callq 0x10f0
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
movl %eax, -0x5c(%rbp)
subl $0x4, %eax
je 0x12cec
jmp 0x12cd8
movl -0x5c(%rbp), %eax
subl $0x5, %eax
je 0x12cf5
jmp 0x12ce2
movl -0x5c(%rbp), %eax
subl $0x6, %eax
je 0x12cfe
jmp 0x12d4c
movl $0x40, -0x20(%rbp)
jmp 0x12d4c
movl $0x400, -0x20(%rbp)     # imm = 0x400
jmp 0x12d4c
movq -0x8(%rbp), %rdi
movl $0x14, %esi
movl $0x1, %edx
callq 0x10f0
movq -0x8(%rbp), %rcx
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1040
movl -0x2c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
movl -0x20(%rbp), %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
movq -0x8(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %esi
callq 0x12910
movq %rax, %rcx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rdi
callq 0x1030
leaq 0x2d7f(%rip), %rdi      # 0x15b2e
movb $0x0, %al
callq 0x1080
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x12a37
jmp 0x12dc6
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)