+#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;
+}