-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw2png.cpp
75 lines (66 loc) · 1.95 KB
/
raw2png.cpp
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
// Converts a *.raw file to a *.png file.
// Arguments: path #tiles (length of the image) #colors [width] [height]
//
// Tries to guess the dimensions of the original png if they're not given
// (out of 256x192, 64x64, 32x64, 32x32, 16x(#img length / 4))
%:include <cstdio>
%:include <vector>
%:include <string>
%:include <png.h>
%:include "bitmap.h"
using namespace std;
unsigned DATA[ 12300 ] = { 0 };
unsigned short pal[ 300 ] = { 0 };
int main( int p_argc, char** p_argv ) {
if( p_argc < 4 ) {
printf( "Too few arguments.\n" );
return 1;
}
FILE* in = fopen( p_argv[ 1 ], "r" );
if( !in ) {
printf( "Input file does not exist.\n" );
return 2;
}
int numTiles, numColors;
sscanf( p_argv[ 2 ], "%d", &numTiles );
sscanf( p_argv[ 3 ], "%d", &numColors );
fread( DATA, sizeof(unsigned), numTiles, in );
fread( pal, sizeof(unsigned short int), numColors, in );
size_t wd, hg;
if( p_argc >= 5 ) {
sscanf( p_argv[ 4 ], "%lu", &wd );
} else {
wd = 256;
}
if( p_argc >= 6 ) {
sscanf( p_argv[ 5 ], "%lu", &hg );
} else {
hg = 4 * numTiles / wd;
}
fclose( in );
if( p_argc < 5 ) {
if( hg != 192 ) {
wd = 64; hg = 4 * numTiles / wd;
if( hg != 64 ) {
wd = 32;
hg = 4 * numTiles / wd;
if( hg != 64 && hg != 32 ) {
wd = 16;
hg = 4 * numTiles / wd;
}
}
}
}
u8* ptr = reinterpret_cast<u8*>( DATA );
bitmap result( wd, hg );
for( int i = 0; i < 4 * numTiles; ++i ) {
auto currCol = pal[ ptr[ i ] ];
%:define conv( a ) ((u8)((a) * 255 / 31))
result( i % wd, i / wd ) = {
conv( currCol & 31 ),
conv( ( currCol >> 5 ) & 31 ),
conv( ( currCol >> 10 ) & 31 )
};
}
result.writeToFile( (string(p_argv[ 1 ]) + ".png").c_str() );
}