// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1230, %rsp           # imm = 0x1230
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x1018(%rbp)
leaq -0x10a0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x10f0
leaq -0x11a0(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x10f0
movl $0x0, -0x11b0(%rbp)
movl $0x0, -0x11b4(%rbp)
movq $0x0, -0x11c0(%rbp)
movq $0x0, -0x11c8(%rbp)
movq $0x0, -0x11d0(%rbp)
movq $0x0, -0x11e0(%rbp)
movq $0x0, -0x11e8(%rbp)
movq $0x0, -0x11f0(%rbp)
movq $0x0, -0x11f8(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x120c2
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x2473(%rip), %rdi      # 0x14522
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x12750
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x10a0
cmpq $0x1000, %rax           # imm = 0x1000
jbe 0x120f0
leaq 0x246a(%rip), %rdi      # 0x14547
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x12750
leaq -0x1010(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x1060
leaq -0x1010(%rbp), %rdi
movl $0x2f, %esi
callq 0x10e0
movb $0x0, (%rax)
leaq -0x1010(%rbp), %rsi
leaq 0x63f3(%rip), %rdi      # 0x18518
leaq 0x2443(%rip), %rdx      # 0x1456f
movl $0x2, %ecx
leaq -0x10e68(%rip), %r8     # 0x12d0
callq 0x11940
movl %eax, 0x63e5(%rip)      # 0x18528
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x22ce(%rip), %rsi      # 0x14420
callq 0x1190
movq %rax, -0x1200(%rbp)
cmpq $0x0, %rax
jne 0x12186
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x2406(%rip), %rdi      # 0x14579
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x12750
movq -0x1200(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1170
movq -0x1200(%rbp), %rdi
callq 0x1130
movl %eax, -0x11ac(%rbp)
movq -0x1200(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1170
movl -0x11b0(%rbp), %eax
cmpl -0x11ac(%rbp), %eax
jae 0x1272f
leaq -0x10a0(%rbp), %rax
movq %rax, -0x1210(%rbp)
movl -0x11b4(%rbp), %eax
movl %eax, -0x1204(%rbp)
movq -0x1200(%rbp), %rdi
callq 0x1130
movq -0x1210(%rbp), %rdi
movl -0x1204(%rbp), %ecx
movl %eax, %r8d
movl $0x80, %esi
leaq 0x237c(%rip), %rdx      # 0x1458f
movb $0x0, %al
callq 0x10d0
movq -0x1200(%rbp), %rcx
leaq -0x11d4(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
cmpl $0x0, -0x11d4(%rbp)
je 0x126b0
leaq -0x10a0(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1070
movl -0x11d4(%rbp), %eax
imulq $0xc, %rax, %rdi
callq 0x1150
movq %rax, -0x11e0(%rbp)
movq -0x11e0(%rbp), %rdi
movl -0x11d4(%rbp), %eax
imulq $0xc, %rax, %rsi
movq -0x1200(%rbp), %rcx
movl $0x1, %edx
callq 0x1080
movl $0x0, -0x11a4(%rbp)
movl -0x11a4(%rbp), %eax
cmpl -0x11d4(%rbp), %eax
jae 0x12696
movl -0x11b4(%rbp), %esi
movl -0x11a4(%rbp), %edx
addl $0x1, %edx
movl -0x11d4(%rbp), %ecx
leaq 0x22d4(%rip), %rdi      # 0x14599
movb $0x0, %al
callq 0x10b0
movq -0x11e0(%rbp), %rax
movl -0x11a4(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x2(%rax), %edi
callq 0x11dc0
movq %rax, -0x1018(%rbp)
movq -0x11e0(%rbp), %rax
movl -0x11a4(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %edi
callq 0x1150
movq %rax, -0x11c0(%rbp)
movq -0x1200(%rbp), %rdi
movl -0x11b0(%rbp), %eax
movq -0x11e0(%rbp), %rcx
movl -0x11a4(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
addl 0x8(%rcx), %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1170
movq -0x11c0(%rbp), %rdi
movq -0x11e0(%rbp), %rax
movl -0x11a4(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %esi
movq -0x1200(%rbp), %rcx
movl $0x1, %edx
callq 0x1080
movq -0x11c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x11d8(%rbp)
cmpl $0x20, -0x11d8(%rbp)
jne 0x124f9
movq -0x11c0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x11e8(%rbp)
movq -0x11c0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x11d0(%rbp)
movl $0x0, -0x11a8(%rbp)
cmpl $0x7, -0x11a8(%rbp)
jae 0x124f4
movq -0x11e8(%rbp), %rax
movl -0x11a8(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x124de
movq -0x11c0(%rbp), %rax
movq -0x11e8(%rbp), %rcx
movl -0x11a8(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
addq %rcx, %rax
movq %rax, -0x11f8(%rbp)
movq -0x11c0(%rbp), %rdi
movq -0x11e8(%rbp), %rax
movl -0x11a8(%rbp), %ecx
movl (%rax,%rcx,4), %eax
addq %rax, %rdi
addq $0x4, %rdi
movq -0x11d0(%rbp), %rsi
movq -0x11f8(%rbp), %rax
movzbl 0x2(%rax), %edx
movq -0x11f8(%rbp), %rax
movzbl 0x3(%rax), %ecx
callq 0x11f60
movq %rax, -0x11c8(%rbp)
leaq -0x11a0(%rbp), %rdi
leaq -0x10a0(%rbp), %rcx
movl -0x11a4(%rbp), %r8d
movl -0x11a8(%rbp), %r9d
movq -0x11f8(%rbp), %rax
movzbl (%rax), %r11d
movq -0x11f8(%rbp), %rax
movzbl 0x1(%rax), %r10d
movq -0x1018(%rbp), %rax
movl $0x100, %esi            # imm = 0x100
leaq 0x2121(%rip), %rdx      # 0x145b5
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x10d0
leaq -0x11a0(%rbp), %rdi
movq -0x11c8(%rbp), %rsi
movq -0x11f8(%rbp), %rax
movzbl 0x2(%rax), %edx
movq -0x11f8(%rbp), %rax
movzbl 0x3(%rax), %ecx
callq 0xa0c0
movq -0x11c8(%rbp), %rdi
callq 0x1030
jmp 0x124e0
movl -0x11a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11a8(%rbp)
jmp 0x123c2
jmp 0x1266a
movq -0x11c0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x11f0(%rbp)
movl $0x0, -0x11a8(%rbp)
movl -0x11a8(%rbp), %eax
cmpl -0x11d8(%rbp), %eax
jae 0x12668
movq -0x11c0(%rbp), %rax
movq -0x11f0(%rbp), %rcx
movl -0x11a8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x11d0(%rbp)
movq -0x11c0(%rbp), %rax
movq -0x11f0(%rbp), %rcx
movl -0x11a8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x11f8(%rbp)
movq -0x11c0(%rbp), %rdi
movq -0x11f0(%rbp), %rax
movl -0x11a8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addq %rax, %rdi
addq $0x4, %rdi
movq -0x11d0(%rbp), %rsi
movq -0x11f8(%rbp), %rax
movzbl 0x2(%rax), %edx
movq -0x11f8(%rbp), %rax
movzbl 0x3(%rax), %ecx
callq 0x11f60
movq %rax, -0x11c8(%rbp)
leaq -0x11a0(%rbp), %rdi
leaq -0x10a0(%rbp), %rcx
movl -0x11a4(%rbp), %r8d
movl -0x11a8(%rbp), %r9d
movq -0x11f8(%rbp), %rax
movzbl (%rax), %r11d
movq -0x11f8(%rbp), %rax
movzbl 0x1(%rax), %r10d
movq -0x1018(%rbp), %rax
movl $0x100, %esi            # imm = 0x100
leaq 0x1fab(%rip), %rdx      # 0x145b5
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x10d0
leaq -0x11a0(%rbp), %rdi
movq -0x11c8(%rbp), %rsi
movq -0x11f8(%rbp), %rax
movzbl 0x2(%rax), %edx
movq -0x11f8(%rbp), %rax
movzbl 0x3(%rax), %ecx
callq 0xa0c0
movq -0x11c8(%rbp), %rdi
callq 0x1030
movl -0x11a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11a8(%rbp)
jmp 0x12515
jmp 0x1266a
movq -0x11c0(%rbp), %rdi
callq 0x1030
movq -0x1018(%rbp), %rdi
callq 0x1030
movl -0x11a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11a4(%rbp)
jmp 0x12297
leaq 0x1e23(%rip), %rdi      # 0x144c0
movb $0x0, %al
callq 0x10b0
movq -0x11e0(%rbp), %rdi
callq 0x1030
movq -0x1200(%rbp), %rdi
callq 0x1130
movl $0x800, %ecx            # imm = 0x800
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x12709
movq -0x1200(%rbp), %rax
movq %rax, -0x1218(%rbp)
movq -0x1200(%rbp), %rdi
callq 0x1130
movq -0x1218(%rbp), %rdi
movl $0x800, %ecx            # imm = 0x800
cqto
idivq %rcx
movl $0x800, %esi            # imm = 0x800
subq %rdx, %rsi
movl $0x1, %edx
callq 0x1170
movq -0x1200(%rbp), %rdi
callq 0x1130
movl %eax, -0x11b0(%rbp)
movl -0x11b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11b4(%rbp)
jmp 0x121bf
leaq 0x1e9b(%rip), %rdi      # 0x145d1
movb $0x0, %al
callq 0x10b0
movq -0x1200(%rbp), %rdi
callq 0x1090
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1230, %rsp           # imm = 0x1230
popq %rbp
retq