请考虑以下设置:我在磁盘上有一些图像。我无法修改这些图像,因为它们属于另一个项目。我刚看完它们。
我需要将它们处理为在我的应用中看起来最佳。我的意思是像素着色器处理。 当我完成处理后,输出图像将在某种ListView或其他控件中用作缩略图。
我选择了PixelFormats.Bgra32
作为输出格式,因为为它构建像素着色器非常简单。
我读过但PixelFormats.Pbgra32
本身是由Windows使用的,因此速度更快。
当图像上使用淡入淡出效果时,它仍然更快吗?在托管代码中预先加倍通道而不是将其留给框架是否有意义?
答案 0 :(得分:1)
所以我测试了它的方式(用/ unsafe编译):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace BgraVsPbgra {
public partial class MainWindow : Window {
readonly WrapPanel Panel;
readonly TextBlock Output;
readonly PixelFormat FN = PixelFormats.Bgra32;
readonly PixelFormat FP = PixelFormats.Pbgra32;
readonly List<long> TN = new List<long>();
readonly List<long> TP = new List<long>();
DateTime T0;
long DT => (DateTime.Now - T0).Ticks;
DateTime Now => DateTime.Now;
public MainWindow() {
InitializeComponent();
SizeToContent = SizeToContent.WidthAndHeight;
Title = "Bgra32 vs Pbgra Benchmark";
Panel = new WrapPanel {
Width = 512,
Height = 512
};
Output = new TextBlock {
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Foreground = new SolidColorBrush(Colors.White)
};
(Content as Grid).Children.Add(Panel);
(Content as Grid).Children.Add(Output);
Dispatcher.BeginInvoke(new Action(() => Start()), DispatcherPriority.SystemIdle);
}
private async void Start() {
Output.Text += "Bgra32 vs Pbgra32 benchmark started.\r\n\r\n";
var t0 = DateTime.Now;
var n = 3;
var i = 16384;
for (var p = 1; p <= n; p++) {
Output.Text += $"\r\nPass {p}/{n}.\r\n\r\n";
await Benchmark(i / 4, 0.75, true);
await Benchmark(i / 4, 0.75, false);
await Benchmark(i / 4, 1, true);
await Benchmark(i / 4, 1, false);
}
var t = (DateTime.Now - t0).TotalSeconds;
Output.Text += $"\r\nDone in {t:0.0}s.\r\n";
}
private async Task Benchmark(int n = 256, double opacity = 1, bool cheat = false) {
Output.Text += $"Benchmarking with opacity = {opacity}... ";
if (cheat) Output.Text += "CHEATING!... ";
//Output.Text += "\r\n";
for (int i = 0; i < n; i++) await Dispatcher.BeginInvoke(new Action(async () => await DrawTestAsync(opacity, cheat)), DispatcherPriority.Send);
Output.Text += ($"Pbgra32 advantage: {(TN.Average() / TP.Average() * 100d - 100d):0.0}%\r\n");
}
async Task DrawTestAsync(double opacity = 1, bool cheat = false) {
await Dispatcher.BeginInvoke(new Action(() => {
Panel.Children.Clear();
for (int i = 0; i < 8; i++) {
T0 = Now; Panel.Children.Add(new Image { Source = CreateBitmap(FP, cheat), Width = 128, Height = 128, Opacity = opacity }); TP.Add(DT);
T0 = Now; Panel.Children.Add(new Image { Source = CreateBitmap(FN, cheat), Width = 128, Height = 128, Opacity = opacity }); TN.Add(DT);
}
}), DispatcherPriority.Send);
}
BitmapSource CreateBitmap(PixelFormat pixelFormat, bool cheat = false) {
var bitmap = new WriteableBitmap(256, 256, 96, 96, pixelFormat, null);
bitmap.Lock();
unsafe
{
var pixels = (uint*)bitmap.BackBuffer;
if (pixelFormat == FN || cheat)
for (uint y = 0; y < 256; y++) for (uint x = 0; x < 256; x++) pixels[x + y * 256] = 0x80000000u | (x << 16) | y;
else
for (uint y = 0; y < 256; y++) for (uint x = 0; x < 256; x++) pixels[x + y * 256] = 0x80000000u | (x / 2 << 16) | y / 2;
}
bitmap.Unlock();
bitmap.Freeze();
return bitmap;
}
}
}
我得到了相当随机的结果,但使用Pbgra32
像素格式的大部分时间似乎比使用Bgra32
像素格式要快一些(比如3%)。
使用Pbgra32
格式时,必须为每个频道进行一些额外的操作,但是另外2个分区似乎没有产生任何可衡量的差异。
因此,如果您希望将位图的速度提高约3%,请使用Pbgra32
格式和预乘通道,例如pr = r * a / 255
,这些通道可能看起来很难看:
uint pixel = a << 24 | r * a / 255 << 16 | g * a / 255 << 8 | b * a / 255;
不要制作pixel
功能,否则会导致性能下降。说到性能,不安全像素操作似乎比使用安全CopyPixels()
/ WritePixels()
方法快2到3倍。
不,我不会使用Pbgra32
为位图创建位图或像素着色器。我的意见是:不值得。如果我错了,请纠正我。