How to manipulate in-memory bitmap? | developer.brewmp.com How to manipulate in-memory bitmap? | developer.brewmp.com

Developer

How to manipulate in-memory bitmap?

Forums:

Hello,

There's something I don't understand in the developer FAQ, in
the section [Media - Display and Images] ->
[d. How do I create an IIMAGE object from an in-memory bitmap?]

Could someone explain me what "pMe->pBmpData" is, and how it
could be created?

I replaced the IMemStream by an IFile (by opening my BMP file),
and the sample code worked well.
So my guess is, this pBmpData is an array of bytes containing the
bytes of a .BMP file (header, info, palette, data)...

Well, is there a way to get this array from a BMP file, and to
manipulate easily the pixels inside the array?

Thanks in advance ^_^

Quote:
d. How do I create an IIMAGE object from an in-memory bitmap?
1. Create IMemAStream for the in-memory bitmap (you can also use ISource/ISourceutil interfaces).
2. Create IImage interface, and set the stream using SetStream function of IImage interface.
Now you can use IImage functions to manipulate your in-memory bitmap.
Quote:
Could someone explain me what "pMe->pBmpData" is, and how it
could be created?
Are you talking about IDIB interface?
Quote:
Well, is there a way to get this array from a BMP file, and to
manipulate easily the pixels inside the array?
IDIB structre's pBmp pointer lets you access bitmap at pixel level. You get IDIB pointer from your IBitmap. Please note that many bitmap doesn't support IDIB interface, in that case you cann't get pBmp pointer.
ruben

Quote:
d. How do I create an IIMAGE object from an in-memory bitmap?
1. Create IMemAStream for the in-memory bitmap (you can also use ISource/ISourceutil interfaces).
2. Create IImage interface, and set the stream using SetStream function of IImage interface.
Now you can use IImage functions to manipulate your in-memory bitmap.
Quote:
Could someone explain me what "pMe->pBmpData" is, and how it
could be created?
Are you talking about IDIB interface?
Quote:
Well, is there a way to get this array from a BMP file, and to
manipulate easily the pixels inside the array?
IDIB structre's pBmp pointer lets you access bitmap at pixel level. You get IDIB pointer from your IBitmap. Please note that many bitmap doesn't support IDIB interface, in that case you cann't get pBmp pointer.
ruben

Thanks Ruben for answering :)
But I think I didn't describe well my problem...
So I'll explain the part of the project I'm working at, currently.
1- I have a .BMP file which contains tiled icons (16x16 icons).
These are human faces parts : eyes, hairs, mouths, face forms....
2-With a number, I have to combined these icons to make a whole
complete face icon.
3-Then this face icon will be used inside an IMenuCtl.
(3) involves the fact that I have to use an IImage
(2): I thought I could draw and combine the icons inside an
off screen buffer.... an IBitmap? :/
But I don't know how to do (3)...
Is there a solution, inside Brew Api :(
Now, I'm thinking of opening the bmp file, copying all its data
inside a byte array.... and then copying the data part that
interests me inside another byte array (with bmp header, info
header, palette...)... doing pixels manipulation inside it... and
then doing the IMemAStream technique....
Well, it seems complicated... and I wonder if there's no easier
solution...
thanks :'(

Thanks Ruben for answering :)
But I think I didn't describe well my problem...
So I'll explain the part of the project I'm working at, currently.
1- I have a .BMP file which contains tiled icons (16x16 icons).
These are human faces parts : eyes, hairs, mouths, face forms....
2-With a number, I have to combined these icons to make a whole
complete face icon.
3-Then this face icon will be used inside an IMenuCtl.
(3) involves the fact that I have to use an IImage
(2): I thought I could draw and combine the icons inside an
off screen buffer.... an IBitmap? :/
But I don't know how to do (3)...
Is there a solution, inside Brew Api :(
Now, I'm thinking of opening the bmp file, copying all its data
inside a byte array.... and then copying the data part that
interests me inside another byte array (with bmp header, info
header, palette...)... doing pixels manipulation inside it... and
then doing the IMemAStream technique....
Well, it seems complicated... and I wonder if there's no easier
solution...
thanks :'(

Can't you simply draw each part separately each screen update? It's slower, but it does same memory and if you have to animate it'll get you some more freedom.

Can't you simply draw each part separately each screen update? It's slower, but it does same memory and if you have to animate it'll get you some more freedom.

Yes, of course, I tried to draw the icons and combine them on the
screen...
But then, how can I get the data on the screen and convert it
into an IImage?
Because I need to add the icon in a IMenuCtl item (which only
accepts an IImage)

Yes, of course, I tried to draw the icons and combine them on the
screen...
But then, how can I get the data on the screen and convert it
into an IImage?
Because I need to add the icon in a IMenuCtl item (which only
accepts an IImage)

You could write your own implementation of IImage and pass that to IMenuCtl. Your implementation could either compose the image to the screen every time IIMAGE_Draw() is called, or you could compose it once to an off screen bitmap during contruction, and then just blit that off screen bitmap to the screen in IIMAGE_Draw().

You could write your own implementation of IImage and pass that to IMenuCtl. Your implementation could either compose the image to the screen every time IIMAGE_Draw() is called, or you could compose it once to an off screen bitmap during contruction, and then just blit that off screen bitmap to the screen in IIMAGE_Draw().

Thanks Markb,
I think it would be a good idea :) but I wonder how to do my own implementation of IImage....
I've just tried to do it, just like in the Brew MediaPlayer given example (using VTBL(IImage), DECLARE_VTBL, and so on). But it doesn't work :/ Maybe I didn't do it well, but given to Brew Api doc, it can't do that in C++...
Yes, the whole project is done in C++ :/
What would be nice would be to make a derivated class from IImage... and its method would be called by IMenuCtl...
Ahhhhh!!! :'(

Thanks Markb,
I think it would be a good idea :) but I wonder how to do my own implementation of IImage....
I've just tried to do it, just like in the Brew MediaPlayer given example (using VTBL(IImage), DECLARE_VTBL, and so on). But it doesn't work :/ Maybe I didn't do it well, but given to Brew Api doc, it can't do that in C++...
Yes, the whole project is done in C++ :/
What would be nice would be to make a derivated class from IImage... and its method would be called by IMenuCtl...
Ahhhhh!!! :'(

Hard to say what the problem is based on your description. Can you provide more details? Take a look at AEEAppGen.c for an example of implementing an interface. (IApplet, in this case.)

Hard to say what the problem is based on your description. Can you provide more details? Take a look at AEEAppGen.c for an example of implementing an interface. (IApplet, in this case.)

might just be easier to write your own menu control widget.

might just be easier to write your own menu control widget.

Yeah, I thought so, that it would easier to make my own IMenuCtl. But the icons that I create may be use in other controls (static...). So my 'chiefs' considered it would be a lost of time, re-writing everything and that it would be better to use the "look and feel" of brew controls that are already made.
Well, I manage to solve my problem this way:
- opening the .bmp file (containing all the icons) with an IFileMgr, reading it with the IFile, storing bytes in a buffer of the bmp file size (36ko :/... )
- creating a buffer of sizeof (bmp header + bmp info header + 256colors palette + icon pixels nb)
- working from the big buffer, to the little one... via lots of modulo and divide (:()
- then doing just like in the Brew faq: initializing an IImage with setstream() and my icon buffer. It just avoids me to recreate a file with my icon buffer and open it with ishell_loadimage :/
-----
It was a pain :/ but it works fine. That's what I'll use, until I experiment more brew -_-;
I tried to optimize it as much as I could (because I may have to display many generated icons).

Yeah, I thought so, that it would easier to make my own IMenuCtl. But the icons that I create may be use in other controls (static...). So my 'chiefs' considered it would be a lost of time, re-writing everything and that it would be better to use the "look and feel" of brew controls that are already made.
Well, I manage to solve my problem this way:
- opening the .bmp file (containing all the icons) with an IFileMgr, reading it with the IFile, storing bytes in a buffer of the bmp file size (36ko :/... )
- creating a buffer of sizeof (bmp header + bmp info header + 256colors palette + icon pixels nb)
- working from the big buffer, to the little one... via lots of modulo and divide (:()
- then doing just like in the Brew faq: initializing an IImage with setstream() and my icon buffer. It just avoids me to recreate a file with my icon buffer and open it with ishell_loadimage :/
-----
It was a pain :/ but it works fine. That's what I'll use, until I experiment more brew -_-;
I tried to optimize it as much as I could (because I may have to display many generated icons).

Ok, though if you have a lot of bmp files you are reading from, you will probably want to load the bitmaps from a resource file..
(I think there is something like a 7 file limit for TRUE BREW Testing anyway, someone correct me if i am wroing)
Here is how to get at the raw bitmap data from a resource:
AEEBmp* bmp;
byte * pBmpData;
bmp = (AEEBmp*)ISHELL_LoadResData(pApp->pShell, "resdata.bar", IMG_NUMBER, RESTYPE_IMAGE);
pBmpData=(byte *)(bmp) + *((byte *)(bmp));
// access the raw bmp data starting from pBmpData
// free the data when you are done.
ISHELL_FreeResData(pApp->pShell, bmp);
-Tyndal

Ok, though if you have a lot of bmp files you are reading from, you will probably want to load the bitmaps from a resource file..
(I think there is something like a 7 file limit for TRUE BREW Testing anyway, someone correct me if i am wroing)
Here is how to get at the raw bitmap data from a resource:
AEEBmp* bmp;
byte * pBmpData;
bmp = (AEEBmp*)ISHELL_LoadResData(pApp->pShell, "resdata.bar", IMG_NUMBER, RESTYPE_IMAGE);
pBmpData=(byte *)(bmp) + *((byte *)(bmp));
// access the raw bmp data starting from pBmpData
// free the data when you are done.
ISHELL_FreeResData(pApp->pShell, bmp);
-Tyndal

Thanks tyndal ^_^
Getting the file data with ISHELL_LoadResData is more elegant than using IFile_Read.
I did a test : using ISHELL_LoadResData, creating a IMemAStream with the data, creating an IImage with the IMemAStream, and displaying the image.
It works well, ...if I don't release the IMemAStream :/
The application "failed to free all memory", though.
If I release IMemAStream, the emulator crashes.
Maybe I should copy the data (starting from pBmpData) in
another buffer (sizeof (myfile)) and then give it to IMemAstream...which will release the buffer well...
But then, it will be just like before (-_-)

Thanks tyndal ^_^
Getting the file data with ISHELL_LoadResData is more elegant than using IFile_Read.
I did a test : using ISHELL_LoadResData, creating a IMemAStream with the data, creating an IImage with the IMemAStream, and displaying the image.
It works well, ...if I don't release the IMemAStream :/
The application "failed to free all memory", though.
If I release IMemAStream, the emulator crashes.
Maybe I should copy the data (starting from pBmpData) in
another buffer (sizeof (myfile)) and then give it to IMemAstream...which will release the buffer well...
But then, it will be just like before (-_-)

depeding on what you are doing with your data, look into the bSysMem parameter of IMEMASTREAM_Set(), or write your own free function for IMEMASTREAM_SetEx() that does nothing.
-Tyndal

depeding on what you are doing with your data, look into the bSysMem parameter of IMEMASTREAM_Set(), or write your own free function for IMEMASTREAM_SetEx() that does nothing.
-Tyndal