#include<iostream>
using namespace std;
#include<stdio.h>
#include<windows.h>
void read_sfile(char *fileName,int &imageHeight,int &imageWidth,unsigned char *Imgdata[2000], int &x, int &y)
{
int biBitCount;
int raw_bytesize;
int size=0;
FILE *fp=fopen(fileName,"rb");
if(fp==NULL)
{
cout<<"can not open this file.\n";
exit(1);
}
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER info;
fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
//int y;
x=info.biXPelsPerMeter;
y=info.biYPelsPerMeter;
biBitCount=info.biBitCount;
if(biBitCount==8)
{
size=1024;
}
imageHeight=info.biHeight;
imageWidth=info.biWidth;
raw_bytesize=(imageWidth*3+3)/4*4;
fseek(fp,sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+size,0);
/* image_data=(unsigned char *)malloc(raw_bytesize*imageHeight*sizeof(unsigned char));
fread(image_data,sizeof(unsigned char),raw_bytesize*imageHeight,fp);*/
//unsigned char *Imgdata[2000];
int i,j;
for ( i=0;i<imageHeight;i++)
Imgdata[i]=new unsigned char[(imageWidth*3+3)/4*4];
for ( i=0;i<imageHeight;i++ )
for(j=0;j<(imageWidth*3+3)/4*4;j++)
fread(&Imgdata[i][j],1,1,fp);
// return Imgdata;
// return image_data;
}
void Init_grayimage(char *fileName,int h,int w,unsigned char *Imgdata[2000],int x,int y)
{
FILE *fp1=fopen(fileName,"wb");
if(fp1==NULL)
{
cout<<"can not open this file.\n";
return ;
}
BITMAPFILEHEADER filehead;
filehead.bfType=0x4D42;
int biBitCount=8;
int raw_bytesize=(w+3)/4*4;
filehead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+raw_bytesize*h;
filehead.bfReserved1=0;
filehead.bfReserved2=0;
filehead.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=BI_RGB;
head.biHeight=h;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=raw_bytesize*h;
head.biWidth=w;
head.biXPelsPerMeter=x;
head.biYPelsPerMeter=y;
fwrite(&filehead,sizeof(BITMAPFILEHEADER),1,fp1);
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp1);
RGBQUAD *ipRGB2;
ipRGB2=(RGBQUAD *)malloc(256*sizeof(RGBQUAD));
int i;
for(i=0;i<256;i++)
{
ipRGB2[i].rgbRed = ipRGB2[i].rgbGreen = ipRGB2[i].rgbBlue = i;
ipRGB2[i].rgbReserved=0;
}
fwrite(ipRGB2,sizeof(RGBQUAD),256,fp1);
unsigned char *image2;
// int raw_bytesize;
// raw_bytesize=(w+3)/4*4;
int bytesize;
bytesize=raw_bytesize*h;
image2=(unsigned char *)malloc(bytesize*sizeof(unsigned char));
/*for(i=0;i<bytesize;i++)
image2[i]=(int)((float)(image_data[i]* * 0.299 +(float)(image_data[i+1] * 0.587)+(float)(image_data[i+2] * 0.114 ));*/
int j;
int nLineStart2;
for(i=0;i<h;i++)
{
nLineStart2 = raw_bytesize*i;
for(j=0;j<raw_bytesize;j++)
{
image2[nLineStart2+j]=int( (float)Imgdata[i][3 * j] * 0.114 + \
(float)Imgdata[i][3 * j + 1] * 0.587 + \
(float)Imgdata[i][3 * j + 2] * 0.299 ); }
}
// image2[i*raw_bytesize+]
// for(i=0;i<bytesize;i++)
fwrite(image2,sizeof(unsigned char),bytesize,fp1);
fclose(fp1);
}
void main()
{
int x=0,y=0;
char *fileName="11.bmp";
char *fileName1="new_11.bmp";
unsigned char *Imgdata[2000];
int h=0,w=0;
read_sfile(fileName,h,w, Imgdata,x,y);
Init_grayimage(fileName1,h,w,Imgdata,x,y);
}