wxImage and wxMemoryBuffer Topic is solved

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
bcteh
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Nov 27, 2006 9:56 am

wxImage and wxMemoryBuffer

Post by bcteh »

Hi,

Is that posible to save/convert wxImage to wxMemoryBuffer ?

Regards
Teh
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
That depends on what info you want to store in the buffer.

The most easy case I can think of would be to store the RGB data of the image. You could simply do something like:

Code: Select all

unsigned int byteCount = image.GetWidth()*image.Height()*3;
wxMemoryBuffer buffer( byteCount );
buffer.AppendData(image.GetData(),bytecount);
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
bcteh
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Nov 27, 2006 9:56 am

Post by bcteh »

Hi,

After convert to wxMemoryBuffer , How can i convert/restore (RGB data) back
to wxImage ?

Now, I am able to write wxMemoryBuffer back to wxImage, If i read from file by below code.

wxFile FileIn(filename);
int nDataLength = FileIn.Length();
wxMemoryBuffer BufferIn;
int nBytesRead = FileIn.Read(BufferIn.GetWriteBuf(nDataLength), nDataLength);

wxMemoryInputStream mis(BufferIn.GetData(), BufferIn.GetBufSize());
if (!GetImage().LoadFile(mis) ) {
wxLogError(wxT("Can't load BMP image from stream"));
}

But I try to load the wxMemoryBuffer by the code your provided. It return "Can't load BMP image from stream".

Regards
Teh
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
Honestly, I don't get what you want or expect to happen.

Your initial question was how to store the wxImage in a wxMemoryBuffer. Although this only stores the RGB data, I gave you a solution. Now you want to know how you can load that data??

LoadFile() won't work on this. It is simple RGB data that is stored, not data in a specific image format the LoadFile() or the wxImage handlers could parse...

So, please, explain what you want to do and what seems to be the problem.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
GeraldG
Experienced Solver
Experienced Solver
Posts: 85
Joined: Mon May 16, 2005 12:30 pm

wxImage::SetData

Post by GeraldG »

You use wxImage::GetData to store, then you should use wxImage::SetData to recreate the image.

But see docs, use malloc for creation of buffer and dont delete it (its owned by wxImage after SetData).
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

bcteh wrote:Hi,

After convert to wxMemoryBuffer , How can i convert/restore (RGB data) back
to wxImage ?

Now, I am able to write wxMemoryBuffer back to wxImage, If i read from file by below code.

wxFile FileIn(filename);
int nDataLength = FileIn.Length();
wxMemoryBuffer BufferIn;
int nBytesRead = FileIn.Read(BufferIn.GetWriteBuf(nDataLength), nDataLength);

wxMemoryInputStream mis(BufferIn.GetData(), BufferIn.GetBufSize());
if (!GetImage().LoadFile(mis) ) {
wxLogError(wxT("Can't load BMP image from stream"));
}

But I try to load the wxMemoryBuffer by the code your provided. It return "Can't load BMP image from stream".

Regards
Teh
You must use constructor not LoadFile - wxImage(mis)
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Re: wxImage and wxMemoryBuffer

Post by lester »

bcteh wrote:Hi,

Is that posible to save/convert wxImage to wxMemoryBuffer ?

Regards
Teh
for save image You must use wxImage::SaveFile(wxOutputStream& stream, int type) and wxMemoryOutputStream, from wxMemoryOutputStream You can read data and store into wxMemoryBuffer
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi,

If all you want to do is load an image, modify the pixel data, then save it, I'm not sure why you don't just do the following:

Code: Select all

 wxImage image;
wxString aFileToLoad(_T("myinfile.bmp"));
wxString aFileToSave(_T("myoutfile.bmp"));

image.LoadFile(aFileToLoad);
unsigned char *pixelData = image.GetData();

// modify the pixel data as you please...

image.SaveFile(aFileToSave);

Jim

PS. Using the code tags makes your post much easier to read and follow.
OS: Vista SP1, wxWidgets 2.8.7.
Post Reply