⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitmap.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
   return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
}

int wxBitmap::GetHeight() const
{
   wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_height;
}

int wxBitmap::GetWidth() const
{
   wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_width;
}

int wxBitmap::GetDepth() const
{
   wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_depth;
}

int wxBitmap::GetQuality() const
{
   wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_quality;
}

wxMask *wxBitmap::GetMask() const
{
   wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_bitmapMask;
}

void wxBitmap::SetWidth(int w)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_width = w;
}

void wxBitmap::SetHeight(int h)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_height = h;
}

void wxBitmap::SetDepth(int d)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_depth = d;
}

void wxBitmap::SetQuality(int q)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_quality = q;
}

void wxBitmap::SetOk(bool isOk)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_ok = isOk;
}

#if wxUSE_PALETTE
wxPalette *wxBitmap::GetPalette() const
{
   wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap  GetPalette()") );

   return &M_BITMAPDATA->m_bitmapPalette;
}

void wxBitmap::SetPalette(const wxPalette& palette)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_bitmapPalette = palette ;
}
#endif // wxUSE_PALETTE

void wxBitmap::SetMask(wxMask *mask)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;

    // Remove existing mask if there is one.
    delete M_BITMAPDATA->m_bitmapMask;

    M_BITMAPDATA->m_bitmapMask = mask ;
}

WXHBITMAP wxBitmap::GetHBITMAP() const
{
   wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );

   return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
}

WXHMETAFILE wxBitmap::GetPict( bool *created ) const
{
    wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );

    PicHandle picture = NULL ;       // This is the returned picture
    if ( created )
        (*created) = false ;
    // If bitmap already in Pict format return pointer
    if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) {
       return M_BITMAPDATA->m_hPict;
    }
    else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) {
       // Invalid bitmap
       return NULL;
    }
    else
    {
        if ( GetMask() )
        {
            picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
        }
        else
        {
            picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
        }
        if ( created && picture )
            (*created) = true ;
    }
     return picture ;
}

/*
 * wxMask
 */

wxMask::wxMask()
    : m_maskBitmap(NULL)
{
}

// Construct a mask from a bitmap and a colour indicating
// the transparent area
wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
    : m_maskBitmap(NULL)
{
    Create(bitmap, colour);
}

// Construct a mask from a bitmap and a palette index indicating
// the transparent area
wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
    : m_maskBitmap(NULL)
{
    Create(bitmap, paletteIndex);
}

// Construct a mask from a mono bitmap (copies the bitmap).
wxMask::wxMask(const wxBitmap& bitmap)
    : m_maskBitmap(NULL)
{
    Create(bitmap);
}

wxMask::~wxMask()
{
    if ( m_maskBitmap )
    {
        wxMacDestroyGWorld(  (GWorldPtr) m_maskBitmap ) ;
        m_maskBitmap = NULL ;
    }
}

// Create a mask from a mono bitmap (copies the bitmap).
bool wxMask::Create(const wxBitmap& bitmap)
{
   if ( m_maskBitmap )
   {
       wxMacDestroyGWorld(  (GWorldPtr) m_maskBitmap ) ;
       m_maskBitmap = NULL ;
   }
   wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
                wxT("Cannot create mask from this bitmap type (TODO)"));
   // other types would require a temporary bitmap. not yet implemented

   wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap"));

   m_depth = bitmap.GetDepth() ;
   m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), bitmap.GetDepth() );
   Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() };

   LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
   LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
   CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()),
            GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap),
            &rect, &rect, srcCopy, 0);
   UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
   UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );

   return false;
}

// Create a mask from a bitmap and a palette index indicating
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
{
    // TODO
    wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
    return false;
}

// Create a mask from a bitmap and a colour indicating
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
{
    if ( m_maskBitmap )
    {
        wxMacDestroyGWorld(  (GWorldPtr) m_maskBitmap ) ;
        m_maskBitmap = NULL ;
    }
    wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
                 wxT("Cannot create mask from this bitmap type (TODO)"));
    // other types would require a temporary bitmap. not yet implemented

    wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));

    m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
    m_depth = 1 ;
    LockPixels( GetGWorldPixMap(  (GWorldPtr) m_maskBitmap ) );
    LockPixels( GetGWorldPixMap(  (GWorldPtr) bitmap.GetHBITMAP() ) );
    RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel());

    // this is not very efficient, but I can't think
    // of a better way of doing it
    CGrafPtr    origPort ;
    GDHandle    origDevice ;
    RGBColor  col;
    RGBColor  colors[2] = {
        { 0xFFFF, 0xFFFF, 0xFFFF },
        { 0,      0,      0 }};

    GetGWorld( &origPort , &origDevice ) ;
    for (int w = 0; w < bitmap.GetWidth(); w++)
    {
        for (int h = 0; h < bitmap.GetHeight(); h++)
        {
            SetGWorld(  (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ;
            GetCPixel( w , h , &col ) ;
            SetGWorld(  (GWorldPtr) m_maskBitmap , NULL ) ;
            if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
            {
                SetCPixel( w , h , &colors[0] ) ;
            }
            else
            {
                SetCPixel( w , h , &colors[1] ) ;
            }
        }
    }
    UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
    UnlockPixels( GetGWorldPixMap(  (GWorldPtr) bitmap.GetHBITMAP() ) ) ;
    SetGWorld( origPort , origDevice ) ;

    return true;
}

bool wxMask::PointMasked(int x, int y)
{
   GWorldPtr origPort;
   GDHandle  origDevice;
   RGBColor  color;
   bool masked = true;

   GetGWorld( &origPort, &origDevice);

   //Set port to mask and see if it masked (1) or not ( 0 )
   SetGWorld( (GWorldPtr) m_maskBitmap, NULL);
   LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
   GetCPixel(x,y, &color);
   masked = !(color.red == 0 && color.green == 0 && color.blue == 0);
   UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));

   SetGWorld( origPort, origDevice);

   return masked;
}

/*
 * wxBitmapHandler
 */

wxBitmapHandler::~wxBitmapHandler()
{
}

bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
{
    return false;
}

bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
        int desiredWidth, int desiredHeight)
{
    return false;
}

bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
{
    return false;
}

/*
 * Standard handlers
 */

class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
{
    DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
public:
    inline wxPICTResourceHandler()
    {
        m_name = wxT("Macintosh Pict resource");
        m_extension = wxEmptyString;
        m_type = wxBITMAP_TYPE_PICT_RESOURCE;
    };

    virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
          int desiredWidth, int desiredHeight);
};
IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)

bool  wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
          int desiredWidth, int desiredHeight)
{
    Str255 theName ;
    wxMacStringToPascal( name , theName ) ;

    PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
    if ( thePict )
    {
        PictInfo theInfo ;

        GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
        DetachResource( (Handle) thePict ) ;
        M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
        M_BITMAPHANDLERDATA->m_hPict = thePict ;
        M_BITMAPHANDLERDATA->m_width =  theInfo.sourceRect.right - theInfo.sourceRect.left ;
        M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;

        M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
        M_BITMAPHANDLERDATA->m_ok = true ;
        M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
//      M_BITMAPHANDLERDATA->m_bitmapPalette;
//      M_BITMAPHANDLERDATA->m_quality;
        return true ;
    }
    return false ;
}

void wxBitmap::InitStandardHandlers()
{
    AddHandler(new wxPICTResourceHandler) ;
    AddHandler(new wxICONResourceHandler) ;
}

// ----------------------------------------------------------------------------
// raw bitmap access support
// ----------------------------------------------------------------------------

void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
{
    if ( !Ok() )
    {
        // no bitmap, no data (raw or otherwise)
        return NULL;
    }

   if ( M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld )
   {
       wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") );

       return NULL;
   }

   GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
   PixMapHandle hPixMap = GetGWorldPixMap(gworld);
   wxCHECK_MSG( hPixMap && *hPixMap, NULL,
                    _T("GetRawData(): failed to get PixMap from GWorld?") );

   wxCHECK_MSG( (*hPixMap)->pixelSize == bpp, NULL,
                    _T("GetRawData(): pixel format mismatch") );

   if ( !LockPixels(hPixMap) )
   {
       wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") );

       return NULL;
   }

   data.m_width = GetWidth();
   data.m_height = GetHeight();
   data.m_stride = (*hPixMap)->rowBytes & 0x7fff;

   M_BITMAPDATA->m_hasAlpha = false;

   return GetPixBaseAddr(hPixMap);
}

void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
{
    if ( !Ok() )
        return;

    if ( M_BITMAPDATA->m_hasAlpha )
    {
        wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;

        int w = data.GetWidth(),
            h = data.GetHeight();

        wxBitmap bmpMask(GetWidth(), GetHeight(), 32);
        wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h));
        wxAlphaPixelData::Iterator pMask(dataMask),
                                   p(data);
        for ( int y = 0; y < h; y++ )
        {
            wxAlphaPixelData::Iterator rowStartMask = pMask,
                                       rowStart = p;

            for ( int x = 0; x < w; x++ )
            {
                const wxAlphaPixelData::Iterator::ChannelType
                    alpha = p.Alpha();

                pMask.Red() = alpha;
                pMask.Green() = alpha;
                pMask.Blue() = alpha;

                ++p;
                ++pMask;
            }

            p = rowStart;
            p.OffsetY(data, 1);

            pMask = rowStartMask;
            pMask.OffsetY(dataMask, 1);
        }

        SetMask(new wxMask(bmpMask));
    }

    GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
    PixMapHandle hPixMap = GetGWorldPixMap(gworld);
    if ( hPixMap )
    {
        UnlockPixels(hPixMap);
    }
}

void wxBitmap::UseAlpha()
{
    // remember that we are using alpha channel, we'll need to create a proper
    // mask in UngetRawData()
    M_BITMAPDATA->m_hasAlpha = true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -