3
3
* Copyright (c) 2024 National Cheng Kung University, Taiwan
4
4
* All rights reserved.
5
5
*
6
- *
7
6
* This file integrates the GIF decoder originally developed by Marcel Rodrigues
8
7
* as part of the gifdec project. You can find the original gifdec project at:
9
8
* https://github.com/lecram/gifdec
17
16
#include "twin.h"
18
17
#include "twin_private.h"
19
18
20
- typedef struct _twin_gif_palette {
19
+ typedef struct {
21
20
int size ;
22
21
uint8_t colors [0x100 * 3 ];
23
- } twin_gif_palette_t ;
22
+ } gif_palette_t ;
24
23
25
- typedef struct _twin_gif_gce {
24
+ typedef struct {
26
25
uint16_t delay ;
27
26
uint8_t tindex ;
28
27
uint8_t disposal ;
29
28
int input ;
30
29
int transparency ;
31
- } twin_gif_gce_t ;
30
+ } gif_gce_t ;
32
31
33
32
typedef struct _twin_gif {
34
33
int fd ;
35
34
off_t anim_start ;
36
35
twin_coord_t width , height ;
37
36
twin_coord_t depth ;
38
37
twin_count_t loop_count ;
39
- twin_gif_gce_t gce ;
40
- twin_gif_palette_t * palette ;
41
- twin_gif_palette_t lct , gct ;
38
+ gif_gce_t gce ;
39
+ gif_palette_t * palette ;
40
+ gif_palette_t lct , gct ;
42
41
uint16_t fx , fy , fw , fh ;
43
42
uint8_t bgindex ;
44
43
uint8_t * canvas , * frame ;
@@ -67,9 +66,8 @@ static uint16_t read_num(int fd)
67
66
return bytes [0 ] + (((uint16_t ) bytes [1 ]) << 8 );
68
67
}
69
68
70
- static twin_gif_t * _twin_gif_open (const char * fname )
69
+ static twin_gif_t * gif_open (const char * fname )
71
70
{
72
- int fd ;
73
71
uint8_t sigver [3 ];
74
72
uint16_t width , height , depth ;
75
73
uint8_t fdsz , bgidx , aspect ;
@@ -78,7 +76,7 @@ static twin_gif_t *_twin_gif_open(const char *fname)
78
76
int gct_sz ;
79
77
twin_gif_t * gif ;
80
78
81
- fd = open (fname , O_RDONLY );
79
+ int fd = open (fname , O_RDONLY );
82
80
if (fd == -1 )
83
81
return NULL ;
84
82
#ifdef _WIN32
@@ -173,7 +171,7 @@ static table_t *table_new(int key_size)
173
171
return table ;
174
172
}
175
173
176
- /* Add table_t entry_t . Return value:
174
+ /* Add table entry . Return value:
177
175
* 0 on success
178
176
* +1 if key size must be incremented after this addition
179
177
* -1 if could not realloc table
@@ -487,7 +485,7 @@ static void read_ext(twin_gif_t *gif)
487
485
}
488
486
489
487
/* Return 1 if got a frame; 0 if got GIF trailer; -1 if error. */
490
- static int _twin_gif_get_frame (twin_gif_t * gif )
488
+ static int gif_get_frame (twin_gif_t * gif )
491
489
{
492
490
char sep ;
493
491
@@ -507,23 +505,23 @@ static int _twin_gif_get_frame(twin_gif_t *gif)
507
505
return 1 ;
508
506
}
509
507
510
- static void _twin_gif_render_frame (twin_gif_t * gif , uint8_t * buffer )
508
+ static void gif_render_frame (twin_gif_t * gif , uint8_t * buffer )
511
509
{
512
510
memcpy (buffer , gif -> canvas , gif -> width * gif -> height * 3 );
513
511
render_frame_rect (gif , buffer );
514
512
}
515
513
516
- static int _twin_gif_is_bgcolor (const twin_gif_t * gif , const uint8_t * color )
514
+ static int gif_is_bgcolor (const twin_gif_t * gif , const uint8_t * color )
517
515
{
518
516
return !memcmp (& gif -> palette -> colors [gif -> bgindex * 3 ], color , 3 );
519
517
}
520
518
521
- static void _twin_gif_rewind (twin_gif_t * gif )
519
+ static void gif_rewind (twin_gif_t * gif )
522
520
{
523
521
lseek (gif -> fd , gif -> anim_start , SEEK_SET );
524
522
}
525
523
526
- static void _twin_gif_close (twin_gif_t * gif )
524
+ static void gif_close (twin_gif_t * gif )
527
525
{
528
526
close (gif -> fd );
529
527
free (gif -> frame );
@@ -536,7 +534,7 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
536
534
if (!anim )
537
535
return NULL ;
538
536
539
- twin_gif_t * gif = _twin_gif_open (path );
537
+ twin_gif_t * gif = gif_open (path );
540
538
if (!gif ) {
541
539
free (anim );
542
540
return NULL ;
@@ -550,41 +548,40 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
550
548
anim -> height = gif -> height ;
551
549
552
550
int frame_count = 0 ;
553
- while (_twin_gif_get_frame (gif )) {
551
+ while (gif_get_frame (gif ))
554
552
frame_count ++ ;
555
- }
556
553
557
554
anim -> n_frames = frame_count ;
558
555
anim -> frames = malloc (sizeof (twin_pixmap_t * ) * frame_count );
559
556
anim -> frame_delays = malloc (sizeof (twin_time_t ) * frame_count );
560
557
561
- _twin_gif_rewind (gif );
558
+ gif_rewind (gif );
562
559
uint8_t * color , * frame ;
563
560
frame = malloc (gif -> width * gif -> height * 3 );
564
561
if (!frame ) {
565
562
free (anim );
566
- _twin_gif_close (gif );
563
+ gif_close (gif );
567
564
return NULL ;
568
565
}
569
566
for (twin_count_t i = 0 ; i < frame_count ; i ++ ) {
570
567
anim -> frames [i ] =
571
568
twin_pixmap_create (TWIN_ARGB32 , gif -> width , gif -> height );
572
569
if (!anim -> frames [i ]) {
573
570
free (anim );
574
- _twin_gif_close (gif );
571
+ gif_close (gif );
575
572
return NULL ;
576
573
}
577
574
anim -> frames [i ]-> format = TWIN_ARGB32 ;
578
575
579
- _twin_gif_render_frame (gif , frame );
576
+ gif_render_frame (gif , frame );
580
577
color = frame ;
581
578
twin_pointer_t p = twin_pixmap_pointer (anim -> frames [i ], 0 , 0 );
582
579
twin_coord_t row = 0 , col = 0 ;
583
580
for (int j = 0 ; j < gif -> width * gif -> height ; j ++ ) {
584
581
uint8_t r = * (color ++ );
585
582
uint8_t g = * (color ++ );
586
583
uint8_t b = * (color ++ );
587
- if (!_twin_gif_is_bgcolor (gif , color ))
584
+ if (!gif_is_bgcolor (gif , color ))
588
585
* (p .argb32 ++ ) = 0xFF000000U | (r << 16 ) | (g << 8 ) | b ;
589
586
/* Construct background */
590
587
else if (((row >> 3 ) + (col >> 3 )) & 1 )
@@ -599,15 +596,15 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
599
596
}
600
597
/* GIF delay in units of 1/100 second */
601
598
anim -> frame_delays [i ] = gif -> gce .delay * 10 ;
602
- _twin_gif_get_frame (gif );
599
+ gif_get_frame (gif );
603
600
}
604
601
anim -> iter = twin_animation_iter_init (anim );
605
602
if (!anim -> iter ) {
606
603
free (anim );
607
- _twin_gif_close (gif );
604
+ gif_close (gif );
608
605
return NULL ;
609
606
}
610
- _twin_gif_close (gif );
607
+ gif_close (gif );
611
608
return anim ;
612
609
}
613
610
0 commit comments