Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 145 lines (113 sloc) 3.043 kb
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <gif_lib.h>


#if defined(SIMULATOR)
#include <X11/keysym.h>
#endif

#include "led.h"


int g_quit = 0;

int g_wide = 0;
int g_high = 0;


#if defined(SIMULATOR)
void my_callback(LED_HANDLE_T h, unsigned long key)
{
    if (key == 'q')
        g_quit++;
}
#endif


void usage(char *argv0)
{
    fprintf(stderr, "%s fname [repeat_count]\n", argv0);
    fprintf(stderr, " Where fname is the name of .gif file with optional animations and\n");
    fprintf(stderr, " the optional repeat_count specifies the number of times to show it, with 0 being infinite.\n");
    fprintf(stderr, "Note that the display will truncate at %d high, and %d wide.\n", g_high, g_wide);
}

int find_delay(SavedImage *img)
{
    int i;
    int delay = 0;

    for (i = 0; i < img->ExtensionBlockCount; i++)
    {
        ExtensionBlock *e = &img->ExtensionBlocks[i];
        if (e->Function == GRAPHICS_EXT_FUNC_CODE)
        {
            unsigned char *d = (unsigned char *) e->Bytes + 1;
            return *d + ((*(d+1)) * 256);
        }
    }

    return delay;
}

void show_one_image(LED_HANDLE_T p, ColorMapObject *colors, SavedImage *img)
{
    int x;
    int y;
    unsigned char *bits;
    int delay;

    bits = img->RasterBits;
    for (y = 0; y < img->ImageDesc.Height; y++)
        for (x = 0; x < img->ImageDesc.Width; x++)
        {
            GifColorType *ctype;
            int r, g, b;

            ctype = &colors->Colors[*bits++];
            r = ctype->Red;
            g = ctype->Green;
            b = ctype->Blue;

            if (x < g_wide && y < g_high)
                led_set_pixel(p, x, y, MAX_BRIGHT, r, g, b);
        }

    delay = find_delay(img);
    usleep(delay * 10000);
}

void show_gif(LED_HANDLE_T p, GifFileType *f)
{
    int i;
    for (i = 0; i < f->ImageCount; i++)
    {
        show_one_image(p, f->SavedImages[i].ImageDesc.ColorMap ?
                            f->SavedImages[i].ImageDesc.ColorMap : f->SColorMap,
                            &f->SavedImages[i]);
    }
}


int main (int argc, char *argv[])
{
    LED_HANDLE_T p;
    GifFileType *f;
    int repeat_count = 0;
    int count = 0;


    p = led_init();
#if defined(SIMULATOR)
    ledsim_set_x_callback(p, my_callback);
#endif

    led_get_size(p, &g_wide, &g_high);

    if (argc < 2 || argc > 3)
    {
        fprintf(stderr, "Error: supply the name of a .gif file to display.\n");
        usage(argv[0]);
        g_quit++;
    }

    if (argc == 3)
        repeat_count = atoi(argv[2]);

    f = DGifOpenFileName(argv[1]);
    if (! f)
    {
        fprintf(stderr, "Error opening %s\n", argv[1]);
        usage(argv[0]);
        g_quit++;
    }
    else
        if (DGifSlurp(f) != GIF_OK)
        {
            fprintf(stderr, "Error reading rest of %s\n", argv[1]);
            g_quit++;
        }

    while (! g_quit && (repeat_count == 0 || count < repeat_count))
    {
        show_gif(p, f);
        count++;
    }

    led_term(p);

    return 0;
}
Something went wrong with that request. Please try again.