How to load images with .img format in Phab?

Hello everyone,

I need to load some medical images with .img format in my application. But the PxLoadImage() just can load like .jpeg,.bmp,.tif.pcx…not .img format. The .img file I used is a file contain many slices of MRI images, each slice is a 256*256 pixel image. So I intend to read the .img file first and PhCreateImage() and PdDrawImage(). My code is:

                unsigned int    fbuffer[256][256] = { 0 };
                PhPoint_t    p = { 300, 0 };
    	PhDim_t    pp = {512, 512 };
    	static unsigned int slice[150][256][256]={0};
    	int rc,i,j,n;
    	FILE* fp;
    	 PhImage_t   *img;
    	PgColor_t plat;
    	char *bits;
    	fp=fopen("/root/jack/dcs.img","r");
    	 if( fp == NULL ) 
	   {
       		PtTextModifyText(ABW_inf_text, 0,0,-1,"rom file open failed",20);
    	}
    	for (i=0;i<150;i++)
    	{
    		//rc=fread(fname, 1, sizeof(fname), fp);
    		rc=fread(fbuffer, 256, 256, fp);
						    	}/*finally, fbuffer should be the 150th slice of image 256*256*/
    img=PhCreateImage(NULL,256,256,Pg_IMAGE_DIRECT_1555,NULL,0,1);
	
                               /* bits=img->image;
		for (i=0;i<256;i++,bits+=img->bpl)
			for (j=0;j>256;j++)
			((short*)bits)[j]=fbuffer[i][j];*/
PgSetPalette( img->palette, 0, 0, img->colors, Pg_PALSET_SOFT, 0 );
PgDrawImage( fbuffer,  img->type, &p, &img->size, img->bpl,  0 );

At the end, I draw the 150th MRI image with the array fbuffer, I try to write the data in fbuffer to the pointer img->image, but I cannot write in. Can anybody tell me how to write in?
And the image shows un-complete like lost data or maybe have offset, what is the reason? Maybe because I didnot write the data into img->image??
Please help, thank you very much!

Jason

fbuffer is made up of int but you are reading 256x256 bytes and the image is 512x512. That’s weird.

You are not checking the return value of PhCreateImage nor are you of fread(). You should REALLY add some verification. When code don’t do what you want it to do you should listen to what it is trying to tell you…

You are not storing each value returned by PhCreateImage, because at one point in time you need to call PhReleaseImage for each image.

fbuffer is rather big to live on the stack.

well, thanks mario, you are right. The image is 256*256, no error returned with PhCreateImage and fread(), and I wrote similar code in matlab, it’s works fine.


pixx=256;
pixy=256;
nslices=150;

slice={};
fh=fopen('DCS_2008_06_16_3_1.img','r');
mx=0;
mn=1e10;
for n=1:nslices
 data=fread (fh,[pixy,pixx],'uint16');
 mx=max(mx,max(max(data)));
 mn=min(mn,min(min(data)));
 %data=uint16(data);
 slice=[slice,{data}];
end
fclose (fh);

mx=mx/2;

curslice=1;
measstr=[];
imagesc(slice{curslice});

I know the function of imagesc is different from PgDrawImage(), but the problem seems like happens on the data of each slice in Phab. The precision of the data is uint16 (Unsigned integer; 16 bits) in matlab. But after I change the fbuffer to Unsigned short, the result is the same, just half image appeared.

“fbuffer is rather big to live on the stack”, what to do with this? Can I just increase the draw buffer size with PgSetDrawBufferSize(0x8000);

The fact that you changed fbuffer from int to short changes nothing in the behavior of the program has your code makes no references to its size in the first place. The usage of hardcoded value in fread is a real bad practice. Either use #define or sizeof(). It’s important to have a single definition of the size of operation.

The image is 256x256 pixel and that is how many BYTES you are loading with fread. For an image of type Pg_IMAGE_DIRECT_1555 each pixel is represented with 16 bits, thus 2 BYTES. That doesn’t match. Given the mathlab code I suspect you are freading only half the data.

Your problem has nothing to do with Photon or PgDrawImage, but rather your knowledge of the C language. Notice how the MathLab code says : fread (fh,[pixy,pixx],‘uint16’); , which mean load in an array of size pixx by pixy made of element uint16. You have not inform the C fread you are reading 16 bits value. I would change fread to be:

fread( fbuffer, 2, 256*256, fp);

I would not use constant but did it here for simplicity. Also given that the file is big and you read it in one operation I would use open/read instead which has far less overhead and is faster.

Well, thank you mario. You are right, I should do more home work on C language. :wink:

I need to get the coordinate (x,y) of a point on the image using mouse, like point to one position on the image and press the left mouse button, then the coordinate
is recorded. I knew this should use a event handler, but how to read the coordinate from the image??

Actually, I draw the image in a raw widget, and I can get the left up corner and the right down corner, and the image is
drawed begin with the left up corner, and I’d like put this point as (0,0) of the image (256*256), any method for the rest of the points?
Many thanks in advance!

Ok, got it with PhPointerEvent_t…