2 * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
26 #include <SDL_image.h>
28 #define SWAP(x, y) do { \
29 x ^= y; y ^= x; x ^= y; \
32 #define ABS(x) ((x) < 0 ? -(x) : (x))
34 #define IS_CENTER(cx, cy, x, y) \
35 ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
41 ReadPoints (char *filename)
48 file = fopen (filename, "r");
49 fscanf (file, "%d\n", &psize);
50 points = malloc (sizeof (SDL_Rect) * psize);
55 for (i = 0; i < psize; i++)
57 getline (&buffer, &len, file);
58 points[i].x = strtol (buffer, &next, 0);
59 points[i].y = strtol (next+1, NULL, 0);
67 static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
69 static int inc, err, thre, swap;
70 static int x1, y1, x2, y2;
73 next = (cur + 1) % psize;
74 if ((points[next].x == rect.x && points[next].y == rect.y) || cur == -1)
77 next = (cur + 1) % psize;
86 if (ABS (inc) > ABS (thre))
96 rect.x = (swap ? y : x);
97 rect.y = (swap ? x : y);
98 (x2 < x1) ? x-- : x++;
100 if (err >= ABS (thre))
103 y += (inc < 0) ? -1 : 1;
109 ShowPoint (SDL_Surface *screen, SDL_Surface *image)
111 SDL_BlitSurface (image, NULL, screen, NULL);
112 SDL_UpdateRect (screen, 0, 0, 0, 0);
116 CairoFromSDL (SDL_Surface *surface)
118 return cairo_image_surface_create_for_data (surface->pixels,
120 surface->w, surface->h,
125 CairoToSDL (cairo_surface_t *surface)
127 return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
128 cairo_image_surface_get_width (surface),
129 cairo_image_surface_get_height (surface),
131 cairo_image_surface_get_stride (surface),
132 0x00ff0000, 0x0000ff00, 0x000000ff,
137 CairoTarget (SDL_Surface *image)
139 static unsigned char *data = NULL;
140 static cairo_surface_t *surface = NULL;
141 static cairo_t *ctx = NULL;
142 cairo_surface_t *source;
143 if (data == NULL && surface == NULL && ctx == NULL)
145 data = malloc (WIDTH * HEIGHT * 4);
146 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
147 WIDTH, HEIGHT, WIDTH * 4);
148 ctx = cairo_create (surface);
149 cairo_scale (ctx, 2, 2);
151 source = CairoFromSDL (image);
152 cairo_set_source_surface (ctx, source, 0, 0);
154 cairo_surface_destroy (source);
155 return CairoToSDL (surface);
159 GetNextImage (SDL_Surface *image)
164 center = GetNextPoint (), GetNextPoint (), GetNextPoint ();
165 slice = SDL_CreateRGBSurface (SDL_SWSURFACE, WIDTH/2, HEIGHT/2,
167 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
168 center.x -= (WIDTH/2) / 2;
169 center.y -= (HEIGHT/2) / 2;
172 SDL_BlitSurface (image, ¢er, slice, NULL);
173 SDL_UpdateRect (slice, 0, 0, 0, 0);
174 scale = CairoTarget (slice);
175 SDL_FreeSurface (slice);
180 ShowNext (Uint32 interval, void *data)
183 event.type = SDL_USEREVENT;
185 SDL_PushEvent ((SDL_Event *) &event);
190 main (int argc, char **argv)
196 ReadPoints ("pro-gnu");
197 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
198 screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
199 image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
200 SDL_AddTimer (0, ShowNext, NULL);
201 while (SDL_WaitEvent (&event))
203 if (event.type == SDL_KEYDOWN)
205 else if (event.type == SDL_USEREVENT)
207 slice = GetNextImage (image);
208 ShowPoint (screen, slice);
209 SDL_FreeSurface (slice);
212 SDL_FreeSurface (image);