将彩色bmp转换为灰度bmp? [英] Converting color bmp to grayscale bmp?
问题描述
我正在尝试将彩色BMP文件转换为灰度BMP.输入的bmp是24位,而我这次在输出处产生的是相同的24位bmp,只是这次是灰度的.
I am trying to convert a colored BMP file to gray-scale BMP. The input bmp is 24 bit and I am producing the same 24 bit bmp at the output, only this time in gray-scale.
我正在使用的代码是
for(int x = 0; x < max; x++)
{
int lum;
lum = (r[x]*0.30) + (g[x]*0.59) + (b[x]*0.11);
r[x] = lum;
g[x] = lum;
b[x] = lum;
}
r
,g
,b
数组是RGB颜色分量,我将它们放在char *r,*g,*b
中.
The r
, g
, b
arrays are the RGB color components and I have them in a char *r,*g,*b
.
由于某些原因,我没有得到干净的输出.我附上我在此问题上得到的输出,它不完整,并在地方包含白色和黑色区域.那么我在这里做错了什么?
For some reasons I am not getting a clean output. I am attaching the output I am getting with this question, its patchy and contains white and black areas at palces. So what am I doing wrong here?
- 是因为计算lum时数据丢失还是在将int作为字符存储时出了什么问题?
- 灰度bmp不能为24位吗?还是转换后我存储rgb值的方式有问题?
在此方面的任何帮助将不胜枚举.谢谢.
Any help in this will be much appriciated. Thanks.
推荐答案
这些实际上应该是unsigned char
;如果char
恰好在您的平台上签名,则此代码将无法满足您的期望.
These should really be unsigned char
; if char
happens to be signed on your platform, then this code won't do what you expect.
这篇关于将彩色bmp转换为灰度bmp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!