LCOV - code coverage report
Current view: top level - gifdec - gifread.c (source / functions) Coverage Total Hit
Test: gifread.info Lines: 94.6 % 56 53
Test Date: 2024-03-08 00:28:24 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /* gifdec example -- simple GIF player using SDL2
       2              :  * compiling:
       3              :  *   cc `pkg-config --cflags --libs sdl2` -o example gifdec.c example.c
       4              :  * executing:
       5              :  *   ./example animation.gif
       6              :  * */
       7              : 
       8              : #include <stdio.h>
       9              : #include <stdlib.h>
      10              : #include <stdint.h>
      11              : #include <signal.h>
      12              : #include <unistd.h>
      13              : 
      14              : #include "gifdec.h"
      15              : 
      16              : #ifdef GCOV
      17              : void __gcov_dump();
      18              : 
      19              : void gc_handler(int signum) {
      20              :     __gcov_dump();
      21              :     exit(1);
      22              : }
      23              : #endif
      24              : 
      25           62 : void handle_plain_text(
      26              :         struct gd_GIF *gif, uint16_t tx, uint16_t ty,
      27              :         uint16_t tw, uint16_t th, uint8_t cw, uint8_t ch,
      28              :         uint8_t fg, uint8_t bg
      29              :     ) {
      30           62 :     printf("plain_text_ext tx=%u ty=%u tw=%u th=%u cw=%u ch=%u fg=%u bg=%u\ntext: ",
      31           62 :             tx,ty,tw,th,cw,ch,fg,bg);
      32              :     unsigned char c;
      33           62 :     while (read(gif->fd, &c, 1)) {
      34           62 :         if (c == 0) break;
      35            0 :         write(1,&c,1);
      36              :     }
      37           62 :     printf("\n");
      38           62 : }
      39              : 
      40            6 : void handle_comment(struct gd_GIF *gif) {
      41            6 :     printf("comment: ");
      42              :     unsigned char c;
      43           12 :     while (read(gif->fd, &c, 1)) {
      44           12 :         if (c == 0) break;
      45            6 :         write(1,&c,1);
      46              :     }
      47            6 :     printf("\n");
      48            6 : }
      49              : 
      50              : int
      51          596 : main(int argc, char *argv[])
      52              : {
      53              :     gd_GIF *gif;
      54          596 :     char title[32] = {0};
      55              :     uint8_t *color, *frame;
      56              :     void *addr;
      57              :     int i, j;
      58              :     uint32_t pixel;
      59              :     int ret, paused, quit;
      60              :     uint32_t t0, t1, delay, delta;
      61              : 
      62              : #ifdef GCOV
      63              :     for (int sig = 1; sig <= SIGRTMAX; ++sig) {
      64              :         signal(sig, gc_handler);
      65              :     }
      66              : #endif
      67              : 
      68          596 :     if (argc != 2) {
      69            0 :         fprintf(stderr, "usage:\n  %s gif-file\n", argv[0]);
      70            0 :         return 1;
      71              :     }
      72          596 :     gif = gd_open_gif(argv[1]);
      73          596 :     if (!gif) {
      74          146 :         fprintf(stderr, "Could not open %s\n", argv[1]);
      75          146 :         return 1;
      76              :     }
      77          450 :     frame = malloc(gif->width * gif->height * 3);
      78          450 :     if (!frame) {
      79          160 :         fprintf(stderr, "Could not allocate frame\n");
      80          160 :         return 1;
      81              :     }
      82          290 :     gif->plain_text = handle_plain_text;
      83          290 :     gif->comment = handle_comment;
      84          290 :     snprintf(title, sizeof(title) - 1, "GIF %dx%d %d colors",
      85              :              gif->width, gif->height, gif->palette->size);
      86          290 :     color = &gif->gct.colors[gif->bgindex * 3];
      87          290 :     int num_bgcolors = 0;
      88          290 :     int loopcount = 1;
      89          772 :     while (loopcount) {
      90          485 :         ret = gd_get_frame(gif);
      91          485 :         if (ret == -1)
      92            3 :             break;
      93          482 :         gd_render_frame(gif, frame);
      94          482 :         color = frame;
      95      8985338 :         for (i = 0; i < gif->height; i++) {
      96  >10233*10^7 :             for (j = 0; j < gif->width; j++) {
      97  >10232*10^7 :                 if (!gd_is_bgcolor(gif, color)) {
      98  10810515272 :                     num_bgcolors++;
      99  10810515272 :                 }
     100  >10232*10^7 :                 color += 3;
     101  >10232*10^7 :             }
     102      8984856 :         }
     103          482 :         if (ret == 0) {
     104          287 :             loopcount--;
     105          287 :             gd_rewind(gif);
     106          287 :         }
     107              :     }
     108          290 :     free(frame);
     109          290 :     gd_close_gif(gif);
     110          290 :     return 0;
     111          596 : }
        

Generated by: LCOV version 2.0-1