diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/NoiseGenerationHelper.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/NoiseGenerationHelper.cs index dfaf767..02b7a41 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/NoiseGenerationHelper.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/NoiseGenerationHelper.cs @@ -19,19 +19,37 @@ namespace BetterLyrics.WinUI3.Helper static readonly string NoiseOverlayFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Assets", NoiseOverlayFileName); + /// + /// 生成 BGRA 格式的灰阶噪声像素数据 + /// + public static byte[] GenerateNoiseBitmapBGRA(int width, int height) + { + var random = new Random(); + var pixelData = new byte[width * height * 4]; + for (int i = 0; i < width * height; i++) + { + byte gray = (byte)random.Next(0, 256); + pixelData[i * 4 + 0] = gray; // B + pixelData[i * 4 + 1] = gray; // G + pixelData[i * 4 + 2] = gray; // R + pixelData[i * 4 + 3] = 255; // A + } + return pixelData; + } + /// /// 生成单色灰阶随机噪声 /// /// 输出文件路径 /// 图片宽度 /// 图片高度 - public static async Task GenerateNoiseBitmapAsync(int width, int height) + public static BitmapFile GenerateNoiseBitmap(int width, int height) { const uint NumOfGrayscale = 16; uint bitCount = NextPowerOfTwo((uint)Math.Round(Math.Sqrt(NumOfGrayscale))); var palette = BitmapFileCreator.CreateGrayscalePalette(16); - var pixelData = await GenerateRandomNoise(width, height, bitCount); + var pixelData = GenerateRandomNoise(width, height, bitCount); var fileHeader = BitmapFileCreator.CreateFileHeader(palette, pixelData); var infoHeader = BitmapFileCreator.CreateInfoHeader(width, height, bitCount); @@ -95,16 +113,16 @@ namespace BetterLyrics.WinUI3.Helper /// 填充高度 /// 单个调色盘索引所占比特位数 /// 字节数据 - private static Task GenerateRandomNoise(int width, int height, uint bitCount) + private static byte[] GenerateRandomNoise(int width, int height, uint bitCount) { // 创建位图行字节数,4K 对齐 int rowSize = ((width * (int)bitCount + 31) >> 5) << 2; // 创建随机位图数据 Random rnd = new(); - return Task.Run(() => Enumerable.Range(0, rowSize * height) + return Enumerable.Range(0, rowSize * height) .Select(i => (byte)rnd.Next(0x00, 0xFF)) - .ToArray()); + .ToArray(); } private static uint NextPowerOfTwo(uint value) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Draw.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Draw.cs index dd520ac..9c59ab3 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Draw.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Draw.cs @@ -9,6 +9,7 @@ using Microsoft.Graphics.Canvas.UI.Xaml; using Microsoft.UI; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Numerics; using Windows.Foundation; @@ -174,10 +175,11 @@ namespace BetterLyrics.WinUI3.ViewModels private void DrawAlbumArtBackground(ICanvasAnimatedControl control, CanvasDrawingSession ds) { - ds.Transform = Matrix3x2.CreateRotation(_rotateAngle, control.Size.ToVector2() * 0.5f); + //ds.Transform = Matrix3x2.CreateRotation(_rotateAngle, control.Size.ToVector2() * 0.5f); using var overlappedCovers = new CanvasCommandList(control.Device); using var overlappedCoversDs = overlappedCovers.CreateDrawingSession(); + overlappedCoversDs.Transform = Matrix3x2.CreateRotation(_rotateAngle, control.Size.ToVector2() * 0.5f); if (_lastAlbumArtCanvasBitmap != null) { @@ -188,6 +190,8 @@ namespace BetterLyrics.WinUI3.ViewModels DrawBackgroundImgae(control, overlappedCoversDs, _albumArtCanvasBitmap, _albumArtBgTransition.Value); } + overlappedCoversDs.Transform = Matrix3x2.Identity; + IGraphicsEffectSource blurredCover = new GaussianBlurEffect { BlurAmount = _albumArtBgBlurAmount, @@ -219,7 +223,7 @@ namespace BetterLyrics.WinUI3.ViewModels }; ds.DrawImage(coverOverlayEffect); - ds.Transform = Matrix3x2.Identity; + //ds.Transform = Matrix3x2.Identity; } private void DrawAlbumArt(ICanvasAnimatedControl control, CanvasDrawingSession ds) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Update.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Update.cs index 82d1da2..f3553af 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Update.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel.Update.cs @@ -11,6 +11,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Numerics; +using System.Threading.Tasks; +using Windows.Graphics.Imaging; using Windows.UI; namespace BetterLyrics.WinUI3.ViewModels @@ -71,6 +73,8 @@ namespace BetterLyrics.WinUI3.ViewModels _albumArtSize = MathF.Max(0, _albumArtSize); _titleY = _albumArtY + _albumArtSize * 1.05f; + + UpdateCoverAcrylicOverlay(control); } if (_isDisplayTypeChanged || _isCanvasWidthChanged) @@ -439,5 +443,17 @@ namespace BetterLyrics.WinUI3.ViewModels } _immersiveBgOpacityTransition.StartTransition(targetOpacity); } + + private void UpdateCoverAcrylicOverlay(ICanvasAnimatedControl control) + { + var ret = NoiseOverlayHelper.GenerateNoiseBitmapBGRA((int)_canvasWidth, (int)_canvasHeight); + _coverAcrylicNoiseCanvasBitmap = CanvasBitmap.CreateFromBytes( + control, + ret, + (int)_canvasWidth, + (int)_canvasHeight, + Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized + ); + } } }