-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkmnsprite2raw.cpp
202 lines (176 loc) · 8.15 KB
/
pkmnsprite2raw.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Converts a pkmn sprite to a rsd file.
// Arguments: ###[.*].png, where ### is the id of the corresponding sprite
// HEIGHT, WIDTH, NUM_FRAMES (if > 0, generates *rsd), THERSHOLD, MAX_ITEMS_PER_DIR
//
// Outputs the file into folder (###)/30 (padded with zeros to a 2 character name) if ###
// exists; otherwise outputs to working dir
//
// The sprite may not contain more than 16 colors. The program automatically combines
// similar colors and will do so very aggressively to enforce this color limit.
#include <cstdio>
#include <filesystem>
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include <png.h>
#include "bitmap.h"
#define conv( a ) ( (u8) ( (a) *31 / 255 ) )
#define revconv( a ) ( ( (a) *255 / 31 ) )
#define green( a ) ( revconv( ( ( a ) >> 10 ) & 31 ) )
#define blue( a ) ( revconv( ( ( a ) >> 5 ) & 31 ) )
#define red( a ) ( revconv( (a) &31 ) )
using namespace std;
namespace fs = filesystem;
u8 image_data[ 256 * 256 * 20 / 4 + 100 ] = { 0 };
unsigned short pal[ 300 ] = { 0 };
typedef tuple<u8, u8, u8> t3;
map<unsigned short, u8> palidx;
void print_tiled( FILE* p_f, u8* p_image_data ) {
// Tile the given sprite into 8x8 blocks
// 64x64 chunk
for( size_t y = 0; y < 64; y += 8 ) {
for( size_t x = 0; x < 32; x += 4 ) {
for( size_t by = 0; by < 8; ++by ) {
fwrite( p_image_data + ( y + by ) * 48 + x, 1, 4, p_f );
}
}
}
// 64x32 chunk
for( size_t y = 0; y < 64; y += 8 ) {
for( size_t x = 32; x < 48; x += 4 ) {
for( size_t by = 0; by < 8; ++by ) {
fwrite( p_image_data + ( y + by ) * 48 + x, 1, 4, p_f );
}
}
}
// 32x64 chunk
for( size_t y = 64; y < 96; y += 8 ) {
for( size_t x = 0; x < 32; x += 4 ) {
for( size_t by = 0; by < 8; ++by ) {
fwrite( p_image_data + ( y + by ) * 48 + x, 1, 4, p_f );
}
}
}
// 32x32 chunc
for( size_t y = 64; y < 96; y += 8 ) {
for( size_t x = 32; x < 48; x += 4 ) {
for( size_t by = 0; by < 8; ++by ) {
fwrite( p_image_data + ( y + by ) * 48 + x, 1, 4, p_f );
}
}
}
}
// Computes distance between colors
int col_dis( int p_1, int p_2 ) {
return abs( red( p_1 ) - red( p_2 ) ) + abs( green( p_1 ) - green( p_2 ) )
+ abs( blue( p_1 ) - blue( p_2 ) );
}
int main( int p_argc, char** p_argv ) {
if( p_argc < 2 ) {
printf( "Too few arguments.\n" );
return 1;
}
int start = 0;
bitmap in( p_argv[ 1 ] );
u8 NUM_FRAMES = 0, HEIGHT = 96, WIDTH = 96, THRESHOLD = 10;
if( p_argc >= 3 ) { sscanf( p_argv[ 2 ], "%hhu", &NUM_FRAMES ); }
if( p_argc >= 4 ) { sscanf( p_argv[ 3 ], "%hhu", &THRESHOLD ); }
u8 col = !!in( 0, 0 ).m_transparent;
bool genraw = false;
if( !NUM_FRAMES ) { genraw = ( NUM_FRAMES = 1 ); }
size_t SCALE = 1;
if( in.m_width == 192 && in.m_height == 192 ) { SCALE = 2; }
palidx[ 0 ] = 0;
for( size_t frame = 0; frame < NUM_FRAMES; ++frame )
for( size_t y = 0; y < HEIGHT; y++ )
for( size_t x = 0; x < WIDTH; x++ ) {
size_t nx = x + WIDTH * frame;
unsigned short conv_color = ( conv( in( nx * SCALE, y * SCALE ).m_red ) )
| ( conv( in( nx * SCALE, y * SCALE ).m_green ) << 5 )
| ( conv( in( nx * SCALE, y * SCALE ).m_blue ) << 10 )
| ( 1 << 15 );
if( in( nx * SCALE, y * SCALE ).m_transparent ) { conv_color = 0; }
// printf( "\x1b[48;2;%u;%u;%um%u %u %u\x1b[0;00m
// ->\x1b[48;2;%u;%u;%um %x\x1b[0;00m\n",
// in( nx, y ).m_red, in( nx, y ).m_green, in(nx, y ).m_blue,
// in( nx, y ).m_red, in( nx, y ).m_green, in(nx, y ).m_blue,
// red( conv_color ), blue( conv_color ), green( conv_color
// ), conv_color );
if( !palidx.count( conv_color ) ) {
// Check if the new color is very close to an existing color
u8 min_del = 255, del_p = 0;
for( u8 p = 1 + start; p < 16; ++p ) {
if( col_dis( conv_color, pal[ p ] ) < min_del ) {
min_del = col_dis( conv_color, pal[ p ] );
del_p = p;
}
}
if( min_del < THRESHOLD && col + start ) {
fprintf( stderr,
"[%s] replacing \x1b[48;2;%u;%u;%um%3hx\x1b[0;00m"
" with \x1b[48;2;%u;%u;%um%3hx\x1b[0;00m (%hu)\n",
p_argv[ 1 ], red( conv_color ), blue( conv_color ),
green( conv_color ), conv_color, red( pal[ del_p ] ),
blue( pal[ del_p ] ), green( pal[ del_p ] ), pal[ del_p ], del_p );
palidx[ conv_color ] = del_p;
} else if( col + start > 16 ) {
fprintf( stderr, "[%s] To COLORFUL:", p_argv[ 1 ] );
fprintf( stderr,
" replacing \x1b[48;2;%u;%u;%um%3hx\x1b[0;00m"
" with \x1b[48;2;%u;%u;%um%3hx\x1b[0;00m\n",
red( conv_color ), blue( conv_color ), green( conv_color ),
conv_color, red( pal[ del_p ] ), blue( pal[ del_p ] ),
green( pal[ del_p ] ), pal[ del_p ] );
palidx[ conv_color ] = del_p;
} else {
// fprintf( stderr, " adding color
// \x1b[48;2;%u;%u;%um%3hx\x1b[0;00m\n",
// red( conv_color ), blue( conv_color ),
// green( conv_color
// ), conv_color );
pal[ col + start ] = conv_color;
palidx[ conv_color ] = col++;
}
}
image_data[ WIDTH * HEIGHT * frame + y * WIDTH + x ] = start + palidx[ conv_color ];
}
// in.writeToFile( (string(p_argv[ 1 ]) + ".test.png").c_str() );
size_t numTiles = HEIGHT * WIDTH * NUM_FRAMES, numColors = 16;
// As we are dealing with sprites here, two neighboring pixels share a single byte.
for( size_t i = 0; i < numTiles / 2; ++i ) {
image_data[ i ] = ( image_data[ 2 * i + 1 ] << 4 ) | image_data[ 2 * i ];
}
string rspth = string( p_argv[ 1 ] );
rspth.pop_back( );
rspth.pop_back( );
rspth.pop_back( );
rspth.pop_back( );
unsigned short pkmnidx;
FILE* fout;
if( !sscanf( p_argv[ 1 ], "%03hu", &pkmnidx ) ) {
// Output to cwd
fout = fopen( ( rspth + ( genraw ? ".raw" : ".rsd" ) ).c_str( ), "wb" );
} else {
char buffer[ 50 ];
snprintf( buffer, 40, "%02hhu/%hu/", pkmnidx / MAX_ITEMS_PER_DIR, pkmnidx );
fs::create_directories( buffer );
fout = fopen( ( buffer + rspth + ( genraw ? ".raw" : ".rsd" ) ).c_str( ), "wb" );
}
u8 meta[ 3 ] = { NUM_FRAMES, WIDTH, HEIGHT };
fwrite( pal, sizeof( unsigned short int ), numColors, fout );
if( !genraw ) { fwrite( meta, sizeof( u8 ), 3, fout ); }
for( size_t fr = 0; fr < NUM_FRAMES; ++fr ) print_tiled( fout, image_data + fr * 16 * 32 );
fclose( fout );
/*
// DEBUG: Print icon on terminal
for( size_t x = 0; x < numTiles; ++x ) {
printf( "\x1b[48;2;%u;%u;%um%3hx\x1b[0;00m", red( pal[ image_data[ x ] ] ),
blue( pal[ image_data[ x ] ] ), green( pal[ image_data[ x ] ] ), image_data[ x ] );
if( ( x & 15 ) == 15 )
printf( "\n" );
if( x == numTiles / 2 - 1 )
printf( "\n" );
}
*/
}