forked from emilk/sproxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportExport.cpp
593 lines (460 loc) · 18 KB
/
ImportExport.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include <map>
#include <QImage>
#include <QColor>
#include <QFileInfo>
#include "ImportExport.h"
static QList<Importer*> importers;
static QList<Exporter*> exporters;
void register_importer(Importer *p) { if (p && !importers.contains(p)) importers.append(p); }
void unregister_importer(Importer *p) { int i=importers.indexOf(p); if (i>=0) importers.removeAt(i); }
const QList<Importer*>& get_importers() { return importers; }
void register_exporter(Exporter *p) { if (p && !exporters.contains(p)) exporters.append(p); }
void unregister_exporter(Exporter *p) { int i=exporters.indexOf(p); if (i>=0) exporters.removeAt(i); }
const QList<Exporter*>& get_exporters() { return exporters; }
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class ImageImporter : public Importer
{
public:
virtual QString name() { return "Image files"; }
virtual QString filter() { return "*.bmp *.gif *.jpg *.jpeg *.png *.tiff *.tif"; }
virtual bool doImport(const QString &filename, UndoManager *um,
SproxelProjectPtr project, VoxelGridGroupPtr)
{
QImage imported;
if (!imported.load(filename)) return false;
imported = imported.mirrored();
// For now we always import into the Z axis and resize if we need to
const int imageSizeX = imported.width();
const int imageSizeY = imported.height();
VoxelGridGroupPtr spr(new VoxelGridGroup(Imath::V3i(imageSizeX, imageSizeY, 1), ColorPalettePtr()));
// Splat the data in
for (int x = 0; x < imageSizeX; x++)
{
for (int y = 0; y < imageSizeY; y++)
{
const Imath::V3i locale(x, y, 0);
const QColor qcolor = imported.pixel(x, y);
const Imath::Color4f color(qcolor.redF(),
qcolor.greenF(),
qcolor.blueF(),
qcolor.alphaF());
spr->set(locale, color);
}
}
spr->setName(QFileInfo(filename).baseName());
um->addSprite(project, -1, spr);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class SproxelPngImporter : public Importer
{
public:
virtual QString name() { return "Sproxel PNG files"; }
virtual QString filter() { return "*.png"; }
virtual bool doImport(const QString &filename, UndoManager *um,
SproxelProjectPtr project, VoxelGridGroupPtr)
{
QImage readMe;
if (!readMe.load(filename, "PNG")) return false;
VoxelGridLayerPtr layer=VoxelGridLayer::fromQImage(readMe, project->mainPalette);
if (!layer) return false;
VoxelGridGroupPtr spr(new VoxelGridGroup(layer));
spr->setName(QFileInfo(filename).baseName());
um->addSprite(project, -1, spr);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class SproxelCsvImporter : public Importer
{
public:
virtual QString name() { return "Sproxel CSV files"; }
virtual QString filter() { return "*.csv"; }
virtual bool doImport(const QString &filename, UndoManager *um,
SproxelProjectPtr project, VoxelGridGroupPtr)
{
int fscanfStatus = 0;
FILE* fp = fopen(qPrintable(filename), "rb");
if (!fp) return false;
// Read the dimensions
Imath::V3i size(0);
fscanfStatus = fscanf(fp, "%d,%d,%d\n", &size.x, &size.y, &size.z);
VoxelGridGroupPtr spr(new VoxelGridGroup(size, ColorPalettePtr()));
// Read the data
Imath::Color4f color;
const Imath::V3i& cellDim = size;
for (int y = cellDim.y-1; y >= 0; y--)
{
for (int z = 0; z < cellDim.z; z++)
{
for (int x = 0; x < cellDim.x; x++)
{
int r, g, b, a;
fscanfStatus = fscanf(fp, "#%02X%02X%02X%02X,", &r, &g, &b, &a);
color.r = r / (float)0xff;
color.g = g / (float)0xff;
color.b = b / (float)0xff;
color.a = a / (float)0xff;
spr->set(Imath::V3i(x,y,z), color);
if (x != cellDim.x-1)
fscanfStatus = fscanf(fp, ",");
}
fscanfStatus = fscanf(fp, "\n");
}
fscanfStatus = fscanf(fp, "\n");
}
fclose(fp);
spr->setName(QFileInfo(filename).baseName());
um->addSprite(project, -1, spr);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
static QString ensure_ext(const QString &s, const QString &ext)
{
if (s.endsWith(ext, Qt::CaseInsensitive)) return s;
return s+ext;
}
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class SproxelPngExporter : public Exporter
{
public:
virtual QString name() { return "Sproxel PNG files"; }
virtual QString filter() { return "*.png"; }
virtual bool doExport(const QString &filename, SproxelProjectPtr, VoxelGridGroupPtr spr)
{
VoxelGridLayerPtr grid=spr->bakeLayers();
QImage img=grid->makeQImage();
return img.save(ensure_ext(filename, ".png"), "PNG");
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class SproxelCsvExporter : public Exporter
{
public:
virtual QString name() { return "Sproxel CSV files"; }
virtual QString filter() { return "*.csv"; }
virtual bool doExport(const QString &filename, SproxelProjectPtr, VoxelGridGroupPtr spr)
{
FILE* fp = fopen(qPrintable(ensure_ext(filename, ".csv")), "wb");
if (!fp) return false;
const Imath::Box3i dim=spr->bounds();
const Imath::V3i cellDim = dim.size()+Imath::V3i(1);
fprintf(fp, "%d,%d,%d\n", cellDim.x, cellDim.y, cellDim.z);
// The csv is laid out human-readable (top->bottom, Y-up, XZ, etc)
for (int y = cellDim.y-1; y >= 0; y--)
{
for (int z = 0; z < cellDim.z; z++)
{
for (int x = 0; x < cellDim.x; x++)
{
const Imath::V3i curLoc=Imath::V3i(x,y,z)+dim.min;
Imath::Color4f col = spr->get(curLoc);
fprintf(fp, "#%02X%02X%02X%02X",
(int)(col.r*0xff),
(int)(col.g*0xff),
(int)(col.b*0xff),
(int)(col.a*0xff));
if (x != cellDim.x-1)
fprintf(fp, ",");
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
fclose(fp);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class ObjExporter : public Exporter
{
public:
bool asTriangles;
ObjExporter() : asTriangles(false) {}
virtual QString name() { return "OBJ files"; }
virtual QString filter() { return "*.obj"; }
static void objWritePoly(FILE* fp, bool asTriangles,
int v0, int v1, int v2, int v3)
{
if (!asTriangles)
{
fprintf(fp, "f %d %d %d %d\n", v0, v1, v2, v3);
}
else
{
fprintf(fp, "f %d %d %d\n", v0, v1, v2);
fprintf(fp, "f %d %d %d\n", v2, v3, v0);
}
}
virtual bool doExport(const QString &in_filename, SproxelProjectPtr, VoxelGridGroupPtr spr)
{
QString filename=ensure_ext(in_filename, ".obj");
// Get file basename and extension
QFileInfo fi(filename);
QString basename = fi.completeBaseName();
QString basedir = fi.absolutePath();
// Shorthand
const Imath::Box3i dim=spr->bounds();
const Imath::V3i cellDim=dim.size()+Imath::V3i(1);
const int sx = cellDim.x;
const int sy = cellDim.y;
const int sz = cellDim.z;
// Create and write the material file
std::map<std::string, std::string> mtlMap;
// Build up the material lists
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
for (int x = 0; x < sx; x++)
{
const Imath::Color4f& color = spr->get(Imath::V3i(x, y, z)+dim.min);
if (color.a == 0.0f) continue;
char mtlName[64];
sprintf(mtlName, "mtl%d", (int)mtlMap.size());
char colorString[64];
sprintf(colorString, "Kd %.4f %.4f %.4f", color.r, color.g, color.b);
mtlMap.insert(std::pair<std::string, std::string>(std::string(colorString), std::string(mtlName)));
}
}
}
// Write .mtl file
QString mtlFilename = basedir + "/" + basename + ".mtl";
FILE* fp = fopen(mtlFilename.toLocal8Bit().constData(), "wb");
if (!fp) return false;
for(std::map<std::string, std::string>::iterator p = mtlMap.begin();
p != mtlMap.end();
++p)
{
fprintf(fp, "newmtl %s\n", p->second.c_str());
fprintf(fp, "illum 4\n");
fprintf(fp, "%s\n", p->first.c_str());
fprintf(fp, "Ka 0.00 0.00 0.00\n");
fprintf(fp, "Tf 1.00 1.00 1.00\n");
fprintf(fp, "Ni 1.00\n");
fprintf(fp, "\n");
}
fclose(fp);
// Create and write the obj file
fp = fopen(filename.toLocal8Bit().constData(), "wb");
if (!fp) return false;
// Geometry
const int vertListLength = (sx+1) * (sy+1) * (sz+1);
int* vertList = new int[vertListLength];
memset(vertList, 0, sizeof(int)*vertListLength);
// Material library
fprintf(fp, "mtllib %s.mtl\n", basename.toLocal8Bit().constData());
// The object's name
fprintf(fp, "g %s\n", basename.toLocal8Bit().constData());
// Populate the vert list
int vertIndex = 1;
for (int y = 0; y < (sy+1); y++)
{
for (int z = 0; z < (sz+1); z++)
{
for (int x = 0; x < (sx+1); x++)
{
int neighbors = 0;
if ((x!=0) && (y!=0) && (z!=0) && (spr->get(Imath::V3i(x-1, y-1, z-1)+dim.min).a != 0.0f)) neighbors++;
if ((x!=0) && (y!=0) && (z!=sz) && (spr->get(Imath::V3i(x-1, y-1, z )+dim.min).a != 0.0f)) neighbors++;
if ((x!=0) && (y!=sy) && (z!=0) && (spr->get(Imath::V3i(x-1, y, z-1)+dim.min).a != 0.0f)) neighbors++;
if ((x!=0) && (y!=sy) && (z!=sz) && (spr->get(Imath::V3i(x-1, y, z )+dim.min).a != 0.0f)) neighbors++;
if ((x!=sx) && (y!=0) && (z!=0) && (spr->get(Imath::V3i(x, y-1, z-1)+dim.min).a != 0.0f)) neighbors++;
if ((x!=sx) && (y!=0) && (z!=sz) && (spr->get(Imath::V3i(x, y-1, z )+dim.min).a != 0.0f)) neighbors++;
if ((x!=sx) && (y!=sy) && (z!=0) && (spr->get(Imath::V3i(x, y, z-1)+dim.min).a != 0.0f)) neighbors++;
if ((x!=sx) && (y!=sy) && (z!=sz) && (spr->get(Imath::V3i(x, y, z )+dim.min).a != 0.0f)) neighbors++;
if (neighbors == 0 || neighbors == 8)
continue;
const int vlIndex = (y*(sz+1)*(sx+1)) + (z*(sx+1)) + (x);
vertList[vlIndex] = vertIndex;
vertIndex++;
}
}
}
// Write the verts to the OBJ
for (int y = 0; y < (sy+1); y++)
{
for (int z = 0; z < (sz+1); z++)
{
for (int x = 0; x < (sx+1); x++)
{
Imath::V3i voxelToCheck = Imath::V3i(x,y,z);
if (x == sx) voxelToCheck.x--;
if (y == sy) voxelToCheck.y--;
if (z == sz) voxelToCheck.z--;
const Imath::M44d mat = spr->voxelTransform(voxelToCheck+dim.min);
Imath::V3d vert;
mat.multVecMatrix(Imath::V3d((x == sx) ? 0.5f : -0.5f,
(y == sy) ? 0.5f : -0.5f,
(z == sz) ? 0.5f : -0.5f), vert);
const int vlIndex = (y*(sz+1)*(sx+1)) + (z*(sx+1)) + (x);
if (vertList[vlIndex] != 0)
{
fprintf(fp, "v %f %f %f\n", vert.x, vert.y, vert.z);
}
}
}
}
// Create all faces
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
for (int x = 0; x < sx; x++)
{
const Imath::Color4f& color = spr->get(Imath::V3i(x, y, z)+dim.min);
if (color.a == 0.0f)
continue;
// Check for crossings
bool crossNegX = false;
bool crossPosX = false;
if (x == 0)
crossNegX = true;
else if (color.a != spr->get(Imath::V3i(x-1,y,z)+dim.min).a)
crossNegX = true;
if (x == sx-1)
crossPosX = true;
else if (color.a != spr->get(Imath::V3i(x+1,y,z)+dim.min).a)
crossPosX = true;
bool crossNegY = false;
bool crossPosY = false;
if (y == 0)
crossNegY = true;
else if (color.a != spr->get(Imath::V3i(x,y-1,z)+dim.min).a)
crossNegY = true;
if (y == sy-1)
crossPosY = true;
else if (color.a != spr->get(Imath::V3i(x,y+1,z)+dim.min).a)
crossPosY = true;
bool crossNegZ = false;
bool crossPosZ = false;
if (z == 0)
crossNegZ = true;
else if (color.a != spr->get(Imath::V3i(x,y,z-1)+dim.min).a)
crossNegZ = true;
if (z == sz-1)
crossPosZ = true;
else if (color.a != spr->get(Imath::V3i(x,y,z+1)+dim.min).a)
crossPosZ = true;
// If there are any crossings, you will need a material
if (crossNegX || crossPosX || crossNegY || crossPosY || crossNegZ || crossPosZ)
{
char colorString[64];
sprintf(colorString, "Kd %.4f %.4f %.4f", color.r, color.g, color.b);
const std::string mtl = mtlMap.find(colorString)->second;
fprintf(fp, "usemtl %s\n", mtl.c_str());
}
// Fill in the voxels
const int* vl = vertList;
const int vi = ((y) *(sz+1)*(sx+1)) + ((z) *(sx+1)) + (x);
const int viNextZ = ((y) *(sz+1)*(sx+1)) + ((z+1)*(sx+1)) + (x);
const int viNextY = ((y+1)*(sz+1)*(sx+1)) + ((z) *(sx+1)) + (x);
const int viNextZY = ((y+1)*(sz+1)*(sx+1)) + ((z+1)*(sx+1)) + (x);
if (crossNegX)
objWritePoly(fp, asTriangles, vl[vi], vl[viNextZ], vl[viNextZY], vl[viNextY]);
if (crossPosX)
objWritePoly(fp, asTriangles, vl[vi+1], vl[viNextY+1], vl[viNextZY+1], vl[viNextZ+1]);
if (crossNegY)
objWritePoly(fp, asTriangles, vl[vi], vl[vi+1], vl[viNextZ+1], vl[viNextZ]);
if (crossPosY)
objWritePoly(fp, asTriangles, vl[viNextY], vl[viNextZY], vl[viNextZY+1], vl[viNextY+1]);
if (crossNegZ)
objWritePoly(fp, asTriangles, vl[vi], vl[viNextY], vl[viNextY+1], vl[vi+1]);
if (crossPosZ)
objWritePoly(fp, asTriangles, vl[viNextZ], vl[viNextZ+1], vl[viNextZY+1], vl[viNextZY]);
}
}
}
delete[] vertList;
fclose(fp);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class ObjTriangleExporter : public ObjExporter
{
public:
ObjTriangleExporter() { asTriangles=true; }
virtual QString name() { return "OBJ triangle files"; }
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class PalImporter : public Importer
{
public:
virtual QString name() { return "JASC-PAL palette files"; }
virtual QString filter() { return "*.pal"; }
virtual bool doImport(const QString &filename, UndoManager *um,
SproxelProjectPtr project, VoxelGridGroupPtr)
{
FILE *fp=fopen(qPrintable(filename), "rt");
if (!fp) return false;
SproxelColor colors[256];
char str[9];
int num=0;
if (fscanf(fp, "%8s\n", str)!=1 || strcmp(str, "JASC-PAL")!=0) goto error;
if (fscanf(fp, "%4s\n", str)!=1 || strcmp(str, "0100")!=0) goto error;
if (fscanf(fp, "%u\n", &num)!=1) goto error;
if (num>256) num=256;
for (int i=0; i<num; ++i)
{
int r, g, b;
if (fscanf(fp, "%u %u %u\n", &r, &g, &b)!=3) goto error;
colors[i]=SproxelColor(r/255.0f, g/255.0f, b/255.0f, 1);
}
fclose(fp);
um->beginMacro("Import palette");
for (int i=0; i<num; ++i) um->setPaletteColor(project->mainPalette, i, colors[i]);
um->endMacro();
return true;
error:
fclose(fp);
return false;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
class PalExporter : public Exporter
{
public:
virtual QString name() { return "JASC-PAL palette files"; }
virtual QString filter() { return "*.pal"; }
static int convert(float f)
{
int i=int(f*255);
if (i>255) return 255;
if (i<0) return 0;
return i;
}
virtual bool doExport(const QString &filename, SproxelProjectPtr project, VoxelGridGroupPtr)
{
FILE *fp=fopen(qPrintable(ensure_ext(filename, ".pal")), "wb");
if (!fp) return false;
ColorPalettePtr pal=project->mainPalette;
fprintf(fp, "JASC-PAL\n0100\n%u\n", pal->numColors());
for (int i=0; i<pal->numColors(); ++i)
{
SproxelColor c=pal->color(i);
fprintf(fp, "%u %u %u\n", convert(c.r), convert(c.g), convert(c.b));
}
fclose(fp);
return true;
}
};
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
void register_builtin_importers_exporters()
{
#define REG(name) static name s_##name; register_importer(& s_##name);
REG(SproxelPngImporter)
REG(SproxelCsvImporter)
REG(ImageImporter)
REG(PalImporter)
#undef REG
#define REG(name) static name s_##name; register_exporter(& s_##name);
REG(ObjExporter)
REG(ObjTriangleExporter)
REG(SproxelPngExporter)
REG(SproxelCsvExporter)
REG(PalExporter)
#undef REG
}