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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt.Drawing/Image.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
// Image.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
using Xwt.Engine;
 
30
using System.Reflection;
 
31
using System.IO;
 
32
 
 
33
namespace Xwt.Drawing
 
34
{
 
35
        public sealed class Image: XwtObject, IDisposable
 
36
        {
 
37
                static ImageBackendHandler handler;
 
38
                
 
39
                static Image ()
 
40
                {
 
41
                        handler = WidgetRegistry.CreateSharedBackend<ImageBackendHandler> (typeof(Image));
 
42
                }
 
43
                
 
44
                protected override IBackendHandler BackendHandler {
 
45
                        get {
 
46
                                return handler;
 
47
                        }
 
48
                }
 
49
                
 
50
                internal Image (object backend): base (backend)
 
51
                {
 
52
                }
 
53
                
 
54
                public Image (Image image): base (handler.Copy (image.Backend))
 
55
                {
 
56
                }
 
57
                
 
58
                public static Image FromResource (Type type, string resource)
 
59
                {
 
60
                        var img = handler.LoadFromResource (type.Assembly, resource);
 
61
                        if (img == null)
 
62
                                throw new InvalidOperationException ("Resource not found: " + resource);
 
63
                        return new Image (img);
 
64
                }
 
65
                
 
66
                public static Image FromResource (Assembly asm, string resource)
 
67
                {
 
68
                        var img = handler.LoadFromResource (asm, resource);
 
69
                        if (img == null)
 
70
                                throw new InvalidOperationException ("Resource not found: " + resource);
 
71
                        return new Image (img);
 
72
                }
 
73
                
 
74
                public static Image FromFile (string file)
 
75
                {
 
76
                        return new Image (handler.LoadFromFile (file));
 
77
                }
 
78
                
 
79
                public static Image FromStream (Stream stream)
 
80
                {
 
81
                        return new Image (handler.LoadFromStream (stream));
 
82
                }
 
83
                
 
84
                public static Image FromIcon (string id, IconSize size)
 
85
                {
 
86
                        return new Image (handler.LoadFromIcon (id, size));
 
87
                }
 
88
                
 
89
                public Size Size {
 
90
                        get { return handler.GetSize (Backend); }
 
91
                }
 
92
                
 
93
                public void SetPixel (int x, int y, Color color)
 
94
                {
 
95
                        handler.SetPixel (Backend, x, y, color);
 
96
                }
 
97
                
 
98
                public Color GetPixel (int x, int y)
 
99
                {
 
100
                        return handler.GetPixel (Backend, x, y);
 
101
                }
 
102
                
 
103
                public Image Scale (double scale)
 
104
                {
 
105
                        double w = Size.Width * scale;
 
106
                        double h = Size.Height * scale;
 
107
                        return new Image (handler.Resize (Backend, w, h));
 
108
                }
 
109
                
 
110
                public Image Scale (double scaleX, double scaleY)
 
111
                {
 
112
                        double w = Size.Width * scaleX;
 
113
                        double h = Size.Height * scaleY;
 
114
                        return new Image (handler.Resize (Backend, w, h));
 
115
                }
 
116
                
 
117
                public Image Resize (double width, double height)
 
118
                {
 
119
                        return new Image (handler.Resize (Backend, width, height));
 
120
                }
 
121
                
 
122
                public Image ResizeToFitBox (double width, double height)
 
123
                {
 
124
                        double r = Math.Min (width / Size.Width, height / Size.Height);
 
125
                        return new Image (handler.Resize (Backend, Size.Width * r, Size.Height * r));
 
126
                }
 
127
                
 
128
                public Image ToGrayscale ()
 
129
                {
 
130
                        throw new NotImplementedException ();
 
131
                }
 
132
                
 
133
                public Image ChangeOpacity (double opacity)
 
134
                {
 
135
                        return new Image (handler.ChangeOpacity (Backend, opacity));
 
136
                }
 
137
                
 
138
                public void CopyArea (int srcX, int srcY, int width, int height, Image dest, int destX, int destY)
 
139
                {
 
140
                        handler.CopyArea (Backend, srcX, srcY, width, height, dest.Backend, destX, destY);
 
141
                }
 
142
                
 
143
                public Image Crop (int srcX, int srcY, int width, int height)
 
144
                {
 
145
                        return new Image (handler.Crop (Backend, srcX, srcY, width, height));
 
146
                }
 
147
                
 
148
                public void Dispose ()
 
149
                {
 
150
                        handler.Dispose (Backend);
 
151
                }
 
152
        }
 
153
}
 
154