-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Bitmaps.cs
105 lines (94 loc) · 3.84 KB
/
Bitmaps.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Splat
{
public enum CompressedBitmapFormat
{
Png, Jpeg,
}
/// <summary>
/// Represents the platform-specific image loader class. Unless you are
/// testing image loading, you don't usually need to implement this.
/// </summary>
public interface IBitmapLoader
{
/// <summary>
/// Loads a bitmap from a byte stream
/// </summary>
/// <param name="sourceStream">The stream to load the image from.</param>
/// <param name="desiredWidth">The desired width of the image.</param>
/// <param name="desiredHeight">The desired height of the image.</param>
/// <returns>A future result representing the loaded image</returns>
Task<IBitmap> Load(Stream sourceStream, float? desiredWidth, float? desiredHeight);
/// <summary>
/// Loads from the application's resources (i.e. from bundle on Cocoa,
/// from Pack URIs on Windows, etc)
/// </summary>
/// <param name="source">The source resource, as a relative path.</param>
/// <param name="desiredWidth">Desired width.</param>
/// <param name="desiredHeight">Desired height.</param>
/// <returns>A future result representing the loaded image</returns>
Task<IBitmap> LoadFromResource(string source, float? desiredWidth, float? desiredHeight);
/// <summary>
/// Creates an empty bitmap of the specified dimensions
/// </summary>
/// <param name="width">The width of the canvas</param>
/// <param name="height">The height of the canvas</param>
/// <returns>A new image. Use ToNative() to convert this to a native bitmap</returns>
IBitmap Create(float width, float height);
}
/// <summary>
/// Represents a bitmap image that was loaded via a ViewModel. Every platform
/// provides FromNative and ToNative methods to convert this object to the
/// platform-specific versions.
/// </summary>
public interface IBitmap : IDisposable
{
/// <summary>
/// Width in pixel units (depending on platform)
/// </summary>
float Width { get; }
/// <summary>
/// Height in pixel units (depending on platform)
/// </summary>
float Height { get; }
/// <summary>
/// Saves an image to a target stream
/// </summary>
/// <param name="format">The format to save the image in.</param>
/// <param name="quality">If JPEG is specified, this is a quality
/// factor between 0.0 and 1.0f where 1.0f is the best quality.</param>
/// <param name="target">The target stream to save to.</param>
/// <returns>A signal indicating the Save has completed.</returns>
Task Save(CompressedBitmapFormat format, float quality, Stream target);
}
/// <summary>
/// This class loads and creates bitmap resources in a platform-independent
/// way.
/// </summary>
public static class BitmapLoader
{
static BitmapLoader()
{
var platBitmapLoader = AssemblyFinder.AttemptToLoadType<IBitmapLoader>("Splat.PlatformBitmapLoader");
_Current = platBitmapLoader;
}
// TODO: This needs to be improved once we move the "Detect in Unit Test
// Runner" code into Splat
static IBitmapLoader _Current;
public static IBitmapLoader Current {
get {
var ret = _Current;
if (ret == null) {
throw new Exception("Could not find a default bitmap loader. This should never happen, your dependency resolver is broken");
}
return ret;
}
set { _Current = value; }
}
}
}