CxImage图像处理类库说明3(转载)
生活随笔
收集整理的這篇文章主要介紹了
CxImage图像处理类库说明3(转载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,我們需要聲明這個函數: bool Jitter(long radius=2)
{
????// 檢查圖像是否合法,這應當是這個函數的第一行
????if (!pDib) return false;
????
????// 局部變量
????long nx,ny;
????
????// 臨時圖像,用于存儲算法的部分結果
????CxImage tmp(*this,pSelection!=0,true,true);
????
????// 限制函數僅僅作用在選區(通過Selection...()函數定義)的最小區域
????// 這將加快整個循環的速度,提高算法效率
????long xmin,xmax,ymin,ymax;
????if (pSelection){
????????xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
????????ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
????} else {
????????xmin = ymin = 0;
????????xmax = head.biWidth; ymax=head.biHeight;
????}
????
????// 主循環 : 垂直方向掃描圖像
????for(long y=ymin; y <ymax; y++){
????
????????// 監視循環的進度
????????info.nProgress = (long)(100*y/head.biHeight);
????
????????// 檢查應用程序是否已經退出
????????if (info.nEscape) break;
????
????????// 主循環 : 水平方向掃描圖像
????????for(long x=xmin; x<xmax; x++){
????
????????// 如果選區功能啟用了,則僅僅處理選區內部的像素
#if CXIMAGE_SUPPORT_SELECTION
????????????if (SelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
????????????{
????????????????// 主算法
????????????????nx=x+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
????????????????ny=y+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
????????????????if (!IsInside(nx,ny)) {
????????????????????nx=x;
????????????????????ny=y;
????????????????}
????????????????// 保存結果到臨時圖像中.
????????????????// 如果可以,24位圖像請使用PixelColor,
????????????????// 而8,4和1位圖像請使用PixelIndex,這樣可以加快速度。
????????????????if (head.biClrUsed==0){
????????????????????tmp.SetPixelColor(x,y,GetPixelColor(nx,ny));
????????????????} else {
????????????????????tmp.SetPixelIndex(x,y,GetPixelIndex(nx,ny));
????????????????}
????????????????// 如果啟用了透明度功能,則處理透明圖層中的像素
#if CXIMAGE_SUPPORT_ALPHA
????????????????tmp.AlphaSet(x,y,AlphaGet(nx,ny));
#endif //CXIMAGE_SUPPORT_ALPHA
????????????}
????????}
????}
????// 保存結果并退出
????Transfer(tmp);
????return true;
}
// bmp -> jpg
image.Load("image.bmp", CXIMAGE_FORMAT_BMP);
if (image.IsValid()){
????if(!image.IsGrayScale()) image.IncreaseBpp(24);
????image.SetJpegQuality(80);
????image.Save("image.jpg",CXIMAGE_FORMAT_JPG);
}
// png -> tif
image.Load("image.png", CXIMAGE_FORMAT_PNG);
if (image.IsValid()){
????image.Save("image.tif",CXIMAGE_FORMAT_TIF);
}
CxImage* newImage = new CxImage();
newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),
?????????????????????? "PNG"),CXIMAGE_FORMAT_PNG);
或者
//Load the resource IDR_JPG1 from DLL
CxImage* newImage = new CxImage();
HINSTANCE hdll=LoadLibrary("imagelib.dll");
if (hdll){
????HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG");
????newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll);
????FreeLibrary(hdll);
}
或者
//Load a bitmap resource;
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
?????????????????????????? MAKEINTRESOURCE(IDB_BITMAP1)));
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
? 在ximage.h頭文件的CXIMAGE_SUPPORT_DSP部分,你可以在public區域的任何部分聲明這個函數。
? 現在,我們開始定義這個函數:
bool CxImage::Jitter(long radius){
????// 檢查圖像是否合法,這應當是這個函數的第一行
????if (!pDib) return false;
????
????// 局部變量
????long nx,ny;
????
????// 臨時圖像,用于存儲算法的部分結果
????CxImage tmp(*this,pSelection!=0,true,true);
????
????// 限制函數僅僅作用在選區(通過Selection...()函數定義)的最小區域
????// 這將加快整個循環的速度,提高算法效率
????long xmin,xmax,ymin,ymax;
????if (pSelection){
????????xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
????????ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
????} else {
????????xmin = ymin = 0;
????????xmax = head.biWidth; ymax=head.biHeight;
????}
????
????// 主循環 : 垂直方向掃描圖像
????for(long y=ymin; y <ymax; y++){
????
????????// 監視循環的進度
????????info.nProgress = (long)(100*y/head.biHeight);
????
????????// 檢查應用程序是否已經退出
????????if (info.nEscape) break;
????
????????// 主循環 : 水平方向掃描圖像
????????for(long x=xmin; x<xmax; x++){
????
????????// 如果選區功能啟用了,則僅僅處理選區內部的像素
#if CXIMAGE_SUPPORT_SELECTION
????????????if (SelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
????????????{
????????????????// 主算法
????????????????nx=x+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
????????????????ny=y+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
????????????????if (!IsInside(nx,ny)) {
????????????????????nx=x;
????????????????????ny=y;
????????????????}
????????????????// 保存結果到臨時圖像中.
????????????????// 如果可以,24位圖像請使用PixelColor,
????????????????// 而8,4和1位圖像請使用PixelIndex,這樣可以加快速度。
????????????????if (head.biClrUsed==0){
????????????????????tmp.SetPixelColor(x,y,GetPixelColor(nx,ny));
????????????????} else {
????????????????????tmp.SetPixelIndex(x,y,GetPixelIndex(nx,ny));
????????????????}
????????????????// 如果啟用了透明度功能,則處理透明圖層中的像素
#if CXIMAGE_SUPPORT_ALPHA
????????????????tmp.AlphaSet(x,y,AlphaGet(nx,ny));
#endif //CXIMAGE_SUPPORT_ALPHA
????????????}
????????}
????}
????// 保存結果并退出
????Transfer(tmp);
????return true;
}
?
示例: 如何轉換一種格式到另外一種格式?
CxImage??image;// bmp -> jpg
image.Load("image.bmp", CXIMAGE_FORMAT_BMP);
if (image.IsValid()){
????if(!image.IsGrayScale()) image.IncreaseBpp(24);
????image.SetJpegQuality(80);
????image.Save("image.jpg",CXIMAGE_FORMAT_JPG);
}
// png -> tif
image.Load("image.png", CXIMAGE_FORMAT_PNG);
if (image.IsValid()){
????image.Save("image.tif",CXIMAGE_FORMAT_TIF);
}
? 如何從資源中加載圖像
//Load the resource IDR_PNG1 from the PNG resource typeCxImage* newImage = new CxImage();
newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),
?????????????????????? "PNG"),CXIMAGE_FORMAT_PNG);
或者
//Load the resource IDR_JPG1 from DLL
CxImage* newImage = new CxImage();
HINSTANCE hdll=LoadLibrary("imagelib.dll");
if (hdll){
????HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG");
????newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll);
????FreeLibrary(hdll);
}
或者
//Load a bitmap resource;
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
?????????????????????????? MAKEINTRESOURCE(IDB_BITMAP1)));
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
?
轉載于:https://www.cnblogs.com/carekee/articles/2174673.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的CxImage图像处理类库说明3(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做外挂小记(3)--C#调用读写内存类篇
- 下一篇: 使用MASM02 - Win32汇编语言