1. 程式人生 > >關於影象處理的幾段程式碼,存此待查

關於影象處理的幾段程式碼,存此待查

根據源圖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