2010年2月3日 星期三

CuImageEx

// CuImageEx.h



class CuImageEx : public IuImageEx 
{
    //圖高
    UINT    _nHeight;

    //圖寬
    UINT    _nWidth;

    // 圖的色素  :  16  
    UINT    _nBitCount;

    // 存放 圖形點陣列
    BYTE    *_bits;


public:
    CuImageEx();
    virtual ~CuImageEx();

    //////////////////////////////////////////////////////////////////
    // IuUnknown 公開虛擬界面 
    virtual ULONG Release();

    //////////////////////////////////////////////////////////////////
    // IuImage 公開虛擬界面 
    virtual BOOL Create(int nWidth, int nHeight, int nBPP = 16);

    virtual UINT    Height(){ return _nHeight; }
    virtual void    Height(UINT nHeight){ _nHeight = nHeight; }

    virtual UINT    Width(){ return _nWidth; }
    virtual void    Width(UINT nWidth){ _nWidth = nWidth; }

    virtual UINT    BitCount(){ return _nBitCount; }
    virtual void    BitCount(UINT nBitCount){ _nBitCount = nBitCount; }

    virtual COLORREF GetPixel( int x, int y);
    virtual BYTE*   Data(void){ return _bits; }


    //////////////////////////////////////////////////////////////////
    // 未公開虛擬界面 的 public 函式

    void Data(BYTE *pDate){ _bits = pDate; }

    void Destroy();

private:
    CuImageEx& operator =(CuImageEx& img);

};

 
// CuImageEx.cpp


CuImageEx::CuImageEx()
:_nHeight(0)
,_nWidth(0)
,_nBitCount(0)
,_bits(NULL)
{
}

CuImageEx::~CuImageEx()
{
    Destroy();
}

ULONG CuImageEx::Release()
{
    delete this;
    return 0;
}

BOOL CuImageEx::Create(int nWidth, int nHeight, int nBPP)
{

    Destroy();

    Height(nHeight);
    Width(nWidth);
    BitCount(nBPP);

    _bits = new BYTE[nWidth * nHeight * (nBPP >> 3)];

    return (_bits != NULL);

}

void CuImageEx::Destroy()
{

    Height(0);
    Width(0);
    BitCount(0);

    delete [] _bits;
    _bits = NULL;

}

COLORREF CuImageEx::GetPixel( int x, int y)
{

    WORD *pBits = (WORD*)this->Data();

    if( !pBits )
        return RGB(0, 0, 0);

    int nW = Width() ;

    // 
    if( nW % 2 )
        nW++;

    WORD wPixel = pBits[y * nW + x];

    return RGB( (wPixel >> 10 ) & 0x1f,
        (wPixel >> 5) & 0x1f,
        wPixel & 0x1f);
}

CuImageEx& CuImageEx::operator =(CuImageEx& img)
{
    
    if( this == &img )
        return *this;
    
    this->Create(img.Width() , img.Height(), img.BitCount() );

    memcpy(_bits, img._bits, _nWidth  * _nHeight * ( _nBitCount >> 3 ) );
        
    return *this;

}
與CuImageWnd 相異的地方在於, 若已經有一份點陣DC時, 其實其他的圖型運算是可以使用自製效能優化過的元件! 在這裡縮小圖結構方便替換運算元件!

沒有留言:

張貼留言