]> git.draconx.ca Git - liblbx.git/blob - src/gui/render.c
a96cc06a0b0e83f03b6f80ca944d6e48e70186f5
[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
20 #include <config.h>
21 #include <assert.h>
22 #include <gtk/gtk.h>
23
24 #include "render.h"
25 #include "image.h"
26
27 /* LBX images can have up to three palettes, with each superseding the last. */
28 struct lbx_colour palette_external[256];
29 struct lbx_colour palette_internal[256];
30 struct lbx_colour palette_override[256];
31
32 static inline unsigned scale6to8(unsigned x)
33 {
34         assert(x <= 0x3f);
35
36         return x*0xff / 0x3f;
37 }
38
39 static void get_colour(unsigned char index, unsigned char out[static 4])
40 {
41         struct lbx_colour *colour;
42
43         if (palette_override[index].active)
44                 colour = palette_override + index;
45         else if (palette_internal[index].active)
46                 colour = palette_internal + index;
47         else if (palette_external[index].active)
48                 colour = palette_external + index;
49         else
50                 colour = &(struct lbx_colour) { .red = 0x3f, .blue = 0x3f };
51
52         out[0] = scale6to8(colour->red);
53         out[1] = scale6to8(colour->green);
54         out[2] = scale6to8(colour->blue);
55         out[3] = -1; /* opaque */
56 }
57
58 int render_to_pixbuf(struct lbx_image *image, GdkPixbuf *pixbuf, unsigned frame)
59 {
60         unsigned char **framedata, **framemask, *outbuf;
61         unsigned stride;
62
63         assert(image->width  == gdk_pixbuf_get_width(pixbuf));
64         assert(image->height == gdk_pixbuf_get_height(pixbuf));
65
66         framedata = lbx_img_getframe(image, frame);
67         g_return_val_if_fail(framedata, -1);
68         framemask = lbx_img_getmask(image);
69
70         outbuf = gdk_pixbuf_get_pixels(pixbuf);
71         stride = gdk_pixbuf_get_rowstride(pixbuf);
72
73         for (unsigned i = 0; i < image->height; i++) {
74                 unsigned char (*px)[4] = (void *)(outbuf + i*stride);
75
76                 for (unsigned j = 0; j < image->width; j++) {
77                         if (framemask[i][j])
78                                 get_colour(framedata[i][j], px[j]);
79                         else
80                                 px[j][3] = 0; /* transparent */
81                 }
82         }
83
84         return 0;
85 }