// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/face-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	char execpath[4096];
	char *namestring = NULL, dirname[128] = {0}, filename[256] = {0};
	unsigned int i, j, datsize, datoffset = 0;
	unsigned int groupnum = 0;
	unsigned char *data = NULL, *rgba = NULL;
	uint16_t *palsource = NULL;
	
	uint32_t numfaces, numframes;
	
	faceinfo *faces = NULL;
	uint32_t *simpleoffsets = NULL;
	animationinfo *fullani = NULL;
	imageinfo *image = NULL;
	
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s [FACE.DAT]\n", argv[0]);
		return 1;
	}
	
	if(strlen(argv[0]) > 4096) BREAK("Path to executable exceeds 4096 chars!\n")
	strcpy(execpath, argv[0]);
	cleanExecPath(execpath);
	
	numcommondic = loaddic(&commondic, execpath, "mgs1-face", DIC_HASH_SINGLE_EXT, StrCode16);
	
	FILE *f;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	datsize = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	while(datoffset<datsize) {
		//~ printf("offset %08x\n", (unsigned int)ftell(f));
		snprintf(dirname, 128, "%04d-%08x", groupnum, (unsigned int)ftell(f));
		
		fread(&numfaces, 4, 1, f);
		if(numfaces) {
			createDirectory(dirname);
			faces = malloc(sizeof(faceinfo)*numfaces);
			fread(faces, sizeof(faceinfo)*numfaces, 1, f);
			
			for(i = 0; i < numfaces; i++) {
				printf("group %d face %02d of %02d\r", groupnum, i+1, numfaces);
				namestring = getNameFromDetails(faces[i].hash);
				
				data = malloc(faces[i].size);
				fseek(f, datoffset+faces[i].offset+4, SEEK_SET);
				fread(data, faces[i].size, 1, f);
				
				numframes = *(uint32_t *)data;
				
				if(numframes == 0x20) {
					/* simple */
					//~ printf("simple\n");
					simpleoffsets = (uint32_t *)(data+4);
					palsource = (uint16_t *)(data+32);
					for(j = 0; j < 7; j++) {
						if(simpleoffsets[j]) {
							//~ printf("x");
							image = (imageinfo *)(data+simpleoffsets[j]);
							rgba = pixelsToRGBA(data+simpleoffsets[j]+4, palsource, image->w, image->h);
							snprintf(filename, 256, "%s/%03d-%02d-x%03d-y%03d-%s", dirname, i, j, image->x, image->y, namestring);
							lodepng_encode32_file(filename, rgba, image->w, image->h);
							free(rgba);
						}
						//~ else printf("-");
					}
					//~ printf("\n");
				}
				else {
					/* full */
					fullani = (animationinfo *)(data+4);
					for(j = 0; j < numframes; j++) {
						palsource = (uint16_t *)(data+fullani[j].palette);
						image = (imageinfo *)(data+fullani[j].bitmap);
						//~ printf("off %08x - w %03x - h %03d\n", fullani[j].bitmap+4, image->w, image->h);
						rgba = pixelsToRGBA(data+fullani[j].bitmap+4, palsource, image->w, image->h);
						snprintf(filename, 256, "%s/%03d-%02d-x%03d-y%03d-%s", dirname, i, j, image->x, image->y, namestring);
						lodepng_encode32_file(filename, rgba, image->w, image->h);
						free(rgba);
					}
				}
				free(data);
				free(namestring);
			}
			printf("\n");
			free(faces);
		}
		if(ftell(f)%0x800) fseek(f, (0x800-(ftell(f)%0x800)), SEEK_CUR);
		datoffset = ftell(f);
		groupnum++;
	}
	
	
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11e8, %rsp           # imm = 0x11E8
movq %rsi, %rbx
movl %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
leaq 0xe0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x1100
movq (%rbx), %r14
cmpl $0x1, %ebp
jg 0xa3a3
leaq 0x26e3(%rip), %rdi      # 0xca7a
movq %r14, %rsi
xorl %eax, %eax
callq 0x10c0
jmp 0xa3bf
movq %r14, %rdi
callq 0x10b0
cmpq $0x1001, %rax           # imm = 0x1001
jb 0xa3d4
leaq 0x274d(%rip), %rdi      # 0xcb07
callq 0x1080
pushq $0x1
popq %rax
addq $0x11e8, %rsp           # imm = 0x11E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1060
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x10f0
movb $0x0, (%rax)
leaq 0x6d14(%rip), %rdi      # 0x11110
leaq 0x269c(%rip), %rdx      # 0xca9f
leaq -0x912e(%rip), %r8      # 0x12dc
pushq $0x2
popq %rcx
movq %r15, %rsi
callq 0x9eec
movl %eax, 0x6d05(%rip)      # 0x11120
movq 0x8(%rbx), %rdi
leaq 0x2536(%rip), %rsi      # 0xc95c
callq 0x11a0
testq %rax, %rax
je 0xa78e
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1180
movq %r15, %rdi
callq 0x1140
movq %rax, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1180
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movq %r15, 0x38(%rsp)
cmpl 0x30(%rsp), %r12d
jae 0xa773
movq %r15, %rdi
callq 0x1140
movl $0x80, %esi
leaq 0x60(%rsp), %rdi
leaq 0x262e(%rip), %rdx      # 0xcabf
movl 0x4(%rsp), %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x10e0
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x1090
movl 0x1c(%rsp), %r14d
testq %r14, %r14
je 0xa72c
leaq 0x60(%rsp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1070
imulq $0xc, %r14, %rbx
movq %rbx, %rdi
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x1090
addl $0x4, %r12d
xorl %ebx, %ebx
movq %r12, 0x48(%rsp)
movq %r14, 0x40(%rsp)
cmpq %r14, %rbx
je 0xa71a
leaq 0x1(%rbx), %rdx
leaq 0x25b4(%rip), %rdi      # 0xcac9
movl 0x4(%rsp), %esi
movq %rdx, 0x50(%rsp)
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10c0
movq %rbx, 0x28(%rsp)
imulq $0xc, %rbx, %r14
movq 0x20(%rsp), %rbp
movzwl 0x2(%rbp,%r14), %edi
callq 0xa1e6
movq %rax, 0x8(%rsp)
movl 0x4(%rbp,%r14), %ebx
movq %rbx, %rdi
callq 0x1160
movq %rax, %r13
movl 0x8(%rbp,%r14), %esi
addl %r12d, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1180
movq %r13, %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x1090
movl (%r13), %eax
movq %rax, 0x58(%rsp)
cmpq $0x20, %rax
jne 0xa63e
leaq 0x20(%r13), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
cmpq $0x7, %rbp
je 0xa6ef
movl 0x4(%r13,%rbp,4), %r15d
testq %r15, %r15
je 0xa636
leaq (%r15,%r13), %rdi
addq $0x4, %rdi
movzbl 0x2(%r13,%r15), %r14d
movzbl 0x3(%r13,%r15), %r12d
movq 0x10(%rsp), %rsi
movl %r14d, %edx
movl %r12d, %ecx
callq 0xa2c6
movq %rax, %rbx
movzbl (%r13,%r15), %r10d
movzbl 0x1(%r13,%r15), %r11d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x24e8(%rip), %rdx      # 0xcae5
leaq 0x68(%rsp), %rcx
movq 0x30(%rsp), %r8
movl %ebp, %r9d
xorl %eax, %eax
pushq 0x10(%rsp)
pushq %r11
pushq %r10
callq 0x10e0
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x8144
movq %rbx, %rdi
callq 0x1030
incq %rbp
jmp 0xa597
leaq 0x8(%r13), %rax
xorl %r12d, %r12d
cmpq %r12, 0x58(%rsp)
je 0xa6ef
movl -0x4(%rax), %esi
movl (%rax), %r15d
addq %r13, %rsi
leaq (%r15,%r13), %rdi
addq $0x4, %rdi
movzbl 0x2(%r13,%r15), %ebp
movzbl 0x3(%r13,%r15), %r14d
movl %ebp, %edx
movl %r14d, %ecx
movq %rax, 0x10(%rsp)
callq 0xa2c6
movq %rax, %rbx
movzbl (%r13,%r15), %r10d
movzbl 0x1(%r13,%r15), %r11d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x243f(%rip), %rdx      # 0xcae5
leaq 0x68(%rsp), %rcx
movq 0x30(%rsp), %r8
movl %r12d, %r9d
xorl %eax, %eax
pushq 0x10(%rsp)
pushq %r11
pushq %r10
callq 0x10e0
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x8144
movq %rbx, %rdi
callq 0x1030
movq 0x10(%rsp), %rax
incq %r12
addq $0xc, %rax
jmp 0xa645
movq %r13, %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x50(%rsp), %rbx
movq 0x38(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r14
jmp 0xa501
pushq $0xa
popq %rdi
callq 0x1040
movq 0x20(%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1140
shlq $0x35, %rax
je 0xa75f
movq %r15, %rdi
callq 0x1140
cqto
movl $0x800, %ecx            # imm = 0x800
idivq %rcx
movl $0x800, %esi            # imm = 0x800
subq %rdx, %rsi
movq %r15, %rdi
pushq $0x1
popq %rdx
callq 0x1180
movq %r15, %rdi
callq 0x1140
movq %rax, %r12
incl 0x4(%rsp)
jmp 0xa46d
leaq 0x2387(%rip), %rdi      # 0xcb01
callq 0x1080
movq %r15, %rdi
callq 0x10a0
xorl %eax, %eax
jmp 0xa3c2
movq 0x8(%rbx), %rsi
leaq 0x2310(%rip), %rdi      # 0xcaa9
jmp 0xa39a