]> git.draconx.ca Git - liblbx.git/blob - src/gui/render.c
8a1af81991f5c20d231ed5ec220e09eb4ec3fbbf
[liblbx.git] / src / gui / render.c
1 #include <assert.h>
2 #include <gtk/gtk.h>
3
4 #include "render.h"
5 #include "image.h"
6
7 /* LBX images can have up to three palettes, with each superseding the last. */
8 struct lbx_colour palette_external[256];
9 struct lbx_colour palette_internal[256];
10 struct lbx_colour palette_override[256];
11
12 static void get_colour(unsigned char index, unsigned char out[static 4])
13 {
14         struct lbx_colour *colour;
15
16         if (palette_override[index].active)
17                 colour = palette_override + index;
18         else if (palette_internal[index].active)
19                 colour = palette_internal + index;
20         else if (palette_external[index].active)
21                 colour = palette_external + index;
22         else
23                 colour = &(struct lbx_colour) { .red = 0xff, .blue = 0xff };
24
25         out[0] = colour->red;
26         out[1] = colour->green;
27         out[2] = colour->blue;
28         out[3] = -1; /* opaque */
29 }
30
31 int render_to_pixbuf(LBX_IMG *image, GdkPixbuf *pixbuf, unsigned frame)
32 {
33         unsigned char **framedata, **framemask, *outbuf;
34         struct lbx_imginfo info;
35         unsigned stride;
36
37         lbximg_getinfo(image, &info);
38         assert(info.width  == gdk_pixbuf_get_width(pixbuf));
39         assert(info.height == gdk_pixbuf_get_height(pixbuf));
40
41         framedata = lbximg_getframe(image, frame);
42         g_return_val_if_fail(framedata, -1);
43         framemask = lbximg_getmask(image);
44
45         outbuf = gdk_pixbuf_get_pixels(pixbuf);
46         stride = gdk_pixbuf_get_rowstride(pixbuf);
47
48         for (unsigned i = 0; i < info.height; i++) {
49                 unsigned char (*px)[4] = (void *)(outbuf + i*stride);
50
51                 for (unsigned j = 0; j < info.width; j++) {
52                         if (framemask[i][j])
53                                 get_colour(framedata[i][j], px[j]);
54                         else
55                                 px[j][3] = 0; /* transparent */
56                 }
57         }
58
59         return 0;
60 }