Rescale points and use its individual scale to show image
[cascardo/movie.git] / movie.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #define _GNU_SOURCE
20 #include <glib.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24
25 #include "point.h"
26
27 #define SWAP(x, y) do { \
28         x ^= y; y ^= x; x ^= y; \
29         } while (0)
30
31 void
32 InsertLine (GArray *points, Point *src, Point *dst)
33 {
34   Point rect;
35   int inc, err, thre, swap;
36   int x1, y1, x2, y2;
37   int x, y;
38   rect.name = NULL;
39   err = 0;
40   swap = 0;
41   x1 = src->x;
42   y1 = src->y;
43   x2 = dst->x;
44   y2 = dst->y;
45   inc = y2 - y1;
46   thre = x2 - x1;
47   if (ABS (inc) > ABS (thre))
48   {
49     SWAP (inc, thre);
50     SWAP (x1, y1);
51     SWAP (x2, y2);
52     swap = 1;
53   }
54   for (y = y1, x = x1; (x2 < x1) ? (x >= x2) : (x <= x2); (x2 < x1) ? x-- : x++)
55   {
56     rect.x = (swap ? y : x);
57     rect.y = (swap ? x : y);
58     g_array_append_val (points, rect);
59     err += ABS (inc);
60     if (err >= ABS (thre))
61     {
62       err -= ABS (thre);
63       y += (inc < 0) ? -1 : 1;
64     }
65   }
66 }
67
68 GArray *
69 ReadPoints (char *filename)
70 {
71   GArray *points;
72   FILE *file;
73   char *buffer;
74   char *next;
75   size_t len;
76   ssize_t r;
77   int i = 0;
78   Point last;
79   Point rect;
80   file = fopen (filename, "r");
81   points = g_array_new (FALSE, TRUE, sizeof (Point));
82   buffer = NULL;
83   len = 0;
84   rect.x = rect.y = 0;
85   rect.name = NULL;
86   last = rect;
87   while (!feof (file))
88   {
89     r = getline (&buffer, &len, file);
90     buffer[r - 1] = '\0';
91     rect.x = strtol (buffer, &next, 0);
92     rect.y = strtol (next+1, &next, 0);
93     strtol (next, &next, 0);
94     while (isspace (*next)) next++;
95     rect.name = g_strdup (next);
96     if (i > 0)
97       InsertLine (points, &last, &rect);
98     g_array_append_val (points, rect);
99     last = rect;
100     i++;
101   }
102   fclose (file);
103   return points;
104 }
105
106 void
107 rescale_points (GArray *points)
108 {
109   Point *point;
110   int i;
111   for (i = 0; i < points->len; i++)
112     {
113       point = &(g_array_index (points, Point, i));
114       point->rx = 4.0;
115       point->ry = 4.0;
116       point->x *= point->rx;
117       point->y *= point->ry;
118     }
119 }