wxWidgets Logo

Docs/technote

Tutorials
FAQ
Licence

How to create bitmaps from data



Sender: chris@hel.co.uk
Date: Thu, 05 Jun 1997 10:34:52 +0100
From: Chris Breeze 
To: guillaume HELLE 
Cc: wxWidgets Users 
Subject: Re: Pb with wxBitmap

guillaume HELLE wrote:
> 
> In fact i have the same crash under Win95 VC4.2 and Motif1.2 gcc 2.7.2
> 
> Under Motif i have this X message :
> 
> X Error of failed request:  BadMatch (invalid parameter attributes)
>   Major opcode of failed request:  62 (X_CopyArea)
>   Serial number of failed request:  1500
>   Current serial number in output stream:  1674
Looking at the wx source code (src/x/wx_gdi.cxx) it seems that only
wxBitmaps of depth 1 can be created from data. If you set the depth
to some other value it still constructs a 1-bit deep Pixmap.
X11 allows you to blit pixmaps of the same depth using XCopyArea or
copy individual planes (in the gc's foreground and background colours)
using XCopyPlane. When you try to create a wxBimap from data with a
depth other than 1 the code gets confused and tries to use XCopyArea to
copy a 1-bit pixmap onto your (probably) 8-bit screen and generates the
error message above.

> I don't remenber it under Win95 but i got a wx error message before crash.
>
> How i can get the good deepth of the device context ?
You can get the depth of the screen using ::wxDisplayDepth() and
then create a wxBitmap of the same depth.

> My real problem is to initialize wxBitmap (or wxImage) with raw data...
> A solution ?
One possible solution is to construct a wxBitmap of the required size
and then initialise it afterwards.
e.g.

    int height = 50, width = 50, depth = ::wxDisplayDepth();
#ifdef wx_msw
    char* img = new char[width * height];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            img[i * width + j] = (i + j) % 256;
        }
    }
    bitmap_ = new wxBitmap(img, height, width, depth);
#else
    bitmap_ = new wxBitmap(height, width, depth);

    Display *dpy = wxGetDisplay();
    GC gc = XCreateGC(dpy, bitmap_->x_pixmap, 0, 0);
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            XSetForeground(dpy, gc, (i + j) % 256);
            XDrawPoint(dpy, bitmap_->x_pixmap, gc, i, j);
        }
    }
    XFreeGC(dpy, gc);
#endif

It is slow and clumsy, but it works on Win95 and Motif 1.2.
Accessing wxBitmap's x_pixmap member variable is not a good idea
as it breaks wxBitmap's encapsulation and may not work in future
versions of wx.
You may also have problems with colours since you are writing
physical values into the wxBitmap. The actual colour that appears
on the screen will depend upon the colour map.

Regards,
Chris
-- 
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
/ Chris Breeze, Software Engineer             mailto:chris@hel.co.uk /
/ Hitachi Europe Limited, Maidenhead, England                        /
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/


 
 

Quick Links

 
  Tutorials
Mailing Lists
Latest Docs

Community

wxForum
wxCommunity
Applications
Add-ons
wxWiki
 
 
 
 

wx Solutions

 
   
 
 
 

wxWidgets Book

 
 

Buy it From:
Amazon.com
Amazon.co.uk

 
 
 
 

Donate Now

 
 
 
 
SourceForge.net Logo

Site design and update scripts by Kevin Ollivier, with special thanks to Brad Anderson for his improvements to the sidebar, intro table and navbar designs, Bryan Petty for the new wxWidgets blocks graphics and logo text, and to the wxWidgets community for all their helpful suggestions, comments and testing!