|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Reflection; |
| 10 | +using System.Windows.Forms; |
| 11 | +using SkiaSharp; |
| 12 | + |
| 13 | +namespace WinDynamicDesktop.Skia |
| 14 | +{ |
| 15 | + sealed class ImageCache |
| 16 | + { |
| 17 | + readonly int maxWidth; |
| 18 | + readonly int maxHeight; |
| 19 | + readonly object cacheLock = new object(); |
| 20 | + readonly Dictionary<Uri, SKImage> images = new Dictionary<Uri, SKImage>(); |
| 21 | + |
| 22 | + public SKImage this[Uri uri] |
| 23 | + { |
| 24 | + get |
| 25 | + { |
| 26 | + lock (cacheLock) |
| 27 | + { |
| 28 | + if (images.TryGetValue(uri, out var image)) |
| 29 | + { |
| 30 | + return image; |
| 31 | + } |
| 32 | + |
| 33 | + var img = CreateImage(uri); |
| 34 | + if (img != null) |
| 35 | + { |
| 36 | + images.Add(uri, img); |
| 37 | + } |
| 38 | + return img; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void Clear() |
| 44 | + { |
| 45 | + lock (cacheLock) |
| 46 | + { |
| 47 | + foreach (var image in images.Values) |
| 48 | + { |
| 49 | + image?.Dispose(); |
| 50 | + } |
| 51 | + images.Clear(); |
| 52 | + } |
| 53 | + GC.Collect(); |
| 54 | + } |
| 55 | + |
| 56 | + public ImageCache(bool limitDecodeSize = true) |
| 57 | + { |
| 58 | + if (limitDecodeSize) |
| 59 | + { |
| 60 | + int maxArea = 0; |
| 61 | + foreach (Screen screen in Screen.AllScreens) |
| 62 | + { |
| 63 | + int area = screen.Bounds.Width * screen.Bounds.Height; |
| 64 | + if (area > maxArea) |
| 65 | + { |
| 66 | + maxArea = area; |
| 67 | + maxWidth = screen.Bounds.Width; |
| 68 | + maxHeight = screen.Bounds.Height; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + maxWidth = int.MaxValue; |
| 75 | + maxHeight = int.MaxValue; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private SKImage CreateImage(Uri uri) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + Stream stream = null; |
| 84 | + |
| 85 | + if (uri.IsAbsoluteUri && uri.Scheme == "file") |
| 86 | + { |
| 87 | + string path = uri.LocalPath; |
| 88 | + if (File.Exists(path)) |
| 89 | + { |
| 90 | + stream = File.OpenRead(path); |
| 91 | + } |
| 92 | + } |
| 93 | + else if (!uri.IsAbsoluteUri) |
| 94 | + { |
| 95 | + // Embedded resource |
| 96 | + stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(uri.OriginalString); |
| 97 | + } |
| 98 | + |
| 99 | + if (stream == null) |
| 100 | + { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + using (stream) |
| 105 | + { |
| 106 | + using (var codec = SKCodec.Create(stream)) |
| 107 | + { |
| 108 | + if (codec == null) |
| 109 | + { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + var info = codec.Info; |
| 114 | + |
| 115 | + // Calculate target dimensions |
| 116 | + int targetWidth = info.Width; |
| 117 | + int targetHeight = info.Height; |
| 118 | + |
| 119 | + if (info.Width > maxWidth || info.Height > maxHeight) |
| 120 | + { |
| 121 | + float scale = Math.Min((float)maxWidth / info.Width, (float)maxHeight / info.Height); |
| 122 | + targetWidth = (int)(info.Width * scale); |
| 123 | + targetHeight = (int)(info.Height * scale); |
| 124 | + } |
| 125 | + |
| 126 | + // Decode at native size |
| 127 | + using (var sourceBitmap = new SKBitmap(info)) |
| 128 | + { |
| 129 | + if (codec.GetPixels(sourceBitmap.Info, sourceBitmap.GetPixels()) != SKCodecResult.Success) |
| 130 | + { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + // If scaling is needed, create scaled version with high quality |
| 135 | + if (targetWidth != sourceBitmap.Width || targetHeight != sourceBitmap.Height) |
| 136 | + { |
| 137 | + using (var scaledBitmap = new SKBitmap(targetWidth, targetHeight, info.ColorType, info.AlphaType)) |
| 138 | + { |
| 139 | + sourceBitmap.ScalePixels(scaledBitmap, new SKSamplingOptions(SKCubicResampler.Mitchell)); |
| 140 | + var image = SKImage.FromBitmap(scaledBitmap); |
| 141 | + return image; |
| 142 | + } |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + // No scaling needed |
| 147 | + var image = SKImage.FromBitmap(sourceBitmap); |
| 148 | + return image; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + catch |
| 155 | + { |
| 156 | + return null; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments