if BUILD_LBXGUI
bin_PROGRAMS += lbxgui
-lbxgui_SOURCES = src/gui/lbxgui.c
+lbxgui_SOURCES = src/gui/render.c src/gui/lbxgui.c
lbxgui_LDADD = liblbx.la $(GTK_LIBS)
lbxgui_CPPFLAGS = -I$(top_srcdir)/src
lbxgui_CFLAGS = $(GTK_CFLAGS)
#include "lbx.h"
#include "image.h"
+#include "render.h"
#include "bg.xbm"
static GdkGC *bg_gc;
static LBX_IMG *image;
-/* LBX images can have 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 GdkPixbuf *framebuf;
}
}
-static void get_colour(unsigned char index, unsigned char out[static 3])
-{
- 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 = 0xff, .blue = 0xff };
-
- out[0] = colour->red;
- out[1] = colour->green;
- out[2] = colour->blue;
-}
-
-static int render_frame(LBX_IMG *img, GdkPixbuf *buf, unsigned frame)
-{
- unsigned char **imgdata, **imgmask;
- struct lbx_imginfo info;
- unsigned char *data;
- unsigned stride;
-
- lbximg_getinfo(image, &info);
- imgdata = lbximg_getframe(image, frame);
- if (!imgdata) {
- printf("failed to decode frame %u\n", frame);
- return -1;
- }
- imgmask = lbximg_getmask(image);
-
- data = gdk_pixbuf_get_pixels(buf);
- stride = gdk_pixbuf_get_rowstride(buf);
-
- for (unsigned i = 0; i < info.height; i++) {
- unsigned char (*p)[4] = (void *)(data + i*stride);
-
- for (unsigned j = 0; j < info.width; j++) {
- if (imgmask[i][j]) {
- get_colour(imgdata[i][j], p[j]);
- p[j][3] = -1;
- } else {
- p[j][3] = 0;
- }
- }
- }
-
- return 0;
-}
-
void set_frame(GtkSpinButton *spin, gpointer data)
{
unsigned frame = gtk_spin_button_get_value_as_int(spin);
- render_frame(image, framebuf, frame);
+ render_to_pixbuf(image, framebuf, frame);
gdk_window_invalidate_rect(canvas->window, NULL, FALSE);
}
--- /dev/null
+#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 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 = 0xff, .blue = 0xff };
+
+ out[0] = colour->red;
+ out[1] = colour->green;
+ out[2] = colour->blue;
+ out[3] = -1; /* opaque */
+}
+
+int render_to_pixbuf(LBX_IMG *image, GdkPixbuf *pixbuf, unsigned frame)
+{
+ unsigned char **framedata, **framemask, *outbuf;
+ struct lbx_imginfo info;
+ unsigned stride;
+
+ lbximg_getinfo(image, &info);
+ assert(info.width == gdk_pixbuf_get_width(pixbuf));
+ assert(info.height == gdk_pixbuf_get_height(pixbuf));
+
+ framedata = lbximg_getframe(image, frame);
+ g_return_val_if_fail(framedata, -1);
+ framemask = lbximg_getmask(image);
+
+ outbuf = gdk_pixbuf_get_pixels(pixbuf);
+ stride = gdk_pixbuf_get_rowstride(pixbuf);
+
+ for (unsigned i = 0; i < info.height; i++) {
+ unsigned char (*px)[4] = (void *)(outbuf + i*stride);
+
+ for (unsigned j = 0; j < info.width; j++) {
+ if (framemask[i][j])
+ get_colour(framedata[i][j], px[j]);
+ else
+ px[j][3] = 0; /* transparent */
+ }
+ }
+
+ return 0;
+}
--- /dev/null
+#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(LBX_IMG *image, GdkPixbuf *pixbuf, unsigned frame);
+
+#endif