If picture does no exist, warn and exit
[cascardo/movie.git] / scale.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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <cairo.h>
23 #include <SDL.h>
24 #include <SDL_image.h>
25  
26 cairo_surface_t *
27 CairoFromSDL (SDL_Surface *surface)
28 {
29   return cairo_image_surface_create_for_data (surface->pixels,
30                                               CAIRO_FORMAT_ARGB32,
31                                               surface->w, surface->h,
32                                               surface->pitch);
33 }
34
35 SDL_Surface *
36 CairoToSDL (cairo_surface_t *surface)
37 {
38   return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
39                                    cairo_image_surface_get_width (surface),
40                                    cairo_image_surface_get_height (surface),
41                                    32,
42                                    cairo_image_surface_get_stride (surface),
43                                    0x00ff0000, 0x0000ff00, 0x000000ff,
44                                    0xff000000);
45 }
46
47 SDL_Surface *
48 CairoTarget (SDL_Surface *image, double scale)
49 {
50   unsigned char *data = NULL;
51   cairo_surface_t *surface = NULL;
52   cairo_t *ctx = NULL;
53   cairo_surface_t *source;
54   SDL_Surface *dest;
55   int w, h;
56   w = image->w * scale;
57   h = image->h * scale;
58   data = malloc (w * h * 4);
59   surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
60                                                  w, h, w * 4);
61   ctx = cairo_create (surface);
62   cairo_scale (ctx, scale, scale);
63   source = CairoFromSDL (image);
64   cairo_set_source_surface (ctx, source, 0, 0);
65   cairo_paint (ctx);
66   cairo_surface_destroy (source);
67   dest = CairoToSDL (surface);
68   cairo_surface_destroy (surface);
69   cairo_destroy (ctx);
70   return dest;
71 }
72
73 SDL_Surface *
74 CairoScale (SDL_Surface *image, double scale)
75 {
76   SDL_Surface *newfmt;
77   SDL_Surface *slice;
78   SDL_Surface *scaled_image;
79   newfmt = SDL_CreateRGBSurface (SDL_SWSURFACE, 1, 1,
80                                  32,
81                                  0x00ff0000,
82                                  0x0000ff00,
83                                  0x000000ff,
84                                  0xff000000);
85   slice = SDL_ConvertSurface (image, newfmt->format, SDL_SWSURFACE);
86   SDL_FreeSurface (newfmt);
87   scaled_image = CairoTarget (slice, scale);
88   SDL_FreeSurface (slice);
89   return scaled_image;
90 }