在 C# 中实现颜色过滤
我正在尝试在我的应用程序中实现 Photoshop 风格的颜色过滤功能。我有一个位图和 4 个复选框(R、G、B、A)。我想知道最快的方法是什么
目前我正在按如下方式执行此操作
Byte[] rgbValues = new Byte[data.Stride * data.Height];
for (int row = 0; row < data.Height; row++)
{
// Loop through each pixel on this scan line
int bufPos = (m_height - row - 1) * m_width;
int index = row * data.Stride;
for (int col = 0; col < data.Width; col++, bufPos++, index += 4)
{
bool drawCheckerBoard = true; // for alpha
UInt32 rgba = m_image[bufPos];
UInt32 r = EnableRedChannel ? ((rgba >> 0) & 0xFF) : 0x00;
UInt32 g = EnableGreenChannel ? ((rgba >> 8) & 0xFF) : 0x00;
UInt32 b = EnableBlueChannel ? ((rgba >> 16) & 0xFF) : 0x00;
UInt32 a = (rgba >> 24) & 0xFF;
...
...
}
}
,然后是通常的 Marshal.Copy 和解锁位等...
如您所见,这并不是真正的优化方法,我想要一些建议更快的方法。
谢谢
I am trying to implement a Photoshop style color filtering feature in my application. I have a Bitmap, and 4 check-boxes (R,G,B,A). I wanted to know what is the fastest way of doing it
Currently I am doing it as follows
Byte[] rgbValues = new Byte[data.Stride * data.Height];
for (int row = 0; row < data.Height; row++)
{
// Loop through each pixel on this scan line
int bufPos = (m_height - row - 1) * m_width;
int index = row * data.Stride;
for (int col = 0; col < data.Width; col++, bufPos++, index += 4)
{
bool drawCheckerBoard = true; // for alpha
UInt32 rgba = m_image[bufPos];
UInt32 r = EnableRedChannel ? ((rgba >> 0) & 0xFF) : 0x00;
UInt32 g = EnableGreenChannel ? ((rgba >> 8) & 0xFF) : 0x00;
UInt32 b = EnableBlueChannel ? ((rgba >> 16) & 0xFF) : 0x00;
UInt32 a = (rgba >> 24) & 0xFF;
...
...
}
}
and then the usual Marshal.Copy and unlock bits etc...
As you can see it is not really an optimized way, I wanted some suggestions for a faster method.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚您想对单独的 r、g、b 和 做什么。 a 值,但我立即看到的一件事是您可以将启用标志移出循环。
更进一步,如果您实际上不需要拉出 r、g、b& 图像,则可以构建一个复合蒙版。一个值。
您可能还会发现将 m_image[] 设为字节数组很有帮助,这样只需调整偏移量并跨过数据即可更轻松地挑选出各个颜色通道。
It's unclear what you want to do with the individual r,g,b & a values, but one thing I see right off is that you can move your enable flags out of the loop.
Going one step beyond that, you can build a composite mask if you don't actually need to pull out the r,g,b& a values.
You might also find it helpful to have your of your m_image[] be an array of
byte
s, this would make it easier to pick out the individual color channels just by adjusting your offset and stride through the data.