20130320
[gdash.git] / src / gtk / gtkpixbuffactory.cpp
blobdc5b3024e1e70618d1f133c2771374b10c33b99a
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>
22 #include "gtk/gtkpixbuffactory.hpp"
23 #include "cave/helper/colors.hpp"
24 #include "gtk/gtkpixmap.hpp"
26 GTKPixbuf *GTKPixbufFactory::create_composite_color(const Pixbuf &src, const GdColor &c, unsigned char alpha) const {
27 GTKPixbuf const &srcgtk = static_cast<GTKPixbuf const &>(src);
28 guint32 color = (c.get_r()<<16) | (c.get_g()<<8) | (c.get_b()<<0);
29 GdkPixbuf *pb = gdk_pixbuf_composite_color_simple(srcgtk.get_gdk_pixbuf(), src.get_width(), src.get_height(), GDK_INTERP_NEAREST, 255-alpha, 1, color, color);
30 return new GTKPixbuf(pb);
33 GTKPixbuf *GTKPixbufFactory::create_subpixbuf(Pixbuf &src, int x, int y, int w, int h) const {
34 GTKPixbuf &srcgtk = static_cast<GTKPixbuf &>(src);
35 GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf(srcgtk.get_gdk_pixbuf(), x, y, w, h);
36 g_assert(sub!=NULL);
37 // gdk references pixbuf automatically, so it won't be cleared until the new GTKPixbuf is deleted, too
38 return new GTKPixbuf(sub);
41 Pixmap *GTKPixbufFactory::create_pixmap_from_pixbuf(const Pixbuf &pb, bool format_alpha) const {
42 GTKPixbuf *scaled = static_cast<GTKPixbuf *>(create_scaled(pb));
43 GdkDrawable *pixmap;
44 GdkBitmap *mask;
46 if (format_alpha)
47 gdk_pixbuf_render_pixmap_and_mask(scaled->get_gdk_pixbuf(), &pixmap, &mask, 128); // 128 = alpha threshold
48 else {
49 gdk_pixbuf_render_pixmap_and_mask(scaled->get_gdk_pixbuf(), &pixmap, NULL, 128); // 128 = alpha threshold
50 mask=NULL;
52 delete scaled;
54 return new GTKPixmap(pixmap, mask);
57 GTKPixbuf *GTKPixbufFactory::create_rotated(const Pixbuf &src, Rotation r) const {
58 GdkPixbuf const *srcgtk = static_cast<GTKPixbuf const &>(src).get_gdk_pixbuf();
59 GdkPixbuf *pb=NULL;
60 switch (r) {
61 case None:
62 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_NONE);
63 break;
64 case CounterClockWise:
65 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
66 break;
67 case UpsideDown:
68 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
69 break;
70 case ClockWise:
71 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_CLOCKWISE);
72 break;
74 g_assert(pb!=NULL);
75 return new GTKPixbuf(pb);
79 GTKPixbufFactory::GTKPixbufFactory(GdScalingType scaling_type_, bool pal_emulation_)
80 : PixbufFactory(scaling_type_, pal_emulation_) {
84 GTKPixbuf *GTKPixbufFactory::create(int w, int h) const {
85 return new GTKPixbuf(w, h);
89 GTKPixbuf *GTKPixbufFactory::create_from_inline(int length, unsigned char const *data) const {
90 return new GTKPixbuf(length, data);
94 GTKPixbuf *GTKPixbufFactory::create_from_file(const char *filename) const {
95 return new GTKPixbuf(filename);