TPCCLIB
Loading...
Searching...
No Matches
vol.c File Reference

Storing and processing of 3D PET image volume data with no time information (frames). More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

void volInit (VOL *vol)
void svolInit (SVOL *svol)
void volEmpty (VOL *vol)
void svolEmpty (SVOL *svol)
int volAllocate (VOL *vol, int planes, int rows, int columns)
int svolAllocate (SVOL *svol, int planes, int rows, int columns)
int img2vol (IMG *img, VOL *vol, int frame)
int img2svol (IMG *img, SVOL *svol, int frame)
int vol2img (VOL *vol, IMG *img, int frame)
int svol2img (SVOL *svol, IMG *img, int frame)
void volInfo (VOL *vol, FILE *fp)
void svolInfo (SVOL *svol, FILE *fp)
void volContents (VOL *vol, VOL_RANGE r, FILE *fp)
int volMax (VOL *vol, VOL_RANGE *r, VOL_PIXEL *maxp, float *maxv, VOL_PIXEL *minp, float *minv)
int volAvg (VOL *vol, VOL_RANGE *r, float *avg)
int vrdReorder (VOL_RANGE *vol_range)
int vrdVxlNr (VOL_RANGE *vol_range)
int string_to_xyz (char *str, int *x, int *y, int *z)
int vrd2vol (VOL_RANGE *r, VOL *vol, float in, float out, char *status)
int vrdRead (char *vrdfile, VOL_RANGE *vol_range, char *status)

Variables

int VOL_TEST
char * _volStatusMessage []

Detailed Description

Storing and processing of 3D PET image volume data with no time information (frames).

Author
Vesa Oikonen

Definition in file vol.c.

Function Documentation

◆ img2svol()

int img2svol ( IMG * img,
SVOL * svol,
int frame )

Copy one time frame (1..dimt) from 4D image to 3D short int volume. Svol can be but need not to be allocated.

Parameters
imgimage structure
svolshort volume structure
frameframe number [1..number of frames]
Returns
0 if ok, 1 invalid image status, 2 invalid input

Definition at line 305 of file vol.c.

305 {
306 int ret;
307 unsigned short int zi, yi, xi, fi;
308 float fmin, fmax, g;
309
310 if(VOL_TEST) printf("img2svol(img, %d, svol)\n", frame);
311 /* Check input */
312 if(svol==NULL) return(1);
313 svol->statmsg=_volStatusMessage[1];
314 if(img->status!=IMG_STATUS_OCCUPIED) return(1);
315 if(frame<1 || img->dimt<frame) return(2);
316 if(svol->status==IMG_STATUS_UNINITIALIZED) return(1);
317
318 /* Allocate memory (if needed) for volume */
319 ret=svolAllocate(svol, img->dimz, img->dimy, img->dimx);
320 if(ret) return(ret);
321
322 /* Copy data */
323 fi=frame-1;
324 svol->orientation=img->orientation;
325 svol->sizex=img->sizex; svol->sizey=img->sizey; svol->sizez=img->sizez;
326 ret=imgFrameMinMax(img, frame, &fmin, &fmax); if(ret) return(10+ret);
327 if(fabs(fmin)>fabs(fmax)) g=fabs(fmin); else g=fabs(fmax);
328 if(g!=0) g=32766./g; else g=1.0;
329 for(zi=0; zi<svol->dimz; zi++)
330 for(yi=0; yi<svol->dimy; yi++)
331 for(xi=0; xi<svol->dimx; xi++)
332 svol->v[zi][yi][xi]=(short int)temp_roundf(g*img->m[zi][yi][xi][fi]);
333 svol->scale_factor=1.0/g;
334
335 return(0);
336}
int imgFrameMinMax(IMG *img, int frame, float *minvalue, float *maxvalue)
Definition imgminmax.c:171
#define IMG_STATUS_OCCUPIED
int VOL_TEST
Definition vol.c:6
#define IMG_STATUS_UNINITIALIZED
int temp_roundf(float e)
Definition petc99.c:20
float sizex
unsigned short int dimx
float **** m
char status
float sizey
unsigned short int dimz
unsigned short int dimy
int orientation
float sizez
float sizey
unsigned short int dimx
unsigned short int dimz
int orientation
float sizez
unsigned short int dimy
float scale_factor
char status
char * statmsg
short int *** v
float sizex
char * _volStatusMessage[]
Definition vol.c:13
int svolAllocate(SVOL *svol, int planes, int rows, int columns)
Definition vol.c:204

◆ img2vol()

int img2vol ( IMG * img,
VOL * vol,
int frame )

Copy one time frame (1..dimt) from 4D image to 3D volume. Vol can be but need not to be allocated.

Parameters
imgimage structure
volvolume structure
frameframe number [1..number of frames]
Returns
0 if ok, 1 invalid image status, 2 invalid input

Definition at line 267 of file vol.c.

267 {
268 int ret;
269 unsigned short int zi, yi, xi, fi;
270
271 if(VOL_TEST) printf("img2vol(img, %d, vol)\n", frame);
272 /* Check input */
273 if(vol==NULL) return(1);
275 if(img->status!=IMG_STATUS_OCCUPIED) return(1);
276 if(frame<1 || img->dimt<frame) return(2);
277 if(vol->status==IMG_STATUS_UNINITIALIZED) return(1);
278
279 /* Allocate memory (if needed) for volume */
280 ret=volAllocate(vol, img->dimz, img->dimy, img->dimx);
281 if(ret) return(ret);
282
283 /* Copy data */
284 fi=frame-1;
285 vol->orientation=img->orientation;
286 vol->sizex=img->sizex; vol->sizey=img->sizey; vol->sizez=img->sizez;
287 for(zi=0; zi<vol->dimz; zi++)
288 for(yi=0; yi<vol->dimy; yi++)
289 for(xi=0; xi<vol->dimx; xi++)
290 vol->v[zi][yi][xi]=img->m[zi][yi][xi][fi];
291
292 return(0);
293}
char status
unsigned short int dimx
unsigned short int dimy
int orientation
unsigned short int dimz
float sizex
char * statmsg
float *** v
float sizez
float sizey
int volAllocate(VOL *vol, int planes, int rows, int columns)
Definition vol.c:139

◆ string_to_xyz()

int string_to_xyz ( char * str,
int * x,
int * y,
int * z )

Read pixel location from a string representation of it.

See also
string_to_xyzf, vrdRead
Returns
Returns 0 if successful, >0 if not.
Parameters
strString in format x,y,z or x y z ; this string is not changed
xPixel location in x dimension
yPixel location in y dimension
zPixel location in z dimension

Definition at line 653 of file vol.c.

662 {
663 char *cptr, tmp[256];
664
665 strncpy(tmp, str, 255); tmp[255]=(char)0;
666 cptr=strtok(tmp, " ,;:()|-"); if(cptr==NULL) return 1;
667 *x=atoi(cptr); if(*x<1) return 1;
668 cptr=strtok(NULL, " ,;:()|-"); if(cptr==NULL) return 2;
669 *y=atoi(cptr); if(*y<1) return 1;
670 cptr=strtok(NULL, " ,;:()|-"); if(cptr==NULL) return 3;
671 *z=atoi(cptr); if(*z<1) return 1;
672 return 0;
673}

Referenced by vrdRead().

◆ svol2img()

int svol2img ( SVOL * svol,
IMG * img,
int frame )

Copy 3D short int volume as one time frame (1..dimt) into 4D image. Img must be allocated.

Parameters
svolshort volume structure
imgimage structure
frameframe number [1..img->dimt]
Returns
0 if ok, 1 invalid image, 2 invalid frame number, 3 (x,y) dimension inconsistency, 4 plane number inconsistency

Definition at line 385 of file vol.c.

385 {
386 unsigned short int zi, yi, xi, fi;
387
388 if(VOL_TEST) printf("svol2img(svol, img, %d)\n", frame);
389 /* Check input */
390 if(svol==NULL || svol->status!=IMG_STATUS_OCCUPIED) return(1);
391 svol->statmsg=_volStatusMessage[1];
392 if(img==NULL || img->status!=IMG_STATUS_OCCUPIED) return(1);
393 if(frame<1 || img->dimt<frame) return(2);
394 if(img->dimx!=svol->dimx || img->dimy!=svol->dimy) return(3);
395 if(img->dimz!=svol->dimz) return(4);
396
397 /* Copy data */
398 fi=frame-1;
399 for(zi=0; zi<svol->dimz; zi++)
400 for(yi=0; yi<svol->dimy; yi++)
401 for(xi=0; xi<svol->dimx; xi++)
402 img->m[zi][yi][xi][fi]=(svol->scale_factor)*(float)svol->v[zi][yi][xi];
403
404 return(0);
405}

◆ svolAllocate()

int svolAllocate ( SVOL * svol,
int planes,
int rows,
int columns )

Allocate memory for 3D short int volume. Returns 0 if ok.

Parameters
svolshort volume structure
planesnumber of planes [>=1]
rowsnumber of rows [>=1]
columnsnumber of columns [>=1]
Returns
0 if ok, 1 invalid image status, 2 invalid input, 5 failed to allocate memory for planes, 6 failed to allocate memory for rows, 8 failed to allocate memory for rows

Definition at line 204 of file vol.c.

204 {
205 unsigned short int zi, ri;
206 int vxlNr, vi;
207 short int **rptr, *cptr;
208
209 if(VOL_TEST) printf("svoiAllocate(*svol, %d, %d, %d)\n", planes, rows, columns);
210 /* Check arguments */
211 if(svol==NULL) return(1); else svol->statmsg=_volStatusMessage[1];
212 if(svol->status==IMG_STATUS_UNINITIALIZED) return(1);
213 if(planes<1 || rows<1 || columns<1) return(2);
214 vxlNr=planes*rows*columns;
215
216 /* Check if correct volume size is already allocated */
217 if(svol->status>=IMG_STATUS_OCCUPIED) {
218 if(planes==svol->dimz && rows==svol->dimy && columns==svol->dimx) {
219 for(vi=0; vi<vxlNr; vi++) svol->_vxl[vi]=0;
220 return(0); /* that's it */
221 } else {
222 svolEmpty(svol);
223 }
224 }
225 /* Allocate memory for volume data */
226 svol->_pln=(short int***)malloc((size_t)planes*sizeof(short int**));
227 if(svol->_pln==NULL) {
228 return(5);}
229 svol->_row=(short int**)malloc((size_t)planes*rows*sizeof(short int*));
230 if(svol->_row==NULL) {
231 free(svol->_pln); return(6);}
232 svol->_col=svol->_vxl=(short int*)calloc((size_t)planes*rows*columns, sizeof(short int));
233 if(svol->_vxl==NULL) {
234 free(svol->_pln); free(svol->_row); return(8);
235 }
236 /* Set data pointers */
237 rptr=svol->_row; cptr=svol->_col;
238 for(zi=0; zi<planes; zi++) {
239 svol->_pln[zi]=rptr;
240 for(ri=0; ri<rows; ri++) {
241 *rptr++=cptr; cptr+=columns;
242 }
243 }
244 svol->v=svol->_pln;
245 svol->plane=svol->_pln;
246 svol->column=svol->_col;
247 svol->row=svol->_row;
248 svol->voxel=svol->_vxl;
249 /* Ok */
250 svol->dimz=planes; svol->dimy=rows; svol->dimx=columns;
251 svol->statmsg=_volStatusMessage[0];
253 return(0);
254}
short int ** row
short int * voxel
short int *** plane
short int * column
void svolEmpty(SVOL *svol)
Definition vol.c:103

Referenced by img2svol().

◆ svolEmpty()

void svolEmpty ( SVOL * svol)

Free memory allocated for short int volume.

Parameters
svolshort int volume structure

Definition at line 103 of file vol.c.

103 {
104 if(VOL_TEST) printf("svolEmpty()\n");
105 if(svol==NULL || svol->status<IMG_STATUS_OCCUPIED) return;
106 /* Free up memory */
107 if(svol->_vxl!=NULL) free(svol->_vxl);
108 //if(svol->_col!=NULL) free(svol->_col); Same as _vxl
109 if(svol->_row!=NULL) free(svol->_row);
110 if(svol->_pln!=NULL) free(svol->_pln);
111 /* Set variables */
112 svol->statmsg=_volStatusMessage[0];
113 svol->orientation=0;
114 svol->dimx=svol->dimy=svol->dimz=0;
115 svol->sizex=svol->sizey=svol->sizez=0;
116 svol->scale_factor=1.0;
117 svol->v=(short int***)NULL;
118 svol->voxel=(short int*)NULL;
119 svol->column=(short int*)NULL;
120 svol->row=(short int**)NULL;
121 svol->plane=(short int***)NULL;
122 /* Set status */
124}
#define IMG_STATUS_INITIALIZED

Referenced by svolAllocate().

◆ svolInfo()

void svolInfo ( SVOL * svol,
FILE * fp )

Prints short int volume information to specified file pointer, e.g. stdout

Parameters
svolshort volume structure
fptarget file pointer

Definition at line 439 of file vol.c.

439 {
440 if(VOL_TEST) printf("svolInfo()\n");
441 if(svol==NULL || svol->status<=IMG_STATUS_UNINITIALIZED) {
442 fprintf(fp, "Volume data is not initialized.\n"); return;}
443 if(svol->status==IMG_STATUS_INITIALIZED) {
444 fprintf(fp, "Volume data is initialized but empty.\n"); return;}
445 if(svol->status==IMG_STATUS_ERROR) fprintf(stdout, "Volume data has errors.\n");
446 fprintf(fp, "Volume status: %s\n", svol->statmsg);
447 fprintf(fp, "Patient orientation: %d\n", svol->orientation);
448 fprintf(fp, "Voxel sizes (x, y, z): %g %g %g mm\n",
449 svol->sizex, svol->sizey, svol->sizez);
450 fprintf(fp, "Dimensions (x, y, z): %d %d %d\n",
451 svol->dimx, svol->dimy, svol->dimz);
452 fprintf(fp, "Scale factor: %g\n", svol->scale_factor);
453 return;
454}
#define IMG_STATUS_ERROR

◆ svolInit()

void svolInit ( SVOL * svol)

Initiate short int volume before any use of SVOL data; this should be called once.

Parameters
svolshort int volume structure

Definition at line 50 of file vol.c.

50 {
51 if(VOL_TEST) printf("svolInit()\n");
52 if(svol==NULL) return;
53 memset(svol, 0, sizeof(SVOL));
56 svol->orientation=0;
57 svol->dimx=svol->dimy=svol->dimz=0;
58 svol->sizex=svol->sizey=svol->sizez=0;
59 svol->scale_factor=1.0;
60 svol->v=(short int***)NULL;
61 svol->voxel=(short int*)NULL;
62 svol->column=(short int*)NULL;
63 svol->row=(short int**)NULL;
64 svol->plane=(short int***)NULL;
65}

◆ vol2img()

int vol2img ( VOL * vol,
IMG * img,
int frame )

Copy 3D volume as one time frame (1..dimt) into 4D image. Img must be allocated.

Parameters
volvolume structure
imgimage structure
frameframe number [1..number of frames]
Returns
0 if ok, 1 invalid image status, 2 invalid input, 3 image<->volume (x,y) dimensions do not match, 4 image<->volume planes do not match.

Definition at line 351 of file vol.c.

351 {
352 unsigned short int zi, yi, xi, fi;
353
354 if(VOL_TEST) printf("vol2img(vol, img, %d)\n", frame);
355 /* Check input */
356 if(vol==NULL || vol->status!=IMG_STATUS_OCCUPIED) return(1);
358 if(img==NULL || img->status!=IMG_STATUS_OCCUPIED) return(1);
359 if(frame<1 || img->dimt<frame) return(2);
360 if(img->dimx!=vol->dimx || img->dimy!=vol->dimy) return(3);
361 if(img->dimz!=vol->dimz) return(4);
362
363 /* Copy data */
364 fi=frame-1;
365 for(zi=0; zi<vol->dimz; zi++)
366 for(yi=0; yi<vol->dimy; yi++)
367 for(xi=0; xi<vol->dimx; xi++)
368 img->m[zi][yi][xi][fi]=vol->v[zi][yi][xi];
369
370 return(0);
371}

◆ volAllocate()

int volAllocate ( VOL * vol,
int planes,
int rows,
int columns )

Allocate memory for 3D image volume. Returns 0 if ok.

Parameters
volvolume structure
planesnumber of planes [>=1]
rowsnumber of rows [>=1]
columnsnumber of columns [>=1]
Returns
0 if ok, 1 invalid image status, 2 invalid input, 5 failed to allocate memory for planes, 6 failed to allocate memory for rows, 8 failed to allocate memory for rows

Definition at line 139 of file vol.c.

139 {
140 unsigned short int zi, ri;
141 int vxlNr, vi;
142 float **rptr, *cptr;
143
144 if(VOL_TEST) printf("voiAllocate(*vol, %d, %d, %d)\n", planes, rows, columns);
145 /* Check arguments */
146 if(vol==NULL) return(1); else vol->statmsg=_volStatusMessage[1];
147 if(vol->status==IMG_STATUS_UNINITIALIZED) return(1);
148 if(planes<1 || rows<1 || columns<1) return(2);
149 vxlNr=planes*rows*columns;
150
151 /* Check if correct volume size is already allocated */
152 if(vol->status>=IMG_STATUS_OCCUPIED) {
153 if(planes==vol->dimz && rows==vol->dimy && columns==vol->dimx) {
154 for(vi=0; vi<vxlNr; vi++) vol->_vxl[vi]=0;
155 return(0); /* that's it */
156 } else {
157 volEmpty(vol);
158 }
159 }
160 /* Allocate memory for volume data */
161 vol->_pln=(float***)malloc((size_t)planes*sizeof(float**));
162 if(vol->_pln==NULL) {
163 return(5);}
164 vol->_row=(float**)malloc((size_t)planes*rows*sizeof(float*));
165 if(vol->_row==NULL) {
166 free(vol->_pln); return(6);}
167 vol->_col=vol->_vxl=(float*)calloc((size_t)planes*rows*columns, sizeof(float));
168 if(vol->_vxl==NULL) {
169 free(vol->_pln); free(vol->_row); return(8);
170 }
171 /* Set data pointers */
172 rptr=vol->_row; cptr=vol->_col;
173 for(zi=0; zi<planes; zi++) {
174 vol->_pln[zi]=rptr;
175 for(ri=0; ri<rows; ri++) {
176 *rptr++=cptr; cptr+=columns;
177 }
178 }
179 vol->v=vol->_pln;
180 vol->plane=vol->_pln;
181 vol->column=vol->_col;
182 vol->row=vol->_row;
183 vol->voxel=vol->_vxl;
184 /* Ok */
185 vol->dimz=planes; vol->dimy=rows; vol->dimx=columns;
188 return(0);
189}
float * column
float ** row
float *** plane
float * voxel
void volEmpty(VOL *vol)
Definition vol.c:74

Referenced by img2vol().

◆ volAvg()

int volAvg ( VOL * vol,
VOL_RANGE * r,
float * avg )

Calculates average voxel value inside specified volume range.

Returns
0 if ok, 1 invalid image status, 2 inconsistent range ending, 3 inconsistent range dimensions, 4 inconsistent dimensions
Parameters
volPointer to VOL image structure
rPointer to volume range inside VOL; enter NULL if whole VOL is used
avgTarget for mean value

Definition at line 562 of file vol.c.

569 {
570 int zi, yi, xi, n=0;
571
572 if(vol==NULL || vol->status!=IMG_STATUS_OCCUPIED) return(1);
573 if(r!=NULL) {
574 if(r->z1<1 || r->y1<1 || r->x1<1) return(2);
575 if(r->z2<r->z1 || r->y2<r->y1 || r->x2<r->x1) return(3);
576 if(r->z2>vol->dimz || r->y2>vol->dimy || r->x2>vol->dimx) return(4);
577 }
578
579 *avg=0.0;
580 if(r!=NULL) {
581 for(zi=r->z1-1; zi<r->z2; zi++) {
582 for(yi=r->y1-1; yi<r->y2; yi++) {
583 for(xi=r->x1-1; xi<r->x2; xi++) {
584 *avg+=vol->v[zi][yi][xi]; n++;
585 }
586 }
587 }
588 } else {
589 for(zi=0; zi<vol->dimz; zi++) {
590 for(yi=0; yi<vol->dimy; yi++) {
591 for(xi=0; xi<vol->dimx; xi++) {
592 *avg+=vol->v[zi][yi][xi]; n++;
593 }
594 }
595 }
596 }
597 if(n>0) *avg/=(float)n;
598 return(0);
599}

◆ volContents()

void volContents ( VOL * vol,
VOL_RANGE r,
FILE * fp )

Prints matrix values inside specified range to file pointer.

Parameters
volvolume structure
rvolume range structure
fptarget file pointer

Definition at line 464 of file vol.c.

464 {
465 int zi, yi, xi;
466
467 if(vol==NULL || vol->status!=IMG_STATUS_OCCUPIED) return;
468 if(r.z1<1 || r.y1<1 || r.x1<1) return;
469 if(r.z2<r.z1 || r.y2<r.y1 || r.x2<r.x1) return;
470 if(r.z2>vol->dimz || r.y2>vol->dimy || r.x2>vol->dimx) return;
471
472 for(zi=r.z1-1; zi<r.z2; zi++) {
473 fprintf(fp, "pl=%03d ", zi+1);
474 for(xi=r.x1-1; xi<r.x2; xi++) fprintf(fp, " x=%05d", xi+1);
475 fprintf(fp, "\n");
476 for(yi=r.y1-1; yi<r.y2; yi++) {
477 fprintf(fp, "y=%05d", yi+1);
478 for(xi=r.x1-1; xi<r.x2; xi++)
479 fprintf(fp, " %7.3f", vol->v[zi][yi][xi]);
480 fprintf(fp, "\n");
481 }
482 }
483}

◆ volEmpty()

void volEmpty ( VOL * vol)

Free memory allocated for volume.

Parameters
volvolume structure

Definition at line 74 of file vol.c.

74 {
75 if(VOL_TEST) printf("volEmpty()\n");
76 if(vol==NULL || vol->status<IMG_STATUS_OCCUPIED) return;
77 /* Free up memory */
78 if(vol->_vxl!=NULL) free(vol->_vxl);
79 //if(vol->_col!=NULL) free(vol->_col); Same as _vxl
80 if(vol->_row!=NULL) free(vol->_row);
81 if(vol->_pln!=NULL) free(vol->_pln);
82 /* Set variables */
84 vol->orientation=0;
85 vol->dimx=vol->dimy=vol->dimz=0;
86 vol->sizex=vol->sizey=vol->sizez=0;
87 vol->v=(float***)NULL;
88 vol->voxel=(float*)NULL;
89 vol->column=(float*)NULL;
90 vol->row=(float**)NULL;
91 vol->plane=(float***)NULL;
92 /* Set status */
94}

Referenced by volAllocate().

◆ volInfo()

void volInfo ( VOL * vol,
FILE * fp )

Prints volume information to specified file pointer, e.g. stdout

Parameters
volvolume structure
fptarget file pointer

Definition at line 415 of file vol.c.

415 {
416 if(VOL_TEST) printf("volInfo()\n");
417 if(vol==NULL || vol->status<=IMG_STATUS_UNINITIALIZED) {
418 fprintf(fp, "Volume data is not initialized.\n"); return;}
420 fprintf(fp, "Volume data is initialized but empty.\n"); return;}
421 if(vol->status==IMG_STATUS_ERROR) fprintf(stdout, "Volume data has errors.\n");
422 fprintf(fp, "Volume status: %s\n", vol->statmsg);
423 fprintf(fp, "Patient orientation: %d\n", vol->orientation);
424 fprintf(fp, "Voxel sizes (x, y, z): %g %g %g mm\n",
425 vol->sizex, vol->sizey, vol->sizez);
426 fprintf(fp, "Dimensions (x, y, z): %d %d %d\n",
427 vol->dimx, vol->dimy, vol->dimz);
428 return;
429}

◆ volInit()

void volInit ( VOL * vol)

Initiate volume before any use of VOL data; this should be called once.

Parameters
volpointer to volume structure

Definition at line 26 of file vol.c.

26 {
27 if(VOL_TEST) printf("volInit()\n");
28 if(vol==NULL) return;
29 memset(vol, 0, sizeof(VOL));
32 vol->orientation=0;
33 vol->dimx=vol->dimy=vol->dimz=0;
34 vol->sizex=vol->sizey=vol->sizez=0;
35 vol->v=(float***)NULL;
36 vol->voxel=(float*)NULL;
37 vol->column=(float*)NULL;
38 vol->row=(float**)NULL;
39 vol->plane=(float***)NULL;
40}

◆ volMax()

int volMax ( VOL * vol,
VOL_RANGE * r,
VOL_PIXEL * maxp,
float * maxv,
VOL_PIXEL * minp,
float * minv )

Finds max and/or min voxel inside specified volume range.

Returns
0 if ok, 1 invalid volume status, 2 invalid range endings, 3 inconsistent range dimensions, 4 inconsistent dimensions
Parameters
volPointer to VOL image structure
rPointer to volume range inside VOL; enter NULL if whole VOL is used
maxpPixel where max pixel position is written; NULL if not needed
maxvTarget for max value; NULL if not needed
minpPixel where min pixel position is written; NULL if not needed
minvTarget for min value; NULL if not needed

Definition at line 492 of file vol.c.

505 {
506 int zi, yi, xi;
507 float lmax, lmin;
508
509 if(vol==NULL || vol->status!=IMG_STATUS_OCCUPIED) return(1);
510
511 if(r!=NULL) {
512 if(r->z1<1 || r->y1<1 || r->x1<1) return(2);
513 if(r->z2<r->z1 || r->y2<r->y1 || r->x2<r->x1) return(3);
514 if(r->z2>vol->dimz || r->y2>vol->dimy || r->x2>vol->dimx) return(4);
515
516 zi=r->z1-1; yi=r->y1-1; xi=r->x1-1; lmax=lmin=vol->v[zi][yi][xi];
517 if(maxp!=NULL) {maxp->z=zi+1; maxp->y=yi+1; maxp->x=xi+1;}
518 if(minp!=NULL) {minp->z=zi+1; minp->y=yi+1; minp->x=xi+1;}
519 for(zi=r->z1-1; zi<r->z2; zi++) {
520 for(yi=r->y1-1; yi<r->y2; yi++) {
521 for(xi=r->x1-1; xi<r->x2; xi++) {
522 if(lmax<vol->v[zi][yi][xi]) {
523 lmax=vol->v[zi][yi][xi];
524 if(maxp!=NULL) {maxp->z=zi+1; maxp->y=yi+1; maxp->x=xi+1;}
525 } else if(lmin>vol->v[zi][yi][xi]) {
526 lmin=vol->v[zi][yi][xi];
527 if(minp!=NULL) {minp->z=zi+1; minp->y=yi+1; minp->x=xi+1;}
528 }
529 }
530 }
531 }
532 } else {
533 zi=yi=xi=0; lmax=lmin=vol->v[zi][yi][xi];
534 if(maxp!=NULL) {maxp->z=zi+1; maxp->y=yi+1; maxp->x=xi+1;}
535 if(minp!=NULL) {minp->z=zi+1; minp->y=yi+1; minp->x=xi+1;}
536 for(zi=0; zi<vol->dimz; zi++) {
537 for(yi=0; yi<vol->dimy; yi++) {
538 for(xi=0; xi<vol->dimx; xi++) {
539 if(lmax<vol->v[zi][yi][xi]) {
540 lmax=vol->v[zi][yi][xi];
541 if(maxp!=NULL) {maxp->z=zi+1; maxp->y=yi+1; maxp->x=xi+1;}
542 } else if(lmin>vol->v[zi][yi][xi]) {
543 lmin=vol->v[zi][yi][xi];
544 if(minp!=NULL) {minp->z=zi+1; minp->y=yi+1; minp->x=xi+1;}
545 }
546 }
547 }
548 }
549 }
550 if(maxv!=NULL) *maxv=lmax;
551 if(minv!=NULL) *minv=lmin;
552 return(0);
553}

◆ vrd2vol()

int vrd2vol ( VOL_RANGE * r,
VOL * vol,
float in,
float out,
char * status )

Set volume voxel values based on volume range definition.

Returns
Returns 0 if successful.
See also
vrdRead, vrdVxlNr, volAllocate, vol2img
Parameters
rImage volume range.
volPre-allocated image volume data structure.
inValue to write into voxels inside the given volume range; enter nanf("") to not change the values.
outValue to write into voxels outside the given volume range; enter nanf("") to not change the values.
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed

Definition at line 681 of file vol.c.

695 {
696 if(vol==NULL || vol->status!=IMG_STATUS_OCCUPIED) {
697 if(status!=NULL) strcpy(status, "invalid VOL struct");
698 return(1);
699 }
700 if(r->z2<r->z1 || r->y2<r->y1 || r->x2<r->x1) vrdReorder(r);
701 if(r->z1<1 || r->y1<1 || r->x1<1 ||
702 r->z2>vol->dimz || r->y2>vol->dimy || r->x2>vol->dimx)
703 {
704 if(status!=NULL) strcpy(status, "invalid volume range");
705 return(2);
706 }
707 if(isnan(in) && isnan(out)) {
708 if(status!=NULL) strcpy(status, "new values not given");
709 return(0);
710 }
711 int zi, yi, xi;
712
713 if(!isnan(in)) {
714 for(zi=r->z1-1; zi<r->z2; zi++)
715 for(yi=r->y1-1; yi<r->y2; yi++)
716 for(xi=r->x1-1; xi<r->x2; xi++)
717 vol->v[zi][yi][xi]=in;
718 }
719
720 if(!isnan(out)) {
721 for(zi=1; zi<=vol->dimz; zi++)
722 for(yi=1; yi<=vol->dimy; yi++)
723 for(xi=1; xi<=vol->dimx; xi++) {
724 if(zi>=r->z1 && zi<=r->z2 &&
725 yi>=r->y1 && yi<=r->y2 &&
726 xi>=r->x1 && xi<=r->x2)
727 continue;
728 vol->v[zi-1][yi-1][xi-1]=out;
729 }
730 }
731
732 if(status!=NULL) strcpy(status, "ok");
733 return(0);
734}
int vrdReorder(VOL_RANGE *vol_range)
Definition vol.c:607

◆ vrdRead()

int vrdRead ( char * vrdfile,
VOL_RANGE * vol_range,
char * status )

Read Volume Range Definition File.

Returns
Returns 0 if successful.
See also
vrdReorder, vrdVxlNr, irdRead
Parameters
vrdfileVolume Range Definition File filename, which contains image volume corners (x y z) in IFT format: corner1 = x y z corner2 = x y z
vol_rangeImage volume range
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed

Definition at line 742 of file vol.c.

751 {
752 int ret, ii, x, y, z;
753 IFT ift;
754 char key[256];
755
756 /* Check that input is ok */
757 if(vrdfile==NULL || strlen(vrdfile)<1 || vol_range==NULL) {
758 if(status!=NULL) strcpy(status, "program error");
759 return 1;
760 }
761 /* Read VDF as IFT file */
762 iftInit(&ift); ret=iftRead(&ift, vrdfile, 1, 0); if(ret) {
763 if(status!=NULL) strcpy(status, ift.status);
764 iftEmpty(&ift); return 2;
765 }
766 /* Try to find keys 'corner1' and 'corner2' */
767 strcpy(key, "corner1"); ii=iftGet(&ift, key, 0);
768 if(ii>=0) {
769 ret=string_to_xyz(ift.item[ii].value, &x, &y, &z);
770 if(ret==0) {
771 vol_range->x1=x; vol_range->y1=y; vol_range->z1=z;
772 strcpy(key, "corner2"); ii=iftGet(&ift, key, 0);
773 if(ii>=0) {
774 ret=string_to_xyz(ift.item[ii].value, &x, &y, &z);
775 vol_range->x2=x; vol_range->y2=y; vol_range->z2=z;
776 if(ret==0) {
777 vrdReorder(vol_range);
778 if(status!=NULL) strcpy(status, "ok");
779 iftEmpty(&ift); return 0;
780 }
781 }
782 }
783 }
784 /* We are here only if keys were not found */
785 /* Lets not care about keys at all */
786 for(ii=0, ret=0; ii<ift.keyNr; ii++) {
787 if(ret==0 && string_to_xyz(ift.item[ii].value, &x, &y, &z)==0)
788 {
789 vol_range->x1=x; vol_range->y1=y; vol_range->z1=z;
790 ret++; continue;
791 }
792 if(ret==1 && string_to_xyz(ift.item[ii].value, &x, &y, &z)==0)
793 {
794 vol_range->x2=x; vol_range->y2=y; vol_range->z2=z;
795 ret++; break;
796 }
797 }
798 if(ret<2) {
799 if(status!=NULL) strcpy(status, "volume definitions not found");
800 iftEmpty(&ift); return 2;
801 }
802
803 vrdReorder(vol_range);
804 if(status!=NULL) strcpy(status, "ok");
805 iftEmpty(&ift);
806 return 0;
807}
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int iftGet(IFT *ift, char *key, int verbose)
Definition iftsrch.c:15
int keyNr
Definition libtpcmisc.h:270
const char * status
Definition libtpcmisc.h:277
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279
int string_to_xyz(char *str, int *x, int *y, int *z)
Definition vol.c:653

◆ vrdReorder()

int vrdReorder ( VOL_RANGE * vol_range)

Reorder Volume Range Definition.

Returns
Returns 0 if successful.
See also
vrdRead, vrdVxlNr, irdRead
Parameters
vol_rangeImage volume range; start and end range are set in correct order

Definition at line 607 of file vol.c.

610 {
611 int i;
612
613 /* Check that input is ok */
614 if(vol_range==NULL) return 1;
615 /* Change the order if necessary */
616 if(vol_range->x1<0 || vol_range->x2<0) return 2;
617 if(vol_range->x2<vol_range->x1) {
618 i=vol_range->x1; vol_range->x1=vol_range->x2; vol_range->x2=i;}
619 if(vol_range->y1<0 || vol_range->y2<0) return 3;
620 if(vol_range->y2<vol_range->y1) {
621 i=vol_range->y1; vol_range->y1=vol_range->y2; vol_range->y2=i;}
622 if(vol_range->z1<0 || vol_range->z2<0) return 4;
623 if(vol_range->z2<vol_range->z1) {
624 i=vol_range->z1; vol_range->z1=vol_range->z2; vol_range->z2=i;}
625 return 0;
626}

Referenced by vrd2vol(), and vrdRead().

◆ vrdVxlNr()

int vrdVxlNr ( VOL_RANGE * vol_range)

Get the number of voxels in Volume Range Definition.

Returns
Returns the nr of voxels in the volume range.
See also
vrdReorder, vrdRead
Parameters
vol_rangeImage volume range; start and end range must be in correct order

Definition at line 634 of file vol.c.

637 {
638 int x, y, z;
639
640 if(vol_range==NULL) return(0);
641 z=1+vol_range->z2-vol_range->z1;
642 x=1+vol_range->x2-vol_range->x1;
643 y=1+vol_range->y2-vol_range->y1;
644 return(z*x*y);
645}

Variable Documentation

◆ _volStatusMessage

char* _volStatusMessage[]
Initial value:
= {
"ok",
"fault in calling routine",
"out of memory"
}

Status (error) messages from volume processing

Definition at line 13 of file vol.c.

13 {
14 /* 0 */ "ok",
15 /* 1 */ "fault in calling routine",
16 /* 2 */ "out of memory"
17};

Referenced by img2svol(), img2vol(), svol2img(), svolAllocate(), svolEmpty(), svolInit(), vol2img(), volAllocate(), volEmpty(), and volInit().

◆ VOL_TEST

int VOL_TEST

Verbose prints from Vol functions

Definition at line 6 of file vol.c.

Referenced by img2svol(), img2vol(), svol2img(), svolAllocate(), svolEmpty(), svolInfo(), svolInit(), vol2img(), volAllocate(), volEmpty(), volInfo(), and volInit().