]> git.draconx.ca Git - liblbx.git/commitdiff
lbxgui: Render LBX directly to Cairo surfaces.
authorNick Bowler <nbowler@draconx.ca>
Wed, 29 Jan 2014 05:30:13 +0000 (00:30 -0500)
committerNick Bowler <nbowler@draconx.ca>
Fri, 31 Jan 2014 05:39:01 +0000 (00:39 -0500)
This replaces the GDK pixbuf with a Cairo image surface, and updates
the render path to the new liblbx API.

Makefile.am
src/gui/image.c [new file with mode: 0644]
src/gui/lbxgui.c
src/gui/lbxgui.h [new file with mode: 0644]
src/gui/render.c [deleted file]
src/gui/render.h [deleted file]

index 101f774649d2bcf717b10229f8f49f04eaa84bb5..cd7d3737ffeccfbdac16f0fbb5dfecfce2db98ae 100644 (file)
@@ -44,7 +44,7 @@ lbximg_SOURCES += src/png.c
 endif
 
 nodist_lbxgui_SOURCES = src/gui/lbxgui.glade.c
-lbxgui_SOURCES = src/gui/lbxgui.c src/gui/render.c src/gui/render.h \
+lbxgui_SOURCES = src/gui/lbxgui.c src/gui/lbxgui.h src/gui/image.c \
        src/gui/bg.xbm
 lbxgui_LDFLAGS = $(AM_LDFLAGS) -export-dynamic
 lbxgui_LDADD = liblbx.la $(GTK_LIBS)
diff --git a/src/gui/image.c b/src/gui/image.c
new file mode 100644 (file)
index 0000000..973bc51
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+ *  2ooM: The Master of Orion II Reverse Engineering Project
+ *  Rendering routines for Cairo surfaces.
+ *  Copyright © 2014 Nick Bowler
+ *
+ *  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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+#include <cairo/cairo.h>
+#include <glib.h>
+
+#include "lbxgui.h"
+
+static const cairo_user_data_key_t last_frame_key;
+
+/*
+ * Scale 6-bit colour values (0-63) to 8-bit (0-255) as evenly as possible.
+ */
+static inline guint32 scale6to8(unsigned x)
+{
+       assert(x <= 0x3f);
+
+       return x*0xff / 0x3f;
+}
+
+/*
+ * Output a single row of pixel data in cairo ARGB32 format.
+ */
+static void write_argb(unsigned char *argb, unsigned char *index,
+                       unsigned n, unsigned x, unsigned y, unsigned stride,
+                       const struct lbx_colour *palette)
+{
+       argb += (unsigned long) y * stride;
+       argb += 4ul * x;
+
+       for (unsigned i = 0; i < n; i++) {
+               const struct lbx_colour *c;
+               guint32 px = 0xffff00ff;
+
+               c = palette+index[i];
+               if (c->active) {
+                       px = 0xff000000
+                          | (scale6to8(c->red)   << 16)
+                          | (scale6to8(c->green) <<  8)
+                          |  scale6to8(c->blue);
+               }
+
+               memcpy(argb + 4ul*i, &px, 4);
+       }
+}
+
+/*
+ * Update a cairo RGBA32 surface according to the drawing commands in the
+ * specified frame.
+ */
+static int render_argb(cairo_surface_t *dst, struct lbx_image *img,
+                       unsigned frame, const struct lbx_colour *palette)
+{
+       unsigned char *row, *sdata;
+       unsigned x, y, stride;
+       int ret = 0;
+       long rc;
+
+       g_return_val_if_fail(cairo_surface_get_type(dst) == CAIRO_SURFACE_TYPE_IMAGE, -1);
+       g_return_val_if_fail(cairo_image_surface_get_format(dst) == CAIRO_FORMAT_ARGB32, -1);
+       g_return_val_if_fail(cairo_image_surface_get_width(dst) == img->width, -1);
+       g_return_val_if_fail(cairo_image_surface_get_height(dst) == img->height, -1);
+
+       sdata = cairo_image_surface_get_data(dst);
+       stride = cairo_image_surface_get_stride(dst);
+
+       row = malloc(img->width);
+       if (!row)
+               return -1;
+
+       rc = lbx_img_seek(img, frame);
+       if (rc < 0) {
+               ret = -1;
+               goto out;
+       }
+
+       cairo_surface_flush(dst);
+       while ((rc = lbx_img_read_row_header(img, &x, &y)) != 0) {
+               if (rc < 0) {
+                       ret = -1;
+                       break;
+               }
+
+               rc = lbx_img_read_row_data(img, row);
+               if (rc < 0) {
+                       ret = -1;
+                       break;
+               }
+
+               write_argb(sdata, row, rc, x, y, stride, palette);
+       }
+       cairo_surface_mark_dirty(dst);
+out:
+       free(row);
+       return ret;
+}
+
+static int get_last_frame(cairo_surface_t *s)
+{
+       int *data = cairo_surface_get_user_data(s, &last_frame_key);
+
+       return data ? *data : -1;
+}
+
+static void set_last_frame(cairo_surface_t *s, int frame)
+{
+       int *data = cairo_surface_get_user_data(s, &last_frame_key);
+
+       if (!data) {
+               cairo_status_t rc;
+
+               data = malloc(sizeof *data);
+               if (!data)
+                       return;
+
+               rc = cairo_surface_set_user_data(s, &last_frame_key,
+                                                data, free);
+               if (rc != CAIRO_STATUS_SUCCESS) {
+                       free(data);
+                       return;
+               }
+       }
+
+       *data = frame;
+}
+
+/*
+ * Render the specified frame onto a cairo surface.  If this is the first
+ * time rendering a frame onto this surface, it is not assumed to contain
+ * any particular data.  Otherwise, unless there was an intervening call to
+ * lbxgui_render_restart, the surface is assumed to contain valid frame data
+ * from the most recent call to this function.
+ */
+int lbxgui_render_argb(cairo_surface_t *dst, struct lbx_image *img,
+                       unsigned frame, const struct lbx_colour *palette)
+{
+       int last_frame = get_last_frame(dst), ref_frame = 0;
+
+       g_return_val_if_fail(frame < img->frames, -1);
+       g_return_val_if_fail(dst, -1);
+
+       if (img->chunk)
+               ref_frame = (frame / img->chunk) * img->chunk;
+
+       if (last_frame < ref_frame || last_frame > frame) {
+               cairo_t *cr = cairo_create(dst);
+               cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
+               cairo_paint(cr);
+               cairo_destroy(cr);
+
+               last_frame = -1;
+       }
+
+       for (unsigned i = MAX(last_frame+1, ref_frame); i <= frame; i++) {
+               int rc = render_argb(dst, img, i, palette);
+               if (rc < 0) {
+                       set_last_frame(dst, -1);
+                       return rc;
+               }
+
+               set_last_frame(dst, i);
+       }
+
+       return 0;
+}
+
+/*
+ * "Forget" the last rendered frame on the specified surface so that the next
+ * image will be redrawn from scratch.
+ */
+void lbxgui_render_restart(cairo_surface_t *dst)
+{
+       g_return_if_fail(dst);
+
+       set_last_frame(dst, -1);
+}
+
+/*
+ * Copies the active elements in the src palette over the corresponding
+ * elements in the dst palette.
+ */
+void lbxgui_stack_palette(struct lbx_colour *dst, const struct lbx_colour *src)
+{
+       for (unsigned i = 0; i < 256; i++) {
+               if (src[i].active)
+                       dst[i] = src[i];
+       }
+}
index 14da3787fc916cfe1b8c2a3a9f3b65dfae1b2ddd..e9726a6b58c9855b49320299d719c049606d99d7 100644 (file)
@@ -27,7 +27,7 @@
 
 #include "lbx.h"
 #include "image.h"
-#include "render.h"
+#include "lbxgui.h"
 
 #include "bg.xbm"
 
@@ -36,12 +36,27 @@ static GtkBuilder *builder;
 
 static GtkWidget *canvas;
 
+static cairo_surface_t *framebuf;
 static cairo_pattern_t *bg_pattern;
 static bool bg_stipple;
 
 static struct lbx_image *image;
+static struct lbx_colour palette[256];
 
-static GdkPixbuf *framebuf;
+/* We support stacking up to three palettes, with each superseding the last. */
+static struct lbx_colour palette_external[256];
+static struct lbx_colour palette_internal[256];
+static struct lbx_colour palette_override[256];
+
+static void refresh_palette(void)
+{
+       memcpy(palette, palette_external, sizeof palette);
+       lbxgui_stack_palette(palette, palette_internal);
+       lbxgui_stack_palette(palette, palette_override);
+
+       if (framebuf)
+               lbxgui_render_restart(framebuf);
+}
 
 void play_toggled(GtkToggleButton *button, gpointer data)
 {
@@ -65,8 +80,8 @@ void set_frame(GtkSpinButton *spin, gpointer data)
 {
        unsigned frame = gtk_spin_button_get_value_as_int(spin);
 
-       if (image && framebuf)
-               render_to_pixbuf(image, framebuf, frame);
+       if (image)
+               lbxgui_render_argb(framebuf, image, frame, palette);
        gdk_window_invalidate_rect(canvas->window, NULL, FALSE);
 }
 
@@ -74,6 +89,8 @@ static void redraw_image(void)
 {
        GtkSpinButton *spin;
 
+       refresh_palette();
+
        spin = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "framespin"));
        set_frame(spin, NULL);
 }
@@ -141,9 +158,6 @@ gboolean canvas_expose(GtkWidget *canvas, GdkEventExpose *event, gpointer data)
 {
        cairo_t *cr;
 
-       if (!framebuf)
-               return FALSE;
-
        cr = gdk_cairo_create(canvas->window);
 
        if (bg_stipple) {
@@ -160,7 +174,7 @@ gboolean canvas_expose(GtkWidget *canvas, GdkEventExpose *event, gpointer data)
                cairo_fill(cr);
        }
 
-       gdk_cairo_set_source_pixbuf(cr, framebuf, 0, 0);
+       cairo_set_source_surface(cr, framebuf, 0, 0);
        gdk_cairo_rectangle(cr, &event->area);
        cairo_fill(cr);
 
@@ -169,22 +183,19 @@ gboolean canvas_expose(GtkWidget *canvas, GdkEventExpose *event, gpointer data)
        return TRUE;
 }
 
-static int alloc_framebuffer(struct lbx_image *image)
+static int alloc_framebuffer(struct lbx_image *img)
 {
        GtkSpinButton *spin;
 
-       if (framebuf)
-               g_object_unref(framebuf);
-
-       framebuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
-               image->width, image->height);
-       g_return_val_if_fail(framebuf, -1);
+       cairo_surface_destroy(framebuf);
+       framebuf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
+                                             img->width, img->height);
 
        spin = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "framespin"));
-       gtk_spin_button_set_range(spin, 0, image->frames-1);
+       gtk_spin_button_set_range(spin, 0, img->frames-1);
        gtk_spin_button_set_value(spin, 0);
 
-       gtk_widget_set_size_request(canvas, image->width, image->height);
+       gtk_widget_set_size_request(canvas, img->width, img->height);
        return 0;
 }
 
@@ -287,10 +298,7 @@ void set_image(GtkComboBox *combo)
                        lbx_img_close(img);
                }
 
-               if (alloc_framebuffer(img) == -1) {
-                       puts("crap");
-                       lbx_img_close(img);
-               }
+               alloc_framebuffer(img);
        }
 
        image = img;
diff --git a/src/gui/lbxgui.h b/src/gui/lbxgui.h
new file mode 100644 (file)
index 0000000..9241652
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef LBXGUI_H_
+#define LBXGUI_H_
+
+#include "image.h"
+
+int lbxgui_render_argb(cairo_surface_t *dst, struct lbx_image *img,
+                       unsigned frame, const struct lbx_colour *palette);
+
+void lbxgui_render_restart(cairo_surface_t *dst);
+void lbxgui_stack_palette(struct lbx_colour *dst, const struct lbx_colour *src);
+
+#endif
diff --git a/src/gui/render.c b/src/gui/render.c
deleted file mode 100644 (file)
index a96cc06..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- *  2ooM: The Master of Orion II Reverse Engineering Project
- *  Routines for rendering LBX images to a GdkPixbuf.
- *  Copyright (C) 2010 Nick Bowler
- *
- *  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 3 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, see <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <assert.h>
-#include <gtk/gtk.h>
-
-#include "render.h"
-#include "image.h"
-
-/* LBX images can have up to three palettes, with each superseding the last. */
-struct lbx_colour palette_external[256];
-struct lbx_colour palette_internal[256];
-struct lbx_colour palette_override[256];
-
-static inline unsigned scale6to8(unsigned x)
-{
-       assert(x <= 0x3f);
-
-       return x*0xff / 0x3f;
-}
-
-static void get_colour(unsigned char index, unsigned char out[static 4])
-{
-       struct lbx_colour *colour;
-
-       if (palette_override[index].active)
-               colour = palette_override + index;
-       else if (palette_internal[index].active)
-               colour = palette_internal + index;
-       else if (palette_external[index].active)
-               colour = palette_external + index;
-       else
-               colour = &(struct lbx_colour) { .red = 0x3f, .blue = 0x3f };
-
-       out[0] = scale6to8(colour->red);
-       out[1] = scale6to8(colour->green);
-       out[2] = scale6to8(colour->blue);
-       out[3] = -1; /* opaque */
-}
-
-int render_to_pixbuf(struct lbx_image *image, GdkPixbuf *pixbuf, unsigned frame)
-{
-       unsigned char **framedata, **framemask, *outbuf;
-       unsigned stride;
-
-       assert(image->width  == gdk_pixbuf_get_width(pixbuf));
-       assert(image->height == gdk_pixbuf_get_height(pixbuf));
-
-       framedata = lbx_img_getframe(image, frame);
-       g_return_val_if_fail(framedata, -1);
-       framemask = lbx_img_getmask(image);
-
-       outbuf = gdk_pixbuf_get_pixels(pixbuf);
-       stride = gdk_pixbuf_get_rowstride(pixbuf);
-
-       for (unsigned i = 0; i < image->height; i++) {
-               unsigned char (*px)[4] = (void *)(outbuf + i*stride);
-
-               for (unsigned j = 0; j < image->width; j++) {
-                       if (framemask[i][j])
-                               get_colour(framedata[i][j], px[j]);
-                       else
-                               px[j][3] = 0; /* transparent */
-               }
-       }
-
-       return 0;
-}
diff --git a/src/gui/render.h b/src/gui/render.h
deleted file mode 100644 (file)
index f24e47b..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef LBXGUI_RENDER_H_
-#define LBXGUI_RENDER_H_
-
-#include "image.h"
-
-extern struct lbx_colour palette_external[256];
-extern struct lbx_colour palette_internal[256];
-extern struct lbx_colour palette_override[256];
-
-int render_to_pixbuf(struct lbx_image *image, GdkPixbuf *pixbuf, unsigned frame);
-
-#endif