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.
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
);
30 pixbuf
=gdk_pixbuf_new_from_stream(is
, NULL
, &error
);
33 std::runtime_error
exc(std::string("cannot load image ")+error
->message
);
39 GTKPixbuf::GTKPixbuf(const char *filename
)
42 pixbuf
=gdk_pixbuf_new_from_file(filename
, &error
);
44 std::runtime_error
exc(std::string("cannot load image ")+error
->message
);
50 GTKPixbuf::GTKPixbuf(int w
, int h
)
52 pixbuf
=gdk_pixbuf_new(GDK_COLORSPACE_RGB
, TRUE
, 8, w
, h
);
55 GTKPixbuf::GTKPixbuf(GdkPixbuf
*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
;
85 if (dx
+w
>dest
.get_width()) {
86 w
=dest
.get_width()-dx
;
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));
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
);