README.md edited online with Bitbucket
[gdash.git] / src / gtk / gtkpixbuf.cpp
blobb695c2d628629708ef67649311db5b8a2b35800d
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 "config.h"
26 #include <gtk/gtk.h>
27 #include <stdexcept>
29 #include "cave/colors.hpp"
30 #include "gtk/gtkpixbuf.hpp"
33 GTKPixbuf::GTKPixbuf(int w, int h) {
34 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, w, h);
38 GTKPixbuf::GTKPixbuf(GdkPixbuf *pb)
39 : pixbuf(pb) {
40 /* convert to our own format if necessary */
41 if (!gdk_pixbuf_get_has_alpha(pixbuf) || gdk_pixbuf_get_bits_per_sample(pixbuf) != 8
42 || gdk_pixbuf_get_n_channels(pixbuf) != 4) {
43 int w = gdk_pixbuf_get_width(pixbuf);
44 int h = gdk_pixbuf_get_height(pixbuf);
45 GdkPixbuf *newpixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, w, h);
46 gdk_pixbuf_copy_area(pixbuf, 0, 0, w, h, newpixbuf, 0, 0);
47 g_object_unref(pixbuf);
48 pixbuf = newpixbuf;
53 int GTKPixbuf::get_width() const {
54 return gdk_pixbuf_get_width(pixbuf);
58 int GTKPixbuf::get_height() const {
59 return gdk_pixbuf_get_height(pixbuf);
63 void GTKPixbuf::blit_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const {
64 GdkPixbuf *destpb = static_cast<GTKPixbuf &>(dest).pixbuf;
66 gdk_pixbuf_composite(pixbuf, destpb, dx, dy, w, h, dx - x, dy - y, 1, 1, GDK_INTERP_NEAREST, 255);
70 void GTKPixbuf::copy_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const {
71 // gdk_pixbuf_copy_area does not like clipping
72 if (dy + h > dest.get_height()) {
73 h = dest.get_height() - dy;
74 if (h < 0)
75 return;
77 if (dx + w > dest.get_width()) {
78 w = dest.get_width() - dx;
79 if (w < 0)
80 return;
82 GdkPixbuf *destpb = static_cast<GTKPixbuf &>(dest).pixbuf;
83 gdk_pixbuf_copy_area(pixbuf, x, y, w, h, destpb, dx, dy);
87 void GTKPixbuf::fill_rect(int x, int y, int w, int h, const GdColor &c) {
88 GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf(pixbuf, x, y, w, h);
89 unsigned char r, g, b;
90 c.get_rgb(r, g, b);
91 gdk_pixbuf_fill(sub, (guint32(r) << 24) | (guint32(g) << 16) | (guint32(b) << 8) | (0xff << 0));
92 g_object_unref(sub);
96 unsigned char *GTKPixbuf::get_pixels() const {
97 return gdk_pixbuf_get_pixels(pixbuf);
101 int GTKPixbuf::get_pitch() const {
102 return gdk_pixbuf_get_rowstride(pixbuf);
106 GTKPixbuf::~GTKPixbuf() {
107 g_object_unref(pixbuf);