-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextract-video-offset.c
115 lines (103 loc) · 3.99 KB
/
extract-video-offset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "ntrcom/nitrocompression.h"
#include "phoenixgfx.h"
#include "lodepng/lodepng.h"
#define BREAK(x) { printf(x); return 1; }
typedef struct {
uint32_t offset;
uint32_t size;
}__attribute__((packed)) pacEntry;
int main( int argc, char** argv ) {
if ( argc < 5 )
{
printf( "Not enough arguments given!\nUsage: %s [file] [offset in hex] [tilesx] [tilesy] [type] [offset of source palette]\nType is:\n0 - new video type\n1 - old video type (requires offset of source palette)\nFiles will be extracted in current working directory.\n", argv[0] );
return 1;
}
uint32_t i, numFiles, listOffset, givenOffset;
unsigned int resultsize, compressedsize, tilesx, tilesy, type, sourceoffset, bpp;
givenOffset = strtoul(argv[2], NULL, 16);
tilesx = strtoul(argv[3], NULL, 10);
tilesy = strtoul(argv[4], NULL, 10);
type = strtoul(argv[5], NULL, 10);
if(type) sourceoffset = strtoul(argv[6], NULL, 16);
char outputname[32] = { 0 };
unsigned char *workbuffer = NULL, *resultbuffer = NULL, *rgbaPixelData = NULL, *paletteData = NULL;
pacEntry workentry, helpentry;
FILE *f;
if( !(f = fopen( argv[1], "rb" ))) {
printf("Couldnt open file %s\n", argv[1]);
return 1;
}
fseek( f, givenOffset, SEEK_SET );
fread( &numFiles, 4, 1, f );
switch(type) {
case 0: {
for( i = 0; i < numFiles; i++ ) {
fread( &workentry, sizeof(pacEntry), 1, f );
printf("Current file offset: %08x - Current file size %08x\n", givenOffset + workentry.offset, workentry.size);
listOffset = ftell(f);
fseek( f, givenOffset + workentry.offset, SEEK_SET );
snprintf( outputname, 32, "%08d-%08x.png", i, i );
workbuffer = malloc(workentry.size);
fread( workbuffer, workentry.size, 1, f );
compressedsize = workentry.size;
resultbuffer = unpackBuffer( workbuffer, &resultsize, &compressedsize );
// generates a RGBA image
rgbaPixelData = tiledImageToRGBA( resultbuffer, tilesx, tilesy, image4bpp, 1 );
lodepng_encode32_file( outputname, rgbaPixelData, tilesx*8, tilesy*8 );
free( workbuffer );
free( resultbuffer );
free( rgbaPixelData );
fseek( f, listOffset, SEEK_SET );
}
break;
}
case 1: {
fseek( f, sourceoffset+4, SEEK_SET );
fread( &helpentry, sizeof(pacEntry), 1, f );
paletteData = malloc(helpentry.size);
bpp = (helpentry.size == 32)?image4bpp:image8bpp;
fseek( f, sourceoffset + helpentry.offset, SEEK_SET );
fread( paletteData, helpentry.size, 1, f );
fseek( f, givenOffset+4, SEEK_SET );
for( i = 0; i < numFiles; i++ ) {
fread( &workentry, sizeof(pacEntry), 1, f );
printf("Current file offset: %08x - Current file size %08x\n", givenOffset + workentry.offset, workentry.size);
listOffset = ftell(f);
fseek( f, givenOffset + workentry.offset, SEEK_SET );
snprintf( outputname, 32, "%08d-%08x.png", i, i );
workbuffer = malloc(workentry.size);
fread( workbuffer, workentry.size, 1, f );
compressedsize = workentry.size;
resultbuffer = unpackBuffer( workbuffer, &resultsize, &compressedsize );
if(bpp == image8bpp) {
if((tilesx*8*tilesy*8) != resultsize) BREAK("size dont match with uncompressed size\n");
}
else if(bpp == image4bpp) {
if(((tilesx*8*tilesy*8)/2) != resultsize) BREAK("size dont match with uncompressed size\n");
}
// generates a RGBA image
//~ rgbaPixelData = tiledImageToRGBA( resultbuffer, tilesx, tilesy, bpp, 1 );
rgbaPixelData = linearImageWithPaletteToRGBA( resultbuffer, paletteData, tilesx*8, tilesy*8, bpp, 1 );
lodepng_encode32_file( outputname, rgbaPixelData, tilesx*8, tilesy*8 );
free( workbuffer );
free( resultbuffer );
free( rgbaPixelData );
fseek( f, listOffset, SEEK_SET );
}
free(paletteData);
break;
}
default: {
printf("argh\n");
return 1;
}
}
printf("end of video at %08x\n", givenOffset + workentry.offset + workentry.size );
printf("bpp was %d %u\n", (bpp == image4bpp)?4:8, helpentry.size);
printf("Done.\n");
fclose(f);
return 0;
}