日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GDIPlus灰度化图像

發(fā)布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GDIPlus灰度化图像 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

將RGB彩色圖像轉(zhuǎn)為8位的索引顏色


先定義一個宏

// Greyscale conversion #define GREY(r, g, b) (BYTE)(((WORD)r * 77 + (WORD)g * 150 + (WORD)b * 29) >> 8 //#define GREY(r, g, b) (BYTE)(((WORD)r * 169 + (WORD)g * 256 + (WORD)b * 87) >> 9)


// Grayscale, 將RGB轉(zhuǎn)為8bit void GDIPlusImage::IPFuncGrayscale() {Bitmap* ima = this->m_pBitmap;if (!ima) {return;}// Build new 8bpp greyscale bitmapint width = ima->GetWidth();int height = ima->GetHeight();Rect rect(0, 0, width, height);BitmapData bitmapData_org, bitmapData_new;Bitmap *pGrayImg = new Bitmap(width, height,PixelFormat8bppIndexed); ColorPalette *pal = (ColorPalette *)malloc(sizeof(ColorPalette)+256*sizeof(ARGB));pal->Count = 256;pal->Flags = 2;for (int i = 0; i < 256; i++){pal->Entries[i] = Color::MakeARGB(255,i,i,i);}pGrayImg->SetPalette(pal);pGrayImg->LockBits(&rect,ImageLockModeWrite,PixelFormat8bppIndexed,&bitmapData_new); //鎖定位圖,然后對其進行讀寫內(nèi)存操作,相關信息保存到BitmapData中Status iSucess = ima->LockBits(&rect,ImageLockModeRead | ImageLockModeWrite,ima->GetPixelFormat() ,&bitmapData_org);BYTE *_pixels = (BYTE*)bitmapData_org.Scan0; //原圖rect區(qū)域內(nèi)存位置的起始指針,以BYTE作為單元類型BYTE *_newpixles = (BYTE*)bitmapData_new.Scan0; //目標位圖rect區(qū)域的起始指針BYTE _grey;// build pixlesswitch(ima->GetPixelFormat()) //像素格式不同,灰度化處理方式也不同{case PixelFormat24bppRGB:{int _strideoff24 = bitmapData_org.Stride - 3*width;int _strideoff8 = bitmapData_new.Stride - width;// 灰度化for (int i=0; i < height; i++){for (int j=0;j < width;j++){ BYTE* _pixels_b; //bBYTE* _pixels_g; //gBYTE* _pixels_r; //r_pixels_b = _pixels++; //blue_pixels_g = _pixels++; //green_pixels_r = _pixels++; //red_grey = GREY(*_pixels_r, *_pixels_g, *_pixels_b); *_newpixles = _grey; //根據(jù)紅綠藍求出此像素的灰度值_newpixles += 1;}_pixels += _strideoff24;_newpixles += _strideoff8;}}break;case PixelFormat32bppARGB:{int _strideoff32 = bitmapData_org.Stride - 4*width;int _strideoff8 = bitmapData_new.Stride - width;// 灰度化for (int i=0;i<height;i++){for (int j=0;j<width;j++){ BYTE* _pixels_b;BYTE* _pixels_g; BYTE* _pixels_r; _pixels_b = _pixels++; //blue_pixels_g = _pixels++; //green_pixels_r = _pixels++; //red_grey = GREY(*_pixels_r, *_pixels_g, *_pixels_b); *_newpixles = _grey;_pixels ++; //忽略alpha位置_newpixles += 1;}_pixels += _strideoff32;_newpixles += _strideoff8;}}break;case PixelFormat8bppIndexed: // 因為比Clone(rect,ima->GetPixelFormat())要快{// 直接復制memcpy(_newpixles, _pixels, height * bitmapData_new.Stride);}break;default:break;}ima->UnlockBits(&bitmapData_org);pGrayImg->UnlockBits(&bitmapData_new);free (pal); // 釋放掉malloc開辟的空間m_pBitmap = pGrayImg; }

總結

以上是生活随笔為你收集整理的GDIPlus灰度化图像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。