20130427
[gdash.git] / src / gfx / pixbuf.cpp
blob8c822db44414d9021db3242d3b5cf0adf90eccdf
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <glib.h>
26 #include "config.h"
27 #include "cave/colors.hpp"
28 #include "gfx/pixbuf.hpp"
31 /// Virtual dtor.
32 Pixbuf::~Pixbuf() {
36 /// Creates a guint32 value, which can be raw-written to a pixbuf memory area.
37 guint32 Pixbuf::rgba_pixel_from_color(const GdColor &col, unsigned a) {
38 unsigned char r, g, b;
39 col.get_rgb(r, g, b);
40 return (guint32(r) << rshift) | (guint32(g) << gshift) | (guint32(b) << bshift) | (a << ashift);
59 static inline int
60 c64_png_colors(int r, int g, int b, int a) {
61 static const int c64_png_cols[] = {
62 /* abgr */
63 /* 0000 */ 0, /* transparent */
64 /* 0001 */ 0,
65 /* 0010 */ 0,
66 /* 0011 */ 0,
67 /* 0100 */ 0,
68 /* 0101 */ 0,
69 /* 0110 */ 0,
70 /* 0111 */ 0,
71 /* 1000 */ 1, /* black - background */
72 /* 1001 */ 2, /* red - foreg1 */
73 /* 1010 */ 5, /* green - amoeba */
74 /* 1011 */ 4, /* yellow - foreg3 */
75 /* 1100 */ 6, /* blue - slime */
76 /* 1101 */ 3, /* purple - foreg2 */
77 /* 1110 */ 7, /* black around arrows (used in editor) is coded as cyan */
78 /* 1111 */ 8, /* white is the arrow */
81 /* take most significant bit of each */
82 int c = (a >> 7) * 8 + (b >> 7) * 4 + (g >> 7) * 2 + (r >> 7) * 1;
84 return c64_png_cols[c];
88 /* takes a c64_gfx.png-coded 32-bit pixbuf, and creates a paletted pixbuf in our internal format. */
89 std::vector<unsigned char> Pixbuf::c64_gfx_data_from_pixbuf(Pixbuf const &image) {
90 std::vector<unsigned char> c64_gfx_data(image.get_width()*image.get_height());
92 int out = 0;
93 for (int y = 0; y < image.get_height(); y++) {
94 const guint32 *p = image.get_row(y);
95 for (int x = 0; x < image.get_width(); x++) {
96 int r = (p[x] & image.rmask) >> image.rshift;
97 int g = (p[x] & image.gmask) >> image.gshift;
98 int b = (p[x] & image.bmask) >> image.bshift;
99 int a = (p[x] & image.amask) >> image.ashift;
100 c64_gfx_data[out++] = c64_png_colors(r, g, b, a);
104 return c64_gfx_data;