位深度为8的二值图转位深度为1二值图

位深度为8的二值图转位深度为1二值图

typedef  struct  tagBITMAPFILEHEADERx
{
    unsigned short int  bfType;       //位图文件的类型,必须为BM   2
    unsigned long       bfSize;       //文件大小,以字节为单位    4
    unsigned short int  bfReserverd1; //位图文件保留字,必须为0 2
    unsigned short int  bfReserverd2; //位图文件保留字,必须为0 2
    unsigned long       bfbfOffBits;  //位图文件头到数据的偏移量,以字节为单位4
}BITMAPFILEHEADERx;//14
typedef  struct  tagBITMAPINFOHEADERx
{
    long biSize;                        //该结构大小,字节为单位   4
    long  biWidth;                     //图形宽度以象素为单位     4
    long  biHeight;                     //图形高度以象素为单位    4
    short int  biPlanes;               //目标设备的级别,必须为1   2
    short int  biBitcount;             //颜色深度,每个象素所需要的位数  2
    int   biCompression;        //位图的压缩类型                4
    long  biSizeImage;              //位图的大小,以字节为单位   4
    long  biXPelsPermeter;       //位图水平分辨率,每米像素数     4
    long  biYPelsPermeter;       //位图垂直分辨率,每米像素数       4
    long  biClrUsed;            //位图实际使用的颜色表中的颜色数    4
    long  biClrImportant;       //位图显示过程中重要的颜色数        4
}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;

    //54-62共八个字节表示调色板信息
    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);
    //单色位图,1表示白色,0表示黑色,结果图背景为白(1),线条为黑(0)
   // printf("temp->widthStep:%d,width %d\n",temp->widthStep,widthStep);
    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;
    //cvReleaseImage(&temp);
    if(temp)
    {
     cvReleaseImage(&temp);
    }
    *outbitbmp=pbit;
    return alldatalen;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值