Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - How to load images with .img format in Phab?: (13 Items)
   
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: 


Code:

                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,512,512,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 ); 


Any thing wrong with the code please? And 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
Re: How to load images with .img format in Phab?  
Here is an outline of what you need to done to get your image in a photon widget:

- Creating your photon image base on the data in the img file
-- Choose an appropriate format when calling calling PgCreateImage()
-- “fill-up” the created image with the data based on the raw img data. You need to know the format of the raw img 
data. Extract the color/index value of at an x,y from the raw img data, and use the PiSetPixel(..x,y..) on the photon 
image to set it. Do this for all pixels.
-- (NOTE: When using direct images types you don’t need to set palette)

- Rendering options:
-- PtLabel – use your image as Pt_ARG_LABEL_IMAGE, the label will take care of the rest and display the image for you
-- PtRaw –  you will need to write your own render function in which you will do PgDrawImage() with your image
Re: How to load images with .img format in Phab?  
> Here is an outline of what you need to done to get your image in a photon 
> widget:
> 
> - Creating your photon image base on the data in the img file
> -- Choose an appropriate format when calling calling PgCreateImage()
> -- “fill-up” the created image with the data based on the raw img data. You 
> need to know the format of the raw img data. Extract the color/index value of 
> at an x,y from the raw img data, and use the PiSetPixel(..x,y..) on the photon
>  image to set it. Do this for all pixels.
> -- (NOTE: When using direct images types you don’t need to set palette)
> 
> - Rendering options:
> -- PtLabel – use your image as Pt_ARG_LABEL_IMAGE, the label will take care 
> of the rest and display the image for you
> -- PtRaw –  you will need to write your own render function in which you will
>  do PgDrawImage() with your image


Thank you very much Misha, I got the image.
Now, I need to get the coordinate (x,y) of a point on the image, like 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, I should read the cooedinates base on the raw widget or the image directly?

Many thanks in advance!
Re: How to load images with .img format in Phab?  
You need to attach a raw event handler the raw widget with the Ph_EV_BUT_PRESS mask. When an event of this type arrives,
 your callback will be called. You should use PhGetRects() to obtain the pointer’s position. The event translation is 
relative to your canvas, since you render the image yourself, and (unless you changed it) the image’s position is 
relative to the widget’s origin. You can translate the pointer event rectangle to be relative to the widget’s origin: 
PhTranslateRect( event_rect, &canvas.ul ); 
Now you can subtract the image position from the event position and will get a rectangle relative to your image: 
PhDetranslateRect( ev_rect, &image_pos ); If the ev_rect.ul is negative or ev_rect.lr is greater than image dimensions 
your are outside the image.

The event's docs: http://www.qnx.com/developers/docs/6.4.0/photon/prog_guide/events.html
Re: How to load images with .img format in Phab?  
> You need to attach a raw event handler the raw widget with the Ph_EV_BUT_PRESS
>  mask. When an event of this type arrives, your callback will be called. You 
> should use PhGetRects() to obtain the pointer’s position. The event 
> translation is relative to your canvas, since you render the image yourself, 
> and (unless you changed it) the image’s position is relative to the widget’s
>  origin. You can translate the pointer event rectangle to be relative to the 
> widget’s origin: PhTranslateRect( event_rect, &canvas.ul ); 
> Now you can subtract the image position from the event position and will get a
>  rectangle relative to your image: PhDetranslateRect( ev_rect, &image_pos ); If the 
> ev_rect.ul is negative or ev_rect.lr is greater than image dimensions your are
>  outside the image.
> 
> The event's docs: http://www.qnx.com/developers/docs/6.4.0/photon/prog_guide/
> events.html


Thanks, I got it with PhPointerEvent_t, but the coordinate is Integer, can it be more precise? If not, it's ok, no big 
effect.
One more question about the image, the image shows in blue color now with black backgroud, how can I change blue to gray
? PgGray()??It's not working...

Many thanks and regards,
Re: How to load images with .img format in Phab?  
Coordinates are shorts.
I guess you didn't set background color or image format correctly -- the "background" of your image is filled in by you 
after all. The widget background color is rendered (usually) by the PtSuperClassDraw() call in the draw function.
Re: How to load images with .img format in Phab?  
> Coordinates are shorts.
> I guess you didn't set background color or image format correctly -- the "
> background" of your image is filled in by you after all. The widget background
>  color is rendered (usually) by the PtSuperClassDraw() call in the draw 
> function.


How to fill the "background" color? The image is two bytes per pixel so I choose image direct format like:
PhCreateImage(NULL,256,256,Pg_IMAGE_DIRECT_1555,NULL,0,1); 
PtSuperClassDraw() is used in the draw function as well. But I don't know the image format is correct or not, but it 
works fine except the color. I tried every format actually.
Re: How to load images with .img format in Phab?  
Something is missing, please post the code. 
What is the format of your img data?
Re: How to load images with .img format in Phab?  
> Something is missing, please post the code. 
> What is the format of your img data?
The format is Unsigned short, the code is below,
Thank you very much!

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"

#include <malloc.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>

#include <Ph.h>
#include <Pt.h>

#include <photon/PxImage.h>

void raw_draw_f( PtWidget_t *widget, PhTile_t *damage ) 

 {
 	unsigned short   fbuffer[256][256] = { 0 };
 	int rc,i,j,n;
 	FILE* fp;
    	PhImage_t   *img,*img1;
    	PhPoint_t    p = { 0, 0 };
    	double num=0;
	PtSuperClassDraw( PtBasic, widget, damage );
	PhRect_t  raw_canvas;

	PtCalcCanvas (ABW_raw, &raw_canvas);
	num=raw_canvas.ul.x;
	fp=fopen("/root/jack/dcs.img","rb");
    	 if( fp == NULL ) 
	   	{
       		PtTextModifyText(ABW_inf_text, 0,0,-1,"rom file open failed",20);
    	}
    	for (i=0;i<75;i++)
    	{
    		rc=fread(fbuffer,2,256*256, fp);
    	}
		
		PgSetDrawBufferSize( 0x8000 );
		if ((img=PhCreateImage(NULL,256,256,Pg_IMAGE_DIRECT_1555,NULL,0,1))!=NULL)
		{
			PtTextModifyText(ABW_inf_text, 0,0,-1,"11",2);
		}
		
		if(PiSetPixel(img,0,0,fbuffer[0][0])==0)
		{
				PtTextModifyText(ABW_inf_text, 0,0,-1,"11",2);
		}
		
		for(j=0;j<256;j++)
		{
			for(n=0;n<256;n++)
			{   
		
				PiSetPixel(img,j,n,fbuffer[j][n]);
			
			}
		}
		//img->colors=PgGray(160);
		 PgDrawImage(img->image, img->type, &raw_canvas.ul, &img->size, img->bpl, 0 );
		 	PtSetResource (ABW_m1, Pt_ARG_NUMERIC_VALUE, &num, 0);
		//PtSetResource (ABW_m2, Pt_ARG_NUMERIC_VALUE, 1, 0);
		 //PgWaitDrawComplete();
	}

Re: How to load images with .img format in Phab?  
Ok, each pixel requires 16 bits, but what is the content?
For example, the format ARGB1555 defines:
1 bit - pixel for alpha,
5 bits -- red, 5 bits -- green, 5 bits -- blue, and it also defines a packing order.
What is the packing order of the img data inside the short?
Re: How to load images with .img format in Phab?  
> Ok, each pixel requires 16 bits, but what is the content?
> For example, the format ARGB1555 defines:
> 1 bit - pixel for alpha,
> 5 bits -- red, 5 bits -- green, 5 bits -- blue, and it also defines a packing 
> order.
> What is the packing order of the img data inside the short?


Sorry, I don't know the packing order, I cannot find it in the reference, I need ask someone else. Do you mean that if 
the packing order is  4 bit - pixel for alpha,
4bits -- red, 4 bits -- green, 4bits -- blue, then the format should be
ARGB4444? The format needs to be chosen according to the packing order.
Re: How to load images with .img format in Phab?  
Yes, that is what I mean. If photon image doesn't support what img data has, you will need to decypher the img data and 
fill up the destination image with the proper data.
Re: How to load images with .img format in Phab?  
> Yes, that is what I mean. If photon image doesn't support what img data has, 
> you will need to decypher the img data and fill up the destination image with 
> the proper data.


Understood, thank you very much for your help and patience!