20130313
[gdash.git] / src / gfx / pixbuf.cpp
blobca0af84834fde908479df66b0a1d6768be425d53
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <glib.h>
19 #include "config.h"
20 #include "cave/helper/colors.hpp"
21 #include "gfx/pixbuf.hpp"
23 /// Creates a guint32 value, which can be raw-written to a pixbuf memory area.
24 guint32 Pixbuf::rgba_pixel_from_color(const GdColor& col, unsigned a)
26 return (col.get_r()<<rshift) | (col.get_g()<<gshift) | (col.get_b()<<bshift) | (a<<ashift);
45 static inline int
46 c64_png_colors(int r, int g, int b, int a)
48 static const int c64_png_cols[]={
49 /* abgr */
50 /* 0000 */ 0, /* transparent */
51 /* 0001 */ 0,
52 /* 0010 */ 0,
53 /* 0011 */ 0,
54 /* 0100 */ 0,
55 /* 0101 */ 0,
56 /* 0110 */ 0,
57 /* 0111 */ 0,
58 /* 1000 */ 1, /* black - background */
59 /* 1001 */ 2, /* red - foreg1 */
60 /* 1010 */ 5, /* green - amoeba */
61 /* 1011 */ 4, /* yellow - foreg3 */
62 /* 1100 */ 6, /* blue - slime */
63 /* 1101 */ 3, /* purple - foreg2 */
64 /* 1110 */ 7, /* black around arrows (used in editor) is coded as cyan */
65 /* 1111 */ 8, /* white is the arrow */
68 /* take most significant bit of each */
69 int c=(a>>7)*8 + (b>>7)*4 + (g>>7)*2 + (r>>7)*1;
71 return c64_png_cols[c];
75 /* takes a c64_gfx.png-coded 32-bit pixbuf, and creates a paletted pixbuf in our internal format. */
76 std::vector<unsigned char> Pixbuf::c64_gfx_data_from_pixbuf(Pixbuf const& image)
78 std::vector<unsigned char> c64_gfx_data(image.get_width()*image.get_height());
80 image.lock();
81 int out=0;
82 for (int y=0; y<image.get_height(); y++) {
83 const guint32 *p=image.get_row(y);
84 for (int x=0; x<image.get_width(); x++) {
85 int r=(p[x] & image.rmask) >> image.rshift;
86 int g=(p[x] & image.gmask) >> image.gshift;
87 int b=(p[x] & image.bmask) >> image.bshift;
88 int a=(p[x] & image.amask) >> image.ashift;
89 c64_gfx_data[out++]=c64_png_colors(r, g, b, a);
92 image.unlock();
94 return c64_gfx_data;