GP32 Damn Crash!


Alessandro

Member
Joined
Apr 2, 2004
Messages
127
Age
46
Location
Liguria, Italy
Website
Visit site
Hi,
first of all lemme say that the new version of the GP32iDE it's on the way ;) I've finally added the support for Mr.Mirko's SDK... B)

Now I have a HUGE problem that make me smash the head on the monitor...

I've noticed that if I call this function once, nothing happends, but if I call it twice or more, the GeePee emu crashes... Here's my code:

Code:
typedef struct IMAGE{
 int width;
 int height;
 unsigned short *imageData;
}IMAGE;

typedef struct MYENTRY{
	char name[255];
	IMAGE myimage;
}MYENTRY;


int LoadBMP( char *filename, IMAGE *bitmap )
{
   BMPHeader head;
   BMPInfo infos;
   F_HANDLE *img;
   RGBTriplet_Struct Pixel;
   int x,y;

   if( GpFileOpen(filename,OPEN_R,&img)==SM_OK )
      if( GpFileRead( img, &head,14, 1 )==SM_OK )
         if( GpFileRead( img, &infos,40, 1 )==SM_OK )
         {
            int i=0,size;

            bitmap->width=infos.biWidth;
            bitmap->height=infos.biHeight;
            size=infos.biHeight*infos.biWidth;
  	bitmap->imageData=gm_malloc(size);

    	for( y=0;y<infos.biHeight;y++ )
            for( x=0;x<infos.biWidth;x++ )
      {
            GpFileRead( img, &Pixel,3,1 );
            bitmap->imageData[x*infos.biWidth + infos.biHeight-(infos.biHeight-y)]=GpMakeCol16( Pixel.R,Pixel.G,Pixel.B);
        }
  	GpFileClose( img );
            return 1;
     }
  GpFileClose( img );
  return NULL;
}

I use to call this as:
Code:
  MYENTRY fsDirectoryContent[1024];
  LoadBMP( "gp:\\gpmm\\test.bmp", &fsDirectoryContent[0].icon );

Where am I wrong?!?! I'm goin' crazy.. please helpme :)))

Thanks to all

Later
Alessandro
 
Hm,
bitmap->imageData=gm_malloc(size);
and

bitmap->imageData[x*infos.biWidth + infos.biHeight-(infos.biHeight-y)=GpMakeCol16( Pixel.R,Pixel.G,Pixel.B);


is perhaps not a good idea..
try
bitmap->imageData=gm_malloc(size * sizeof(unsigned short));


---
mithris
 
mithris is right, but I think you have another error:

change


F_HANDLE *img;


to


F_HANDLE img;


And if works the first time, it's because you are lucky and you are not accessing memory allocated by other sections of the program.
But the second time you are accessing memory allocated by another section of your program. I think it's because of the first error: if you allocate only size bytes but you use size * 2 bytes (unsigned short is 2 bytes) you are accessing memory that you have not allocated for this purpose.

If someone has another explanation...
 
Yeah! B)
Thanks guys... Now it works... It's a bit slow when loading.. maybe because I read from file each time?

Should I read the whole thing in mem and "decode" the image from there? Will it speed it up? ;)

Thanks
Alessandro
 
Yep, that would probably speed it up, but it might also cause some memory fragmentation which is a bad thing.

---
mithris
 
Back
Top