From 6045cac966af7bdc0309dae41c59bf024df73096 Mon Sep 17 00:00:00 2001
From: Julien Danjou <julien@danjou.info>
Date: Thu, 23 Oct 2008 09:56:06 +0200
Subject: [PATCH] surface: add get_data object

This allows to get data as a string and to either write it directly
or put it back in another library.

Signed-off-by: Julien Danjou <julien@danjou.info>
---
 obj_surface.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/obj_surface.c b/obj_surface.c
index 748cdc4..c68af49 100644
--- a/obj_surface.c
+++ b/obj_surface.c
@@ -374,6 +374,20 @@ surface_set_fallback_resolution (lua_State *L) {
     return 0;
 }
 
+static int
+surface_get_data (lua_State *L) {
+    unsigned char *data;
+    cairo_surface_t **obj = luaL_checkudata(L, 1, MT_NAME_SURFACE);
+    int width = cairo_image_surface_get_width(*obj);
+    int height = cairo_image_surface_get_height(*obj);
+    int stride = cairo_format_stride_for_width(cairo_image_surface_get_format(*obj), width);
+    data = cairo_image_surface_get_data(*obj);
+
+    lua_pushlstring(L, data, height * stride);
+
+    return 1;
+}
+
 #if CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE
 static int
 surface_set_size (lua_State *L) {
@@ -475,6 +489,7 @@ surface_methods[] = {
     { "set_fallback_resolution", surface_set_fallback_resolution },
     { "set_size", surface_set_size },
     { "show_page", surface_show_page },
+    { "get_data", surface_get_data },
 #if CAIRO_HAS_PNG_FUNCTIONS
     { "write_to_png", surface_write_to_png },
 #endif
-- 
1.5.6.5

