add another way of noise byte generation

This commit is contained in:
Zhe Fang
2025-07-24 12:51:46 -04:00
parent 02dbbf983c
commit 77f2474562
3 changed files with 45 additions and 7 deletions

View File

@@ -19,19 +19,37 @@ namespace BetterLyrics.WinUI3.Helper
static readonly string NoiseOverlayFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Assets", NoiseOverlayFileName);
/// <summary>
/// 生成 BGRA 格式的灰阶噪声像素数据
/// </summary>
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;
}
/// <summary>
/// 生成单色灰阶随机噪声
/// </summary>
/// <param name="outputPath">输出文件路径</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
public static async Task<BitmapFile> 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
/// <param name="height">填充高度</param>
/// <param name="bitCount">单个调色盘索引所占比特位数</param>
/// <returns>字节数据</returns>
private static Task<byte[]> 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)

View File

@@ -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)

View File

@@ -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
);
}
}
}