20130313
[gdash.git] / src / gtk / gtkpixbuf.cpp
blob1312159c580081fd2db30cd4dd78649bafc23646
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 "config.h"
19 #include <gtk/gtk.h>
20 #include <cstdlib>
21 #include <stdexcept>
23 #include "cave/helper/colors.hpp"
24 #include "gtk/gtkpixbuf.hpp"
26 GTKPixbuf::GTKPixbuf(int length, unsigned char const *data)
28 GInputStream *is=g_memory_input_stream_new_from_data(data, length, NULL);
29 GError *error=NULL;
30 pixbuf=gdk_pixbuf_new_from_stream(is, NULL, &error);
31 g_object_unref(is);
32 if (!pixbuf) {
33 std::runtime_error exc(std::string("cannot load image ")+error->message);
34 g_error_free(error);
35 throw exc;
39 GTKPixbuf::GTKPixbuf(const char *filename)
41 GError *error=NULL;
42 pixbuf=gdk_pixbuf_new_from_file(filename, &error);
43 if (!pixbuf) {
44 std::runtime_error exc(std::string("cannot load image ")+error->message);
45 g_error_free(error);
46 throw exc;
50 GTKPixbuf::GTKPixbuf(int w, int h)
52 pixbuf=gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, w, h);
55 GTKPixbuf::GTKPixbuf(GdkPixbuf *pixbuf_)
56 : pixbuf(pixbuf_)
60 int GTKPixbuf::get_width() const
62 return gdk_pixbuf_get_width(pixbuf);
65 int GTKPixbuf::get_height() const
67 return gdk_pixbuf_get_height(pixbuf);
70 void GTKPixbuf::blit_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const
72 GdkPixbuf *destpb=static_cast<GTKPixbuf&>(dest).pixbuf;
74 gdk_pixbuf_composite(pixbuf, destpb, dx, dy, w, h, dx-x, dy-y, 1, 1, GDK_INTERP_NEAREST, 255);
77 void GTKPixbuf::copy_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const
79 // gdk_pixbuf_copy_area does not like clipping
80 if (dy+h>dest.get_height()) {
81 h=dest.get_height()-dy;
82 if (h<0)
83 return;
85 if (dx+w>dest.get_width()) {
86 w=dest.get_width()-dx;
87 if (w<0)
88 return;
90 GdkPixbuf *destpb=static_cast<GTKPixbuf&>(dest).pixbuf;
91 gdk_pixbuf_copy_area(pixbuf, x, y, w, h, destpb, dx, dy);
94 bool GTKPixbuf::has_alpha() const
96 return gdk_pixbuf_get_has_alpha(pixbuf);
99 void GTKPixbuf::fill_rect(int x, int y, int w, int h, const GdColor& c)
101 GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf(pixbuf, x, y, w, h);
102 gdk_pixbuf_fill(sub, (c.get_r()<<24) | (c.get_g()<<16) | (c.get_b()<<8) | (0xff << 0));
103 g_object_unref(sub);
106 unsigned char *GTKPixbuf::get_pixels() const
108 return gdk_pixbuf_get_pixels(pixbuf);
111 int GTKPixbuf::get_pitch() const
113 return gdk_pixbuf_get_rowstride(pixbuf);
116 GTKPixbuf::~GTKPixbuf()
118 g_object_unref(pixbuf);