Added some functions to scale SDL Surfaces using cairo
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 13 Aug 2008 22:50:45 +0000 (19:50 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 13 Aug 2008 23:07:26 +0000 (20:07 -0300)
scale.c [new file with mode: 0644]

diff --git a/scale.c b/scale.c
new file mode 100644 (file)
index 0000000..7777f42
--- /dev/null
+++ b/scale.c
@@ -0,0 +1,90 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <cairo.h>
+#include <SDL.h>
+#include <SDL_image.h>
+cairo_surface_t *
+CairoFromSDL (SDL_Surface *surface)
+{
+  return cairo_image_surface_create_for_data (surface->pixels,
+                                             CAIRO_FORMAT_ARGB32,
+                                             surface->w, surface->h,
+                                             surface->pitch);
+}
+
+SDL_Surface *
+CairoToSDL (cairo_surface_t *surface)
+{
+  return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
+                                   cairo_image_surface_get_width (surface),
+                                  cairo_image_surface_get_height (surface),
+                                  32,
+                                  cairo_image_surface_get_stride (surface),
+                                  0x00ff0000, 0x0000ff00, 0x000000ff,
+                                  0xff000000);
+}
+
+SDL_Surface *
+CairoTarget (SDL_Surface *image, double scale)
+{
+  unsigned char *data = NULL;
+  cairo_surface_t *surface = NULL;
+  cairo_t *ctx = NULL;
+  cairo_surface_t *source;
+  SDL_Surface *dest;
+  int w, h;
+  w = image->w * scale;
+  h = image->h * scale;
+  data = malloc (w * h * 4);
+  surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
+                                                w, h, w * 4);
+  ctx = cairo_create (surface);
+  cairo_scale (ctx, scale, scale);
+  source = CairoFromSDL (image);
+  cairo_set_source_surface (ctx, source, 0, 0);
+  cairo_paint (ctx);
+  cairo_surface_destroy (source);
+  dest = CairoToSDL (surface);
+  cairo_surface_destroy (surface);
+  cairo_destroy (ctx);
+  return dest;
+}
+
+SDL_Surface *
+CairoScale (SDL_Surface *image, double scale)
+{
+  SDL_Surface *newfmt;
+  SDL_Surface *slice;
+  SDL_Surface *scaled_image;
+  newfmt = SDL_CreateRGBSurface (SDL_SWSURFACE, 1, 1,
+                                 32,
+                                0x00ff0000,
+                                0x0000ff00,
+                                0x000000ff,
+                                0xff000000);
+  slice = SDL_ConvertSurface (image, newfmt->format, SDL_SWSURFACE);
+  SDL_FreeSurface (newfmt);
+  scaled_image = CairoTarget (slice, scale);
+  SDL_FreeSurface (slice);
+  return scaled_image;
+}