Problem with bitmap

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
Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Problem with bitmap

Post by Eman » Thu Oct 08, 2020 7:50 pm

Hello
I have a 2d array of int:
int **grid;
grid = new int*[row];
for(int i=0 ; i<row ; i++)
grid = new int[col];
for(int i=0; i<row ; i++)
for(int j=0; j<col ;j++)
grid[j] = 0; // free cells = white

grid [0][0] = 1 ; // upper-left corner: blocked cell = black
grid [0][col-1] = 4 ; // upper-right: start cell = red
grid [row-1][0] = 5; // lower-left corner: goal cell = green
grid [row-1][col-1] = 6; // lower-right: path cell = blue

but, when filling wxBitmap with this array, the upper-right and lower-left are swapped, so that:
the upper-right is green, and the lower-right is red.

The complete code in this folder:
Attachments
BitmapApp.zip
Folder containing app files
(3.02 KiB) Downloaded 20 times

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

Re: Problem with bitmap

Post by doublemax » Thu Oct 08, 2020 10:38 pm

Code: Select all

for(int i=0; i<row ; i++)
  for(int j=0; j<col ;j++)
    if(grid[i][j] == 1) // blocked = black
      gp->SetCellColor(i,j,black);
In this code "j" is "x" and "i" is "y". But SetCellColor() takes x first, then y. So just swap the parameters
Use the source, Luke!

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Problem with bitmap

Post by Eman » Fri Oct 09, 2020 1:30 am

Thanx
This means that the bitmap coordination differs from 2d array

I used the same loop to color the cells of wxGrid, and it works.

I will try to used 2d array of wxBitmap

Code: Select all

    wxBitmap **array_of_bitmap;
	array_of_bitmap = new wxBitmap*[numrows];
	for(int i=0 ; i<numrows ; i++)
	{
	  array_of_bitmap[i] = new wxBitmap[numcols];
	}

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Problem with bitmap

Post by Eman » Fri Oct 09, 2020 2:05 am

doublemax wrote:
Thu Oct 08, 2020 10:38 pm

Code: Select all

for(int i=0; i<row ; i++)
  for(int j=0; j<col ;j++)
    if(grid[i][j] == 1) // blocked = black
      gp->SetCellColor(i,j,black);
In this code "j" is "x" and "i" is "y". But SetCellColor() takes x first, then y. So just swap the parameters
is there any way to deal with coordinates in bitmap the same way as if it is array or table

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

Re: Problem with bitmap

Post by doublemax » Fri Oct 09, 2020 7:27 am

is there any way to deal with coordinates in bitmap the same way as if it is array or table
I don't see where the problem is. Just consider the gridpanel class a black box where the method happens to take the parameters in x,y order. But you can of course change that yourself easily if you like.
Use the source, Luke!

Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 251
Joined: Tue Jun 07, 2016 1:07 pm

Re: Problem with bitmap

Post by Kvaz1r » Fri Oct 09, 2020 7:28 am

What exactly are you trying to do with bitmap?
From the documentation:
If you need direct access the bitmap data instead going through drawing to it using wxMemoryDC you need to use the wxPixelData class (either wxNativePixelData for RGB bitmaps or wxAlphaPixelData for bitmaps with an additionally alpha channel).
And example (also from the documentation):

Code: Select all

wxBitmap bmp;
wxNativePixelData data(bmp);
if ( !data )
{
    // ... raw access to bitmap data unavailable, do something else ...
    return;
}
if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
{
    // ... complain: the bitmap it too small ...
    return;
}
wxNativePixelData::Iterator p(data);
// we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
p.Offset(data, 10, 10);
for ( int y = 0; y < 10; ++y )
{
    wxNativePixelData::Iterator rowStart = p;
    for ( int x = 0; x < 10; ++x, ++p )
    {
        p.Red() = r;
        p.Green() = g;
        p.Blue() = b;
    }
    p = rowStart;
    p.OffsetY(data, 1);
}

Post Reply