Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a utility to show a gif

  • Loading branch information...
commit 890aaf97e7a8cca005afe7d4cd831b069296d2ff 1 parent b69703c
@jwhite66 authored
Showing with 152 additions and 1 deletion.
  1. +8 −1 Makefile
  2. +144 −0 showgif.c
View
9 Makefile
@@ -72,7 +72,8 @@ OBJECTS=$(OUTDIR)/generated_led.o $(OUTDIR)/Print.o $(OUTDIR)/HardwareSerial.o $
all: $(TARGET_HEX) $(OUTDIR)/drive $(OUTDIR)/makemap \
$(OUTDIR)/ledscroll $(OUTDIR)/ledscrollsim \
$(OUTDIR)/war $(OUTDIR)/warsim \
- $(OUTDIR)/tetris $(OUTDIR)/tetrissim
+ $(OUTDIR)/tetris $(OUTDIR)/tetrissim \
+ $(OUTDIR)/showgif $(OUTDIR)/showgifsim
$(OUTDIR)/message.h: Makefile $(OUTDIR)/makemap
$(OUTDIR)/makemap 6x10.bdf "$(MESSAGE)" > $(OUTDIR)/message.h
@@ -125,6 +126,12 @@ $(OUTDIR)/tetrissim: tetris.c tetris.h led.h $(OUTDIR)/libledsim.a $(OUTDIR)/fif
$(OUTDIR)/tetris: tetris.c tetris.h led.h $(OUTDIR)/libled.a $(OUTDIR)/fifo.o
gcc -Wall -I. $(LIBLEDCFLAGS) -o $@ $< $(OUTDIR)/fifo.o $(LIBLEDLDFLAGS) -L$(OUTDIR) -lled -l pthread $(LIBCONFIGLDFLAGS) -lm
+$(OUTDIR)/showgifsim: showgif.c led.h $(OUTDIR)/libledsim.a $(OUTDIR)/fifo.o
+ gcc -g -Wall -I. -DSIMULATOR $(SIMCFLAGS) -o $@ $< $(OUTDIR)/fifo.o $(SIMLDFLAGS) -lgif
+
+$(OUTDIR)/showgif: showgif.c led.h $(OUTDIR)/libled.a $(OUTDIR)/fifo.o
+ gcc -g -Wall -I. $(LIBLEDCFLAGS) -o $@ $< $(OUTDIR)/fifo.o $(LIBLEDLDFLAGS) -L$(OUTDIR) -lled -l pthread $(LIBCONFIGLDFLAGS) -lgif
+
$(OUTDIR)/makemap: makemap.c led.h
gcc -Wall -o $@ -I. -I $(OUTDIR) -I /usr/include/freetype2 $< -lfreetype -lm
View
144 showgif.c
@@ -0,0 +1,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;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.