If picture does no exist, warn and exit
[cascardo/movie.git] / movie.c
diff --git a/movie.c b/movie.c
index 6c48c1a..ec850ad 100644 (file)
--- a/movie.c
+++ b/movie.c
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#define _GNU_SOURCE
 #include <glib.h>
-
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
+#include <math.h>
 
 #include "point.h"
 
@@ -34,7 +36,7 @@ InsertLine (GArray *points, Point *src, Point *dst)
   int inc, err, thre, swap;
   int x1, y1, x2, y2;
   int x, y;
-  rect.name = src->name;
+  rect.name = NULL;
   err = 0;
   swap = 0;
   x1 = src->x;
@@ -77,8 +79,12 @@ ReadPoints (char *filename)
   Point last;
   Point rect;
   file = fopen (filename, "r");
-  points = g_array_new (FALSE, TRUE, sizeof (SDL_Rect));
-  names = g_ptr_array_new ();
+  if (file == NULL)
+    {
+      fprintf (stderr, "Could not open file %s\n", filename);
+      exit (1);
+    }
+  points = g_array_new (FALSE, TRUE, sizeof (Point));
   buffer = NULL;
   len = 0;
   rect.x = rect.y = 0;
@@ -91,13 +97,86 @@ ReadPoints (char *filename)
     rect.x = strtol (buffer, &next, 0);
     rect.y = strtol (next+1, &next, 0);
     strtol (next, &next, 0);
-    if (i > 0)
-      InsertLine (points, &last, &rect);
     while (isspace (*next)) next++;
     rect.name = g_strdup (next);
+    if (i > 0)
+      InsertLine (points, &last, &rect);
+    g_array_append_val (points, rect);
     last = rect;
     i++;
   }
   fclose (file);
   return points;
 }
+
+GArray *
+drop_dup_frames (GArray *points, int n)
+{
+  GArray *frames;
+  Point *point;
+  Point *next;
+  int i;
+  int j;
+  int inc;
+  int thre;
+  int err;
+  inc = n;
+  frames = g_array_new (FALSE, TRUE, sizeof (Point));
+  for (i = 0; i < points->len;)
+    {
+      j = i + 1;
+      point = next = &(g_array_index (points, Point, j));
+      while (next->name == NULL && j < points->len)
+        {
+         j++;
+          next = &(g_array_index (points, Point, j));
+       }
+      thre = j - i;
+      err = 0;
+      g_array_append_val (frames, g_array_index (points, Point, i));
+      for (; i < j; i++)
+        {
+         err += inc;
+         while (err > thre)
+           {
+             err -= thre;
+              g_array_append_val (frames, g_array_index (points, Point, i));
+           }
+       }
+    }
+  return frames;
+}
+
+GArray *
+get_scales (int n)
+{
+  GArray *scales;
+  double scale;
+  double factor;
+  scales = g_array_new (FALSE, TRUE, sizeof (double));
+  factor = pow (4.0, 1.0/((double) n/2));
+  factor = 1.0/factor;
+  for (scale = 4.00; scale > 1.0 && scales->len < n/2; scale *= factor)
+    scales = g_array_append_val (scales, scale);
+  factor = 1.0/factor;
+  for (scale = 1.0; scale < 4.0 && scales->len < n; scale *= factor)
+    scales = g_array_append_val (scales, scale);
+  return scales;
+}
+
+void
+rescale_points (GArray *points, GArray *scales)
+{
+  Point *point;
+  double scale;
+  int i;
+  for (i = 0; i < points->len; i++)
+    {
+      point = &(g_array_index (points, Point, i));
+      scale = g_array_index (scales, double, (i % scales->len));
+      point->rx = scale;
+      point->ry = scale;
+      point->x *= point->rx;
+      point->y *= point->ry;
+    }
+}