]> git.draconx.ca Git - liblbx.git/blob - src/gui/render.c
liblbx: Kill the LBXimg typedef.
[liblbx.git] / src / gui / render.c
1 /*
2  *  2ooM: The Master of Orion II Reverse Engineering Project
3  *  Routines for rendering LBX images to a GdkPixbuf.
4  *  Copyright (C) 2010 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <assert.h>
20 #include <gtk/gtk.h>
21
22 #include "render.h"
23 #include "image.h"
24
25 /* LBX images can have up to three palettes, with each superseding the last. */
26 struct lbx_colour palette_external[256];
27 struct lbx_colour palette_internal[256];
28 struct lbx_colour palette_override[256];
29
30 static void get_colour(unsigned char index, unsigned char out[static 4])
31 {
32         struct lbx_colour *colour;
33
34         if (palette_override[index].active)
35                 colour = palette_override + index;
36         else if (palette_internal[index].active)
37                 colour = palette_internal + index;
38         else if (palette_external[index].active)
39                 colour = palette_external + index;
40         else
41                 colour = &(struct lbx_colour) { .red = 0xff, .blue = 0xff };
42
43         out[0] = colour->red;
44         out[1] = colour->green;
45         out[2] = colour->blue;
46         out[3] = -1; /* opaque */
47 }
48
49 int render_to_pixbuf(struct lbx_image *image, GdkPixbuf *pixbuf, unsigned frame)
50 {
51         unsigned char **framedata, **framemask, *outbuf;
52         struct lbx_imginfo info;
53         unsigned stride;
54
55         lbx_img_getinfo(image, &info);
56         assert(info.width  == gdk_pixbuf_get_width(pixbuf));
57         assert(info.height == gdk_pixbuf_get_height(pixbuf));
58
59         framedata = lbx_img_getframe(image, frame);
60         g_return_val_if_fail(framedata, -1);
61         framemask = lbx_img_getmask(image);
62
63         outbuf = gdk_pixbuf_get_pixels(pixbuf);
64         stride = gdk_pixbuf_get_rowstride(pixbuf);
65
66         for (unsigned i = 0; i < info.height; i++) {
67                 unsigned char (*px)[4] = (void *)(outbuf + i*stride);
68
69                 for (unsigned j = 0; j < info.width; j++) {
70                         if (framemask[i][j])
71                                 get_colour(framedata[i][j], px[j]);
72                         else
73                                 px[j][3] = 0; /* transparent */
74                 }
75         }
76
77         return 0;
78 }