当前位置: 技术问答>linux和unix
linux下24bitsBMP转灰度图,怎么处理,有代码,但是不知道哪里有误.
来源: 互联网 发布时间:2016-06-21
本文导语: #include #include #pragma pack(2); typedef struct { unsigned short bfType; /* 文件类型, 必须为 "BM"(0x4D42) */ unsigned long bfSize; ...
#include
#include
#pragma pack(2);
typedef struct
{
unsigned short bfType; /* 文件类型, 必须为 "BM"(0x4D42) */
unsigned long bfSize; /* 文件的大小(字节) */
unsigned short bfReserved1; /* 保留, 必须为 0 */
unsigned short bfReserved2; /* 保留, 必须为 0 */
unsigned long bfoffBits; /* 位图阵列相对于文件头的偏移量(字节) */
} BITMAPFILEHEADER; /* 文件头结构 */
typedef struct
{
unsigned long biSize; /* size of BITMAPINFOHEADER */
unsigned long biWidth; /* 位图宽度(像素) */
unsigned long biHeight; /* 位图高度(像素) */
unsigned short biPlanes; /* 目标设备的位平面数, 必须置为1 */
unsigned short biBitCount; /* 每个像素的位数, 1,4,8或24 */
unsigned long biCompress; /* 位图阵列的压缩方法,0=不压缩 */
unsigned long biSizeImage; /* 图像大小(字节) */
unsigned long biXPelsPerMeter; /* 目标设备水平每米像素个数 */
unsigned long biYPelsPerMeter; /* 目标设备垂直每米像素个数 */
unsigned long biClrUsed; /* 位图实际使用的颜色表的颜色数 */
unsigned long biClrImportant; /* 重要颜色索引的个数 */
} BITMAPINFOHEADER; /* 位图信息头结构 */
typedef struct
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;
typedef struct
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
}BITMAPINFO, * PBITMAPINFO;
BITMAPFILEHEADER bfile, bbfile;
BITMAPINFOHEADER binfo, bbinfo;
BITMAPINFO bmi; //»Ò¶È͌ʱÓÃÕâžö.
PBITMAPINFO pbmi;
FILE *in, *out;
int open()
{
if((in = fopen("girl.bmp", "rb")) == NULL)
{
printf("Open file failed.n");
exit(-1);
}
if((out = fopen("gray_girl.bmp", "wb")) == NULL)
{
printf("Create copy file failed.n");
exit(-1);
}
return 0;
}
int init_bmpStruct()
{
fread(&bfile.bfType, sizeof(bfile.bfType), 1, in);
fwrite(&bfile.bfType, sizeof(bfile.bfType), 1, out);
fread(&bfile.bfSize, sizeof(bfile.bfSize), 1, in);
fwrite(&bfile.bfSize, sizeof(bfile.bfSize), 1, out);
fread(&bfile.bfReserved1, sizeof(bbfile.bfReserved1), 1, in);
fwrite(&bfile.bfReserved1, sizeof(bfile.bfReserved1), 1, out);
fread(&bfile.bfReserved2, sizeof(bfile.bfReserved2), 1, in);
fwrite(&bfile.bfReserved2, sizeof(bfile.bfReserved2), 1, out);
fread(&bfile.bfoffBits, sizeof(bfile.bfoffBits), 1, in);
fwrite(&bfile.bfoffBits, sizeof(bfile.bfoffBits), 1, out);
fread(&binfo.biSize, sizeof(binfo.biSize), 1, in);
fwrite(&binfo.biSize, sizeof(binfo.biSize), 1, out);
fread(&binfo. biWidth, sizeof(binfo. biWidth), 1, in);
fwrite(&binfo.biWidth, sizeof(binfo.biWidth), 1, out);
fread(&binfo.biHeight, sizeof(binfo.biHeight), 1, in);
fwrite(&binfo.biHeight, sizeof(binfo.biHeight), 1, out);
fread(&binfo.biPlanes, sizeof(binfo.biPlanes), 1, in);
fwrite(&binfo.biPlanes, sizeof(binfo.biPlanes), 1, out);
fread(&binfo.biBitCount, sizeof(binfo.biBitCount), 1, in);
fwrite(&binfo.biBitCount, sizeof(binfo.biBitCount), 1, out);
fread(&binfo.biCompress, sizeof(binfo.biCompress), 1, in);
fwrite(&binfo.biCompress, sizeof(binfo.biCompress), 1, out);
fread(&binfo.biSizeImage, sizeof(binfo.biSizeImage), 1, in);
fwrite(&binfo.biSizeImage, sizeof(binfo.biSizeImage), 1, out);
fread(&binfo.biXPelsPerMeter, sizeof(binfo.biXPelsPerMeter), 1, in);
fwrite(&binfo.biXPelsPerMeter, sizeof(binfo.biXPelsPerMeter), 1, out);
fread(&binfo.biYPelsPerMeter, sizeof(binfo.biYPelsPerMeter), 1, in);
fwrite(&binfo.biYPelsPerMeter, sizeof(binfo.biYPelsPerMeter), 1, out);
fread(&binfo.biClrUsed, sizeof(binfo.biClrUsed), 1, in);
fwrite(&binfo.biClrUsed, sizeof(binfo.biClrUsed), 1, out);
fread(&binfo.biClrImportant, sizeof(binfo.biClrImportant), 1, in);
fwrite(&binfo.biClrImportant, sizeof(binfo.biClrImportant), 1, out);
printf("biSize = %ldn",binfo.biSize);
printf("biWidth = %ldn",binfo.biWidth);
printf("biHeight = %ldn",binfo.biHeight);
printf("biPlanes = %dn",binfo.biPlanes);
printf("biBitCount = %dn",binfo.biBitCount);
printf("biCompress = %dn",binfo.biCompress);
printf("biSizeImage = %ldn",binfo.biSizeImage);
printf("biXpersPerMeter = %ldn",binfo.biXPelsPerMeter);
printf("biYPersPerMeter = %ldn",binfo.biYPelsPerMeter);
printf("biClrUsed = %ldn",binfo.biClrUsed);
printf("biClrImportant = %ldn", binfo.biClrImportant);
return 0;
}
int WritePixtoFile()
{
int i = 0, j = 0;
unsigned long bboffest = bbfile.bfoffBits;
unsigned long boffest = bfile.bfoffBits;
int nBufferWide = 4 * (( binfo.biWidth * 24 + 31 )/ 32);
int bnBufferWide = 4 * (( binfo.biWidth * 24 + 31 )/ 32);
int Y;
unsigned char YY;
int R, G, B;
int RR, GG, BB;
long count = 0;
unsigned char *RGBbuffer = (unsigned char*)malloc(nBufferWide * binfo.biHeight);
unsigned char *bRGBbuffer = (unsigned char*)malloc(bnBufferWide * binfo.biHeight);
fseek(in, bboffest, SEEK_SET); ???这句定位是不是有问题?因为最后保存的图像一片黑.
fread(RGBbuffer, sizeof(unsigned char), nBufferWide * binfo.biHeight, in);
for(i = binfo.biWidth -1 ; i >= 0; i--)
{
for(j = binfo.biHeight - 1; j >=0 ; j--)
{
R = (int)(*(RGBbuffer + i*3 + j));
G = (int)(*(RGBbuffer + i*3 + j + 1));
B = (int)(*(RGBbuffer+ i*3 + j + 2));
Y = ((66 * R + 129 * G + 25 * B + 128)>>8) + 16;
YY = (unsigned char) ((Y255)?255:Y));
RR = YY;
GG = YY;
BB = YY;
printf("YY = %d,", YY);
*(bRGBbuffer + i* 3 +j) = RR;
*(bRGBbuffer + i* 3 +j +1) = RR;
*(bRGBbuffer + i* 3 +j +2) = RR;
//memcpy(bRGBbuffer++, &YY, sizeof(unsigned char));
//printf("YY = %u, count = %ldn", YY, count);
}
}
fwrite(bRGBbuffer ,1 , bnBufferWide * binfo.biHeight,out);
}
int main()
{
open();
init_bmpStruct();
WritePixtoFile();
fclose(in);
fclose(out);
printf("BITMAPFILEHEADER size = %d,BITMAPINFOHEADER size = %d, RGBQUAD size = %dn ", sizeof(BITMAPFILEHEADER), sizeof(BITMAPINFOHEADER), sizeof(RGBQUAD));
return 1;
}
girl.bmp是一个24位的BMP图像,320*240的.
但是保存的gray_girl.bmp,就是一片黑啊~
是不是offset定位错了?但是不知道怎么错的.
哪位朋友知道啊.我是新手.点指一下.非常感激!
#include
#pragma pack(2);
typedef struct
{
unsigned short bfType; /* 文件类型, 必须为 "BM"(0x4D42) */
unsigned long bfSize; /* 文件的大小(字节) */
unsigned short bfReserved1; /* 保留, 必须为 0 */
unsigned short bfReserved2; /* 保留, 必须为 0 */
unsigned long bfoffBits; /* 位图阵列相对于文件头的偏移量(字节) */
} BITMAPFILEHEADER; /* 文件头结构 */
typedef struct
{
unsigned long biSize; /* size of BITMAPINFOHEADER */
unsigned long biWidth; /* 位图宽度(像素) */
unsigned long biHeight; /* 位图高度(像素) */
unsigned short biPlanes; /* 目标设备的位平面数, 必须置为1 */
unsigned short biBitCount; /* 每个像素的位数, 1,4,8或24 */
unsigned long biCompress; /* 位图阵列的压缩方法,0=不压缩 */
unsigned long biSizeImage; /* 图像大小(字节) */
unsigned long biXPelsPerMeter; /* 目标设备水平每米像素个数 */
unsigned long biYPelsPerMeter; /* 目标设备垂直每米像素个数 */
unsigned long biClrUsed; /* 位图实际使用的颜色表的颜色数 */
unsigned long biClrImportant; /* 重要颜色索引的个数 */
} BITMAPINFOHEADER; /* 位图信息头结构 */
typedef struct
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;
typedef struct
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
}BITMAPINFO, * PBITMAPINFO;
BITMAPFILEHEADER bfile, bbfile;
BITMAPINFOHEADER binfo, bbinfo;
BITMAPINFO bmi; //»Ò¶È͌ʱÓÃÕâžö.
PBITMAPINFO pbmi;
FILE *in, *out;
int open()
{
if((in = fopen("girl.bmp", "rb")) == NULL)
{
printf("Open file failed.n");
exit(-1);
}
if((out = fopen("gray_girl.bmp", "wb")) == NULL)
{
printf("Create copy file failed.n");
exit(-1);
}
return 0;
}
int init_bmpStruct()
{
fread(&bfile.bfType, sizeof(bfile.bfType), 1, in);
fwrite(&bfile.bfType, sizeof(bfile.bfType), 1, out);
fread(&bfile.bfSize, sizeof(bfile.bfSize), 1, in);
fwrite(&bfile.bfSize, sizeof(bfile.bfSize), 1, out);
fread(&bfile.bfReserved1, sizeof(bbfile.bfReserved1), 1, in);
fwrite(&bfile.bfReserved1, sizeof(bfile.bfReserved1), 1, out);
fread(&bfile.bfReserved2, sizeof(bfile.bfReserved2), 1, in);
fwrite(&bfile.bfReserved2, sizeof(bfile.bfReserved2), 1, out);
fread(&bfile.bfoffBits, sizeof(bfile.bfoffBits), 1, in);
fwrite(&bfile.bfoffBits, sizeof(bfile.bfoffBits), 1, out);
fread(&binfo.biSize, sizeof(binfo.biSize), 1, in);
fwrite(&binfo.biSize, sizeof(binfo.biSize), 1, out);
fread(&binfo. biWidth, sizeof(binfo. biWidth), 1, in);
fwrite(&binfo.biWidth, sizeof(binfo.biWidth), 1, out);
fread(&binfo.biHeight, sizeof(binfo.biHeight), 1, in);
fwrite(&binfo.biHeight, sizeof(binfo.biHeight), 1, out);
fread(&binfo.biPlanes, sizeof(binfo.biPlanes), 1, in);
fwrite(&binfo.biPlanes, sizeof(binfo.biPlanes), 1, out);
fread(&binfo.biBitCount, sizeof(binfo.biBitCount), 1, in);
fwrite(&binfo.biBitCount, sizeof(binfo.biBitCount), 1, out);
fread(&binfo.biCompress, sizeof(binfo.biCompress), 1, in);
fwrite(&binfo.biCompress, sizeof(binfo.biCompress), 1, out);
fread(&binfo.biSizeImage, sizeof(binfo.biSizeImage), 1, in);
fwrite(&binfo.biSizeImage, sizeof(binfo.biSizeImage), 1, out);
fread(&binfo.biXPelsPerMeter, sizeof(binfo.biXPelsPerMeter), 1, in);
fwrite(&binfo.biXPelsPerMeter, sizeof(binfo.biXPelsPerMeter), 1, out);
fread(&binfo.biYPelsPerMeter, sizeof(binfo.biYPelsPerMeter), 1, in);
fwrite(&binfo.biYPelsPerMeter, sizeof(binfo.biYPelsPerMeter), 1, out);
fread(&binfo.biClrUsed, sizeof(binfo.biClrUsed), 1, in);
fwrite(&binfo.biClrUsed, sizeof(binfo.biClrUsed), 1, out);
fread(&binfo.biClrImportant, sizeof(binfo.biClrImportant), 1, in);
fwrite(&binfo.biClrImportant, sizeof(binfo.biClrImportant), 1, out);
printf("biSize = %ldn",binfo.biSize);
printf("biWidth = %ldn",binfo.biWidth);
printf("biHeight = %ldn",binfo.biHeight);
printf("biPlanes = %dn",binfo.biPlanes);
printf("biBitCount = %dn",binfo.biBitCount);
printf("biCompress = %dn",binfo.biCompress);
printf("biSizeImage = %ldn",binfo.biSizeImage);
printf("biXpersPerMeter = %ldn",binfo.biXPelsPerMeter);
printf("biYPersPerMeter = %ldn",binfo.biYPelsPerMeter);
printf("biClrUsed = %ldn",binfo.biClrUsed);
printf("biClrImportant = %ldn", binfo.biClrImportant);
return 0;
}
int WritePixtoFile()
{
int i = 0, j = 0;
unsigned long bboffest = bbfile.bfoffBits;
unsigned long boffest = bfile.bfoffBits;
int nBufferWide = 4 * (( binfo.biWidth * 24 + 31 )/ 32);
int bnBufferWide = 4 * (( binfo.biWidth * 24 + 31 )/ 32);
int Y;
unsigned char YY;
int R, G, B;
int RR, GG, BB;
long count = 0;
unsigned char *RGBbuffer = (unsigned char*)malloc(nBufferWide * binfo.biHeight);
unsigned char *bRGBbuffer = (unsigned char*)malloc(bnBufferWide * binfo.biHeight);
fseek(in, bboffest, SEEK_SET); ???这句定位是不是有问题?因为最后保存的图像一片黑.
fread(RGBbuffer, sizeof(unsigned char), nBufferWide * binfo.biHeight, in);
for(i = binfo.biWidth -1 ; i >= 0; i--)
{
for(j = binfo.biHeight - 1; j >=0 ; j--)
{
R = (int)(*(RGBbuffer + i*3 + j));
G = (int)(*(RGBbuffer + i*3 + j + 1));
B = (int)(*(RGBbuffer+ i*3 + j + 2));
Y = ((66 * R + 129 * G + 25 * B + 128)>>8) + 16;
YY = (unsigned char) ((Y255)?255:Y));
RR = YY;
GG = YY;
BB = YY;
printf("YY = %d,", YY);
*(bRGBbuffer + i* 3 +j) = RR;
*(bRGBbuffer + i* 3 +j +1) = RR;
*(bRGBbuffer + i* 3 +j +2) = RR;
//memcpy(bRGBbuffer++, &YY, sizeof(unsigned char));
//printf("YY = %u, count = %ldn", YY, count);
}
}
fwrite(bRGBbuffer ,1 , bnBufferWide * binfo.biHeight,out);
}
int main()
{
open();
init_bmpStruct();
WritePixtoFile();
fclose(in);
fclose(out);
printf("BITMAPFILEHEADER size = %d,BITMAPINFOHEADER size = %d, RGBQUAD size = %dn ", sizeof(BITMAPFILEHEADER), sizeof(BITMAPINFOHEADER), sizeof(RGBQUAD));
return 1;
}
girl.bmp是一个24位的BMP图像,320*240的.
但是保存的gray_girl.bmp,就是一片黑啊~
是不是offset定位错了?但是不知道怎么错的.
哪位朋友知道啊.我是新手.点指一下.非常感激!
|
不是吧 写得这么复杂?
我写的 uClinux 下 Frame Buffer 驱动显示 24 位图片的程序
我写的 uClinux 下 Frame Buffer 驱动显示 24 位图片的程序
typedef struct /* 24 bit BMP color */
{
unsigned char b;
unsigned char g;
unsigned char r;
} ColorBGR24BIT;
/*
* 不处理图片是否压缩
* 从下面往上面开始显示
* ScreenWidth、ScreenHeight 分别为屏幕大小和高度
*/
void disp_24bit_bmp(char *filename)
{
int width, height;
short int bitcount;
FILE *fp;
ColorBGR24BIT *colorRead24; /* read the color data(24 bit) */
unsigned int tempcolor;
int i, j;
unsigned int *pD;
unsigned char r, g, b;
if((fp=fopen(filename, "rb"))==NULL)
{
printf("Read %s error.n", filename);
return;
}
fseek(fp, 18, SEEK_SET);
fread(&width, sizeof(width), 1, fp);
fread(&height, sizeof(height), 1, fp);
fseek(fp, 28, SEEK_SET);
fread(&bitcount, sizeof(bitcount), 1, fp);
if(24 !=bitcount)
{
fclose(fp);
printf("Only support 24 bitmap file.n");
return;
}
/* color * width memory */
colorRead24 = (ColorBGR24BIT *)malloc(width*sizeof(ColorBGR24BIT));
fseek(fp, 54, SEEK_SET); /* color data */
for(j=height-1; j>=0; j--)
{
/* fread(colorRead24, width*sizeof(ColorBGR24BIT), 1, fp); */
fread(colorRead24, width*3, 1, fp);
if(width%4) fseek(fp, (width%4), SEEK_CUR); /* fix width and file pointer 24-bits */
if(j >= ScreenHeight) continue;
for(i=0; i= ScreenWidth) break;
r = colorRead24[i].r;
g = colorRead24[i].g;
b = colorRead24[i].b;
tempcolor = RGB(r, g, b);
pD = (unsigned int*)framebase + (j * ScreenWidth+ i); /* 指向 Frame Buffer 内核空间 */
*pD = tempcolor; /* 赋值(32位色) */
}
}
free(colorRead24);
fclose(fp);
printf("Read %s ok, Width=%d, Height=%d.n", filename, width, height);
}
|
Y = ((66 * R + 129 * G + 25 * B + 128)>>8) + 16;
是错误的 66+129+25=220 不是 256(2^8)
灰度 = 红×0.299 + 绿×0.587 + 蓝×0.114
为了不计算浮点数(快速)
Y = ((76 * R + 150 * G + 29 * B + 128)>>8)
是错误的 66+129+25=220 不是 256(2^8)
灰度 = 红×0.299 + 绿×0.587 + 蓝×0.114
为了不计算浮点数(快速)
Y = ((76 * R + 150 * G + 29 * B + 128)>>8)
|
没有环境,没法调
是不是字节对齐的问题
是不是字节对齐的问题
|
R = (int)(*(RGBbuffer + i*3 + j));
G = (int)(*(RGBbuffer + i*3 + j + 1));
B = (int)(*(RGBbuffer+ i*3 + j + 2));
Y = ((66 * R + 129 * G + 25 * B + 128)>>8) + 16;
R,G,B这里应该用char。
Y啊,这个看不懂。
G = (int)(*(RGBbuffer + i*3 + j + 1));
B = (int)(*(RGBbuffer+ i*3 + j + 2));
Y = ((66 * R + 129 * G + 25 * B + 128)>>8) + 16;
R,G,B这里应该用char。
Y啊,这个看不懂。