login

<     >

2015-07-23 13:14:44 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

Add support for GIF loop count.

diff --git a/gif.c b/gif.c
index 667023c..197947c 100644
--- a/gif.c
+++ b/gif.c
@@ -38,8 +38,10 @@ del_trie(Node *root)
     free(root);
 }
 
+static void put_loop(GIF *gif, uint16_t loop);
+
 GIF *
-new_gif(const char *fname, uint16_t w, uint16_t h, uint8_t *gct)
+new_gif(const char *fname, uint16_t w, uint16_t h, uint8_t *gct, int loop)
 {
     GIF *gif = calloc(1, sizeof(*gif) + 2*w*h);
     gif->w = w; gif->h = h;
@@ -55,9 +57,21 @@ new_gif(const char *fname, uint16_t w, uint16_t h, uint8_t *gct)
     write_num(gif->fd, h);
     write(gif->fd, (uint8_t []) {0xF3, 0x00, 0x00}, 3);
     write(gif->fd, gct, 0x30);
+    if (loop >= 0 && loop <= 0xFFFF)
+        put_loop(gif, (uint16_t) loop);
     return gif;
 }
 
+static void
+put_loop(GIF *gif, uint16_t loop)
+{
+    write(gif->fd, (uint8_t []) {'!', 0xFF, 0x0B}, 3);
+    write(gif->fd, "NETSCAPE2.0", 11);
+    write(gif->fd, (uint8_t []) {0x03, 0x01}, 2);
+    write_num(gif->fd, loop);
+    write(gif->fd, "\0", 1);
+}
+
 /* Add packed key to buffer, updating offset and partial.
  *   gif->offset holds position to put next *bit*
  *   gif->partial holds bits to include in next byte */

diff --git a/gif.h b/gif.h
index a9dbe72..951a4d8 100644
--- a/gif.h
+++ b/gif.h
@@ -9,6 +9,6 @@ typedef struct GIF {
     uint8_t buffer[0xFF];
 } GIF;
 
-GIF *new_gif(const char *fname, uint16_t w, uint16_t h, uint8_t *gct);
+GIF *new_gif(const char *fname, uint16_t w, uint16_t h, uint8_t *gct, int loop);
 void add_frame(GIF *gif, uint16_t d);
 void close_gif(GIF* gif);

diff --git a/main.c b/main.c
index c505be3..2ee61a9 100644
--- a/main.c
+++ b/main.c
@@ -111,7 +111,7 @@ render(Term *term, Font *font, GIF *gif, uint16_t delay)
 int
 convert_script(Term *term, const char *timing, const char *dialogue,
                const char *mbf, const char *anim, float div, float max,
-               int cur, int pbcols)
+               int loop, int cur, int pbcols)
 {
     FILE *ft;
     int fd;
@@ -144,7 +144,7 @@ convert_script(Term *term, const char *timing, const char *dialogue,
     }
     w = term->cols * font->header.w;
     h = term->rows * font->header.h;
-    gif = new_gif(anim, w, h, term->plt);
+    gif = new_gif(anim, w, h, term->plt, loop);
     if (!gif) {
         fprintf(stderr, "error: could not create GIF: %s\n", anim);
         goto no_gif;
@@ -219,6 +219,7 @@ help(char *name)
         "  -o output    File name of GIF output\n"
         "  -d divisor   Speedup, as in scriptreplay(1)\n"
         "  -m maxdelay  Maximum delay, as in scriptreplay(1)\n"
+        "  -l count     GIF loop count\n"
         "  -c on|off    Show/hide cursor\n"
         "  -v           Verbose mode (show parser logs)\n"
         "  -q           Quiet mode (don't show progress bar)\n"
@@ -233,6 +234,7 @@ main(int argc, char *argv[])
     char *f;
     char *o;
     float d, m;
+    int l;
     char *t;
     char *s;
     int c;
@@ -247,9 +249,10 @@ main(int argc, char *argv[])
     f = "uw-ttyp0-11.mbf";
     o = "con.gif";
     d = 1.0; m = FLT_MAX;
+    l = -1;
     c = 1;
     q = 0;
-    while ((opt = getopt(argc, argv, "w:h:f:o:d:m:c:vq")) != -1) {
+    while ((opt = getopt(argc, argv, "w:h:f:o:d:m:l:c:vq")) != -1) {
         switch (opt) {
         case 'w':
             w = atoi(optarg);
@@ -269,6 +272,9 @@ main(int argc, char *argv[])
         case 'm':
             m = atof(optarg);
             break;
+        case 'l':
+            l = atoi(optarg);
+            break;
         case 'c':
             if (!strcmp(optarg, "on") || !strcmp(optarg, "1"))
                 c = 1;
@@ -294,7 +300,7 @@ main(int argc, char *argv[])
     t = argv[optind++];
     s = argv[optind++];
     term = new_term(h, w);
-    ret = convert_script(term, t, s, f, o, d, m, c, q ? 0 : size.ws_col-1);
+    ret = convert_script(term, t, s, f, o, d, m, l, c, q ? 0 : size.ws_col-1);
     free(term);
     return ret;
 }