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