关于图像处理的几段代码,存此待查

根据源图RECT、目标Rect计算缩放后(不放大),新的显示Rect

序列化CImage

CImage复制

//根据源图RECT、目标Rect计算缩放后(不放大),新的显示Rect

static void GetStretchRect(CRect*rcResult, const CRect rcDst, const CRect rcSrc);

void CPublicStaticMethods::GetStretchRect(CRect *rcResult, const CRect rcDst, const CRect rcSrc)
{
    //计算按原图比例缩小后的图片的大小
    int nWidth = rcSrc.Width();
    int nHeight = rcSrc.Height();
    int nDstWidth = rcDst.Width();
    int nDstHeight = rcDst.Height();

    int nResultLeft = 0;
    int nResultTop = 0;
    int nResultWidth = nWidth;
    int nResultHeight = nHeight;

    if (nWidth <= nDstWidth && nHeight <= nDstHeight)
    {
        nResultLeft = (nDstWidth - nResultWidth) / 2;
        nResultTop = (nDstHeight - nResultHeight) / 2;
    }
    else
    {
        //源宽高比
        float fSrcScale = (float)nWidth / (float)nHeight;
        //目标宽高比
        float fDstScale = (float)nDstWidth / (float)nDstHeight;
        //如果源比 > 目标比,则以宽为绽放标准
        if (fSrcScale > fDstScale)
        {
            float fScale = (float)nDstWidth / (float)nWidth;
            nResultWidth = nDstWidth;
            nResultHeight = (int)(nHeight * fScale);
            nResultLeft = 0;
            nResultTop = (nDstHeight - nResultHeight) / 2;
        }
        else
        {
            float fScale = (float)nDstHeight / (float)nHeight;
            nResultHeight = nDstHeight;
            nResultWidth = (int)(nWidth * fScale);
            nResultTop = 0;
            nResultLeft = (nDstWidth - nResultWidth) / 2;
        }
    }

    rcResult->left = nResultLeft;
    rcResult->top = nResultTop;
    rcResult->right = nResultLeft + nResultWidth;
    rcResult->bottom = nResultTop + nResultHeight;
}
View Code

 

//序列化CImage
    static void SerializeCImage(CImage&srcImage, CArchive &ar);

void CPublicStaticMethods::SerializeCImage(CImage&srcImage, CArchive &ar)
{
    int srcBitsCount = 0;
    int srcWidth = 0;
    int srcHeight = 0;
    int srcPitch = 0;
    int nColors = 0;//调色板数量
    if (ar.IsStoring())
    {        
        if (srcImage.IsNull())
        {
            ar << srcWidth << srcHeight ;
            return;
        }
        else
        {
            srcBitsCount = srcImage.GetBPP();
            srcWidth = srcImage.GetWidth();
            srcHeight = srcImage.GetHeight();
            srcPitch = srcImage.GetPitch();

            ar << srcWidth << srcHeight;
            if (srcWidth == 0 || srcHeight == 0)
                return;
            ar<< srcBitsCount << srcPitch;
            RGBQUAD pal[256];
            if (srcBitsCount <= 8 && srcImage.IsIndexed())//需要调色板  
            {                
                nColors = srcImage.GetMaxColorTableEntries();                
            }
            ar << nColors;            
            if (nColors>0)
            {
                srcImage.GetColorTable(0, nColors, pal);
                ar.Write(pal, sizeof(RGBQUAD) * nColors);
            }
            BYTE* srcPtr = (BYTE*)srcImage.GetBits();
            //复制图像数据  
            for (int i = 0; i<srcHeight; i++)
            {
                ar.Write(srcPtr + i * srcPitch, abs(srcPitch));
            }
        }
    }
    else
    {
        ar >>  srcWidth >> srcHeight ;
        if (srcWidth == 0 || srcHeight == 0)
            return;
        ar >> srcBitsCount >> srcPitch;
        ar >> nColors;

        //销毁原有图像  
        if (!srcImage.IsNull())
        {
            //destImage.ReleaseDC();
            srcImage.Detach();
            srcImage.Destroy();
        }
        //创建新图像  
        if (srcBitsCount == 32)   //支持alpha通道  
        {
            srcImage.Create(srcWidth, srcHeight, srcBitsCount, 1);
        }
        else
        {
            srcImage.Create(srcWidth, srcHeight, srcBitsCount, 0);
        }
        //加载调色板  
        if (nColors>0&&nColors<=256)//需要调色板  
        {
            RGBQUAD pal[256];
            ar.Read(pal, sizeof(RGBQUAD) * nColors);
            srcImage.SetColorTable(0, nColors, pal);//复制调色板程序  
        }
        //目标图像参数  
        BYTE *srcPtr = (BYTE*)srcImage.GetBits();
        //复制图像数据  
        for (int i = 0; i<srcHeight; i++)
        {
            ar.Read(srcPtr + i * srcPitch, abs(srcPitch));            
        }
    }
}
View Code

//CImage复制

BOOL CPublicStaticMethods::ImageCopy(const CImage &srcImage, CImage &destImage)
{
    int i;//循环变量  
    if (srcImage.IsNull())
        return FALSE;
    //源图像参数  
    BYTE* srcPtr = (BYTE*)srcImage.GetBits();
    int srcBitsCount = srcImage.GetBPP();
    int srcWidth = srcImage.GetWidth();
    int srcHeight = srcImage.GetHeight();
    int srcPitch = srcImage.GetPitch();
    //销毁原有图像  
    if (!destImage.IsNull())
    {
        //destImage.ReleaseDC();
        destImage.Detach();
        destImage.Destroy();
    }
    //创建新图像  
    if (srcBitsCount == 32)   //支持alpha通道  
    {
        destImage.Create(srcWidth, srcHeight, srcBitsCount, 1);
    }
    else
    {
        destImage.Create(srcWidth, srcHeight, srcBitsCount, 0);
    }
    //加载调色板  
    if (srcBitsCount <= 8 && srcImage.IsIndexed())//需要调色板  
    {
        RGBQUAD pal[256];
        int nColors = srcImage.GetMaxColorTableEntries();
        if (nColors>0)
        {
            srcImage.GetColorTable(0, nColors, pal);
            destImage.SetColorTable(0, nColors, pal);//复制调色板程序  
        }
    }
    //目标图像参数  
    BYTE *destPtr = (BYTE*)destImage.GetBits();
    int destPitch = destImage.GetPitch();
    //复制图像数据  
    for (i = 0; i<srcHeight; i++)
    {
        memcpy(destPtr + i * destPitch, srcPtr + i * srcPitch, abs(srcPitch));
    }

    return TRUE;
}
View Code

 

posted @ 2018-11-24 14:38  吾非无心  阅读(211)  评论(0编辑  收藏  举报