PocketMatrix forums

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

EasyCE Bugs to be aware of!

Hi all, my last project pointed me to a few bugs with easyce that you may wish to know about. I was having it crash when ram usage was getting low... here are a few things that you should look at:

the tgazload (tgaload) functions use a buffer to place your file in to process it (strip headers etc), there is no checking the mem allocation for this!

The function does not do a check to see if your file exists first either... also handy.

It is also handy to have the function check the destination location is ok.

here is my "moded" code:

Code:

bool tgazload( char* file, int w, int h, IPIXEL* dest, PIXEL* pal )
{
//load
FILE *fp1=fopen(fullpath(file),"r");
if ((fp1) && (dest))
{
fclose(fp1);
int size = w * h + 20 + 768;
BYTE* tgabuff = new BYTE[size];

if (tgabuff)
{
gzFile tga = gzopen( fullpath( file ), "rb" );
gzread( tga, tgabuff, size );
gzclose( tga );
parseheader( tgabuff );
// Store the data at the specified target address
if (pal)
{
for ( int i = 0; i < 256; i++ )
{
int b = *(tgabuff + 18 + i * 3);
int g = *(tgabuff + 18 + i * 3 + 1);
int r = *(tgabuff + 18 + i * 3 + 2);
pal[i] = (PIXEL)(((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3));
}
}
IPIXEL* src = tgabuff + 18 + 768 + (h - 1) * w;
IPIXEL* dst = (IPIXEL*)dest;
for ( int y = 0; y < h; y++ )
{
memcpy( dst, src, w );
dst += w;
src -= w;
}
// Release temporary storage
delete tgabuff;
return true;
}
else return false;
}
else return false;
}


I wonder if someone could re-write this function to not require the buffer? For big files it requires your buffer as well as its temp buffer.... i.e to load a 2Mb image, you need 4Mb of ram.

-Brendan
a better method might be to preprocess the targa file to the foramt you want then use them as resources, no buffer needed then or file loading.

cheers
jim.
I normally store them gzipped. Thus making it hard to directly reference the pic, or in some cases a sub-section of the pic. (tile based graphics, animations etc)

-Brendan
golan_trevize_x · 02 Feb 2003, 07:20 UTC — About resources
Jim,

Just wanted to ask about what you mentioned - using resources. How is it done in eVC++? I once gave it a go a while back, but had a lot of trouble making it work.

eg. Say I have a gzipped file. Currently in my program, I'm using the gzopen command to open that file, which is stored in a directory along with my program on the PocketPC. Now, there is also a gzopenfrommem function which accepts a const long (presumably a pointer to the memory).

So is there a way I can put my gzipped file into a resource and then make it open this way? The LoadResource function returns a HANDLE, but I'm not sure what to do from there.

Thanks for any help!

Nathan
golan_trevize_x · 02 Feb 2003, 07:26 UTC
Actually, I can't seem to find the implementation for gzopenfrommem. Maybe it's been taken out in the WindowsCE port of the zlib library that I'm using.
golan_trevize_x · 03 Feb 2003, 00:52 UTC
Sorry for the triple post here. I have figured out how to read gzip files from a resource loaded into memory. Basically I'm using the slightly lower level function uncompress, which is part of zlib.

For more information about this, there's a thread back in Nov/Dec 2002 you can look at. Just do a search for "zlib" on the forums.

(I had to make my own gzip "format" which begins with two longs, the uncompressed size and the compressed size, followed by the compressed data as a byte stream. I found a DLL implementation of zlib, so was able to create a little program in VB which zips up a file into this simple format.)
Also, because of the tgazload function wasting mem with a buffer, load your large images first:

e.g

if you have a 500kb and a 2mb file, load the 2 mb one first, e.g

to load the 2mb img requires 4mb, but then back to 2mb once loaded, then another 1mb to load the 500kb img, back to 500kb once loaded, so the peak usage only goes as high as 4mb.

if you do it in reverse order, it would already be using the 500kb before it allocated the 4mb to load the 2mb image, thus using 500kb more.... or 4.5mb.

so basically load your images in desending size order!

-Brendan