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.
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();
54 pb
= gdk_pixbuf_rotate_simple(srcgtk
, GDK_PIXBUF_ROTATE_NONE
);
56 case CounterClockWise
:
57 pb
= gdk_pixbuf_rotate_simple(srcgtk
, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
);
60 pb
= gdk_pixbuf_rotate_simple(srcgtk
, GDK_PIXBUF_ROTATE_UPSIDEDOWN
);
63 pb
= gdk_pixbuf_rotate_simple(srcgtk
, GDK_PIXBUF_ROTATE_CLOCKWISE
);
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
);
79 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_stream(is
, NULL
, &error
);
82 std::runtime_error
exc(std::string("cannot load image ") + error
->message
);
86 return new GTKPixbuf(pixbuf
);
90 Pixbuf
*GTKPixbufFactory::create_from_file(const char *filename
) const {
92 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
94 std::runtime_error
exc(std::string("cannot load image ") + error
->message
);
98 return new GTKPixbuf(pixbuf
);