20130313
[gdash.git] / src / gtk / gtkpixbuffactory.cpp
blobae89ab76b9260bf304a470c30c9868339932b957
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
28 GTKPixbuf const &srcgtk = static_cast<GTKPixbuf const &>(src);
29 guint32 color = (c.get_r()<<16) | (c.get_g()<<8) | (c.get_b()<<0);
30 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);
31 return new GTKPixbuf(pb);
34 GTKPixbuf *GTKPixbufFactory::create_subpixbuf(Pixbuf &src, int x, int y, int w, int h) const
36 GTKPixbuf &srcgtk = static_cast<GTKPixbuf &>(src);
37 GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf(srcgtk.get_gdk_pixbuf(), x, y, w, h);
38 g_assert(sub!=NULL);
39 // gdk references pixbuf automatically, so it won't be cleared until the new GTKPixbuf is deleted, too
40 return new GTKPixbuf(sub);
43 Pixmap *GTKPixbufFactory::create_pixmap_from_pixbuf(const Pixbuf& pb, bool format_alpha) const
45 GTKPixbuf *scaled = static_cast<GTKPixbuf *>(create_scaled(pb));
46 GdkDrawable *pixmap;
47 GdkBitmap *mask;
49 if (format_alpha)
50 gdk_pixbuf_render_pixmap_and_mask(scaled->get_gdk_pixbuf(), &pixmap, &mask, 128); // 128 = alpha threshold
51 else {
52 gdk_pixbuf_render_pixmap_and_mask(scaled->get_gdk_pixbuf(), &pixmap, NULL, 128); // 128 = alpha threshold
53 mask=NULL;
55 delete scaled;
57 return new GTKPixmap(pixmap, mask);
60 GTKPixbuf *GTKPixbufFactory::create_rotated(const Pixbuf &src, Rotation r) const
62 GdkPixbuf const *srcgtk = static_cast<GTKPixbuf const&>(src).get_gdk_pixbuf();
63 GdkPixbuf *pb=NULL;
64 switch (r) {
65 case None:
66 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_NONE);
67 break;
68 case CounterClockWise:
69 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
70 break;
71 case UpsideDown:
72 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
73 break;
74 case ClockWise:
75 pb=gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_CLOCKWISE);
76 break;
78 g_assert(pb!=NULL);
79 return new GTKPixbuf(pb);
83 GTKPixbufFactory::GTKPixbufFactory(GdScalingType scaling_type_, bool pal_emulation_)
84 : PixbufFactory(scaling_type_, pal_emulation_)
89 GTKPixbuf *GTKPixbufFactory::create(int w, int h) const
91 return new GTKPixbuf(w, h);
95 GTKPixbuf *GTKPixbufFactory::create_from_inline(int length, unsigned char const *data) const
97 return new GTKPixbuf(length, data);
101 GTKPixbuf *GTKPixbufFactory::create_from_file(const char *filename) const
103 return new GTKPixbuf(filename);