If picture does no exist, warn and exit
[cascardo/movie.git] / movie.c
diff --git a/movie.c b/movie.c
index b2d85e3..ec850ad 100644 (file)
--- a/movie.c
+++ b/movie.c
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define WIDTH 800
-#define HEIGHT 600
-#define FPS (200)
-#define FRAME_INTERVAL (1000/FPS)
-
+#define _GNU_SOURCE
 #include <glib.h>
-
 #include <stdio.h>
 #include <stdlib.h>
-#include <SDL.h>
-#include <SDL_image.h>
+#include <ctype.h>
+#include <math.h>
+
+#include "point.h"
 
 #define SWAP(x, y) do { \
        x ^= y; y ^= x; x ^= y; \
        } while (0)
 
-#define IS_CENTER(cx, cy, x, y) \
-       ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
-
-GArray *points;
-GPtrArray *names;
-
 void
-InsertLine (GArray *points, SDL_Rect *src, SDL_Rect *dst)
+InsertLine (GArray *points, Point *src, Point *dst)
 {
-  SDL_Rect rect;
+  Point rect;
   int inc, err, thre, swap;
   int x1, y1, x2, y2;
   int x, y;
-  rect.w = WIDTH;
-  rect.h = HEIGHT;
+  rect.name = NULL;
   err = 0;
   swap = 0;
   x1 = src->x;
@@ -76,23 +66,29 @@ InsertLine (GArray *points, SDL_Rect *src, SDL_Rect *dst)
   }
 }
 
-void
+GArray *
 ReadPoints (char *filename)
 {
+  GArray *points;
   FILE *file;
   char *buffer;
   char *next;
   size_t len;
   ssize_t r;
   int i = 0;
-  SDL_Rect last;
-  SDL_Rect rect;
+  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 = rect.w = rect.h = 0;
+  rect.x = rect.y = 0;
+  rect.name = NULL;
   last = rect;
   while (!feof (file))
   {
@@ -101,75 +97,86 @@ ReadPoints (char *filename)
     rect.x = strtol (buffer, &next, 0);
     rect.y = strtol (next+1, &next, 0);
     strtol (next, &next, 0);
+    while (isspace (*next)) next++;
+    rect.name = g_strdup (next);
     if (i > 0)
       InsertLine (points, &last, &rect);
-    while (isspace (*next)) next++;
-    g_ptr_array_add (names, strdup (next));
+    g_array_append_val (points, rect);
     last = rect;
     i++;
   }
   fclose (file);
+  return points;
 }
 
-void
-ShowPoint (SDL_Surface *screen, SDL_Surface *image, SDL_Rect center, double scale)
+GArray *
+drop_dup_frames (GArray *points, int n)
 {
-  center.x = (center.x - WIDTH/2) * scale;
-  center.y = (center.y - HEIGHT/2) * scale;
-  center.w = WIDTH;
-  center.h = HEIGHT;
-  SDL_BlitSurface (image, &center, screen, NULL);
-  SDL_UpdateRect (screen, 0, 0, 0, 0);
+  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;
 }
 
-int
-main (int argc, char **argv)
+GArray *
+get_scales (int n)
 {
-  SDL_Surface *screen;
-  SDL_Surface *image;
-  SDL_Rect rect;
-  SDL_Event event;
-  Uint32 last, now, deslast, start;
-  int i, des, desl;
-  ReadPoints ("pro-gnu");
-  SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
-  screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
-  image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
-  start = deslast = last = SDL_GetTicks ();
-  desl = des = i = 0;
-  while (1)
-  {
-    if (SDL_PollEvent (&event))
-    {
-      if (event.type == SDL_KEYDOWN)
-        break;
-    }
-    now = SDL_GetTicks ();
-    /* skip */
-    if (now > deslast + 1000)
-    {
-      printf ("%f %f\n", (double) (now - start) / (double) des, (double) (now - deslast) / (double) (des - desl));
-      desl = des;
-      deslast = now;
-    }
-    while (now > last + FRAME_INTERVAL)
+  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++)
     {
-      last += FRAME_INTERVAL;
-      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;
     }
-    if (i > points->len)
-      i = 0;
-    rect = g_array_index (points, SDL_Rect, i);
-    ShowPoint (screen, image, rect, 1.0);
-    SDL_Delay (FRAME_INTERVAL - (now - last));
-    i++;
-    des++;
-  }
-  SDL_FreeSurface (image);
-  SDL_Quit ();
-  g_array_free (points, TRUE);
-  for (i = 0; i < names->len; i++)
-    free (g_ptr_array_index (names, i));
-  g_ptr_array_free (names, TRUE);
-  return 0;
 }