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

📄 bitmap.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                int mask = 1 << bit ;
                if ( linestart[index] & mask )
                {
                    SetCPixel( x , y , &colors[1] ) ;
                }
                else
                {
                    SetCPixel( x , y , &colors[0] ) ;
                }
            }
        }
        UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;

        SetGWorld( origPort , origDevice ) ;
    }
    else
    {
        wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
    }
}

wxBitmap::wxBitmap(int w, int h, int d)
{
    (void)Create(w, h, d);
}

wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
{
    (void) Create(data, type, width, height, depth);
}

wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
{
    LoadFile(filename, type);
}

bool wxBitmap::CreateFromXpm(const char **bits)
{
    wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
    wxXPMDecoder decoder;
    wxImage img = decoder.ReadData(bits);
    wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
    *this = wxBitmap(img);
    return true;
}

wxBitmap::wxBitmap(const char **bits)
{
    (void) CreateFromXpm(bits);
}

wxBitmap::wxBitmap(char **bits)
{
    (void) CreateFromXpm((const char **)bits);
}

wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
{
   wxCHECK_MSG( Ok() &&
                (rect.x >= 0) && (rect.y >= 0) &&
                (rect.x+rect.width <= GetWidth()) &&
                (rect.y+rect.height <= GetHeight()),
                wxNullBitmap, wxT("invalid bitmap or bitmap region") );


   wxBitmap ret( rect.width, rect.height, GetDepth() );
   wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );

   GWorldPtr origPort;
   GDHandle  origDevice;

   GetGWorld( &origPort, &origDevice );

   // Update the subbitmaps reference data
   wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData();

   ref->m_numColors     = M_BITMAPDATA->m_numColors;
#if wxUSE_PALETTE
    ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette;
#endif // wxUSE_PALETTE
   ref->m_bitmapType    = M_BITMAPDATA->m_bitmapType;

   // Copy sub region of this bitmap
   if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict)
   {
       printf("GetSubBitmap:  Copy a region of a Pict structure - TODO\n");
   }
   else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld)
   {
       // Copy mask
       if(GetMask())
       {
           GWorldPtr submask, mask;
           RGBColor  color;

           mask = (GWorldPtr) GetMask()->GetMaskBitmap();
           submask = wxMacCreateGWorld(rect.width, rect.height, GetMask()->GetDepth() );
           LockPixels(GetGWorldPixMap(mask));
           LockPixels(GetGWorldPixMap(submask));

           for(int yy = 0; yy < rect.height; yy++)
           {
               for(int xx = 0; xx < rect.width; xx++)
               {
                   SetGWorld(mask, NULL);
                   GetCPixel(rect.x + xx, rect.y + yy, &color);
                   SetGWorld(submask, NULL);
                   SetCPixel(xx,yy, &color);
               }
           }
           UnlockPixels(GetGWorldPixMap(mask));
           UnlockPixels(GetGWorldPixMap(submask));
           ref->m_bitmapMask = new wxMask;
           ref->m_bitmapMask->SetMaskBitmap(submask);
       }

       // Copy bitmap
       if(GetHBITMAP())
       {
           GWorldPtr subbitmap, bitmap;
           RGBColor  color;

           bitmap = (GWorldPtr) GetHBITMAP();
           subbitmap = (GWorldPtr) ref->m_hBitmap ;
           LockPixels(GetGWorldPixMap(bitmap));
           LockPixels(GetGWorldPixMap(subbitmap));

           for(int yy = 0; yy < rect.height; yy++)
           {
               for(int xx = 0; xx < rect.width; xx++)
               {
                   SetGWorld(bitmap, NULL);
                   GetCPixel(rect.x + xx, rect.y + yy, &color);
                   SetGWorld(subbitmap, NULL);
                   SetCPixel(xx, yy, &color);
               }
           }
           UnlockPixels(GetGWorldPixMap(bitmap));
           UnlockPixels(GetGWorldPixMap(subbitmap));
       }
   }
   SetGWorld( origPort, origDevice );

   return ret;
}

bool wxBitmap::Create(int w, int h, int d)
{
    UnRef();

    m_refData = new wxBitmapRefData;

    M_BITMAPDATA->m_width = w;
    M_BITMAPDATA->m_height = h;
    M_BITMAPDATA->m_depth = d;

    M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
    M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
    M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
    return M_BITMAPDATA->m_ok;
}

int wxBitmap::GetBitmapType() const
{
   wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") );

   return M_BITMAPDATA->m_bitmapType;
}

void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;
    else
        DisposeBitmapRefData( M_BITMAPDATA ) ;

    M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
    M_BITMAPDATA->m_hBitmap = bmp ;
    M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
}

void wxBitmap::SetHICON(WXHICON ico)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;
    else
        DisposeBitmapRefData( M_BITMAPDATA ) ;

    M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ;
    M_BITMAPDATA->m_hIcon = ico ;
    M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ;
}

void wxBitmap::SetPict(WXHMETAFILE pict)
{
    if (!M_BITMAPDATA)
        m_refData = new wxBitmapRefData;
    else
        DisposeBitmapRefData( M_BITMAPDATA ) ;

    M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ;
    M_BITMAPDATA->m_hPict = pict ;
    M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ;
}

bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
{
    UnRef();

    wxBitmapHandler *handler = FindHandler(type);

    if ( handler )
    {
        m_refData = new wxBitmapRefData;

        return handler->LoadFile(this, filename, type, -1, -1);
    }
    else
    {
        wxImage loadimage(filename, type);
        if (loadimage.Ok()) {
            *this = loadimage;
            return true;
        }
    }
    wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
    return false;
}

bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
{
    UnRef();

    m_refData = new wxBitmapRefData;

    wxBitmapHandler *handler = FindHandler(type);

    if ( handler == NULL ) {
        wxLogWarning(wxT("no bitmap handler for type %d defined."), type);

        return false;
    }

    return handler->Create(this, data, type, width, height, depth);
}

wxBitmap::wxBitmap(const wxImage& image, int depth)
{
    wxCHECK_RET( image.Ok(), wxT("invalid image") );
    wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") );

    m_refData = new wxBitmapRefData();

    // width and height of the device-dependent bitmap
    int width = image.GetWidth();
    int height = image.GetHeight();

    // Create picture

    Create( width , height , 32 ) ;

    CGrafPtr origPort ;
    GDHandle origDevice ;

    PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ;
    LockPixels( pixMap );

    GetGWorld( &origPort , &origDevice ) ;
    SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;

    // Render image
    register unsigned char* data = image.GetData();
    char* destinationBase = GetPixBaseAddr( pixMap );
    register unsigned char* destination = (unsigned char*) destinationBase ;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            *destination++ = 0 ;
            *destination++ = *data++ ;
            *destination++ = *data++ ;
            *destination++ = *data++ ;
        }
        destinationBase += ((**pixMap).rowBytes & 0x7fff);
        destination = (unsigned char*) destinationBase ;
    }
    if ( image.HasAlpha() )
    {
      unsigned char *alpha = image.GetAlpha();

      wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
      RGBColor color ;
      wxBitmap maskBitmap ;

      maskBitmap.Create( width, height, 24);
      LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
      SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);

      for (int y = 0; y < height; y++)
      {
          for (int x = 0; x < width; x++)
          {
              memset( &color , 255 - *alpha , sizeof( color ) );
              SetCPixel(x,y, &color);

              alpha += 1 ;
          }
      }  // for height
      SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
      SetMask(new wxMask( maskBitmap ));
      UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
    }
    else if ( image.HasMask() )
    {
      data = image.GetData();

      wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
      RGBColor white = { 0xffff, 0xffff, 0xffff };
      RGBColor black = { 0     , 0     , 0      };
      wxBitmap maskBitmap ;

      maskBitmap.Create( width, height, 1);
      LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
      SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);

      for (int y = 0; y < height; y++)
      {
          for (int x = 0; x < width; x++)
          {
              if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
              {
                SetCPixel(x,y, &white);
              }
              else {
                SetCPixel(x,y, &black);
              }
              data += 3 ;
          }
      }  // for height
      SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
      SetMask(new wxMask( maskBitmap ));
      UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
    }

    UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
    SetGWorld( origPort, origDevice );
}

wxImage wxBitmap::ConvertToImage() const
{
    wxImage image;

    wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );

    // create an wxImage object
    int width = GetWidth();
    int height = GetHeight();
    image.Create( width, height );

    unsigned char *data = image.GetData();

    wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );

    GWorldPtr origPort;
    GDHandle  origDevice;
    RgnHandle maskRgn = NULL ;
    GWorldPtr tempPort = NULL ;
    int      index;
    RGBColor color;
    // background color set to RGB(16,16,16) in consistent with wxGTK
    unsigned char mask_r=16, mask_g=16, mask_b=16;
    SInt16   r,g,b;
    wxMask  *mask = GetMask();

    GetGWorld( &origPort, &origDevice );
    if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
    {
        tempPort = wxMacCreateGWorld( width , height , -1) ;
    }
    else
    {
        tempPort =  (GWorldPtr) GetHBITMAP() ;
    }
    LockPixels(GetGWorldPixMap(tempPort));
    SetGWorld( tempPort, NULL);
    if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon )
    {
        Rect bitmaprect = { 0 , 0 , height, width };
        if ( GetBitmapType() == kMacBitmapTypeIcon )
        {
            ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ;
            maskRgn = NewRgn() ;
            BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ;
        }
         else
             ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ;
    }
    // Copy data into image
    index = 0;
    for (int yy = 0; yy < height; yy++)
    {
        for (int xx = 0; xx < width; xx++)
        {
            GetCPixel(xx,yy, &color);
            r = ((color.red ) >> 8);
            g = ((color.green ) >> 8);
            b = ((color.blue ) >> 8);
            data[index    ] = r;
            data[index + 1] = g;
            data[index + 2] = b;
            if ( maskRgn )
            {
                Point pt ;
                pt.h = xx ;
                pt.v = yy ;
                if ( !PtInRgn( pt , maskRgn ) )
                {
                    data[index    ] = mask_r;
                    data[index + 1] = mask_g;
                    data[index + 2] = mask_b;
                }
            }
            else
            {
                if (mask)
                {
                    if (mask->PointMasked(xx,yy))
                    {
                        data[index    ] = mask_r;
                        data[index + 1] = mask_g;
                        data[index + 2] = mask_b;
                    }
                }
            }
            index += 3;
        }
    }
    if (mask || maskRgn )
    {
        image.SetMaskColour( mask_r, mask_g, mask_b );
        image.SetMask( true );
    }

    // Free resources
    UnlockPixels(GetGWorldPixMap( tempPort ));
    SetGWorld(origPort, origDevice);
    if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
    {
        wxMacDestroyGWorld( tempPort ) ;
    }
    if ( maskRgn )
    {
        DisposeRgn( maskRgn ) ;
    }

    return image;
}


bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
                        const wxPalette *palette) const
{
    wxBitmapHandler *handler = FindHandler(type);

    if ( handler )
    {
        return handler->SaveFile(this, filename, type, palette);
    }
    else
    {
        wxImage image = ConvertToImage();

        return image.SaveFile(filename, type);
    }

    wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
    return false;
}

bool wxBitmap::Ok() const

⌨️ 快捷键说明

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