24bit彩色图像转8bit灰度图

        char szFileName[MAX_PATH] = "";
        OPENFILENAME file={0};
        file.lStructSize=sizeof(file);
        file.lpstrFile=szFileName;
        file.nMaxFile=MAX_PATH;
        file.lpstrFilter="BMP Files(*.bmp)/0*.bmp/0JPG Files(*.jpg)/0*.jpg/0All Files/0*.*/0/0";
        file.nFilterIndex=1;
        if(::GetOpenFileName(&file)){
            imgFile = new char[sizeof(szFileName) + 1];
            strcpy(imgFile, szFileName);
            //::MessageBox(NULL,imgFile,"TEST",MB_OK);
        }

        char* tmp = new char[255];
        sprintf(tmp,"Open File: %s",imgFile);


        if(imgFile == NULL || strlen(imgFile)<4){
            AfxMessageBox("Image FileName Not Right.");
            return;
        }

 

    //判断图像格式
        USES_CONVERSION;
        Bitmap bmp(A2W(imgFile));//加载BMP   
        int iPF= bmp.GetPixelFormat();
   
        sprintf(tmp,"data.PixelFormat=%d",  iPF);
        AfxMessageBox(tmp);

        if(iPF == PixelFormat8bppIndexed){
            objCDib = new CDib();
            objCDib->LoadFile(imgFile);

            //view->RedrawWindow();
            view->Invalidate();
        }else if(iPF == PixelFormat24bppRGB){               
            // Specify a pixel format.
            UINT w = bmp.GetWidth();
            UINT h = bmp.GetHeight();

            // Lock the bitmap's bits.
            Rect rect(0, 0, w, h);
           
            BitmapData data;
            bmp.LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &data);
            unsigned char *pData = static_cast<unsigned char*>(data.Scan0);
           

            int w0 = w;
            if(w % 4 !=0){
                //w0 += 4-w%4; //这里不需要对齐
            }           

            int bitwidth = w*3;
            if(w*3 % 4 !=0){
                bitwidth += 4-w*3%4;//原数据已对齐
            }

            sprintf(tmp,"w=%d,w0=%d,bitwidth=%d",w,w0,bitwidth);
            AfxMessageBox(tmp);

            BYTE* NewImg = new BYTE[w0*h];
            memset(NewImg,0,w0*h);

            for(int i=h-1;i>=0;i--) {   
                //memcpy(NewImg+i*ByteWidth,img+i*width*3,width*3);
                for(int j=0;j<w;j++){
                     UINT R = *(pData+i*bitwidth + j*3);
                     UINT G = *(pData+i*bitwidth + j*3 +1);
                     UINT B = *(pData+i*bitwidth + j*3 +2);
                     UINT Y = 0.299*R+0.587*G+0.114*B;
                     NewImg[i*w0+j] = R;
                }               
            }
           
            //释放位图资源
            bmp.UnlockBits(&data);

 

 

注:对24bit位图,一个像素使用3个字节存储。对齐是指行的总字节数对齐,即bitmapWidth*3 % 4必须为0,而不是bitmapWidth%4等于0。

 

对齐方法:

 

            int bitwidth = w*3;
            if(w*3 % 4 !=0){
                bitwidth += 4-w*3%4;//原数据已对齐
            }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值