-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
805 lines (661 loc) · 22.4 KB
/
main.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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
main.c
planettool
Copyright © 2009–2010 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "FPMPNG.h"
#include "SphericalPixelSource.h"
#include "PTPowerManagement.h"
// Sources
#include "LatLongGridGenerator.h"
#include "ReadLatLong.h"
#include "ReadCube.h"
#include "MatrixTransformer.h"
#include "CosineBlurFilter.h"
// Sinks
#include "RenderToLatLong.h"
#include "RenderToCube.h"
#include "RenderToMercator.h"
#include "RenderToGallPeters.h"
static void LoadErrorHandler(const char *message, bool isError, void *context);
static void RenderErrorHandler(const char *message, void *context);
typedef struct
{
const char *name;
const char shortcut;
} FilterEntryBase;
typedef struct
{
FilterEntryBase keys;
SphericalPixelSourceConstructorFunction constructor;
SphericalPixelSourceDestructorFunction destructor;
} SourceEntry;
typedef struct
{
FilterEntryBase keys;
SphericalPixelSinkFunction sink;
size_t defaultSize;
} SinkEntry;
typedef struct
{
RenderFlags flags;
const SourceEntry *source;
const SinkEntry *sink;
size_t size;
size_t defaultSize;
OOMatrix transform;
double cosBlurBackFactor;
double cosBlurFrontFactor;
bool showHelp;
bool showVersion;
bool quiet;
bool sixteenBit;
bool cosBlur;
const char *sourcePath;
const char *sinkPath;
} Settings;
static bool InterpretArguments(int argc, const char * argv[], Settings *settings);
static bool PrintProgress(size_t numerator, size_t denominator, void *context);
int main (int argc, const char * argv[])
{
FPMInit();
srand(time(NULL));
PTStartPreventingSleep();
// Work out what the user wants.
Settings settings;
memset(&settings, 0, sizeof settings);
settings.transform = kIdentityMatrix;
if (!InterpretArguments(argc, argv, &settings))
{
return (settings.showHelp || settings.showVersion) ? 0 : EXIT_FAILURE;
}
assert(settings.source != NULL && settings.sink != NULL);
// Read input file, if any.
FloatPixMapRef sourcePM = NULL;
if (settings.sourcePath != NULL)
{
if (!settings.quiet) printf("Reading...\n");
sourcePM = FPMCreateWithPNG(settings.sourcePath, kFPMGammaLinear, NULL, NULL, NULL);
if (sourcePM == NULL)
{
fprintf(stderr, "Could not load %s\n", settings.sourcePath);
return EXIT_FAILURE;
}
}
// Run source constructor.
void *sourceContext = NULL;
SphericalPixelSourceFunction source = NULL;
if (settings.source->constructor != NULL)
{
if (!settings.source->constructor(sourcePM, settings.flags, &source, &sourceContext))
{
return EXIT_FAILURE;
}
}
SphericalPixelSourceDestructorFunction destructor = settings.source->destructor;
// Set up matrix filter if necessary.
if (!OOMatrixIsIdentity(settings.transform))
{
void *transformContext = NULL;
if (!MatrixTransformerSetUp(source, destructor, sourceContext, settings.transform, &transformContext))
{
return EXIT_FAILURE;
}
source = MatrixTransformer;
destructor = MatrixTransformerDestructor;
sourceContext = transformContext;
}
// Set up cos blur filter if requested.
if (settings.cosBlur)
{
void *cosBlurContext = NULL;
if (!CosineBlurFilterSetUp(source, destructor, sourceContext, settings.size, settings.cosBlurBackFactor, settings.cosBlurFrontFactor, &cosBlurContext))
{
return EXIT_FAILURE;
}
source = CosineBlurFilter;
destructor = CosineBlurFilterDestructor;
sourceContext = cosBlurContext;
}
// Render.
ProgressCallbackFunction progressCB = NULL;
unsigned progressCtxt = 0;
if (!settings.quiet)
{
printf("Rendering...\n");
progressCB = PrintProgress;
}
FloatPixMapRef resultPM = settings.sink->sink(settings.size, settings.flags, source, sourceContext, progressCB, RenderErrorHandler, &progressCtxt);
FPMRelease(&sourcePM);
if (!settings.quiet) printf("\n");
if (resultPM == NULL)
{
fprintf(stderr, "Rendering failed.\n");
return EXIT_FAILURE;
}
// Destructimacate.
if (destructor != NULL) destructor(sourceContext);
// Write output.
if (!settings.quiet) printf("Writing...\n");
FPMWritePNGFlags flags = kFPMWritePNGDither;
if (settings.sixteenBit) flags = kFPMWritePNG16BPC;
if (!FPMWritePNG(resultPM, settings.sinkPath, flags, kFPMGammaLinear, kFPMGammaSRGB, LoadErrorHandler, NULL, NULL))
{
return EXIT_FAILURE;
}
if (!settings.quiet) printf("Done.\n");
return 0;
}
static void LoadErrorHandler(const char *message, bool isError, void *context)
{
fprintf(stderr, "%s: %s\n", isError ? "ERROR" : "WARNING", message);
}
static void RenderErrorHandler(const char *message, void *context)
{
fprintf(stderr, "%s\n", message);
}
/* Argument parser functions.
These will never be called with NULL arguments, and argv will have at least
minArgs (as specified in the Arguments struct) elements.
*/
typedef bool (*ArgumentParserFunction)(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseInput(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseGenerate(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseOutput(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseSize(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseFast(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseJitter(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseSixteenBit(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseRotate(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseCosBlur(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseFlip(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseHelp(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseVersion(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static bool ParseQuiet(int argc, const char *argv[], int *consumedArgs, Settings *settings);
static const SourceEntry sGenerators[] =
{
{{ "grid1", 'g', }, LatLongGridGeneratorConstructor, NULL }
};
enum { sGeneratorCount = sizeof sGenerators / sizeof sGenerators[0] };
static const SourceEntry sReaders[] =
{
{{ "latlong", 'l', }, ReadLatLongConstructor, ReadLatLongDestructor },
{{ "cube", 'c', }, ReadCubeConstructor, ReadCubeDestructor },
{{ "cubex", 'x', }, ReadCubeCrossConstructor, ReadCubeDestructor },
};
enum { sReaderCount = sizeof sReaders / sizeof sReaders[0] };
static const SinkEntry sSinks[] =
{
{{ "latlong", 'l', }, RenderToLatLong, 2048 },
{{ "cube", 'c', }, RenderToCube, 1024 },
{{ "cubex", 'x', }, RenderToCubeCross, 1024 },
{{ "mercator", 'm', }, RenderToMercator, 2048 },
{{ "gall-peters", 'g', }, RenderToGallPeters, 2048 }
};
enum { sSinkCount = sizeof sSinks / sizeof sSinks[0] };
typedef struct
{
char *keyword;
char shortcut;
unsigned minArgs;
ArgumentParserFunction handler;
// Stuff used to generate documentation, such as it is.
char *paramNames;
bool required;
bool hidden;
char *description;
FilterEntryBase *descTypes;
off_t descStride;
unsigned descTypeCount;
} ArgumentHandler;
static const ArgumentHandler sHandlers[] =
{
{
"output", 'o', 2, ParseOutput,
"<outType> <outFile>", true, false, "Type and name of output file. Type must be one of: ", (FilterEntryBase *)sSinks, sizeof(*sSinks), sSinkCount
},
{
"input", 'i', 2, ParseInput,
"<inType> <inFile>", false, false, "Type and name of input file. Type must be one of: ", (FilterEntryBase *)sReaders, sizeof(*sReaders), sReaderCount
},
{
"generate", 'g', 1, ParseGenerate,
"<generator>", false, false, "Type and name of generator. Type must be one of: ", (FilterEntryBase *)sGenerators, sizeof(*sGenerators), sGeneratorCount
},
{
"size", 'S', 1, ParseSize,
"<size>", false, false, "Size of output, in pixels. Interpretation depends on output type.", NULL, 0, 0
},
{
"fast", 'F', 0, ParseFast,
NULL, false, false, "Use faster, low-quality rendering.", NULL, 0, 0
},
{
"jitter", 'J', 0, ParseJitter,
NULL, false, false, "Use jittering for slower, slightly noisy rendering which may look better in some cases.", NULL, 0, 0
},
{
"sixteen-bit", 0, 0, ParseSixteenBit,
NULL, false, false, "Save in sixteen bit per channel format (instead of eight-bit-per-channel format).", NULL, 0, 0
},
{
"flip", 'L', 0, ParseFlip,
NULL, false, false, "Mirror the texture in 3D space (through the YZ plane) while rendering. This produces an \"inside-out\" texture.", NULL, 0, 0
},
{
"rotate", 'R', 3, ParseRotate,
"<ry> <rx> <rz>", false, false, "Rotate the texture around the planet while rendering. The ry axis corresponds to the planet's axis of rotation.", NULL, 0, 0
},
{
"cosblur", 0, 2, ParseCosBlur,
"<unmaskedscale> <maskedscale>", false, true, "Apply cosine blur (converts environment map into diffuse light map).", NULL, 0, 0
},
{
"help", 'H', 0, ParseHelp,
NULL, false, false, "Show this helpful help.", NULL, 0, 0
},
{
"version", 'V', 0, ParseVersion,
NULL, false, false, "Show version number.", NULL, 0, 0
},
{
"quiet", 'Q', 0, ParseQuiet,
NULL, false, false, "Don't print progress information.", NULL, 0, 0
},
};
static const unsigned sHandlerCount = sizeof sHandlers / sizeof sHandlers[0];
static void ShowHelp(bool showHidden);
static void ShowVersion(void);
static bool InterpretArguments(int argc, const char * argv[], Settings *settings)
{
bool error = false;
int index;
for (index = 1; index < argc && !error; index++)
{
const char *keyword = argv[index];
if (keyword[0] == '-')
{
keyword++;
// Check if it's a single-character shortcut, like -I for --input
char shortcut = '\0';
if (keyword[0] != 0 && keyword[1] == 0)
{
shortcut = keyword[0];
}
else
{
// Otherwise, treat -foo and --foo the same.
if (keyword[0] == '-') keyword++;
}
// Search for an appropriate handler.
unsigned handlerIdx;
bool match = false;
for (handlerIdx = 0; handlerIdx < sHandlerCount && !match; handlerIdx++)
{
const ArgumentHandler *handler = &sHandlers[handlerIdx];
if (shortcut != '\0') match = (shortcut == handler->shortcut);
else match = (strcmp(keyword, handler->keyword) == '\0');
if (match)
{
unsigned handlerArgc = argc - index - 1;
if (handlerArgc < handler->minArgs)
{
fprintf(stderr, "Option %s requires %u arguments.\n", argv[index], handler->minArgs);
error = true;
break;
}
int consumedArgs = 0;
error = !handler->handler(handlerArgc, argv + index + 1, &consumedArgs, settings);
if (consumedArgs > 0) index += consumedArgs;
}
}
if (!match && !error)
{
fprintf(stderr, "Unknown option %s.\n", argv[index]);
error = true;
}
}
else
{
fprintf(stderr, "Expected option identifier, got \"%s\".\n", argv[index]);
error = true;
}
}
if (settings->showHelp)
{
ShowHelp(false);
}
else if (settings->showVersion)
{
ShowVersion();
}
if (!error && settings->source == NULL)
{
if (!settings->showHelp || settings->showVersion) fprintf(stderr, "No %s specified. Try planettool --help for help.\n", "input");
error = true;
}
if (!error && settings->sink == NULL)
{
if (!settings->showHelp || settings->showVersion) fprintf(stderr, "No %s specified. Try planettool --help for help.\n", "output");
error = true;
}
if (settings->size == 0) settings->size = settings->defaultSize;
return !error;
}
static bool ParseInput(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
const char *inputType = argv[0];
const char *inputPath = argv[1];
*consumedArgs = 2;
char shortcut = '\0';
if (inputType[0] != '\0' && inputType[1] == '\0')
{
shortcut = inputType[0];
}
unsigned i;
bool match = false;
for (i = 0; i != sReaderCount; i++)
{
const SourceEntry *source = &sReaders[i];
if (shortcut != '\0') match = (shortcut == source->keys.shortcut);
else match = (strcmp(inputType, source->keys.name) == 0);
if (match)
{
settings->source = source;
settings->sourcePath = inputPath;
break;
}
}
if (!match)
{
fprintf(stderr, "Unknown input type \"%s\"\n", inputType);
}
return match;
}
static bool ParseGenerate(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
const char *name = argv[0];
*consumedArgs = 1;
char shortcut = '\0';
if (name[0] != '\0' && name[1] == '\0')
{
shortcut = name[0];
}
unsigned i;
bool match = false;
for (i = 0; i != sGeneratorCount; i++)
{
const SourceEntry *source = &sGenerators[i];
if (shortcut != '\0') match = (shortcut == source->keys.shortcut);
else match = (strcmp(name, source->keys.name) == 0);
if (match)
{
settings->source = source;
settings->sourcePath = NULL;
break;
}
}
if (!match)
{
fprintf(stderr, "Unknown generator \"%s\"\n", name);
}
return match;
}
static bool ParseOutput(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
const char *outputType = argv[0];
const char *outputPath = argv[1];
*consumedArgs = 2;
char shortcut = '\0';
if (outputType[0] != '\0' && outputType[1] == '\0')
{
shortcut = outputType[0];
}
unsigned i;
bool match = false;
for (i = 0; i != sSinkCount; i++)
{
const SinkEntry *sink = &sSinks[i];
if (shortcut != '\0') match = (shortcut == sink->keys.shortcut);
else match = (strcmp(outputType, sink->keys.name) == 0);
if (match)
{
settings->sink = sink;
settings->sinkPath = outputPath;
settings->defaultSize = sink->defaultSize;
break;
}
}
if (!match)
{
fprintf(stderr, "Unknown output type \"%s\"\n", outputType);
}
return match;
}
static bool ParseSize(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
char *end = NULL;
long size = strtoul(argv[0], &end, 10);
*consumedArgs += 1;
if (*end != '\0' || errno == ERANGE || errno == EINVAL)
{
fprintf(stderr, "Could not interpret size argument \"%s\" as a positive integer.\n", argv[0]);
return false;
}
if (size < 1)
{
fprintf(stderr, "Size may not be zero.\n");
return false;
}
settings->size = size;
return true;
}
static bool ParseFast(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->flags |= kRenderFast;
return true;
}
static bool ParseJitter(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->flags |= kRenderJitter;
return true;
}
static bool ParseSixteenBit(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->sixteenBit = true;
return true;
}
static bool ParseOneFloat(const char *string, double *value)
{
assert(string != NULL && value != NULL);
char *end = NULL;
*value = strtof(string, &end);
if (end != string) return true;
else
{
fprintf(stderr, "Could not interpret argument \"%s\" as a number.\n", string);
return false;
}
}
static bool ParseRotate(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
*consumedArgs += 3;
double rx, ry, rz;
if (!ParseOneFloat(argv[0], &rx)) return false;
if (!ParseOneFloat(argv[1], &ry)) return false;
if (!ParseOneFloat(argv[2], &rz)) return false;
OOMatrix rotation = kIdentityMatrix;
rotation = OOMatrixRotateX(rotation, rx * kDegToRad);
rotation = OOMatrixRotateZ(rotation, rz * kDegToRad);
// Y axis (planetary axis) rotation is deliberately applied last. This makes it rotate around the *original* axis of rotation.
rotation = OOMatrixRotateY(rotation, ry * kDegToRad);
settings->transform = OOMatrixMultiply(settings->transform, rotation);
return true;
}
static bool ParseCosBlur(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
*consumedArgs += 2;
if (!ParseOneFloat(argv[0], &settings->cosBlurBackFactor)) return false;
if (!ParseOneFloat(argv[1], &settings->cosBlurFrontFactor)) return false;
settings->cosBlur = true;
return true;
}
static bool ParseFlip(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->transform = OOMatrixScale(settings->transform, -1, 1, 1);
return true;
}
static bool ParseHelp(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->showHelp = true;
return true;
}
static bool ParseVersion(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->showVersion = true;
return true;
}
static bool ParseQuiet(int argc, const char *argv[], int *consumedArgs, Settings *settings)
{
settings->quiet = true;
return true;
}
static void ShowHelp(bool showHidden)
{
printf("Planettool version %s\nplanettool", PLANETTOOL_VERSION);
// ACT I: the Synopsis. Dramatis personae: a gaggle of Shortcuts.
unsigned i;
const ArgumentHandler *handler = NULL;
for (i = 0; i < sHandlerCount; i++)
{
handler = &sHandlers[i];
if (handler->hidden && !showHidden) continue;
printf(" ");
if (!handler->required) printf("[");
if (handler->shortcut != '\0')
{
printf("-%c", handler->shortcut);
}
else
{
printf("--%s", handler->keyword);
}
if (handler->paramNames != NULL)
{
printf(" %s", handler->paramNames);
}
if (!handler->required) printf("]");
}
printf("\n\n");
// ACT II: the Descriptions. Dramatis personae: several Description Strings, divers DescTypes; also Peasblossom, a Faerie.
for (i = 0; i < sHandlerCount; i++)
{
handler = &sHandlers[i];
if (handler->hidden && !showHidden) continue;
#define LABEL_SIZE 16
char label[LABEL_SIZE];
if (handler->shortcut != '\0')
{
snprintf(label, LABEL_SIZE, "--%s, -%c", handler->keyword, handler->shortcut);
}
else
{
snprintf(label, LABEL_SIZE, "--%s", handler->keyword);
}
printf("%*s: %s", LABEL_SIZE, label, handler->description);
if (handler->descTypes != NULL)
{
unsigned j;
for (j = 0; j < handler->descTypeCount; j++)
{
FilterEntryBase *entry = (FilterEntryBase *)((char *)handler->descTypes + handler->descStride * j);
if (j != 0) printf(", ");
printf("\"%s\" (%c)", entry->name, entry->shortcut);
}
}
printf("\n");
}
// ACT III: the Expository Text.
printf("\n"
// "=========|=========|=========|=========|=========|=========|=========|=========|\n"
"Planettool reads a texture map from an input file (in PNG format) or a generator\n"
"function, and writes it to an output file (in PNG format). In so doing, it may\n"
"change the projection and scale of the map, and may rotate it around the planet.\n"
"\n"
"Planettool's design is geared for flexibility and quality. As a side effect, it\n"
"is extremely slow. Don't be alarmed if it takes several minutes to do anything.\n"
"\n"
"EXAMPLES:\n"
"planettool --output cube \"cubemap.png\" --input latlong \"original.png\" --size 512\n"
" Reads original.png, treated as a latitude-longitude map, and remaps it to a\n"
" cube map with a side length of 512 pixels.\n"
"\n"
"planettool -o c cubemap.png -i l original.png -S 512\n"
" Same as above, only less legible for extra geek cred.\n"
"\n"
"planettool -o cube grid.png --generator grid1 --fast --rotate 30 0 0 --flip\n"
" Generate a grid, tilted 30 degrees and projected onto an inside-out cube map\n"
" at low quality.\n"
"\n"
"THE PROJECTION TYPES:\n"
// "=========|=========|=========|=========|=========|=========|=========|=========|\n"
" latlong: Equirectangular projection. In this format, the intervals between\n"
" pixels are constant steps of latitude and longitude. This is\n"
" conceptually simple, but inefficent; lots of pixels are crammed\n"
" together tightly near the poles.\n"
" cube: The surface is divided into six equal areas, which are projected\n"
" onto squares. These are then stacked vertically, in the following\n"
" order:\n"
" +x, -x, +y, -y, +z, -z.\n"
" cubex: The same projection as cube, but the squares are rearranged into a\n"
" more human-friendly layout (which can be printed and folded into a\n"
" cube if you're bored).\n"
" mercator: An angle-preserving map projection. The traditional projection for\n"
" sailors and people who can't be bothered to choose a more approp-\n"
" riate projection for whatever they're doing. Entirely unsuitable\n"
" for texturing, but possibly useful if you want a wall map.\n"
"gall-peters: A variant of the cylindric equal-area projection, in which the\n"
" proportions between different areas are preserved.\n"
"\n"
"THE GENERATORS:\n"
" grid1: A grid with lines spaced ten degrees apart. Longitude lines are\n"
" green in the northern hemisphere, blue in the south. Latitude lines\n"
" are red in the western hemisphere, teal in the east.\n"
// "=========|=========|=========|=========|=========|=========|=========|=========|\n"
);
}
static void ShowVersion(void)
{
printf("%s\n", PLANETTOOL_VERSION);
}
static bool PrintProgress(size_t numerator, size_t denominator, void *context)
{
size_t percentage = (numerator * 100) / denominator;
unsigned *last = context;
if (percentage > *last)
{
printf("\r%zu %%", percentage);
fflush(stdout);
*last = percentage;
}
return true;
}