PocketMatrix forums

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

Image-related questions

Sorry if these are inane or were asked before, but:

1. How do you load JPEG images? I thought PF supported them, but whenever I try to load one I get a GPF.

2. How do you load image(s) from a buffer and not a resource, like for example after loading from a file using fread()?
I had a similar issue early on. I never did get a clear answer on this, but make sure your resource type is IMAGE and see if it works then. I was trying to put JPGs under another name at one point and it kept GFP'ing on me as well, as soon as I put it in IMAGE it worked just fine though.
This is how I load my graphics.

Code:

bool Map2dWnd::WindowInit()
{
using namespace Frog;
DisplayDevice* display = GetDisplay();

...

// Load Robot Icon
#if _WIN32_WCE
m_pRobotIcon = Frog::LoadImage(display,_T("\\Robot.gif"));
#else
m_pRobotIcon = Frog::LoadImage(display,_T("Robot.gif"));
#endif
if (m_pRobotIcon == 0)
{
::MessageBox(0,_T("Unable to load robot icon."),_T("2D Map Window Initialization Error:"),MB_OK);
return false;
}
m_pRobotIcon->SetColorMask(0);

...
}

If LoadImage() fails, it will return a NULL pointer so you can test it for errors. SetColorMask(COLOR) will make the color COLOR transparent.

The important thing to notice, is that WinCE doesn't use "drive letters", but instead uses an absolute path ("\"), pointing to "My Computer". This goes for all fileloading on WinCE.

There is no difference between GIF,JPG,BMP. On PPC you can also load PNG:s, but don't rely to much on alpha shading cause it won't work. Neither will transparent gifs (I've written about this in another thread here somewhere).

2. How do you load image(s) from a buffer and not a resource, like for example after loading from a file using fread()?


If you look at the Blit example, you see how to load from a resource.

There is really no reason why you should want to load the file by yourself into memory, and then decode it (or Load it) into a PF Surface. However, it could be usefull if you get streamed picture data over the network and want to display it on the screen. This is how I did it:

Code:

Surface* frame = Frog::LoadImage(m_pDisplay, (const uint8_t*)start, (const uint8_t*)end);

where start and end is two pointers, pointing to the start and end of the (binary) image data.

For more info about images, see image.h and image.cpp and also, look up imgdecmp.dll on Google.

Hope this'll help ya out.
There is really no reason why you should want to load the file by yourself into memory, and then decode it (or Load it) into a PF Surface.


Really? What about a game with a large number of sprites? Isn't it better to put that all into a file instead of attaching them to the exe as resources?
Yeah, I made the assumption that you were trying to load from a resource. My bad. I prefer to have everything internal as a resource... If I want something to be changeable by the user (i.e., mods for a game) I'll make them internal, but things I generally don't want people changing I make a resource (not that THAT stops anyone, but it does require a certian level of expertise beyond overwriting a file). I also think it's probably a little faster loading from a resource, but I could be wrong. In any case, my suggestion only applies if you are loading from a resource obviously, otherwise just ignore the silly man babbling over in this corner.
No, fzammetti, when I was talking about JPGs, I had resources in mind. Thanks for your help; I got it to work.

Oh, and thanks, adde, I got it to load from a file.

By the way, when I load from a JPG, the quality seems very bad. Is that a limitation of PF, or am I doing something wrong?
Damian wrote:
There is really no reason why you should want to load the file by yourself into memory, and then decode it (or Load it) into a PF Surface.


Really? What about a game with a large number of sprites? Isn't it better to put that all into a file instead of attaching them to the exe as resources?


What I ment was, that there is no reason why you should load the file yourself, using fread() or whatever, into memory and then call the LoadImage() on that data to create a Surface, since the LoadImage() is overloaded to take care of the file process for you. No need to write code if it's already written.

Glad it worked. I find bundeling graphics and other stuff as internal resources a tad bulky. There are only two reasons for this:
1. You are lazy and don't want to keep track of more than a single .exe file.
2. You want to prevent the end user from changing the graphics (or whatever the outsource may be).

I, however, find it most convinient to use external sources for several reasons, and here's two:

1. If I need to change some graphics, I don't have to recompile the whole program.
2. I keep the footprint of my programs down to a minimum.
Yeah, but what if you want to have one file that contains more than one image?
Damian wrote:
Yeah, but what if you want to have one file that contains more than one image?


I assume you mean that you have a large image file that contains several subimages used for sprites or whatever. Then you simply load it as ONE file into ONE Surface, and provide a source rectangle to the Blit function (more info about source and dest. rectangles can be found on other threads in this forum).

And if the images arn't related so you want to have them as separate Surfaces, then you should put them in separate files from the beginning. There is no logic in grouping unrelated images.

Ok. I have another scenario for you. To avoid having to ship several image files (that can be edited by the user) you create your own IMAGES.DAT file where you save all the image files in some format defined by you (For example, start with a special header containing the number of images and the size of each image, and then append each image data (stripping the file header)). Then you use fread to read the IMAGES.DAT file (or portions of it) into memory and use LoadImage() together with the two binary pointers as I mentioned earlier..
adde wrote:
Ok. I have another scenario for you. To avoid having to ship several image files (that can be edited by the user) you create your own IMAGES.DAT file where you save all the image files in some format defined by you (For example, start with a special header containing the number of images and the size of each image, and then append each image data (stripping the file header)). Then you use fread to read the IMAGES.DAT file (or portions of it) into memory and use LoadImage() together with the two binary pointers as I mentioned earlier..


That's what I said in the beginning :D
Well, one advantage of having everything as resources is ease of installation. Having one file that contains everything means no potential problems with missing files, or corrupted files (beyond a corrupt executable of course), and an easier time coding an installer (assuming your doing your own, something like EZSetup I presume takes care of such details for you).

To say making everything resources is lazy I disagree with. There are certainly benefits to both approaches, and not having to keep track of multiple files I would personally consider a benefit. Either approach is perfectly valid and depending on your requirements and desires, one approach may suit you better than the other, but laziness isn't a factor I think for most, if not all people.
OK, thanks for the clarifications. I just assumed that having a 5 MB .exe would be considered bad no matter what.
Well, I think a great many games these days are larger executables (and then some ALSO have large support files!)... I don't think people balk at a 5M total size game like they used to, so long as they can install and run from a memory card of course. No real difference between a single 5M file and 10 500K files then.

Don't take that to mean we shouldn't all be trying to keep files sizes at a minimum, obviously we should in any case.

But again, you simply weigh the risk vs. reward of either approach and choose what works best in any given case. That's always the bottom line.
To address the jpg quality issue... I found that storing a JPEG as a resource and then loading it messed up my images - might have converted 256 colors... however, loading a JPEG from a file does not mess with my images at all

when you mentined a quality problem, were you referring to loading from resource or file?
Viking · 12 Feb 2003, 12:24 UTC — JPEG issue
The real glitch is that you cannot use Progressive JPEG optimization. Use Baseline optimizations (Standart/Optimized) when saving your jpegs.