-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
693 lines (621 loc) · 20.8 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
/* main.c -- gif2spr entrypoint.
* version 0.2
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* 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 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.
*
* For more information, please refer to <http://unlicense.org/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <errno.h>
#ifdef COMPILE_GIFLIB
# include "giflib-5.1.9/gif_lib.h"
#else
# include <gif_lib.h>
#endif
#include "sprite.h"
#define FRAME_BORDER 2
struct DVec2D { double x; double y; };
struct Rect {
int width;
int height;
int left;
int top;
uint8_t *raster;
};
void sprNonFatalError(char const *errString)
{
fputs(errString, stderr);
}
void sprFatalError(char const *errString)
{
sprNonFatalError(errString);
exit(EXIT_FAILURE);
}
#define N_ALIGNMENTS 5
static const char *ALIGNMENT_NAMES[N_ALIGNMENTS] = {
"vp-parallel-upright",
"upright",
"vp-parallel",
"oriented",
"vp-parallel-oriented" };
#define N_BLENDMODES 4
static const char *BLENDMODE_NAMES[N_BLENDMODES] = {
"normal",
"additive",
"index-alpha",
"alpha-test" };
static char *gifFileName = NULL;
static char *sprFileName = NULL;
static char *palFileName = NULL;
static char *originString = NULL;
static char *alignmentOption = NULL;
static char *blendModeOption = NULL;
static char *blendColorCode = NULL;
static enum Spr_version version = SPR_VER_QUAKE;
static bool useDummyFrame = false;
static bool extendFrames = false;
static struct Spr_color gradient
(struct Spr_color color1, struct Spr_color color2, uint8_t value)
{
struct Spr_color outColor;
for (int i = 0; i < 3; i++) {
outColor.rgb[i] = (uint8_t)((color1.rgb[i] * (int)(255-value) +
color2.rgb[i] * (int)value) / 255);
}
return outColor;
}
static void blit
(uint8_t *buffer, const uint8_t *frame, int bufW, int bufH,
int frameW, int frameH, int left, int top, int transparent, int bgIndex)
{
for (int fx = 0; fx < frameW; fx++)
for (int fy = 0; fy < frameH; fy++) {
int bx = left + fx;
int by = top + fy;
if (bx >= 0 && bx < bufW && by >= 0 && by < bufH) {
uint8_t color = frame[fx + frameW * fy];
if (color != transparent) {
buffer[bx + bufW * by] = color;
} else if (bgIndex >= 0) {
buffer[bx + bufW * by] = (uint8_t)bgIndex;
}
}
}
}
static void sampleRect
(const uint8_t *buffer, uint8_t *rectRaster, int bufW, int bufH,
struct Rect rect, int gifTrans, uint8_t sprTrans, uint8_t *lookup)
{
for (int rx = 0; rx < rect.width; rx++)
for (int ry = 0; ry < rect.height; ry++) {
int bx = rect.left + rx;
int by = rect.top + ry;
if (bx >= 0 && bx < bufW && by >= 0 && by < bufH) {
uint8_t color = buffer[bx + bufW * by];
if (color == gifTrans) {
rectRaster[rx + rect.width * ry] = sprTrans;
}
else {
rectRaster[rx + rect.width * ry] = lookup[color];
}
}
else {
rectRaster[rx + rect.width * ry] = SPR_TRANS_IDX;
}
}
}
static struct Rect minRect
(const uint8_t *buffer, int bufW, int bufH, int transparent, int border)
{
int left = 0;
int right = bufW-1;
int top = 0;
int bottom = bufW-1;
int x, y;
bool done = false;
x = left;
while (x < bufW && !done) {
for (y = 0; y < bufH && !done; y++) {
if (buffer[x + bufW * y] != transparent) {
left = x;
done = true;
}
}
x++;
}
done = false;
x = right;
while (x > left && !done) {
for (y = 0; y < bufH && !done; y++) {
if (buffer[x + bufW * y] != transparent) {
right = x;
done = true;
}
}
x--;
}
done = false;
y = top;
while (y < bufH && !done) {
for (x = left; x <= right && !done; x++) {
if (buffer[x + bufW * y] != transparent) {
top = y;
done = true;
}
}
y++;
}
done = false;
y = bottom;
while (y > top && !done) {
for (x = left; x <= right && !done; x++) {
if (buffer[x + bufW * y] != transparent) {
bottom = y;
done = true;
}
}
y--;
}
struct Rect rect;
// if we hit no non-transparent pixels
if (left >= bufW) {
rect.width = 0;
rect.height = 0;
rect.left = 0;
rect.top = 0;
}
else {
left-= border;
right+= border;
top-= border;
bottom+= border;
left = left < 0 ? 0 : left;
top = top < 0 ? 0 : top;
right = right >= bufW ? bufW-1 : right;
bottom = bottom >= bufH ? bufH-1 : bottom;
rect.width = right >= left ? right - left + 1 : 0;
rect.height = bottom >= top ? bottom - top + 1 : 0;
rect.left = left;
rect.top = top;
}
return rect;
}
static int loadArgs(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "-origin") == 0) {
i++;
if (i >= argc)
return 1;
originString = argv[i];
}
else if (strcmp(argv[i], "-palette") == 0 ||
strcmp(argv[i], "-p") == 0) {
i++;
if (i >= argc)
return 2;
palFileName = argv[i];
}
else if (strcmp(argv[i], "-alignment") == 0 ||
strcmp(argv[i], "-a") == 0) {
i++;
if (i >= argc)
return 3;
alignmentOption = argv[i];
}
else if (strcmp(argv[i], "-hl") == 0) {
version = SPR_VER_HL;
}
else if (strcmp(argv[i], "-quake") == 0) {
version = SPR_VER_QUAKE;
}
else if (strcmp(argv[i], "-blendmode") == 0 ||
strcmp(argv[i], "-b") == 0) {
i++;
if (i >= argc)
return 4;
blendModeOption = argv[i];
}
else if (strcmp(argv[i], "-color") == 0 ||
strcmp(argv[i], "-c") == 0) {
i++;
if (i >= argc)
return 5;
blendColorCode = argv[i];
}
else if (strcmp(argv[i], "-dummy") == 0 ||
strcmp(argv[i], "-d") == 0) {
useDummyFrame = true;
}
else if (strcmp(argv[i], "-extend") == 0 ||
strcmp(argv[i], "-e") == 0) {
extendFrames = true;
}
else {
fprintf(stderr, "Unknown option \"%s\"\n", argv[i]);
return 6;
}
}
else if (gifFileName == NULL) {
gifFileName = argv[i];
}
else if (sprFileName == NULL) {
sprFileName = argv[i];
}
else {
return 7;
}
}
if (sprFileName == NULL)
return 8;
else
return 0;
}
static struct DVec2D parseOrigin(char *originString)
{
double x, y;
if (originString == NULL) {
x = 0.5;
y = 0.5;
}
else {
char *end;
char *xToken = strtok(originString, ",");
char *yToken = strtok(NULL, ",");
if (yToken == NULL) {
fputs("Missing origin Y.\n", stderr);
exit(EXIT_FAILURE);
}
errno = 0;
x = strtod(xToken, &end);
if (errno == ERANGE) {
fputs("Origin X is out of range.\n", stderr);
exit(EXIT_FAILURE);
} else if (end == xToken)
{
fputs("Origin X is not a number.\n", stderr);
exit(EXIT_FAILURE);
} else if (isfinite(x) == 0)
{
fputs("Origin X is not finite.\n", stderr);
exit(EXIT_FAILURE);
}
errno = 0;
y = strtod(yToken, &end);
if (errno == ERANGE) {
fputs("Origin Y is out of range.\n", stderr);
exit(EXIT_FAILURE);
} else if (end == yToken)
{
fputs("Origin Y is not a number.\n", stderr);
exit(EXIT_FAILURE);
} else if (isfinite(y) == 0)
{
fputs("Origin Y is not finite.\n", stderr);
exit(EXIT_FAILURE);
}
}
return (struct DVec2D){ x, y };
}
static struct Spr_color parseColor(const char *code) {
struct Spr_color color;
uint32_t colorNum;
if (strlen(code) != 7 || code[0] != '#') {
fprintf(stderr, "Invalid color code \"%s\"\n", code);
exit(EXIT_FAILURE);
}
char *end;
colorNum = strtol(code + 1, &end, 16);
if (end == code + 1) {
fprintf(stderr, "Invalid color code \"%s\"\n", code);
}
color.rgb[0] = (uint8_t)((colorNum >> 16) & 0xff);
color.rgb[1] = (uint8_t)((colorNum >> 8) & 0xff);
color.rgb[2] = (uint8_t)(colorNum & 0xff);
return color;
}
int main(int argc, char *argv[])
{
int err; /* gif error code */
GifFileType *gifFile; /* GIF data read/decoded from file */
ColorMapObject *gifColorMap;
struct Spr_Sprite *sprite;
uint16_t colorCt; /* number of colors */
static struct Spr_color colors[SPR_MAX_PAL_SIZE];
struct Spr_image *images;
float *delays;
struct DVec2D origin;
int alignment = -1;
int blendMode = -1;
struct Spr_color blendColor;
static uint8_t paletteLookup[SPR_MAX_PAL_SIZE];
uint8_t *imgBuffer;
uint8_t *prevBuffer;
size_t canvasPixCount;
uint8_t gifBgIndex = 0;
int loadErr = loadArgs(argc, argv);
if (loadErr != 0) {
/* 1 2 3 4 5 6
* 123456789012345678901234567890123456789012345678901234567890123*/
fputs("USAGE: gif2spr [-a|-alignment ALIGNMENT] [-p|-palette PALFILE] "
/* 7
* 4567890123456*/
"[-origin X,Y]\n", stderr);
fputs(" [-quake] [-hl] [-b|-blendmode BLENDMODE] [-c|-color CODE]"
" [-d|-dummy]\n", stderr);
fputs(" [-e|-extend] GIFFILE SPRFILE\n\n", stderr);
fputs(" ALIGNMENT Sprite orientation. Options "
"(defaults to vp-parallel):\n", stderr);
for (int i = 0; i < N_ALIGNMENTS; i++)
fprintf(stderr, " %s\n", ALIGNMENT_NAMES[i]);
fputs(" PALFILE Palette lump. Defaults to Quake palette.\n",
stderr);
fputs(" X Decimal origin X component. "
"Defaults to 0.5 (center).\n", stderr);
fputs(" Y Decimal origin Y component. "
"Defaults to 0.5 (center).\n", stderr);
fputs(" BLENDMODE (HL only) Options (defaults to normal):\n",
stderr);
for (int i = 0; i < N_BLENDMODES; i++)
fprintf(stderr, " %s\n", BLENDMODE_NAMES[i]);
fputs(" CODE Index-alpha color code. e.g. \"#ff8000\"\n",
stderr);
fputs(" -quake Write sprite in Quake format (default).\n",
stderr);
fputs(" -hl Write sprite in Half-Life format.\n", stderr);
fputs(" -dummy (HL) Append an empty \"dummy\" frame.\n", stderr);
fputs(" -extend Extend frame boundaries to image size.\n", stderr);
fputs(" GIFFILE Input GIF file.\n", stderr);
fputs(" SPRFILE Output SPRITE file.\n", stderr);
exit(EXIT_FAILURE);
}
gifFile = DGifOpenFileName(gifFileName, &err);
if (gifFile == (void *)0) {
fprintf(stderr, "%s:\n", gifFileName);
fprintf(stderr, "%s.\n", GifErrorString(err));
exit(EXIT_FAILURE);
}
if (DGifSlurp(gifFile) == GIF_ERROR) {
fprintf(stderr, "%s:\n", gifFileName);
fputs("Failed to load file.\n", stderr);
exit(EXIT_FAILURE);
}
/* try to use global color map, use 1st frame's if global is null */
gifColorMap = gifFile->SColorMap;
if (gifColorMap == (void *)0) {
gifColorMap = gifFile->SavedImages[0].ImageDesc.ColorMap;
}
if (blendModeOption == (void *)0) {
blendMode = SPR_TEX_NORMAL;
}
else {
for ( int i = 0; i < N_BLENDMODES && blendMode == -1; i++) {
if (strcmp(blendModeOption, BLENDMODE_NAMES[i]) == 0)
blendMode = i;
}
if (blendMode == -1) {
fprintf(stderr, "Unknown blend mode \"%s\"\n", blendModeOption);
exit(EXIT_FAILURE);
}
}
if (blendColorCode == (void *)0) {
blendColor = (struct Spr_color) {{ 255, 255, 255 }};
}
else {
blendColor = parseColor(blendColorCode);
}
if (version == SPR_VER_HL) {
if (blendMode == SPR_TEX_INDEX_ALPHA) {
/* seems to require 256 colors to work, using color at index 255 */
colorCt = SPR_MAX_PAL_SIZE;
int halfBright = Spr_brightness((struct Spr_color) {{128,128,128}});
int blendBright = Spr_brightness(blendColor);
struct Spr_color bgColor;
if (blendBright < halfBright) {
bgColor = (struct Spr_color) {{ 255, 255, 255 }};
}
else {
bgColor = (struct Spr_color) {{ 0, 0, 0 }};
}
for (int i = 0; i < colorCt; i++) {
colors[i] = gradient(bgColor, blendColor, i);
}
}
else {
colorCt = (uint16_t)(gifColorMap->ColorCount);
for (int i = 0; i < colorCt; i++) {
/* copy colors */
colors[i].rgb[0] = gifColorMap->Colors[i].Red;
colors[i].rgb[1] = gifColorMap->Colors[i].Green;
colors[i].rgb[2] = gifColorMap->Colors[i].Blue;
}
}
}
else {
colorCt = SPR_Q_PAL_SIZE;
if (palFileName != (void *)0)
Spr_readPalette(palFileName, colors, sprFatalError);
else
Spr_defaultQPalette(colors);
}
origin = parseOrigin(originString);
if (alignmentOption == (void *)0) {
alignment = SPR_ALIGN_VP_PARALLEL;
}
else
{
for ( int i = 0; i < N_ALIGNMENTS && alignment == -1; i++) {
if (strcmp(alignmentOption, ALIGNMENT_NAMES[i]) == 0)
alignment = i;
}
if (alignment == -1) {
fprintf(stderr, "Unknown alignment type \"%s\"\n", alignmentOption);
exit(EXIT_FAILURE);
}
}
sprite = Spr_new(
version,
alignment,
blendMode,
gifFile->SWidth,
gifFile->SHeight,
SPR_SYNC_RANDOM,
colorCt,
colors,
(int32_t)floor( -origin.x * gifFile->SWidth),
(int32_t)floor((1-origin.y) * gifFile->SHeight) );
canvasPixCount = gifFile->SWidth * gifFile->SHeight;
imgBuffer = malloc(canvasPixCount);
prevBuffer = malloc(canvasPixCount);
images = malloc(sizeof(*images) * gifFile->ImageCount);
delays = malloc(sizeof(*delays) * gifFile->ImageCount);
for (int i = 0; i < gifFile->ImageCount; i++) {
SavedImage gifImage = gifFile->SavedImages[i];
GifImageDesc imgDesc = gifImage.ImageDesc;
GraphicsControlBlock gcb;
ColorMapObject *localColorMap = imgDesc.ColorMap;
int gifTransIndex;
int gifDelay;
int disposal;
if (localColorMap == (void *)0)
localColorMap = gifColorMap;
for (int i = 0; i < localColorMap->ColorCount; i++) {
struct Spr_color color;
color.rgb[0] = localColorMap->Colors[i].Red;
color.rgb[1] = localColorMap->Colors[i].Green;
color.rgb[2] = localColorMap->Colors[i].Blue;
if (version == SPR_VER_HL && blendMode == SPR_TEX_INDEX_ALPHA) {
paletteLookup[i] = Spr_brightness(color);
}
else {
paletteLookup[i] = Spr_nearestIndex(sprite, color);
}
}
if (DGifSavedExtensionToGCB(gifFile, i, &gcb) == GIF_ERROR) {
gifTransIndex = -1;
disposal = DISPOSAL_UNSPECIFIED;
gifDelay = 8;
}
else {
gifTransIndex = gcb.TransparentColor;
disposal = gcb.DisposalMode;
gifDelay = gcb.DelayTime;
}
/* Seems GIMP and browsers treat background as transparent */
gifBgIndex = gifTransIndex;
delays[i] = gifDelay * 0.01; /* convert to seconds */
if (i == 0 || disposal == DISPOSAL_UNSPECIFIED ||
disposal == DISPOSE_BACKGROUND) {
if (disposal == DISPOSE_BACKGROUND) {
memset(imgBuffer, gifBgIndex, canvasPixCount);
}
else {
memset(imgBuffer, gifTransIndex, canvasPixCount);
}
}
if (disposal == DISPOSE_PREVIOUS) {
memcpy(prevBuffer, imgBuffer, canvasPixCount);
}
blit(imgBuffer, gifImage.RasterBits, gifFile->SWidth, gifFile->SHeight,
imgDesc.Width, imgDesc.Height, imgDesc.Left, imgDesc.Top,
gifTransIndex,
disposal == DISPOSE_BACKGROUND ? gifBgIndex : -1);
struct Rect rect;
if (extendFrames) {
rect.left = 0;
rect.top = 0;
rect.width = gifFile->SWidth;
rect.height = gifFile->SHeight;
}
else {
rect = minRect(imgBuffer, gifFile->SWidth, gifFile->SHeight,
gifTransIndex, FRAME_BORDER);
}
images[i].offsetX = rect.left;
images[i].offsetY = -rect.top;
images[i].width = rect.width;
images[i].height = rect.height;
images[i].raster = malloc(rect.width * rect.height);
if (blendMode == SPR_TEX_INDEX_ALPHA) {
sampleRect(imgBuffer, images[i].raster, gifFile->SWidth,
gifFile->SHeight, rect, gifTransIndex, 0, paletteLookup);
}
else {
sampleRect(imgBuffer, images[i].raster, gifFile->SWidth,
gifFile->SHeight, rect, gifTransIndex, SPR_TRANS_IDX,
paletteLookup);
}
if (disposal == DISPOSE_PREVIOUS) {
memcpy(imgBuffer, prevBuffer, canvasPixCount);
}
}
free(imgBuffer);
free(prevBuffer);
if (version == SPR_VER_QUAKE) {
Spr_appendGroupFrame(sprite, delays, images, gifFile->ImageCount);
}
else {
for (int i = 0; i < gifFile->ImageCount; i++) {
Spr_appendSingleFrame(sprite, images + i);
}
if (useDummyFrame) {
struct Spr_image dummy;
dummy.offsetX = 0;
dummy.offsetY = 0;
if (extendFrames) {
dummy.width = gifFile->SWidth;
dummy.height = gifFile->SHeight;
}
else {
dummy.width = 0;
dummy.height = 0;
}
dummy.raster = malloc(dummy.width * dummy.height);
if (blendMode == SPR_TEX_INDEX_ALPHA) {
memset(dummy.raster, 0, dummy.width * dummy.height);
}
else {
memset(dummy.raster, SPR_TRANS_IDX, dummy.width * dummy.height);
}
Spr_appendSingleFrame(sprite, &dummy);
free(dummy.raster);
}
}
free(delays);
for (size_t i = 0; i < gifFile->ImageCount; i++)
free(images[i].raster);
free(images);
if (DGifCloseFile(gifFile, &err) != GIF_OK) {
fprintf(stderr, "%s:\n", gifFileName);
fprintf(stderr, "%s.\n", GifErrorString(err));
}
Spr_write(sprite, sprFileName, sprFatalError);
Spr_free(sprite);
return EXIT_SUCCESS;
}