-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
415 lines (333 loc) · 12.9 KB
/
main.cc
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* File: main.cc
* Author: Nathan Duma
*
* Created on April 30, 2018, 12:27 AM
*/
#include <iostream>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <vector>
#include <string>
#include <fstream>
#include <chrono>
#include <ctime>
#include "lodepng.h"
using namespace std;
/*
Show ASCII art preview of the image
*/
void displayPreview(const vector<unsigned char> &image,
unsigned w, unsigned h, fstream &f) {
if (w > 0 && h > 0) {
unsigned w2 = 48;
if (w < w2) w2 = w;
unsigned h2 = h * w2 / w;
h2 = (h2 * 2) / 3; //compensate for non-square characters in terminal
if (h2 > (w2 * 2)) h2 = w2 * 2; //avoid too large output
f << '+';
for (unsigned x = 0; x < w2; x++) f << '-';
f << '+' << endl;
for (unsigned y = 0; y < h2; y++) {
f << "|";
for (unsigned x = 0; x < w2; x++) {
unsigned x2 = x * w / w2;
unsigned y2 = y * h / h2;
int r = image[y2 * w * 4 + x2 * 4 + 0];
int g = image[y2 * w * 4 + x2 * 4 + 1];
int b = image[y2 * w * 4 + x2 * 4 + 2];
int a = image[y2 * w * 4 + x2 * 4 + 3];
int lightness = ((r + g + b) / 3) * a / 255;
int min = (r < g && r < b) ? r : (g < b ? g : b);
int max = (r > g && r > b) ? r : (g > b ? g : b);
int saturation = max - min;
int letter = 'i'; //i for grey, or r,y,g,c,b,m for colors
if (saturation > 32) {
int h = lightness >= (min + max) / 2;
if (h) letter = (min == r ? 'c' : (min == g ? 'm' : 'y'));
else letter = (max == r ? 'r' : (max == g ? 'g' : 'b'));
}
int symbol = ' ';
if (lightness > 224) symbol = '@';
else if (lightness > 128) symbol = letter - 32;
else if (lightness > 32) symbol = letter;
else if (lightness > 16) symbol = '.';
f << (char) symbol;
}
f << "|";
f << endl;
}
f << '+';
for (unsigned x = 0; x < w2; x++) f << '-';
f << '+' << endl;
}
}
template<typename T>
string T_to_hex(T i) {
stringstream stream;
stream << "0x"
<< setfill('0') << setw(sizeof (T)*2)
<< hex << i;
return stream.str();
}
int main(int argc, char *argv[]) {
cout << "mksprite64 by Nathan Duma." << endl;
cout << "Convert png to c source file for the Nintendo 64, using the sprite & bitmap structure." << endl;
cout << "Type -h for usage" << endl;
if (argc == 1) {
cerr << "ERROR 1: no argument provided." << endl;
return 1;
}
string file, filename, scaleX, scaleY, mode;
scaleX = "1.0";
scaleY = "1.0";
mode = "16";
bool preview = false;
// parse arguments
for (int i = 1; i < argc; i++) {
if ((i + 1) >= argc) {
cerr << "ERROR 2: argument-parameter mismatch" << endl;
return 2;
}
if ((argv[i][0] == '-') && (argv[i][1] != '\0')) {
if (argv[i][1] == 'h') {
cout << "Usage:" << endl;
cout << "Specify file path (must end in .png): -f file.png" << endl;
cout << "OPTIONAL:" << endl;
cout << "Scale in x direction: -sx scaleX" << endl;
cout << "Scale in y direction: -sy scaleY" << endl;
cout << "Scale is 1.0 by default." << endl;
cout << "n-bit mode (n=16 or n=32): -m n" << endl;
cout << "Mode is 16 by default." << endl;
cout << "Show preview in c file: -p t/f" << endl;
cout << "Preview is false by default." << endl;
} else if (argv[i][1] == 's' && (argv[i][2] != '\0')) {
if (argv[i][2] == 'x') {
scaleX = argv[i + 1];
} else if (argv[i][2] == 'y') {
scaleY = argv[i + 1];
} else {
cerr << "ERROR 3: Unknown command: " << argv[i] << endl;
return 3;
}
i++;
} else if (argv[i][1] == 'f') {
file = argv[i + 1];
// get the filename if it's a directory or not
size_t slashLocation = file.find_last_of("/\\");
size_t dotLocation = file.find_last_of(".");
filename = file.substr(
slashLocation == string::npos ? 0 : slashLocation + 1,
dotLocation == string::npos ? file.size() : dotLocation);
i++;
} else if (argv[i][1] == 'p') {
if (argv[i + 1][0] == 't') {
preview = true;
} else if (argv[i + 1][0] == 'f') {
preview = false;
} else {
cerr << "ERROR 3: Unknown command: " << argv[i + 1] << endl;
return 3;
}
i++;
} else if (argv[i][1] == 'm') {
mode = argv[i + 1];
// TODO: check for errors on this
if (!(mode == "16" || mode == "32")) {
cerr << "ERROR 3: Unknown command: " << argv[i + 1] << endl;
return 3;
}
i++;
} else {
cerr << "ERROR 3: Unknown command: " << argv[i] << endl;
return 3;
}
}
}
// decode the image
vector<unsigned char> png;
vector<unsigned char> image; //the raw pixels
vector<vector < string>> fullImage;
unsigned width, height;
//load and decode
unsigned error = lodepng::load_file(png, file);
if (!error) {
error = lodepng::decode(image, width, height, png);
}
//if there's an error, display it
if (error) {
cout << "decoder error " << error << ": " << lodepng_error_text(error) << endl;
return error;
}
fullImage.resize(height);
for (auto &it : fullImage) {
it.resize(width);
}
unsigned short colour_s;
unsigned int colour_i;
int row = 0;
int col = 0;
// we need to put the image into a 2D array
// so that we can make small sprites
// for the bitmap structure
for (int i = 0; i < image.size(); i += 4) {
if (mode == "16") {
int r = image[i] / 8;
int g = image[i + 1] / 8;
int b = image[i + 2] / 8;
int a = (image[i + 3] > 0 ? 1 : 0);
colour_s = (r << 11) | (g << 6) | (b << 1) | a;
fullImage[row][col] = T_to_hex(colour_s);
} else {
int r = image[i];
int g = image[i + 1];
int b = image[i + 2];
int a = image[i + 3];
colour_i = (r << 24) | (g << 16) | (b << 8) | a;
fullImage[row][col] = T_to_hex(colour_i);
}
col++;
if (col == width) {
row++;
col = 0;
}
}
fstream f2("sp_" + filename + ".h", fstream::out);
fstream f("sp_" + filename + ".c", fstream::out);
// have all of them included in 1 h file
// this isn't thread safe so you gotta get lucky
// otherwise you can sleep
ofstream common("common_sprites.h", std::ios_base::app);
common << "#include \"" << "sp_" << filename << ".h\"" << endl;
common.close();
f << "#include \"" << "sp_" + filename + ".h\"" << endl;
f << endl;
// header
f2 << "#ifndef " << "sp_" << filename << "_h" << endl;
f2 << "#define " << "sp_" << filename << "_h" << endl;
f2 << endl;
f2 << "#include <PR/sp.h>" << endl;
f2 << endl;
// split it into texels
// use 32x32
int texelH = 32;
int texelW = 32;
int splitWidth = ceil((double) width / (double) texelW);
int splitHeight = ceil((double) height / (double) texelH);
int totalBoxes = splitWidth*splitHeight;
int boxX = 0;
int boxY = 0;
f2 << "#define " << filename << "TRUEIMAGEH\t" << height << endl;
f2 << "#define " << filename << "TRUEIMAGEW\t" << width << endl;
f2 << "#define " << filename << "IMAGEH\t" << texelH * splitHeight << endl;
f2 << "#define " << filename << "IMAGEW\t" << texelW * splitWidth << endl;
f2 << "#define " << filename << "BLOCKSIZEW\t" << texelW << endl;
f2 << "#define " << filename << "BLOCKSIZEH\t" << texelH << endl;
f2 << "#define " << filename << "SCALEX\t" << scaleX << endl;
f2 << "#define " << filename << "SCALEY\t" << scaleY << endl;
//f << "#define " << filename << "ALPHABIT\t" << "255" << endl;
f2 << "#define " << filename << "MODE\t" << "SP_Z | SP_OVERLAP | SP_TRANSPARENT" << endl;
f2 << endl;
f2 << "// extern varaibles " << endl;
f2 << "extern Bitmap " << filename << "_bitmaps[];" << endl;
f2 << "extern Gfx " << filename << "_dl[];" << endl;
f2 << endl;
f2 << "#define NUM_" << filename << "_BMS (sizeof(" << filename << "_bitmaps" << ")/sizeof(Bitmap))" << endl;
f2 << endl;
f2 << "extern Sprite " << filename << "_sprite;" << endl;
f2 << endl;
f2 << "#endif " << endl;
f2 << endl;
// preview output
if (preview) {
f2 << "#if 0 /* Image preview */" << endl;
displayPreview(image, width, height, f2);
f2 << "#endif" << endl;
f2 << endl;
}
for (int i = 0; i < totalBoxes; i++) {
//cout << "boxes: " << i << endl;
vector<vector < string>> texel;
texel.resize(texelW);
for (auto &it : texel) {
it.resize(texelH);
}
row = 0;
col = 0;
// now get the small texel form the big image
for (int i = boxX * texelW; i < ((boxX * texelW) + texelW); i++) {
for (int j = boxY * texelH; j < ((boxY * texelH) + texelH); j++) {
if (i >= fullImage.size() ||
j >= fullImage[0].size()) {
//cout << "true" << endl;
texel[row][col] = "0xfffe";
} else {
texel[row][col] = fullImage[i][j];
}
//cout << "row: " << row << " col: " << col << endl;
col++;
}
row++;
col = 0;
}
// dummy aligner
f << "static Gfx " << filename << i
<< "_C_dummy_aligner[] = { gsSPEndDisplayList() };" << endl;
f << endl;
f << "u" << mode << " " << filename << i << "_sp" << "[] = {" << endl;
// output stuff here
for (auto &it : texel) {
f << "\t";
for (auto &it2 : it) {
f << it2 << ", ";
}
f << endl;
}
f << endl;
f << "};" << endl;
f << endl;
f << endl;
boxY++;
if (boxY == splitWidth) {
boxX++;
boxY = 0;
}
}
f << endl << endl;
f << "Bitmap " << filename << "_bitmaps[] = {" << endl;
for (int i = 0; i < totalBoxes; i++) {
f << "\t";
f << "{" << filename << "BLOCKSIZEW" << ", "
<< filename << "BLOCKSIZEW" << ", 0, 0, "
<< filename << i << "_sp, "
<< filename << "BLOCKSIZEH" << ", 0},";
f << endl;
}
f << "};" << endl;
f << endl;
f << "Gfx " << filename << "_dl[NUM_DL(NUM_" << filename << "_BMS)];" << endl;
f << endl;
f << "Sprite " << filename << "_sprite = {" << endl;
f << "\t" << "0, 0, /* Position: x,y */" << endl;
f << "\t" << filename << "IMAGEW" << ", " << filename << "IMAGEH" << ", /* Sprite size in texels (x,y) */" << endl;
f << "\t" << filename << "SCALEX" << ", " << filename << "SCALEY" << ", /* Sprite Scale: x,y */" << endl;
f << "\t" << "0, 0, /* Sprite Explosion Spacing: x,y */" << endl;
f << "\t" << filename << "MODE" << ", /* Sprite Attributes */" << endl;
f << "\t" << "0x1234, /* Sprite Depth: Z */" << endl;
f << "\t" << "255, 255, 255, 255, /* Sprite Coloration: RGBA */" << endl;
f << "\t" << "0, 0, NULL, /* Color LookUp Table: start_index, length, address */" << endl;
f << "\t" << "0, 1, /* Sprite Bitmap index: start index, step increment */" << endl;
f << "\t" << "NUM_" << filename << "_BMS, /* Number of bitmaps */" << endl;
f << "\t" << "NUM_DL(" << "NUM_" << filename << "_BMS), /* Number of display list locations allocated */" << endl;
f << "\t" << filename << "BLOCKSIZEH" << ", " << filename << "BLOCKSIZEH" << ", /* Sprite Bitmap Height: Used_height, physical height */" << endl;
f << "\t" << "G_IM_FMT_RGBA, /* Sprite Bitmap Format */" << endl;
f << "\t" << "G_IM_SIZ_" << mode << "b, /* Sprite Bitmap Texel Size */" << endl;
f << "\t" << filename << "_bitmaps, /* Pointer to bitmaps */" << endl;
f << "\t" << filename << "_dl, /* Display list memory */" << endl;
f << "\t" << "NULL, /* next_dl pointer */" << endl;
f << "};" << endl;
f << endl;
f.close();
return 0;
}