33 * Copyright (c) 2024 National Cheng Kung University, Taiwan
44 * All rights reserved.
55 *
6- *
76 * This file integrates the GIF decoder originally developed by Marcel Rodrigues
87 * as part of the gifdec project. You can find the original gifdec project at:
98 * https://github.com/lecram/gifdec
1716#include "twin.h"
1817#include "twin_private.h"
1918
20- typedef struct _twin_gif_palette {
19+ typedef struct {
2120 int size ;
2221 uint8_t colors [0x100 * 3 ];
23- } twin_gif_palette_t ;
22+ } gif_palette_t ;
2423
25- typedef struct _twin_gif_gce {
24+ typedef struct {
2625 uint16_t delay ;
2726 uint8_t tindex ;
2827 uint8_t disposal ;
2928 int input ;
3029 int transparency ;
31- } twin_gif_gce_t ;
30+ } gif_gce_t ;
3231
3332typedef struct _twin_gif {
3433 int fd ;
3534 off_t anim_start ;
3635 twin_coord_t width , height ;
3736 twin_coord_t depth ;
3837 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 ;
4241 uint16_t fx , fy , fw , fh ;
4342 uint8_t bgindex ;
4443 uint8_t * canvas , * frame ;
@@ -67,9 +66,8 @@ static uint16_t read_num(int fd)
6766 return bytes [0 ] + (((uint16_t ) bytes [1 ]) << 8 );
6867}
6968
70- static twin_gif_t * _twin_gif_open (const char * fname )
69+ static twin_gif_t * gif_open (const char * fname )
7170{
72- int fd ;
7371 uint8_t sigver [3 ];
7472 uint16_t width , height , depth ;
7573 uint8_t fdsz , bgidx , aspect ;
@@ -78,7 +76,7 @@ static twin_gif_t *_twin_gif_open(const char *fname)
7876 int gct_sz ;
7977 twin_gif_t * gif ;
8078
81- fd = open (fname , O_RDONLY );
79+ int fd = open (fname , O_RDONLY );
8280 if (fd == -1 )
8381 return NULL ;
8482#ifdef _WIN32
@@ -173,7 +171,7 @@ static table_t *table_new(int key_size)
173171 return table ;
174172}
175173
176- /* Add table_t entry_t . Return value:
174+ /* Add table entry . Return value:
177175 * 0 on success
178176 * +1 if key size must be incremented after this addition
179177 * -1 if could not realloc table
@@ -487,7 +485,7 @@ static void read_ext(twin_gif_t *gif)
487485}
488486
489487/* 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 )
491489{
492490 char sep ;
493491
@@ -507,23 +505,23 @@ static int _twin_gif_get_frame(twin_gif_t *gif)
507505 return 1 ;
508506}
509507
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 )
511509{
512510 memcpy (buffer , gif -> canvas , gif -> width * gif -> height * 3 );
513511 render_frame_rect (gif , buffer );
514512}
515513
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 )
517515{
518516 return !memcmp (& gif -> palette -> colors [gif -> bgindex * 3 ], color , 3 );
519517}
520518
521- static void _twin_gif_rewind (twin_gif_t * gif )
519+ static void gif_rewind (twin_gif_t * gif )
522520{
523521 lseek (gif -> fd , gif -> anim_start , SEEK_SET );
524522}
525523
526- static void _twin_gif_close (twin_gif_t * gif )
524+ static void gif_close (twin_gif_t * gif )
527525{
528526 close (gif -> fd );
529527 free (gif -> frame );
@@ -536,7 +534,7 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
536534 if (!anim )
537535 return NULL ;
538536
539- twin_gif_t * gif = _twin_gif_open (path );
537+ twin_gif_t * gif = gif_open (path );
540538 if (!gif ) {
541539 free (anim );
542540 return NULL ;
@@ -550,41 +548,40 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
550548 anim -> height = gif -> height ;
551549
552550 int frame_count = 0 ;
553- while (_twin_gif_get_frame (gif )) {
551+ while (gif_get_frame (gif ))
554552 frame_count ++ ;
555- }
556553
557554 anim -> n_frames = frame_count ;
558555 anim -> frames = malloc (sizeof (twin_pixmap_t * ) * frame_count );
559556 anim -> frame_delays = malloc (sizeof (twin_time_t ) * frame_count );
560557
561- _twin_gif_rewind (gif );
558+ gif_rewind (gif );
562559 uint8_t * color , * frame ;
563560 frame = malloc (gif -> width * gif -> height * 3 );
564561 if (!frame ) {
565562 free (anim );
566- _twin_gif_close (gif );
563+ gif_close (gif );
567564 return NULL ;
568565 }
569566 for (twin_count_t i = 0 ; i < frame_count ; i ++ ) {
570567 anim -> frames [i ] =
571568 twin_pixmap_create (TWIN_ARGB32 , gif -> width , gif -> height );
572569 if (!anim -> frames [i ]) {
573570 free (anim );
574- _twin_gif_close (gif );
571+ gif_close (gif );
575572 return NULL ;
576573 }
577574 anim -> frames [i ]-> format = TWIN_ARGB32 ;
578575
579- _twin_gif_render_frame (gif , frame );
576+ gif_render_frame (gif , frame );
580577 color = frame ;
581578 twin_pointer_t p = twin_pixmap_pointer (anim -> frames [i ], 0 , 0 );
582579 twin_coord_t row = 0 , col = 0 ;
583580 for (int j = 0 ; j < gif -> width * gif -> height ; j ++ ) {
584581 uint8_t r = * (color ++ );
585582 uint8_t g = * (color ++ );
586583 uint8_t b = * (color ++ );
587- if (!_twin_gif_is_bgcolor (gif , color ))
584+ if (!gif_is_bgcolor (gif , color ))
588585 * (p .argb32 ++ ) = 0xFF000000U | (r << 16 ) | (g << 8 ) | b ;
589586 /* Construct background */
590587 else if (((row >> 3 ) + (col >> 3 )) & 1 )
@@ -599,15 +596,15 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
599596 }
600597 /* GIF delay in units of 1/100 second */
601598 anim -> frame_delays [i ] = gif -> gce .delay * 10 ;
602- _twin_gif_get_frame (gif );
599+ gif_get_frame (gif );
603600 }
604601 anim -> iter = twin_animation_iter_init (anim );
605602 if (!anim -> iter ) {
606603 free (anim );
607- _twin_gif_close (gif );
604+ gif_close (gif );
608605 return NULL ;
609606 }
610- _twin_gif_close (gif );
607+ gif_close (gif );
611608 return anim ;
612609}
613610
0 commit comments