~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/ImageHandler.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ImageHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Xwt.Backends;
 
29
 
 
30
namespace Xwt.GtkBackend
 
31
{
 
32
        public class ImageHandler: ImageBackendHandler
 
33
        {
 
34
                public override object LoadFromStream (System.IO.Stream stream)
 
35
                {
 
36
                        using (Gdk.PixbufLoader loader = new Gdk.PixbufLoader (stream))
 
37
                                return loader.Pixbuf;
 
38
                }
 
39
                
 
40
                public override object LoadFromIcon (string id, IconSize size)
 
41
                {
 
42
                        string stockId = Util.ToGtkStock (id);
 
43
                        var gsize = Util.ToGtkSize (size);
 
44
                        
 
45
                        Gtk.IconSet iconset = Gtk.IconFactory.LookupDefault (stockId);
 
46
                        if (iconset != null) 
 
47
                                return iconset.RenderIcon (Gtk.Widget.DefaultStyle, Gtk.TextDirection.Ltr, Gtk.StateType.Normal, gsize, null, null);
 
48
                        
 
49
                        if (Gtk.IconTheme.Default.HasIcon (stockId)) {
 
50
                                int w, h;
 
51
                                Gtk.Icon.SizeLookup (gsize, out w, out h);
 
52
                                Gdk.Pixbuf result = Gtk.IconTheme.Default.LoadIcon (stockId, h, (Gtk.IconLookupFlags)0);
 
53
                                return result;
 
54
                        }
 
55
                        return null;
 
56
                }
 
57
                
 
58
                public override void SetPixel (object handle, int x, int y, Xwt.Drawing.Color color)
 
59
                {
 
60
                        var pix = (Gdk.Pixbuf)handle;
 
61
                        
 
62
                        unsafe {
 
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);
 
69
                        }
 
70
                }
 
71
                
 
72
                public override Xwt.Drawing.Color GetPixel (object handle, int x, int y)
 
73
                {
 
74
                        var pix = (Gdk.Pixbuf)handle;
 
75
                        
 
76
                        unsafe {
 
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]);
 
80
                        }
 
81
                }
 
82
                
 
83
                public override void Dispose (object backend)
 
84
                {
 
85
                        ((Gdk.Pixbuf)backend).Dispose ();
 
86
                }
 
87
                
 
88
                public override Size GetSize (object handle)
 
89
                {
 
90
                        var pix = (Gdk.Pixbuf)handle;
 
91
                        return new Size (pix.Width, pix.Height);
 
92
                }
 
93
                
 
94
                public override object Resize (object handle, double width, double height)
 
95
                {
 
96
                        var pix = (Gdk.Pixbuf)handle;
 
97
                        return pix.ScaleSimple ((int)width, (int)height, Gdk.InterpType.Bilinear);
 
98
                }
 
99
                
 
100
                public override object Copy (object handle)
 
101
                {
 
102
                        var pix = (Gdk.Pixbuf)handle;
 
103
                        return pix.Copy ();
 
104
                }
 
105
                
 
106
                public override void CopyArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY)
 
107
                {
 
108
                        var pixSrc = (Gdk.Pixbuf)srcHandle;
 
109
                        var pixDst = (Gdk.Pixbuf)destHandle;
 
110
                        pixSrc.CopyArea (srcX, srcY, width, height, pixDst, destX, destY);
 
111
                }
 
112
                
 
113
                public override object Crop (object handle, int srcX, int srcY, int width, int height)
 
114
                {
 
115
                        var pix = (Gdk.Pixbuf)handle;
 
116
                        Gdk.Pixbuf res = new Gdk.Pixbuf (pix.Colorspace, pix.HasAlpha, pix.BitsPerSample, width, height);
 
117
                        res.Fill (0);
 
118
                        pix.CopyArea (srcX, srcY, width, height, res, 0, 0);
 
119
                        return res;
 
120
                }
 
121
                
 
122
                public override object ChangeOpacity (object backend, double opacity)
 
123
                {
 
124
                        Gdk.Pixbuf image = (Gdk.Pixbuf) backend;
 
125
                        Gdk.Pixbuf result = image.Copy ();
 
126
                        result.Fill (0);
 
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));
 
129
                        return result;
 
130
                }
 
131
        }
 
132
}
 
133