|
| 1 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 2 | +using Windows.Graphics.Imaging; |
| 3 | + |
| 4 | +namespace DevWinUI; |
| 5 | + |
| 6 | +public partial class GifImage |
| 7 | +{ |
| 8 | + private static async Task<byte[]> GetPixelsAsync(BitmapFrame frame) |
| 9 | + { |
| 10 | + var pixelData = await frame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); |
| 11 | + return pixelData.DetachPixelData(); |
| 12 | + } |
| 13 | + |
| 14 | + private static WriteableBitmap LoadImage(byte[] pixels, int width, int height) |
| 15 | + { |
| 16 | + var bitmap = new WriteableBitmap(width, height); |
| 17 | + var buffer = bitmap.PixelBuffer; |
| 18 | + pixels.CopyTo(buffer); |
| 19 | + bitmap.Invalidate(); |
| 20 | + return bitmap; |
| 21 | + } |
| 22 | + |
| 23 | + private static void MergePixels(byte[] pixels1, int width, byte[] pixels2, Rect rect) |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + int x0 = (int)rect.X; |
| 28 | + int y0 = (int)rect.Y; |
| 29 | + int stride = (int)rect.Width; |
| 30 | + |
| 31 | + for (int y = 0; y < rect.Height; y++) |
| 32 | + { |
| 33 | + for (int x = 0; x < rect.Width; x++) |
| 34 | + { |
| 35 | + int index1 = (x0 + x + (y0 + y) * width) * 4; |
| 36 | + int index2 = (x + y * stride) * 4; |
| 37 | + if (pixels2[index2 + 3] > 0) |
| 38 | + { |
| 39 | + pixels1[index1 + 0] = pixels2[index2 + 0]; |
| 40 | + pixels1[index1 + 1] = pixels2[index2 + 1]; |
| 41 | + pixels1[index1 + 2] = pixels2[index2 + 2]; |
| 42 | + pixels1[index1 + 3] = pixels2[index2 + 3]; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + catch (Exception ex) |
| 48 | + { |
| 49 | + System.Diagnostics.Debug.WriteLine("MergePixels. {0}", ex.Message); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static async Task<FrameProperties> GetFramePropertiesAsync(BitmapFrame frame) |
| 54 | + { |
| 55 | + const string leftProperty = "/imgdesc/Left"; |
| 56 | + const string topProperty = "/imgdesc/Top"; |
| 57 | + const string widthProperty = "/imgdesc/Width"; |
| 58 | + const string heightProperty = "/imgdesc/Height"; |
| 59 | + const string delayProperty = "/grctlext/Delay"; |
| 60 | + const string disposalProperty = "/grctlext/Disposal"; |
| 61 | + |
| 62 | + var propertiesView = frame.BitmapProperties; |
| 63 | + var requiredProperties = new[] { leftProperty, topProperty, widthProperty, heightProperty }; |
| 64 | + var properties = await propertiesView.GetPropertiesAsync(requiredProperties); |
| 65 | + |
| 66 | + var left = (ushort)properties[leftProperty].Value; |
| 67 | + var top = (ushort)properties[topProperty].Value; |
| 68 | + var width = (ushort)properties[widthProperty].Value; |
| 69 | + var height = (ushort)properties[heightProperty].Value; |
| 70 | + |
| 71 | + var delayMilliseconds = 30.0; |
| 72 | + var shouldDispose = false; |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + var extensionProperties = new[] { delayProperty, disposalProperty }; |
| 77 | + properties = await propertiesView.GetPropertiesAsync(extensionProperties); |
| 78 | + |
| 79 | + if (properties.ContainsKey(delayProperty) && properties[delayProperty].Type == PropertyType.UInt16) |
| 80 | + { |
| 81 | + var delayInHundredths = (ushort)properties[delayProperty].Value; |
| 82 | + if (delayInHundredths >= 3u) // Prevent degenerate frames with no delay time |
| 83 | + { |
| 84 | + delayMilliseconds = 10.0 * delayInHundredths; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (properties.ContainsKey(disposalProperty) && properties[disposalProperty].Type == PropertyType.UInt8) |
| 89 | + { |
| 90 | + var disposal = (byte)properties[disposalProperty].Value; |
| 91 | + if (disposal == 2) |
| 92 | + { |
| 93 | + // 0 = undefined |
| 94 | + // 1 = none (compose next frame on top of this one, default) |
| 95 | + // 2 = dispose |
| 96 | + // 3 = revert to previous (not supported) |
| 97 | + shouldDispose = true; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + // These properties are not required, so it's okay to ignore failure. |
| 104 | + } |
| 105 | + |
| 106 | + return new FrameProperties( |
| 107 | + new Rect(left, top, width, height), |
| 108 | + delayMilliseconds, |
| 109 | + shouldDispose |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments