CxImage图像处理类库说明3(转载)

首先,我们需要声明这个函数:
bool Jitter(long radius=2)

  在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 type
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);

 

posted on 2011-09-13 14:17  carekee  阅读(544)  评论(0)    收藏  举报