PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Bitmap Resource in a LIB

I have built a static lib of my RaptureEngine. Within the project I have a resource file which contains a watermark image, so when the library is built this image is supposed to be embedded within it. I then load this bitmap onto a surface and draw it as needed.

The only problem is that CreateSurface() can't locate the bitmap resource. I'm not 100% positive that the resource is even being included in the library even though VC says it is compiling the resources. Has anyone tried doing this before? I did a Google search and only came up with one thread pertaining to the problem, but it had no resolution. Is there any way to make this work?
Well, this of course isn't the answer your looking for, but thinking in work-around mode...

Could you store your bitmap in an array and during initialization plot it onto a surface? Then just use that surface as you had previously intended to use your watermark surface?

If it's a 256-color image, your just talking about storing an int for each pixel referencing an entry in another array, your palette. Then just plot each point and off you go. A slight delay at startup, but nothing severe. Shouldn't take up much more memory than incorporating a bitmap anyway, even including the extra little bit of code.

Like I said, not the "right" answer, but it might do the trick. As an added bonus, it should make it slightly harder for someone nefarious to change or remove your watermark.
I'm pretty sure that you can't include in a .lib a resource file. All the resources are related with exe and dll file format so simply it can't be included in a .lib.

As fzammetti have said you can convert a bitmap in an array and then load it with the
CreateSurface(DWORD dwFlags, BYTE* pImageFileMem, DWORD dwImageFileSize);
passing the array pointer as the pImageFileMem.
For the conversion you can use a tool that I have used thousand of times when I have programmed the gba http://www.devrs.com/gb/files/b2x.zip
With this little app you can convert a file to a C statements. You can convert directly a .png (or the graph format you want) in a static C array and include it on your .lib. Then you can load it with the CreateSurface... oh, I have written this part before :)
Actually, I thought of that as an option. I was hoping to keep things simple and find a solution to why the bitmap resource doesn't appear to be included in the lib, but if I can't resolve it pretty quickly I'll probably fall back to this idea.

I noticed one of the CreateSurface interfaces took a BYTE * to the image data. I may explore that avenue further.

edit: Thanks Frank and Thomas!
Thomas: That's a pretty slick little program! Do you know if any data needs to be stripped out of the resulting array to work with CreateSurface?
No, you can use it as it is with CreateSurface. I love that little program :D
I didn't have much luck the first time around. The image was just a huge black block. Do I need to specify to generate 16 bit values?
Never mind. I figured it out =)

It appears to work perfectly!
Now we are all waiting for the release of your Rapture Engine :D
Thanks to Frank and Thomas for helping me solve this problem. Here's a little tutorial (which I'll also post on the ppcstudios forums tomorrow) on how to embed an image in your code.

In the case of the RaptureEngine, I needed to embed a watermark image to be drawn within the viewport on evaluation versions of the API. Since this is a static lib, I can't just include it as a resource so it needs to be put directly into the code.

The first thing to do is download the B2X utility from http://www.devrs.com/gb/files/b2x.zip. This is a DOS application, so you'll need to run it out of a DOS window. Extract the EXE and readme file into a folder called b2x.

Next, copy the PNG image you want to use into the b2x folder. Now, in the DOS window, go to the b2x folder and type:

Code:
b2x -c -u -t BYTE -n image < yourimage.png > dump.cpp


This will create the file dump.cpp containing all of the image data in a one dimensional array, looking something like this:

Code:

BYTE image [5488] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,
0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
0x00,0x00,0x00,0x5e,0x00,0x00,0x00,0x32,
0x08,0x02,0x00,0x00,0x00,0x5a,0x86,0xfb,
...
...
...
0xdc,0xd3,0x03,0x11,0x61,0x1c,0xdd,0x2a,
0x86,0x0b,0x00,0x00,0x14,0x2a,0x66,0x9f,
0x93,0x20,0xc3,0x1b,0x00,0x00,0x00,0x00,
0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
};


Copy this entire array and paste it at the top of your source code file where you want to use the image, and delete the size of the array (remember the size though, you'll need it later!) leaving just empty braces [ ]. Now you have your image data embedded within your application.

Next you'll need to create your surface. In this case, we pass the array image as the pointer to the data, and pass the size of the array (5488). We also set the transparency for the image.

Code:

hr = m_cWatermark.CreateSurface(0, image, 5488); if(hr != GD_OK) return hr;
hr = m_cWatermark.SetColorKey(__MAGENTA); if(hr != GD_OK) return hr;


Now, you simply Blt as you would any other image!
gersen wrote:
Now we are all waiting for the release of your Rapture Engine :D


All I need to do is write some documentation up and put the evaluation version out for everyone try. Thanks again! =)
I would also like to thank everyone for this little tip.. :)
Johan wrote:
I would also like to thank everyone for this little tip.. :)


Now if GapiDraw only had a logo image... hint, hint :wink:
I'd just like to point out that there is an image format popular with the X Window System called xpixmap, which is actually a text file composed of C statements describing the image.

So this little trick has been around for a while. It's quite useful.

Sometimes, if I want to do some sort of ad-hoc image processing, I save the file as an xpixmap and run a little awk script over it (maybe to count pixels of a particular colour, or convert the image to a table to import into Excel, or whatever).
I'm assuming this is aimed at pocketFrog - since pocketHAL uses 16bits and the surface.create() uses different parameters.

how can I adjust the tutorial for use with pHAL ?

thanks!!
Actually, it was aimed at GapiDraw. I'm not familiar with pHAL at all, so I wouldn't know where to start. You might reference this thread in the PocketFrog forum as I'm sure Thierry might be able to lend a hand. =)