20130427
[gdash.git] / src / gtk / gtkpixbuffactory.cpp
blob677fcc5b7bfdd4d0a92ffe514c35ee33f3ee214b
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 <cstdlib>
28 #include <stdexcept>
30 #include "gtk/gtkpixbuffactory.hpp"
31 #include "gtk/gtkpixbuf.hpp"
32 #include "cave/colors.hpp"
34 Pixbuf *GTKPixbufFactory::create_composite_color(const Pixbuf &src, const GdColor &c, unsigned char alpha) const {
35 GTKPixbuf const &srcgtk = static_cast<GTKPixbuf const &>(src);
36 guint32 color = c.get_uint_0rgb();
37 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);
38 return new GTKPixbuf(pb);
41 Pixbuf *GTKPixbufFactory::create_subpixbuf(Pixbuf &src, int x, int y, int w, int h) const {
42 GTKPixbuf &srcgtk = static_cast<GTKPixbuf &>(src);
43 GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf(srcgtk.get_gdk_pixbuf(), x, y, w, h);
44 g_assert(sub != NULL);
45 // gdk references pixbuf automatically, so it won't be cleared until the new GTKPixbuf is deleted, too
46 return new GTKPixbuf(sub);
49 Pixbuf *GTKPixbufFactory::create_rotated(const Pixbuf &src, Rotation r) const {
50 GdkPixbuf const *srcgtk = static_cast<GTKPixbuf const &>(src).get_gdk_pixbuf();
51 GdkPixbuf *pb = NULL;
52 switch (r) {
53 case None:
54 pb = gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_NONE);
55 break;
56 case CounterClockWise:
57 pb = gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
58 break;
59 case UpsideDown:
60 pb = gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
61 break;
62 case ClockWise:
63 pb = gdk_pixbuf_rotate_simple(srcgtk, GDK_PIXBUF_ROTATE_CLOCKWISE);
64 break;
66 g_assert(pb != NULL);
67 return new GTKPixbuf(pb);
71 Pixbuf *GTKPixbufFactory::create(int w, int h) const {
72 return new GTKPixbuf(w, h);
76 Pixbuf *GTKPixbufFactory::create_from_inline(int length, unsigned char const *data) const {
77 GInputStream *is = g_memory_input_stream_new_from_data(data, length, NULL);
78 GError *error = NULL;
79 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(is, NULL, &error);
80 g_object_unref(is);
81 if (!pixbuf) {
82 std::runtime_error exc(std::string("cannot load image ") + error->message);
83 g_error_free(error);
84 throw exc;
86 return new GTKPixbuf(pixbuf);
90 Pixbuf *GTKPixbufFactory::create_from_file(const char *filename) const {
91 GError *error = NULL;
92 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
93 if (!pixbuf) {
94 std::runtime_error exc(std::string("cannot load image ") + error->message);
95 g_error_free(error);
96 throw exc;
98 return new GTKPixbuf(pixbuf);