Using wxBitmap (Monochrome) in WxMemoryDC

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
hariprasath
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Jun 28, 2018 10:05 am

Using wxBitmap (Monochrome) in WxMemoryDC

Post by hariprasath » Thu Jun 28, 2018 2:26 pm

Hello,

I am new to wxWidgets. I am using wxMemoryDC with monochrome wxBitmap for drawing. wxWidgets is built with GTK+. Below is my goal:

1. Create Monochrome bitmap - wxBitmap *pbmp = new wxBitmap (100,100,1);
2. Map with Memory DC - wxMemoryDC *pmDC = new wxMemoryDC (*pbmp);
3. Select object - pmDC->SelectObject(*pBitmap);
4. Set pen (black) and brush (white)
5. Draw a rectangle - pmDC->DrawRectanglewxRect(50,50,50,50);
6. Read pixels data using wxNativePixelData using Getpixels().m_ptr
7. Send to device (printer)

Questions:
1. I would like reverse monochrome color: White: 1 Black:0 to White:0 Black:1 so that image background will be in black and rectangle will be in white. How to do this? In windows this can be done like below:
RGBQUAD[0].rgbBlue=0xFF; RGBQUAD[0].rgbGreen=0xFF; RGBQUAD[0].rgbRed=0xFF;
RGBQUAD[1].rgbBlue=0; RGBQUAD[1].rgbGreen=0; RGBQUAD[1].rgbRed=0;

2. I would like to get pixels data of wxBitmap. Does using wxNativePixelData::Getpixels().m_ptr is correct way? What is the best method?

Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by doublemax » Thu Jun 28, 2018 7:56 pm

Support for monochrome bitmaps is very limited in wxWidgets. And wxNativePixelData is designed for 24bit RGB data, so i doubt it will give the expected results for monochrome bitmaps.

Even if it's a waste of memory, i would suggest to work with 24bit RGB bitmaps and only at the very end convert it to a 1bit bitmap.
Use the source, Luke!

hariprasath
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Jun 28, 2018 10:05 am

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by hariprasath » Fri Jun 29, 2018 3:48 pm

Thanks for your reply.

We are using wxWidgets in embedded device which has low RAM (64 MB). And, In my case I don't want to use 24 bit bitmap because it would create memory & performance issue.

So, I would like to do
1. Memory DC with Monochrome bitmap (572*2500)
2. Should be able read bitmap pixels. I need this because, finally I will send this array of pixel data to external device for printing.

Could you tell me the possible ways to do this? I can try that.

Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by doublemax » Fri Jun 29, 2018 9:50 pm

I guess the code will be running under Linux?

I can only test under Windows, and the following code worked for me:

Code: Select all

const int width = 100;
const int height = 100;

wxBitmap b( width, height, 1 );
wxMemoryDC mdc( b );

// clear background
mdc.SetBrush( *wxWHITE );
mdc.SetPen( *wxWHITE );
mdc.DrawRectangle( 0, 0, width, height );

// draw black rectangle
mdc.SetBrush( *wxBLACK );
mdc.SetPen( *wxBLACK );
mdc.DrawRectangle( width/4, height/4, width/2, height/2 );

// invert everything
mdc.Blit( 0,0, width,height, &mdc, 0,0, wxINVERT );

mdc.SelectObject( wxNullBitmap );

b.SaveFile( "d:\\_mono_test.png", wxBITMAP_TYPE_PNG );
However, this is lacking the last part, getting the raw bitmap data. I haven't found a way to do that for 1-bit data.
Use the source, Luke!

hariprasath
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Jun 28, 2018 10:05 am

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by hariprasath » Mon Jul 02, 2018 3:34 pm

Yes, the code will be running in Linux (GTK-2).
Thanks,
Hari

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by doublemax » Mon Jul 02, 2018 4:09 pm

Code: Select all

#ifdef __WXGTK3__
    GdkPixbuf* GetPixbufNoMask() const;
    cairo_t* CairoCreate() const;
    void Draw(cairo_t* cr, int x, int y, bool useMask = true, const wxColour* fg = NULL, const wxColour* bg = NULL) const;
    void SetSourceSurface(cairo_t* cr, int x, int y, const wxColour* fg = NULL, const wxColour* bg = NULL) const;
#else
    GdkPixmap *GetPixmap() const;
    bool HasPixmap() const;
    bool HasPixbuf() const;
    wxBitmap(GdkPixmap* pixmap);
#endif
Under GTK-2 you can access the underlying GdkPixmap using GetPixmap(). I'm pretty sure you can extract the data from there. But as i don't work under Linux, i can't help any further.

If you can't figure it out yourself, please open a new thread under "platform related issues".
Use the source, Luke!

hariprasath
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Jun 28, 2018 10:05 am

Re: Using wxBitmap (Monochrome) in WxMemoryDC

Post by hariprasath » Mon Jul 02, 2018 6:15 pm

Thank you. I will check it
Thanks,
Hari

Post Reply