位深度为8的二值图转位深度为1二值图
typedef struct tagBITMAPFILEHEADERx
{
unsigned short int bfType;
unsigned long bfSize;
unsigned short int bfReserverd1;
unsigned short int bfReserverd2;
unsigned long bfbfOffBits;
}BITMAPFILEHEADERx;
typedef struct tagBITMAPINFOHEADERx
{
long biSize;
long biWidth;
long biHeight;
short int biPlanes;
short int biBitcount;
int biCompression;
long biSizeImage;
long biXPelsPermeter;
long biYPelsPermeter;
long biClrUsed;
long biClrImportant;
}BITMAPINFOHEADERx;
#endif
typedef struct
{
BITMAPFILEHEADERx file;
BITMAPINFOHEADERx info;
}bmp;
int writebmpfile(unsigned char** outbitbmp)
{
bmp m = { 0 };
IplImage* temp = cvLoadImage("bin.bmp", 0);
if(!temp) return -1;
int widthStep = 0;
int alldatalen=0;
widthStep = ((temp->width + 31) / 32) * 4;
m.file.bfType = 0X4D42;
m.file.bfSize = widthStep * temp->height + 62;
m.file.bfReserverd1 = 0;
m.file.bfReserverd2 = 0;
m.file.bfbfOffBits = 62;
m.info.biSize = 40;
m.info.biWidth = temp->width;
m.info.biHeight = temp->height;
m.info.biPlanes = 1;
m.info.biBitcount = 1;
m.info.biCompression = 0;
m.info.biSizeImage = widthStep * temp->height;
m.info.biXPelsPermeter = 0;
m.info.biClrUsed = 0;
m.info.biClrImportant = 0;
uchar Palette[8] = { 0,0,0,0,255,255,255,0 };
uchar* pbit=(uchar*)malloc(widthStep * temp->height+62);
if(!pbit) return -1;
memcpy(pbit,&(m),sizeof(m));
memcpy(pbit+54,Palette,sizeof(Palette));
uchar* data = pbit+62;
memset(data, 0, widthStep * temp->height);
for (int i = 0; i < temp->height; i++)
{
for (int j = 0; j < widthStep; j++)
{
uchar temp_data = 0;
for (int k = 0; k < 8; k++)
{
if (j * 8 + k < temp->width)
{
int temp_value = 0;
if ((uchar)temp->imageData[(temp->height - 1 - i) * temp->widthStep + j * 8 + k] == 255)
{
temp_value = 1 << (7 - k);
temp_data += temp_value;
}
}
}
data[i * widthStep + j] = temp_data;
}
}
alldatalen=widthStep * temp->height+62;
if(temp)
{
cvReleaseImage(&temp);
}
*outbitbmp=pbit;
return alldatalen;
}