5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
namespace Xwt.GtkBackend
32
public class ImageHandler: ImageBackendHandler
34
public override object LoadFromStream (System.IO.Stream stream)
36
using (Gdk.PixbufLoader loader = new Gdk.PixbufLoader (stream))
40
public override object LoadFromIcon (string id, IconSize size)
42
string stockId = Util.ToGtkStock (id);
43
var gsize = Util.ToGtkSize (size);
45
Gtk.IconSet iconset = Gtk.IconFactory.LookupDefault (stockId);
47
return iconset.RenderIcon (Gtk.Widget.DefaultStyle, Gtk.TextDirection.Ltr, Gtk.StateType.Normal, gsize, null, null);
49
if (Gtk.IconTheme.Default.HasIcon (stockId)) {
51
Gtk.Icon.SizeLookup (gsize, out w, out h);
52
Gdk.Pixbuf result = Gtk.IconTheme.Default.LoadIcon (stockId, h, (Gtk.IconLookupFlags)0);
58
public override void SetPixel (object handle, int x, int y, Xwt.Drawing.Color color)
60
var pix = (Gdk.Pixbuf)handle;
63
byte* p = (byte*) pix.Pixels;
64
p += y * pix.Rowstride + x * pix.NChannels;
65
p[0] = (byte)(color.Red * 255);
66
p[1] = (byte)(color.Green * 255);
67
p[2] = (byte)(color.Blue * 255);
68
p[3] = (byte)(color.Alpha * 255);
72
public override Xwt.Drawing.Color GetPixel (object handle, int x, int y)
74
var pix = (Gdk.Pixbuf)handle;
77
byte* p = (byte*) pix.Pixels;
78
p += y * pix.Rowstride + x * pix.NChannels;
79
return Xwt.Drawing.Color.FromBytes (p[0], p[1], p[2], p[3]);
83
public override void Dispose (object backend)
85
((Gdk.Pixbuf)backend).Dispose ();
88
public override Size GetSize (object handle)
90
var pix = (Gdk.Pixbuf)handle;
91
return new Size (pix.Width, pix.Height);
94
public override object Resize (object handle, double width, double height)
96
var pix = (Gdk.Pixbuf)handle;
97
return pix.ScaleSimple ((int)width, (int)height, Gdk.InterpType.Bilinear);
100
public override object Copy (object handle)
102
var pix = (Gdk.Pixbuf)handle;
106
public override void CopyArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY)
108
var pixSrc = (Gdk.Pixbuf)srcHandle;
109
var pixDst = (Gdk.Pixbuf)destHandle;
110
pixSrc.CopyArea (srcX, srcY, width, height, pixDst, destX, destY);
113
public override object Crop (object handle, int srcX, int srcY, int width, int height)
115
var pix = (Gdk.Pixbuf)handle;
116
Gdk.Pixbuf res = new Gdk.Pixbuf (pix.Colorspace, pix.HasAlpha, pix.BitsPerSample, width, height);
118
pix.CopyArea (srcX, srcY, width, height, res, 0, 0);
122
public override object ChangeOpacity (object backend, double opacity)
124
Gdk.Pixbuf image = (Gdk.Pixbuf) backend;
125
Gdk.Pixbuf result = image.Copy ();
127
result = result.AddAlpha (true, 0, 0, 0);
128
image.Composite (result, 0, 0, image.Width, image.Height, 0, 0, 1, 1, Gdk.InterpType.Bilinear, (int)(255 * opacity));