-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgifw.c
724 lines (622 loc) · 23.1 KB
/
gifw.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
/* gifw.c */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
/* GIF File Header Structures */
#pragma pack(push, 1)
typedef struct {
char signature[3];
char version[3];
} GIFHeader;
typedef struct {
uint16_t width;
uint16_t height;
uint8_t packed;
uint8_t bgColorIndex;
uint8_t pixelAspectRatio;
} LogicalScreenDescriptor;
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} ColorTableEntry;
typedef struct {
uint8_t separator;
uint16_t left;
uint16_t top;
uint16_t width;
uint16_t height;
uint8_t packed;
} ImageDescriptor;
typedef struct {
uint8_t introducer;
uint8_t label;
} ExtensionBlock;
typedef struct {
uint8_t blockSize;
uint8_t packed;
uint16_t delayTime;
uint8_t transparentColorIndex;
uint8_t terminator;
} GraphicsControlExtension;
#pragma pack(pop)
/* LZW Decompression Structures */
#define MAX_LZW_BITS 12
#define MAX_LZW_CODES (1 << MAX_LZW_BITS)
#define LZW_TABLE_SIZE (MAX_LZW_CODES * 2)
typedef struct {
int16_t prefix;
uint8_t suffix;
} LZWEntry;
/* Number of threads to use for image processing */
#define NUM_THREADS 4
/* Enumeration for Display Modes */
typedef enum {
STRETCH,
CENTER,
TILE
} DisplayMode;
/* Structure to pass data to threads */
typedef struct {
int thread_id;
uint32_t *dst;
uint8_t *frameBuffer;
int destWidth;
int destHeight;
int gifWidth;
int gifHeight;
int startRow;
int endRow;
} ThreadData;
/* Additional fields for transparency handling */
typedef struct {
uint8_t disposalMethod;
uint8_t transparencyFlag;
uint8_t transparentColorIndex;
} GraphicControlExtensionData;
/* Helper Functions */
uint16_t read_le_uint16(FILE *fp) {
uint8_t bytes[2];
fread(bytes, 1, 2, fp);
return bytes[0] | (bytes[1] << 8);
}
void skip_sub_blocks(FILE *fp) {
uint8_t blockSize;
do {
fread(&blockSize, 1, 1, fp);
fseek(fp, blockSize, SEEK_CUR);
} while (blockSize != 0);
}
/* Function to Read Next Code from LZW Stream */
int read_code(uint8_t **data, int *bitPos, int codeSize) {
int code = 0;
for (int i = 0; i < codeSize; i++) {
code |= ((**data >> *bitPos) & 1) << i;
(*bitPos)++;
if (*bitPos == 8) {
(*data)++;
*bitPos = 0;
}
}
return code;
}
/* Function to Decode LZW Compressed Data */
int lzw_decode(uint8_t *compressedData, int compressedSize, uint8_t *outData, int width, int height, int lzwMinCodeSize) {
int clearCode = 1 << lzwMinCodeSize;
int endCode = clearCode + 1;
int codeSize = lzwMinCodeSize + 1;
int maxCode = (1 << codeSize) - 1;
LZWEntry *table = malloc(sizeof(LZWEntry) * LZW_TABLE_SIZE);
if (!table) {
fprintf(stderr, "Failed to allocate LZW table\n");
return -1;
}
for (int i = 0; i < clearCode; i++) {
table[i].prefix = -1;
table[i].suffix = i;
}
int tableSize = endCode + 1;
uint8_t *dataPtr = compressedData;
int bitPos = 0;
int oldCode = -1;
int outPos = 0;
while (1) {
int code = read_code(&dataPtr, &bitPos, codeSize);
if (code == clearCode) {
codeSize = lzwMinCodeSize + 1;
maxCode = (1 << codeSize) - 1;
tableSize = endCode + 1;
oldCode = -1;
continue;
} else if (code == endCode) {
break;
} else if (code < tableSize) {
int stackSize = 0;
uint8_t stack[MAX_LZW_CODES];
int currentCode = code;
while (currentCode >= clearCode) {
stack[stackSize++] = table[currentCode].suffix;
currentCode = table[currentCode].prefix;
}
stack[stackSize++] = currentCode;
for (int i = stackSize - 1; i >= 0; i--) {
outData[outPos++] = stack[i];
if (outPos >= width * height) break;
}
if (oldCode != -1) {
table[tableSize].prefix = oldCode;
table[tableSize].suffix = currentCode;
tableSize++;
if (tableSize > maxCode && codeSize < MAX_LZW_BITS) {
codeSize++;
maxCode = (1 << codeSize) - 1;
}
}
oldCode = code;
} else {
/* Special case */
int stackSize = 0;
uint8_t stack[MAX_LZW_CODES];
int currentCode = oldCode;
while (currentCode >= clearCode) {
stack[stackSize++] = table[currentCode].suffix;
currentCode = table[currentCode].prefix;
}
stack[stackSize++] = currentCode;
uint8_t firstChar = currentCode;
stack[stackSize++] = firstChar;
for (int i = stackSize - 1; i >= 0; i--) {
outData[outPos++] = stack[i];
if (outPos >= width * height) break;
}
table[tableSize].prefix = oldCode;
table[tableSize].suffix = firstChar;
tableSize++;
if (tableSize > maxCode && codeSize < MAX_LZW_BITS) {
codeSize++;
maxCode = (1 << codeSize) - 1;
}
oldCode = code;
}
if (outPos >= width * height) break;
}
free(table);
return 0;
}
/* Function to Read Compressed Data Blocks */
int read_data_blocks(FILE *fp, uint8_t **data, int *dataSize) {
uint8_t blockSize;
int totalSize = 0;
uint8_t *buffer = NULL;
while (1) {
fread(&blockSize, 1, 1, fp);
if (blockSize == 0) break;
buffer = realloc(buffer, totalSize + blockSize);
fread(buffer + totalSize, 1, blockSize, fp);
totalSize += blockSize;
}
*data = buffer;
*dataSize = totalSize;
return 0;
}
/* Function to decode interlaced images */
void decode_interlaced_image(uint8_t *pixelIndices, int width, int height, uint8_t *decodedPixels) {
int pass = 0;
int i = 0;
int y;
int offsets[] = {0, 4, 2, 1};
int steps[] = {8, 8, 4, 2};
while (pass < 4) {
for (y = offsets[pass]; y < height; y += steps[pass]) {
memcpy(decodedPixels + y * width, pixelIndices + i * width, width);
i++;
}
pass++;
}
}
/* Thread function for bilinear interpolation */
void *bilinear_thread_func(void *arg) {
ThreadData *data = (ThreadData *)arg;
uint32_t *dst = data->dst;
uint8_t *frameBuffer = data->frameBuffer;
int destWidth = data->destWidth;
int destHeight = data->destHeight;
int gifWidth = data->gifWidth;
int gifHeight = data->gifHeight;
int startRow = data->startRow;
int endRow = data->endRow;
for (int y = startRow; y < endRow; y++) {
float srcY = y * ((float)gifHeight / destHeight);
int y0 = (int)srcY;
int y1 = y0 + 1;
float y_weight = srcY - y0;
if (y1 >= gifHeight) y1 = gifHeight - 1;
for (int x = 0; x < destWidth; x++) {
float srcX = x * ((float)gifWidth / destWidth);
int x0 = (int)srcX;
int x1 = x0 + 1;
float x_weight = srcX - x0;
if (x1 >= gifWidth) x1 = gifWidth - 1;
/* Get the four neighboring pixels */
int idx00 = (y0 * gifWidth + x0) * 3;
int idx01 = (y0 * gifWidth + x1) * 3;
int idx10 = (y1 * gifWidth + x0) * 3;
int idx11 = (y1 * gifWidth + x1) * 3;
/* Interpolate red channel */
float r = (1 - x_weight) * (1 - y_weight) * frameBuffer[idx00] +
x_weight * (1 - y_weight) * frameBuffer[idx01] +
(1 - x_weight) * y_weight * frameBuffer[idx10] +
x_weight * y_weight * frameBuffer[idx11];
/* Interpolate green channel */
float g = (1 - x_weight) * (1 - y_weight) * frameBuffer[idx00 + 1] +
x_weight * (1 - y_weight) * frameBuffer[idx01 + 1] +
(1 - x_weight) * y_weight * frameBuffer[idx10 + 1] +
x_weight * y_weight * frameBuffer[idx11 + 1];
/* Interpolate blue channel */
float b = (1 - x_weight) * (1 - y_weight) * frameBuffer[idx00 + 2] +
x_weight * (1 - y_weight) * frameBuffer[idx01 + 2] +
(1 - x_weight) * y_weight * frameBuffer[idx10 + 2] +
x_weight * y_weight * frameBuffer[idx11 + 2];
int dstIdx = y * destWidth + x;
dst[dstIdx] = (((uint8_t)r) << 16) | (((uint8_t)g) << 8) | ((uint8_t)b);
}
}
pthread_exit(NULL);
}
/* Helper function to get current time in milliseconds */
uint64_t get_current_time_ms() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000;
}
/* Main Program */
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 3) {
fprintf(stderr, "usage: %s <animated-gif-file> [stretch|center|tile]\n", argv[0]);
exit(1);
}
const char *filename = argv[1];
DisplayMode mode = STRETCH; // Default display mode
if (argc == 3) {
if (strcmp(argv[2], "stretch") == 0) {
mode = STRETCH;
} else if (strcmp(argv[2], "center") == 0) {
mode = CENTER;
} else if (strcmp(argv[2], "tile") == 0) {
mode = TILE;
} else {
fprintf(stderr, "Invalid display mode. Choose from stretch, center, or tile.\n");
exit(1);
}
}
FILE *gifFile = fopen(filename, "rb");
if (!gifFile) {
fprintf(stderr, "Could not open GIF file %s\n", filename);
exit(1);
}
/* Read GIF Header */
GIFHeader header;
fread(&header, sizeof(GIFHeader), 1, gifFile);
if (strncmp(header.signature, "GIF", 3) != 0) {
fprintf(stderr, "Invalid GIF file\n");
fclose(gifFile);
exit(1);
}
/* Read Logical Screen Descriptor */
LogicalScreenDescriptor lsd;
fread(&lsd, sizeof(LogicalScreenDescriptor), 1, gifFile);
int gifWidth = lsd.width;
int gifHeight = lsd.height;
/* Check for Global Color Table */
int globalColorTableFlag = (lsd.packed & 0x80) >> 7;
int globalColorTableSize = 1 << ((lsd.packed & 0x07) + 1);
/* Read Global Color Table */
ColorTableEntry *globalColorTable = NULL;
if (globalColorTableFlag) {
globalColorTable = malloc(sizeof(ColorTableEntry) * globalColorTableSize);
fread(globalColorTable, sizeof(ColorTableEntry), globalColorTableSize, gifFile);
}
/* Initialize X11 */
Display *display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Could not open X display\n");
fclose(gifFile);
free(globalColorTable);
exit(1);
}
int screen = DefaultScreen(display);
Window root = RootWindow(display, screen);
GC gc = DefaultGC(display, screen);
/* Get Screen Dimensions */
int screenWidth = DisplayWidth(display, screen);
int screenHeight = DisplayHeight(display, screen);
XVisualInfo vinfo;
if (!XMatchVisualInfo(display, screen, 24, TrueColor, &vinfo)) {
fprintf(stderr, "No matching visual\n");
XCloseDisplay(display);
fclose(gifFile);
free(globalColorTable);
exit(1);
}
Visual *visual = vinfo.visual;
/* Adjust image size based on display mode */
int destWidth = screenWidth;
int destHeight = screenHeight;
int offsetX = 0;
int offsetY = 0;
if (mode == STRETCH) {
destWidth = screenWidth;
destHeight = screenHeight;
} else if (mode == CENTER) {
destWidth = gifWidth;
destHeight = gifHeight;
offsetX = (screenWidth - gifWidth) / 2;
offsetY = (screenHeight - gifHeight) / 2;
} else if (mode == TILE) {
destWidth = screenWidth;
destHeight = screenHeight;
}
XImage *ximage = XCreateImage(display, visual, vinfo.depth, ZPixmap, 0,
NULL, destWidth, destHeight, 32, 0);
if (!ximage) {
fprintf(stderr, "Could not create XImage\n");
XCloseDisplay(display);
fclose(gifFile);
free(globalColorTable);
exit(1);
}
ximage->data = malloc(ximage->height * ximage->bytes_per_line);
if (!ximage->data) {
fprintf(stderr, "Could not allocate memory for XImage\n");
XDestroyImage(ximage);
XCloseDisplay(display);
fclose(gifFile);
free(globalColorTable);
exit(1);
}
uint8_t *frameBuffer = malloc(gifWidth * gifHeight * 3);
if (!frameBuffer) {
fprintf(stderr, "Could not allocate memory for frame buffer\n");
free(ximage->data);
XDestroyImage(ximage);
XCloseDisplay(display);
fclose(gifFile);
free(globalColorTable);
exit(1);
}
/* Variables for transparency and interlacing */
GraphicControlExtensionData gceData = {0, 0, 0};
int hasGCE = 0;
/* Main Loop to Read Frames */
int running = 1;
int frameDelay = 100; // default delay in milliseconds
uint8_t *prevFrame = calloc(gifWidth * gifHeight, sizeof(uint8_t));
memset(prevFrame, 0, gifWidth * gifHeight);
while (running) {
uint64_t frameStartTime = get_current_time_ms();
int c = fgetc(gifFile);
if (c == EOF) {
/* Rewind to start */
fseek(gifFile, sizeof(GIFHeader) + sizeof(LogicalScreenDescriptor), SEEK_SET);
if (globalColorTableFlag) {
fseek(gifFile, sizeof(ColorTableEntry) * globalColorTableSize, SEEK_CUR);
}
continue;
}
if (c == 0x2C) {
/* Image Descriptor */
ImageDescriptor id;
id.separator = c;
fread(&id.left, 2, 1, gifFile);
fread(&id.top, 2, 1, gifFile);
fread(&id.width, 2, 1, gifFile);
fread(&id.height, 2, 1, gifFile);
fread(&id.packed, 1, 1, gifFile);
/* Interlace Flag */
int interlaceFlag = (id.packed & 0x40) >> 6;
/* Local Color Table */
int localColorTableFlag = (id.packed & 0x80) >> 7;
int localColorTableSize = 1 << ((id.packed & 0x07) + 1);
ColorTableEntry *colorTable = globalColorTable;
if (localColorTableFlag) {
colorTable = malloc(sizeof(ColorTableEntry) * localColorTableSize);
fread(colorTable, sizeof(ColorTableEntry), localColorTableSize, gifFile);
}
/* LZW Minimum Code Size */
uint8_t lzwMinCodeSize;
fread(&lzwMinCodeSize, 1, 1, gifFile);
/* Read Image Data */
uint8_t *compressedData = NULL;
int compressedSize = 0;
read_data_blocks(gifFile, &compressedData, &compressedSize);
/* Decode Image Data */
uint8_t *pixelIndices = malloc(id.width * id.height);
if (!pixelIndices) {
fprintf(stderr, "Failed to allocate pixel indices\n");
free(compressedData);
continue;
}
lzw_decode(compressedData, compressedSize, pixelIndices, id.width, id.height, lzwMinCodeSize);
/* Handle Interlacing */
uint8_t *decodedPixels = pixelIndices;
if (interlaceFlag) {
decodedPixels = malloc(id.width * id.height);
decode_interlaced_image(pixelIndices, id.width, id.height, decodedPixels);
free(pixelIndices);
}
/* Build Frame Buffer */
for (int y = 0; y < gifHeight; y++) {
for (int x = 0; x < gifWidth; x++) {
int idx = y * gifWidth + x;
uint8_t colorIndex;
if (x >= id.left && x < id.left + id.width && y >= id.top && y < id.top + id.height) {
int imgX = x - id.left;
int imgY = y - id.top;
colorIndex = decodedPixels[imgY * id.width + imgX];
/* Handle Transparency */
if (hasGCE && gceData.transparencyFlag && colorIndex == gceData.transparentColorIndex) {
colorIndex = prevFrame[idx]; // Use previous frame pixel
} else {
prevFrame[idx] = colorIndex;
}
} else {
colorIndex = prevFrame[idx];
}
ColorTableEntry color = colorTable[colorIndex];
int fbIdx = idx * 3;
frameBuffer[fbIdx] = color.red;
frameBuffer[fbIdx + 1] = color.green;
frameBuffer[fbIdx + 2] = color.blue;
}
}
/* Reset GCE data */
hasGCE = 0;
/* Resize and Position Frame Buffer Based on Mode */
uint32_t *dst = (uint32_t *)ximage->data;
memset(dst, 0, ximage->height * ximage->bytes_per_line); // Clear the image
if (mode == STRETCH) {
/* Multithreaded bilinear interpolation */
pthread_t threads[NUM_THREADS];
ThreadData threadData[NUM_THREADS];
int rowsPerThread = destHeight / NUM_THREADS;
for (int i = 0; i < NUM_THREADS; i++) {
threadData[i].thread_id = i;
threadData[i].dst = dst;
threadData[i].frameBuffer = frameBuffer;
threadData[i].destWidth = destWidth;
threadData[i].destHeight = destHeight;
threadData[i].gifWidth = gifWidth;
threadData[i].gifHeight = gifHeight;
threadData[i].startRow = i * rowsPerThread;
threadData[i].endRow = (i == NUM_THREADS - 1) ? destHeight : threadData[i].startRow + rowsPerThread;
pthread_create(&threads[i], NULL, bilinear_thread_func, (void *)&threadData[i]);
}
/* Wait for all threads to complete */
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
} else if (mode == CENTER) {
/* Center the image */
for (int y = 0; y < gifHeight; y++) {
for (int x = 0; x < gifWidth; x++) {
int srcIdx = (y * gifWidth + x) * 3;
uint8_t r = frameBuffer[srcIdx];
uint8_t g = frameBuffer[srcIdx + 1];
uint8_t b = frameBuffer[srcIdx + 2];
int dstX = x + offsetX;
int dstY = y + offsetY;
if (dstX >= 0 && dstX < destWidth && dstY >= 0 && dstY < destHeight) {
int dstIdx = dstY * destWidth + dstX;
dst[dstIdx] = (r << 16) | (g << 8) | b;
}
}
}
} else if (mode == TILE) {
/* Tile the image across the screen */
for (int y = 0; y < destHeight; y++) {
for (int x = 0; x < destWidth; x++) {
int srcX = x % gifWidth;
int srcY = y % gifHeight;
int srcIdx = (srcY * gifWidth + srcX) * 3;
uint8_t r = frameBuffer[srcIdx];
uint8_t g = frameBuffer[srcIdx + 1];
uint8_t b = frameBuffer[srcIdx + 2];
int dstIdx = y * destWidth + x;
dst[dstIdx] = (r << 16) | (g << 8) | b;
}
}
}
/* Create pixmap from ximage */
Pixmap pixmap = XCreatePixmap(display, root, destWidth, destHeight, vinfo.depth);
XPutImage(display, pixmap, gc, ximage, 0, 0, 0, 0, destWidth, destHeight);
/* Set root window background pixmap */
XSetWindowBackgroundPixmap(display, root, pixmap);
/* Clear root window */
XClearWindow(display, root);
/* Remove old pixmap */
XFreePixmap(display, pixmap);
/* Flush changes */
XFlush(display);
/* Calculate processing time */
uint64_t frameEndTime = get_current_time_ms();
uint64_t processingTime = frameEndTime - frameStartTime;
/* Adjust frame delay */
int adjustedDelay = frameDelay - (int)processingTime;
if (adjustedDelay < 0) {
adjustedDelay = 0; // Prevent negative delay
}
/* Sleep for the adjusted frame delay */
usleep(adjustedDelay * 1000);
/* Clean up */
if (localColorTableFlag) {
free(colorTable);
}
free(compressedData);
if (interlaceFlag) {
free(decodedPixels);
} else {
free(pixelIndices);
}
} else if (c == 0x21) {
/* Extension Block */
ExtensionBlock ext;
ext.introducer = c;
fread(&ext.label, 1, 1, gifFile);
if (ext.label == 0xF9) {
/* Graphics Control Extension */
uint8_t blockSize;
fread(&blockSize, 1, 1, gifFile);
uint8_t packed;
uint16_t delayTime;
uint8_t transparentColorIndex;
fread(&packed, 1, 1, gifFile);
fread(&delayTime, 2, 1, gifFile);
fread(&transparentColorIndex, 1, 1, gifFile);
uint8_t terminator;
fread(&terminator, 1, 1, gifFile);
/* Use delay time from GCE */
frameDelay = delayTime * 10; // delay in milliseconds
/* Handle zero or very small delays */
if (frameDelay < 20) {
frameDelay = 20; // Set minimum delay to 20ms (50 FPS)
}
/* Save GCE data for transparency */
gceData.disposalMethod = (packed >> 2) & 0x07;
gceData.transparencyFlag = packed & 0x01;
gceData.transparentColorIndex = transparentColorIndex;
hasGCE = 1;
} else {
/* Skip other extensions */
skip_sub_blocks(gifFile);
}
} else if (c == 0x3B) {
/* GIF Trailer */
/* Loop back to start */
fseek(gifFile, sizeof(GIFHeader) + sizeof(LogicalScreenDescriptor), SEEK_SET);
if (globalColorTableFlag) {
fseek(gifFile, sizeof(ColorTableEntry) * globalColorTableSize, SEEK_CUR);
}
} else {
/* Unknown block */
fprintf(stderr, "Unknown block: 0x%X\n", c);
break;
}
}
/* Cleanup */
free(prevFrame);
free(frameBuffer);
free(ximage->data);
XDestroyImage(ximage);
XCloseDisplay(display);
fclose(gifFile);
if (globalColorTable) {
free(globalColorTable);
}
return 0;
}