If we get an EOF trying to read a uint or a time value, throw an exception.
[beagle.git] / images / Images.cs
blob8f9568a5acbcf1a1436ee313305a9967e51ba1b6
1 //
2 // Images.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Reflection;
30 using Beagle.Util;
32 using Gnome;
34 namespace Beagle {
36 public class Images {
38 // This class is fully static.
39 private Images () { }
41 static private Stream GetStreamInner (string name)
43 Stream stream = null;
45 if (name.StartsWith ("file://")) {
46 name = name.Substring ("file://".Length);
47 if (File.Exists (name))
48 stream = File.OpenRead (name);
49 } else if (name.StartsWith ("/")) {
50 if (File.Exists (name))
51 stream = File.OpenRead (name);
52 } else {
53 Assembly assembly = Assembly.GetExecutingAssembly ();
54 stream = assembly.GetManifestResourceStream (name);
57 return stream;
60 static public Stream GetStream (string name)
62 Stream stream;
64 if (name == null || name.Length == 0)
65 return null;
67 stream = GetStreamInner (name);
68 if (stream == null)
69 stream = GetStreamInner (name + ".png");
70 if (stream == null)
71 stream = GetStreamInner (name + ".jpg");
72 if (stream == null)
73 Console.WriteLine ("Couldn't get stream for image '{0}'", name);
74 return stream;
77 static public Gdk.Pixbuf GetPixbuf (string name)
79 Stream s = GetStream (name);
80 return s != null ? new Gdk.Pixbuf (s) : null;
83 static public Gdk.Pixbuf GetPixbuf (string name, int maxWidth, int maxHeight)
85 Gdk.Pixbuf pixbuf = GetPixbuf (name);
86 if (pixbuf == null)
87 return null;
89 double scaleWidth = maxWidth / (double)pixbuf.Width;
90 double scaleHeight = maxHeight / (double)pixbuf.Height;
92 double s = Math.Min (scaleWidth, scaleHeight);
93 if (s >= 1.0)
94 return pixbuf;
96 int w = (int) Math.Round (s * pixbuf.Width);
97 int h = (int) Math.Round (s * pixbuf.Height);
99 return pixbuf.ScaleSimple (w, h, Gdk.InterpType.Bilinear);
102 static public Gtk.Widget GetWidget (string name)
104 Gdk.Pixbuf pixbuf = GetPixbuf (name);
105 return pixbuf != null ? new Gtk.Image (pixbuf) : null;
108 static public Gtk.Widget GetWidget (string name, int maxWidth, int maxHeight)
110 Gdk.Pixbuf pixbuf = GetPixbuf (name, maxWidth, maxHeight);
111 return pixbuf != null ? new Gtk.Image (pixbuf) : null;