TPCCLIB
Loading...
Searching...
No Matches
libtpcroi.h File Reference

Header file for libtpcroi. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <ctype.h>
#include <time.h>
#include "libtpcmisc.h"
#include "libtpcimgio.h"

Go to the source code of this file.

Data Structures

struct  ROI
struct  RoiList
struct  ROI_list

Macros

#define BACKUP_EXTENSION   ".bak"
#define ROI_RECTANGULAR   0
#define ROI_CIRCULAR   1
#define ROI_ELLIPSE   2
#define ROI_TRACE   3
#define roiEmpty   roi_empty
#define roiInit   roi_init
#define roiPrint   roi_print

Functions

void roi_init (ROI_list *rl)
void roi_empty (ROI_list *rl)
void roi_delete (ROI_list *rl, ROI *roi)
void roi_delete_n (ROI_list *rl, int index)
int roi_read (const char *fname, ROI_list *rl)
int roi_save (const char *fname, ROI_list *rl)
int roi_append (FILE *fp, ROI *roi)
int roi_append_n (FILE *fp, ROI_list *rl, int ind)
int roi_onoff (ROI *roi, int dimx, int dimy, char **m)
int roi_compute_rect (ROI *roi)
int roi_compute_circle (ROI *roi)
int roi_compute_ellipse (ROI *roi)
void roi_print (ROI *roi)
int jsqrt (int n)
int roi_mplane (int matnum)
int roi_mframe (int matnum)
void roiDel (ROI_list *rl, int ind)
int roiComputeRect (ROI *roi)
int roiComputeCirc (ROI *roi)
int roiComputeElli (ROI *roi)
int roiRead (const char *fname, ROI_list *rl)
int roiFillGaps (int *x, int *y, int nr, int **ffx, int **ffy)
int roiOnOff (ROI *roi, int dimx, int dimy, char **m)
void roiOnOffPrint (int dimx, int dimy, char **m)
int roiSave (const char *fname, ROI_list *rl)
int roiAppend (char *fname, ROI_list *rl, int ind)

Variables

char roierrmsg [128]

Detailed Description

Header file for libtpcroi.

Author
Vesa Oikonen

Definition in file libtpcroi.h.

Macro Definition Documentation

◆ BACKUP_EXTENSION

#define BACKUP_EXTENSION   ".bak"

Backup file extension

Definition at line 27 of file libtpcroi.h.

◆ ROI_CIRCULAR

#define ROI_CIRCULAR   1

Definition for ROI types

Definition at line 36 of file libtpcroi.h.

Referenced by roi_onoff(), and roiRead().

◆ ROI_ELLIPSE

#define ROI_ELLIPSE   2

Definition for ROI types

Definition at line 38 of file libtpcroi.h.

Referenced by roi_onoff(), and roiRead().

◆ ROI_RECTANGULAR

#define ROI_RECTANGULAR   0

Definition for ROI types

Definition at line 34 of file libtpcroi.h.

Referenced by roi_onoff(), and roiRead().

◆ ROI_TRACE

#define ROI_TRACE   3

Definition for ROI types

Definition at line 40 of file libtpcroi.h.

Referenced by roi_append(), roi_onoff(), roi_read(), roiAppend(), roiRead(), and roiSave().

◆ roiEmpty

#define roiEmpty   roi_empty

Deprecated function name

Definition at line 151 of file libtpcroi.h.

◆ roiInit

#define roiInit   roi_init

Deprecated function name

Definition at line 153 of file libtpcroi.h.

◆ roiPrint

#define roiPrint   roi_print

Deprecated function name

Definition at line 155 of file libtpcroi.h.

Function Documentation

◆ jsqrt()

int jsqrt ( int n)
extern

Calculate integer square root.

Returns
Square root.
Parameters
nInteger

Definition at line 617 of file ecat_roi.c.

620 {
621 int x, x2, q, r, t;
622
623 if(n<2) return n;
624 t=x=n; while(t>>=2) x>>=1; x++;
625 while(1) {
626 q=n/x; r=n%x;
627 if(x<=q) {x2=x+2; if(q<x2 || (q==x2 && r==0)) break;}
628 x=(x+q)>>1;
629 }
630 return x;
631}

Referenced by roiComputeElli().

◆ roi_append()

int roi_append ( FILE * fp,
ROI * roi )
extern

Append ROIs into given file.

Returns
Non-zero in case of an error.
Parameters
fpROI file pointer
roiROI

Definition at line 564 of file ecat_roi.c.

569 {
570 /* Write first line */
571 if(fprintf(fp, "*%s %f %f %d %d %d %d %d %d %d %d %d %s///%d %d\n",
572 roi->imgfile, roi->zoom, roi->recon_zoom,
573 roi->matnum, roi->type, roi->status,
574 roi->pos_x, roi->pos_y, roi->w, roi->h,
575 roi->t, roi->roi, roi->roiname,
576 0, roi->point_nr ) < 10 )
577 {
578 strcpy(roierrmsg, "cannot write data");
579 fclose(fp); return 1;
580 }
581 /* Write second line, if trace ROI */
582 if(roi->type==ROI_TRACE) {
583 int j;
584 for(j=0; j<roi->point_nr; j++)
585 fprintf(fp, "%d %d ", roi->x[j], roi->y[j]);
586 fprintf(fp, "\n");
587 }
588 return 0;
589}
char roierrmsg[128]
Definition ecat_roi.c:11
#define ROI_TRACE
Definition libtpcroi.h:40
float zoom
Definition libtpcroi.h:47
int pos_x
Definition libtpcroi.h:57
int t
Definition libtpcroi.h:65
int * x
Definition libtpcroi.h:72
int pos_y
Definition libtpcroi.h:59
int type
Definition libtpcroi.h:53
char roiname[256]
Definition libtpcroi.h:69
int matnum
Definition libtpcroi.h:51
int point_nr
Definition libtpcroi.h:78
int roi
Definition libtpcroi.h:67
int status
Definition libtpcroi.h:55
int h
Definition libtpcroi.h:63
float recon_zoom
Definition libtpcroi.h:49
int * y
Definition libtpcroi.h:75
char imgfile[FILENAME_MAX]
Definition libtpcroi.h:45
int w
Definition libtpcroi.h:61

Referenced by roi_append_n(), and roi_save().

◆ roi_append_n()

int roi_append_n ( FILE * fp,
ROI_list * rl,
int ind )
extern

Append given ROI into file.

Returns
Non-zero in case of an error.
Parameters
fpROI file pointer
rlROI list
indROI index

Definition at line 595 of file ecat_roi.c.

602 {
603 RoiList *rois=rl->rois;
604 int pos=0;
605 while(pos<ind) {
606 rois=rois->next;
607 pos++;
608 }
609 return roi_append(fp,rois->roi);
610}
int roi_append(FILE *fp, ROI *roi)
Definition ecat_roi.c:564
RoiList * rois
Definition libtpcroi.h:96
ROI * roi
Definition libtpcroi.h:86
struct _RoiList * next
Definition libtpcroi.h:88

◆ roi_compute_circle()

int roi_compute_circle ( ROI * roi)
extern

Compute circular ROI.

Returns
Non-zero in case of an error.
Parameters
roiROI

Definition at line 287 of file ecat_roi.c.

290 {
291 int points,r;
292 r=roi->w/2;
293 roi->pos_x+=r;
294 roi->pos_y+=roi->w;
295 roi->point_nr=36; // TODO: clever algorithm to decide how many points we need
296 roi->x=malloc(sizeof(int)*roi->point_nr);
297 roi->y=malloc(sizeof(int)*roi->point_nr);
298 for(points=0;points<roi->point_nr;points++) {
299 float rad=(M_PI/180.0)*(360.0*(points/(float)roi->point_nr));
300 roi->x[points]=sin(rad)*r;
301 roi->y[points]=cos(rad)*r-r;
302 }
303 return 0;
304}

◆ roi_compute_ellipse()

int roi_compute_ellipse ( ROI * roi)
extern

Compute ellipse ROI.

Returns
Non-zero in case of an error.
Parameters
roiROI

Definition at line 311 of file ecat_roi.c.

314 {
315 int points;
316 roi->pos_x+=roi->w/2;
317 roi->pos_y+=roi->h;
318 roi->point_nr=40; // TODO: clever algorithm to decide how many points we need
319 roi->x=malloc(sizeof(int)*roi->point_nr);
320 roi->y=malloc(sizeof(int)*roi->point_nr);
321 for(points=0;points<roi->point_nr;points++) {
322 float rad=(M_PI/180.0)*(360.0*(points/(float)roi->point_nr));
323 roi->x[points]=sin(rad)*(roi->w/2);
324 roi->y[points]=cos(rad)*(roi->h/2)-roi->h/2;
325 }
326 return 0;
327
328}

◆ roi_compute_rect()

int roi_compute_rect ( ROI * roi)
extern

Compute rectangular ROI.

Returns
Non-zero in case of an error.
Parameters
roiROI

Definition at line 244 of file ecat_roi.c.

247 {
248 roi->point_nr=4;
249 if((roi->x=malloc(roi->point_nr*sizeof(int)))==NULL)
250 return(-1);
251 if((roi->y=malloc(roi->point_nr*sizeof(int)))==NULL) {
252 free(roi->x); return(-1);}
253 roi->x[0]=0; roi->y[0]=0;
254 roi->x[1]=roi->w; roi->y[1]=0;
255 roi->x[2]=roi->w; roi->y[2]=roi->h;
256 roi->x[3]=0; roi->y[3]=roi->h;
257 return(0);
258}

◆ roi_delete()

void roi_delete ( ROI_list * rl,
ROI * roi )
extern

Deletes ROI from roilist

Parameters
rlROI list
roiROI

Definition at line 89 of file ecat_roi.c.

94 {
95 RoiList *rois=rl->rois;
96 while(rois->roi!=roi) {
97 rois=rois->next;
98 if(!rois) return;
99 }
100 if(rois->prev) rois->prev->next=rois->next; else rl->rois=rois->next;
101 if(rois->next) rois->next->prev=rois->prev;
102 if(rois->roi->x) free(rois->roi->x);
103 if(rois->roi->y) free(rois->roi->y);
104 free(rois->roi);
105 free(rois);
106 rl->nr--;
107}
struct _RoiList * prev
Definition libtpcroi.h:90

◆ roi_delete_n()

void roi_delete_n ( ROI_list * rl,
int ind )
extern

Deletes the index:th ROI

Parameters
rlROI list
indROI index

Definition at line 64 of file ecat_roi.c.

69 {
70 RoiList *rois=rl->rois;
71 int pos=0;
72 while(pos<ind) {
73 rois=rois->next;
74 if(!rois) return;
75 pos++;
76 }
77 if(rois->prev) rois->prev->next=rois->next; else rl->rois=rois->next;
78 if(rois->next) rois->next->prev=rois->prev;
79 if(rois->roi->x) free(rois->roi->x);
80 if(rois->roi->y) free(rois->roi->y);
81 free(rois->roi);
82 free(rois);
83 rl->nr--;
84}

◆ roi_empty()

void roi_empty ( ROI_list * rl)
extern

Allocated memory is freed, all data is cleared.

Parameters
rlROI list

Definition at line 18 of file ecat_roi.c.

21 {
22 int r;
23 RoiList *next;
24#ifdef SUPPORT_LEGACY
25 if(rl->roi) {
26 for(r=0; r<rl->nr; r++) {
27 if(rl->roi[r].point_nr>0) {
28 free(rl->roi[r].x);
29 free(rl->roi[r].y);
30 rl->roi[r].point_nr=0;
31 }
32 }
33 }
34 if(rl->_allocNr>0) free(rl->roi);
35 rl->_allocNr=0;
36#endif
37 rl->nr=0;
38 while(rl->rois) {
39 next=rl->rois->next;
40 free(rl->rois);
41 rl->rois=next;
42 }
43}
ROI * roi
Definition libtpcroi.h:104

◆ roi_init()

void roi_init ( ROI_list * rl)
extern

Before any use, this should be called, but not later.

Parameters
rlROI list

Definition at line 49 of file ecat_roi.c.

52 {
53 rl->roi=(ROI*)NULL;
54 rl->nr=0;
55#ifdef SUPPORT_LEGACY
56 rl->_allocNr=0;
57 rl->rois=NULL;
58#endif
59}

◆ roi_mframe()

int roi_mframe ( int matnum)
extern
Returns
the frame number of specified matrix
Parameters
matnumMatnum

Definition at line 649 of file ecat_roi.c.

652 {
653 Matval matval;
654
655 mat_numdoc(matnum, &matval);
656 return matval.frame;
657}
void mat_numdoc(int matnum, Matval *matval)
Definition ecat63ml.c:254
int frame

Referenced by roi_print().

◆ roi_mplane()

int roi_mplane ( int matnum)
extern
Returns
the plane number of specified matrix
Parameters
matnumMatnum

Definition at line 636 of file ecat_roi.c.

639 {
640 Matval matval;
641
642 mat_numdoc(matnum, &matval);
643 return matval.plane;
644}
int plane

Referenced by roi_print().

◆ roi_onoff()

int roi_onoff ( ROI * roi,
int dimx,
int dimy,
char ** m )
extern

The matrix is filled with 0's (outside of ROI) and 1's (inside the ROI border).

If ROI extends outside image borders, those points are ignored. Matrix coordinates are up-to-bottom and left-to-right.

Remarks
Does not work correctly in case of some trace ROIs.
See also
roiOnOff
Returns
Function returns 0, if everything is OK.
Parameters
roiDefinitions for one region-of-interest
dimxImage matrix x dimension when ROI was drawn (1, 2, 3, or 4 times magnified).
dimyImage matrix y dimension when ROI was drawn.
mAllocated memory for output matrix[dimx][dimy]

Definition at line 451 of file ecat_roi.c.

460 {
461 /* Check the parameters */
462 if(roi==NULL || dimx<2 || dimy<2 || m==NULL) {
463 strcpy(roierrmsg, "invalid arguments for roi_onoff()");
464 return 1;
465 }
466 /* Prepare */
467 float pos_x=roi->pos_x;
468 float pos_y=roi->pos_y;
469 for(int x=0; x<dimx; x++) for(int y=0; y<dimy; y++) m[x][y]=0;
470
471 /* Fill the matrix */
472 switch(roi->type) {
473 case ROI_TRACE: /* Trace ROI */
474 fill_traceroi(m, dimx, dimy, pos_x, pos_y, roi->x, roi->y, roi->point_nr);
475 break;
476 case ROI_RECTANGULAR: /* Rectangle ROI */
477 for(int x=pos_x; x<pos_x+roi->w; x++)
478 for(int y=pos_y; y<pos_y+roi->h; y++)
479 m[x][y]=1;
480 break;
481 case ROI_CIRCULAR:{ /* Circle ROI */
482 int x1, x2;
483 int y1, y2;
484 int r=roi->w;
485 for(int y=0; y<r; y++) {
486 y1=pos_y-y;
487 y2=pos_y+y;
488 x1=temp_roundf(sqrt(r*r-y*y));
489 x2=pos_x+x1;
490 x1=pos_x-x1;
491 if(x1<0) x1=0;
492 if(x2>dimx) x2=dimx;
493 while(x1<x2) {
494 if(y1>=0 && y1<dimy) m[x1][y1]=1;
495 if(y2>=0 && y2<dimy) m[x1][y2]=1;
496 x1++;
497 }
498 }
499 } break;
500 case ROI_ELLIPSE: { /* Ellipse ROI */
501 int x1, x2, a;
502 int y1, y2, b;
503 a=roi->w;
504 b=roi->h;
505 for(int y=0; y<b; y++) {
506 y1=pos_y-y;
507 y2=pos_y+y;
508 x1=sqrt(1-(y*y)/(float)(b*b))*a;
509 x2=pos_x+x1;
510 x1=pos_x-x1;
511 if(x1<0) x1=0;
512 if(x2>dimx) x2=dimx;
513 while(x1<x2) {
514 if(y1>=0 && y1<dimy) m[x1][y1]=1;
515 if(y2>=0 && y2<dimy) m[x1][y2]=1;
516 x1++;
517 }
518 }
519 } break;
520 default:
521 strcpy(roierrmsg,"invalid roi type");
522 return 2;
523 }
524 return 0;
525}
int temp_roundf(float e)
Definition petc99.c:20
#define ROI_CIRCULAR
Definition libtpcroi.h:36
#define ROI_ELLIPSE
Definition libtpcroi.h:38
#define ROI_RECTANGULAR
Definition libtpcroi.h:34

◆ roi_print()

void roi_print ( ROI * roi)
extern

Prints the contents of (individual) roi data structure.

Parameters
roiROI

Definition at line 333 of file ecat_roi.c.

336 {
337 int i;
338
339 printf("imgfile: %s\n", roi->imgfile);
340 printf("zoom: %f\n recon_zoom: %f\n", roi->zoom, roi->recon_zoom);
341#if 0
342 printf("matnum: %d ; plane: %d frame: %d\n",
343 roi->matnum, roi_mplane(roi->matnum), roi_mframe(roi->matnum) );
344#endif
345 printf("type: %d\n status: %d\n", roi->type, roi->status);
346 printf("pos_x=%d pos_y=%d w=%d h=%d t=%d\n",
347 roi->pos_x, roi->pos_y, roi->w, roi->h, roi->t);
348 printf("roi=%d _%s_\n", roi->roi, roi->roiname);
349 printf("points=%d\n", roi->point_nr);
350 if(roi->point_nr>0) {
351 for(i=0; i<roi->point_nr; i++) printf("(%d,%d) ", roi->x[i], roi->y[i]);
352 printf("\n");
353 }
354}
int roi_mplane(int matnum)
Definition ecat_roi.c:636
int roi_mframe(int matnum)
Definition ecat_roi.c:649

◆ roi_read()

int roi_read ( const char * fname,
ROI_list * rl )
extern

Adds ROI file contents (all ROIs) to specified data structure.

Returns
Returns 0, if OK.
Parameters
fnameFilename
rlROI list

Definition at line 114 of file ecat_roi.c.

119 {
120 RoiList *newroientry;
121 RoiList *roilist=rl->rois;
122 ROI *newroi;
123 int verbose=0;
124
125 if(verbose>0) printf("roi_read(%s, rl)\n", fname);
126 if(fname==NULL || rl==NULL) return(1);
127
128 /* read the lines of the ROI file */
129 STR_TOKEN_LIST lines;
130 str_token_list_init(&lines);
131 if(textfileReadLines(fname, &lines)!=0) {
132 strcpy(roierrmsg, "cannot read file");
133 str_token_list_empty(&lines); return(1);
134 }
135 if(verbose>3)
136 for(int i=0; i<lines.token_nr; i++)
137 printf("%d: %s\n", 1+i, lines.tok[i]);
138
139 /* Prepare for the data */
140 if(roilist) while(roilist->next) roilist=roilist->next;
141 int n=0; // roi number
142
143 int li=0;
144 while(li<lines.token_nr) {
145
146 /* Allocate space for the new ROI and initialize it*/
147 newroi=malloc(sizeof(ROI));
148 memset(newroi, 0, sizeof(ROI));
149 newroi->userdata=NULL;
150
151 /* Find first line that starts with '*' */
152 for(; li<lines.token_nr; li++) if(lines.tok[li][0]=='*') break;
153 if(li==lines.token_nr) break;
154
155 /* Read line contents until roiname */
156 int c=sscanf(lines.tok[li], "*%s %f %f %d %d %d %d %d %d %d %d %d ",
157 newroi->imgfile, &newroi->zoom, &newroi->recon_zoom,
158 &newroi->matnum, &newroi->type, &newroi->status,
159 &newroi->pos_x, &newroi->pos_y, &newroi->w, &newroi->h, &newroi->t,
160 &newroi->roi);
161 if(c!=12) {
162 strcpy(roierrmsg, "invalid format");
163 str_token_list_empty(&lines); return(2);
164 }
165
166 /* read roi name and the rest of the line */
167 char buf[256]="", buf2[256];
168 int ti=13;
169 while(strTokenNCpy(lines.tok[li], " \t", ti, buf2, 256)>0) {
170 if(strlen(buf)>0) strcat(buf, " ");
171 strcat(buf, buf2);
172 ti++;
173 }
174 if(strlen(buf)<3) {
175 strcpy(roierrmsg, "invalid roi name");
176 free(newroi->x); free(newroi->y); free(newroi);
177 str_token_list_empty(&lines); return(3);
178 }
179 char *cptr=strstr(buf, "///"); if(cptr!=NULL) {*cptr=(char)0; cptr+=3;}
180 c=strncpyCleanSpaces(newroi->roiname, buf, 128);
181 if(c==0) sprintf(newroi->roiname, "ROI%d", 1+n);
182 strReplaceChar(newroi->roiname, ' ', '_');
183 /* Read point number, skipping useless value before it */
184 c=sscanf(cptr, "%*d %d \n", &newroi->point_nr);
185 if(c!=1) {
186 strcpy(roierrmsg, "invalid roi point number");
187 free(newroi->x); free(newroi->y); free(newroi);
188 str_token_list_empty(&lines); return(4);
189 }
190
191 /* Trace ROI should contain another line with points */
192 if(newroi->type==ROI_TRACE && newroi->point_nr>0) {
193 li++; //printf(" line='%s'\n", lines.tok[li]);
194 if(strTokenNr(lines.tok[li], " \t") < 2*newroi->point_nr) {
195 strcpy(roierrmsg, "cannot read ROI border definition");
196 free(newroi->x); free(newroi->y); free(newroi);
197 str_token_list_empty(&lines); return(7);
198 }
199 newroi->x=malloc(newroi->point_nr*sizeof(int));
200 newroi->y=malloc(newroi->point_nr*sizeof(int));
201 char xs[16], ys[16];
202 int i;
203 for(i=0; i<newroi->point_nr; i++) {
204 if(strTokenNCpy(lines.tok[li], " \t", 2*i+1, xs, 16)<1) break;
205 if(strTokenNCpy(lines.tok[li], " \t", 2*i+2, ys, 16)<1) break;
206 newroi->x[i]=atoi(xs);
207 newroi->y[i]=atoi(ys);
208 } //printf("i=%d / %d\n", i, newroi->point_nr);
209 if(i < newroi->point_nr) {
210 strcpy(roierrmsg, "cannot read ROI border definition");
211 free(newroi->x); free(newroi->y); free(newroi);
212 str_token_list_empty(&lines); return(9);
213 }
214 }
215 /* Append ROI to list */
216 newroientry=malloc(sizeof(RoiList));
217 newroientry->roi=newroi;
218 newroientry->prev=roilist;
219 newroientry->next=NULL;
220 if(roilist) {
221 roilist->next=newroientry;
222 roilist=newroientry;
223 } else {
224 roilist=newroientry;
225 rl->rois=roilist;
226 }
227 rl->nr++;
228 n++; li++;
229 }
230 str_token_list_empty(&lines);
231
232 if(n==0) {
233 strcpy(roierrmsg, "error in ROI file");
234 return(2);
235 }
236 return(0);
237}
void str_token_list_empty(STR_TOKEN_LIST *lst)
Definition readfile.c:26
int textfileReadLines(const char *filename, STR_TOKEN_LIST *lst)
Definition readfile.c:136
void strReplaceChar(char *str, char c1, char c2)
Definition strext.c:159
void str_token_list_init(STR_TOKEN_LIST *lst)
Definition readfile.c:13
int strTokenNCpy(const char *str1, const char *str2, int i, char *str3, int count)
Definition strext.c:45
int strncpyCleanSpaces(char *s1, const char *s2, int maxlen)
Definition strext.c:308
int strTokenNr(const char *str1, const char *str2)
Definition strext.c:17
void * userdata
Definition libtpcroi.h:80

◆ roi_save()

int roi_save ( const char * fname,
ROI_list * rl )
extern

Save ROI list in file.

Returns
Non-zero in case of an error.
Parameters
fnameFile name
rlROI list

Definition at line 530 of file ecat_roi.c.

535 {
536 RoiList *rois;
537 FILE *fp;
538
539 /* Check arguments */
540 if(!fname[0] || rl==NULL || rl->nr<1) {
541 strcpy(roierrmsg, "invalid arguments for roiSave()");
542 return 1;
543 }
544
545 /* Create file */
546 if((fp=fopen(fname, "w"))==NULL) {
547 strcpy(roierrmsg, "cannot open file");
548 return 2;
549 }
550 /* Loop and save the ROIs */
551 rois=rl->rois;
552 while(rois) {
553 if(roi_append(fp,rois->roi)) return 3;
554 rois=rois->next;
555 }
556 return 0;
557}

◆ roiAppend()

int roiAppend ( char * fname,
ROI_list * rl,
int ind )
extern

Append ROIs in given file to given ROI list.

Returns
Non-zero in case of an error.
Parameters
fnameFile name
rlRoilist
indIndex

Definition at line 554 of file ecat_roi_old.c.

561 {
562 int i, j;
563 FILE *fp;
564
565 /* Check arguments */
566 if(!fname[0] || rl==NULL || rl->nr<1) {
567 strcpy(roierrmsg, "invalid arguments for roiAppend()");
568 return(1);
569 }
570
571 /* Create file */
572 if((fp=fopen(fname, "a"))==NULL) {
573 strcpy(roierrmsg, "cannot open file");
574 return(2);
575 }
576
577 /* Check ind, which roi to append to file */
578 if(ind==0) {
579 for(i=0; i<rl->nr; i++) {
580 /* Write first line */
581 if(fprintf(fp, "*%s %f %f %d %d %d %d %d %d %d %d %d %s///%d %d\n",
582 rl->roi[i].imgfile, rl->roi[i].zoom, rl->roi[i].recon_zoom,
583 rl->roi[i].matnum, rl->roi[i].type, rl->roi[i].status,
584 rl->roi[i].pos_x, rl->roi[i].pos_y, rl->roi[i].w, rl->roi[i].h,
585 rl->roi[i].t, rl->roi[i].roi, rl->roi[i].roiname,
586 0, rl->roi[i].point_nr ) < 10 ) {
587 strcpy(roierrmsg, "cannot write data");
588 fclose(fp); return(3);
589 }
590 /* Write second line, if trace ROI */
591 if(rl->roi[i].type!=ROI_TRACE) continue;
592 for(j=0; j<rl->roi[i].point_nr; j++)
593 fprintf(fp, "%d %d ", rl->roi[i].x[j], rl->roi[i].y[j]);
594 fprintf(fp, "\n");
595 }
596 } else if(ind>0) {
597 i=ind-1;
598 /* Write first line */
599 if(fprintf(fp, "*%s %f %f %d %d %d %d %d %d %d %d %d %s///%d %d\n",
600 rl->roi[i].imgfile, rl->roi[i].zoom, rl->roi[i].recon_zoom,
601 rl->roi[i].matnum, rl->roi[i].type, rl->roi[i].status,
602 rl->roi[i].pos_x, rl->roi[i].pos_y, rl->roi[i].w, rl->roi[i].h,
603 rl->roi[i].t, rl->roi[i].roi, rl->roi[i].roiname,
604 0, rl->roi[i].point_nr ) < 10 ) {
605 strcpy(roierrmsg, "cannot write data");
606 fclose(fp); return(3);
607 }
608 /* Write second line, if trace ROI */
609 //if(rl->roi[i].type!=ROI_TRACE) continue;
610 for(j=0; j<rl->roi[i].point_nr; j++)
611 fprintf(fp, "%d %d ", rl->roi[i].x[j], rl->roi[i].y[j]);
612 fprintf(fp, "\n");
613 }
614
615 /* Close file */
616 fclose(fp);
617 return(0);
618}

◆ roiComputeCirc()

int roiComputeCirc ( ROI * roi)
extern

Compute circular ROI.

Returns
Non-zero in case of error.
Parameters
roiROI

Definition at line 199 of file ecat_roi_old.c.

202 {
203 int i, d, x, y, n, r, s;
204
205 r=roi->w;
206 if((roi->x=malloc(sizeof(int)))==NULL) return(-1);
207 if((roi->y=malloc(sizeof(int)))==NULL) {free((char*)roi->x); return(-1);}
208 roi->x[0]=0; roi->y[0]=r;
209 if(r==0) n=1; else {
210 for(x=1, y=r, d=3-2*r, n=1; x<=y; x++) {
211 if(d<0) d=d+4*x+6; else {d=d+4*(x-y)+10; y-=1;}
212 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
213 return(-1);
214 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
215 free((char*)roi->x); free((char*)roi->y); return(-1);}
216 roi->x[n]=x; roi->y[n]=y;
217 n++;
218 }
219 }
220 for(i=n-1, s=i; i>0; i--) {
221 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
222 return(-1);
223 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
224 free((char*)roi->x); free((char*)roi->y); return(-1);}
225 roi->x[n]=roi->y[--s]; roi->y[n++]=roi->x[s];
226 }
227 for(i=n-1, s=i; i>0; i--) {
228 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
229 return(-1);
230 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
231 free((char*)roi->x); free((char*)roi->y); return(-1);}
232 roi->x[n]=roi->x[--s]; roi->y[n++]=-roi->y[s];
233 }
234 for(i=n-1, s=i; i>0; i--) {
235 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
236 return(-1);
237 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
238 free((char*)roi->x); free((char*)roi->y); return(-1);}
239 roi->x[n]=-roi->x[--s]; roi->y[n++]=roi->y[s];
240 }
241 roi->point_nr=n;
242
243 return(0);
244}

Referenced by roiRead().

◆ roiComputeElli()

int roiComputeElli ( ROI * roi)
extern

Compute ellipse ROI.

Returns
Non-zero in case of error.
Parameters
roiROI

Definition at line 251 of file ecat_roi_old.c.

254 {
255 int i, ix, iy, /*ox,*/ oy, n, s, w, h;
256
257 w=roi->w; h=roi->h;
258 if((roi->x=malloc(sizeof(int)))==NULL) return(-1);
259 if((roi->y=malloc(sizeof(int)))==NULL) {free((char*)roi->x); return(-1);}
260 roi->x[0]=0; roi->y[0]=h;
261 n=1; oy=h;
262 for(ix=1; ix<w; ix++) {
263 iy=(jsqrt(h+h*h*(w-ix)*(w+ix)))/w; if(oy-iy>1) break;
264 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
265 return -1;
266 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
267 free((char*)roi->x); free((char*)roi->y); return -1;}
268 roi->x[n]=ix; roi->y[n++]=iy;
269 oy=iy;
270 }
271 /*ox=ix;*/
272 for(iy=oy-1; iy>=0; iy--) {
273 ix=(jsqrt(w+w*w*(h-iy)*(h+iy)))/h;
274 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
275 return -1;
276 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
277 free((char*)roi->x); free((char*)roi->y); return -1;}
278 roi->x[n]=ix; roi->y[n++]=iy;
279 /*ox=ix;*/
280 }
281 for(i=n-1, s=i; i>0; i--) {
282 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
283 return -1;
284 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
285 free((char*)roi->x); free((char*)roi->y); return -1;}
286 roi->x[n]=roi->x[--s]; roi->y[n++]=-roi->y[s];
287 }
288 for(i=n-1, s=i; i>0; i--) {
289 if((roi->x=(int*)realloc((char*)roi->x, (n+1)*sizeof(int)))==NULL)
290 return -1;
291 if((roi->y=(int*)realloc((char*)roi->y, (n+1)*sizeof(int)))==NULL) {
292 free((char*)roi->x); free((char*)roi->y); return -1;}
293 roi->x[n]=-roi->x[--s]; roi->y[n++]=roi->y[s];
294 }
295 roi->point_nr=n;
296
297 return(0);
298}
int jsqrt(int n)
Definition ecat_roi.c:617

Referenced by roiRead().

◆ roiComputeRect()

int roiComputeRect ( ROI * roi)
extern

Compute rectangular ROI.

Returns
Non-zero in case of an error.
Parameters
roiROI

Definition at line 264 of file ecat_roi.c.

267 {
268 roi->point_nr=5;
269 if((roi->x=malloc(roi->point_nr*sizeof(int)))==NULL)
270 return(-1);
271 if((roi->y=malloc(roi->point_nr*sizeof(int)))==NULL) {
272 free((char*)roi->x); return(-1);}
273 roi->x[0]=0; roi->y[0]=0;
274 roi->x[1]=roi->w; roi->y[1]=0;
275 roi->x[2]=roi->w; roi->y[2]=roi->h;
276 roi->x[3]=0; roi->y[3]=roi->h;
277 roi->x[4]=0; roi->y[4]=0;
278 return(0);
279}

Referenced by roiRead().

◆ roiDel()

void roiDel ( ROI_list * rl,
int ind )
extern

If ind=0, all ROIs in list are deleted keeping memory allocated, else only one ROI is deleted (ind=1, first ROI) keeping memory allocated.

Parameters
rlROI list
indROI index

Definition at line 13 of file ecat_roi_old.c.

18 {
19 if(ind==0){
20 for(int r=0; r<rl->nr; r++) {
21 if(rl->roi[r].point_nr>0) {
22 free(rl->roi[r].x);
23 free(rl->roi[r].y);
24 rl->roi[r].point_nr=0;
25 }
26 }
27 rl->nr=0;
28 } else {
29 if(ind>rl->nr) return;
30 /* free x and y for given ROI */
31 if(rl->roi[ind-1].point_nr>0) {
32 free(rl->roi[ind-1].x);
33 free(rl->roi[ind-1].y);
34 rl->roi[ind-1].point_nr=0;
35 }
36 /* copy memory */
37 for(int j=ind; j<rl->nr; j++)
38 memcpy(&rl->roi[j-1], &rl->roi[j], sizeof(ROI));
39 rl->nr--;
40 }
41}

◆ roiFillGaps()

int roiFillGaps ( int * x,
int * y,
int nr,
int ** ffx,
int ** ffy )
extern

Fills the gaps between ROI points; join them with new points.

Allocates memory for new point data in fx and fy.

Returns
Returns the number of new points or =< 0, if error.
Parameters
xROI line x positions
yROI line y positions
nrNr of ROI line points
ffxPointer to new data points
ffyPointer to new data points

Definition at line 307 of file ecat_roi_old.c.

318 {
319 int verbose=0;
320
321 if(verbose>0) {printf("roiFillGaps(.., %d, ..)\n", nr); fflush(stdout);}
322
323 /* Check data */
324 if(nr<1 || x==NULL || y==NULL || ffx==NULL || ffy==NULL) return(0);
325
326 /* Allocate memory for new data */
327 if(verbose>1) {printf(" allocate memory\n"); fflush(stdout);}
328 int *fx, *fy;
329 if((fx=malloc(nr*sizeof(int)))==NULL) return -1;
330 if((fy=malloc(nr*sizeof(int)))==NULL) {free((char*)fx); return -1;}
331
332 /* Process from first to last (last->first is done already) */
333 if(verbose>1) {printf(" process\n"); fflush(stdout);}
334 int nx, ny, x2=0, y2=0;
335 int n=0, j=0;
336 float k;
337 for(int i=1, n=j=0; i<nr; i++) {
338 if(verbose>2) {printf(" i=%d j=%d nr+n=%d \n", i, j, nr+n); fflush(stdout);}
339 int x1=x[i-1];
340 int y1=y[i-1];
341 x2=x[i];
342 y2=y[i];
343 int dx=x2-x1;
344 int dy=y2-y1;
345 fx[j]=x1; fy[j++]=y1;
346 if(abs(dx)<2 && abs(dy)<2) continue;
347 if(abs(dx)>=abs(dy)) {
348 k=(float)dy/(float)dx;
349 if(x2>x1)
350 for(nx=x1+1; nx<x2; nx++) {
351 ny=y1+(int)temp_roundf(k*(nx-x1));
352 fx[j]=nx; fy[j++]=ny; n++;
353 if((fx=(int*)realloc((char*)fx, (nr+n)*sizeof(int)))==NULL) {
354 free((char*)fy); return -1;}
355 if((fy=(int*)realloc((char*)fy, (nr+n)*sizeof(int)))==NULL) {
356 free((char*)fx); return -1;}
357 }
358 else
359 for(nx=x1-1; nx>x2; nx--) {
360 ny=y1+(int)temp_roundf(k*(nx-x1));
361 fx[j]=nx; fy[j++]=ny; n++;
362 if((fx=(int*)realloc((char*)fx, (nr+n)*sizeof(int)))==NULL) {
363 free((char*)fy); return -1;}
364 if((fy=(int*)realloc((char*)fy, (nr+n)*sizeof(int)))==NULL) {
365 free((char*)fx); return -1;}
366 }
367 } else {
368 k=(float)dx/(float)dy;
369 if(y2>y1)
370 for(ny=y1+1; ny<y2; ny++) {
371 nx=x1+(int)temp_roundf(k*(ny-y1));
372 fx[j]=nx; fy[j++]=ny; n++;
373 if((fx=(int*)realloc((char*)fx, (nr+n)*sizeof(int)))==NULL) {
374 free((char*)fy); return -1;}
375 if((fy=(int*)realloc((char*)fy, (nr+n)*sizeof(int)))==NULL) {
376 free((char*)fx); return -1;}
377 }
378 else
379 for(ny=y1-1; ny>y2; ny--) {
380 nx=x1+(int)temp_roundf(k*(ny-y1));
381 fx[j]=nx; fy[j++]=ny; n++;
382 if((fx=(int*)realloc((char*)fx, (nr+n)*sizeof(int)))==NULL) {
383 free((char*)fy); return -1;}
384 if((fy=(int*)realloc((char*)fy, (nr+n)*sizeof(int)))==NULL) {
385 free((char*)fx); return -1;}
386 }
387 }
388 }
389 fx[j]=x2; fy[j++]=y2; n=j;
390 *ffx=fx; *ffy=fy;
391 return(n);
392}

Referenced by roiOnOff().

◆ roiOnOff()

int roiOnOff ( ROI * roi,
int dimx,
int dimy,
char ** m )
extern

The matrix is filled with 0's (outside of ROI), 1's (on the ROI border), and 2's (inside the ROI border).

If ROI extends outside image borders, those points are ignored. Matrix coordinates are up-to-bottom and left-to-right.

See also
roi_onoff
Returns
Function returns 0, if everything is OK.
Parameters
roiDefinitions for one region-of-interest
dimxImage matrix x dimension
dimyImage matrix y dimension
mAllocated memory for output matrix[dimx][dimy]

Definition at line 404 of file ecat_roi_old.c.

413 {
414 int i, j, n, *x, *y, pos_x, pos_y, *fx, *fy;
415 int verbose=0;
416
417 if(verbose>0) {printf("roiOnOff()\n"); fflush(stdout);}
418
419 /* Check the parameters */
420 if(roi==NULL || dimx<2 || dimy<2 || m==NULL) {
421 strcpy(roierrmsg, "invalid arguments for roiOnOff()");
422 return(1);
423 }
424
425 /* Copy ROI points */
426 strcpy(roierrmsg, "out of memory");
427 if((x=malloc((roi->point_nr)*sizeof(int)))==NULL) return(2);
428 if((y=malloc((roi->point_nr)*sizeof(int)))==NULL) {free((char*)x); return(2);}
429 pos_x=roi->pos_x;
430 pos_y=roi->pos_y;
431 for(i=0; i<roi->point_nr; i++) { // list starts, and usually ends, with 0,0
432 x[i]=pos_x+roi->x[i];
433 y[i]=pos_y+roi->y[i];
434 }
435
436 /* Fill the gaps between points */
437 if(verbose>1) {printf("fill gaps\n"); fflush(stdout);}
438 n=roiFillGaps(x, y, roi->point_nr, &fx, &fy);
439 if(n<1) {
440 strcpy(roierrmsg, "cannot fill the gaps in ROI border");
441 free((char*)x); free((char*)y); return(3);
442 }
443
444 /* Set matrix */
445 if(verbose>1) {printf("prepare matrix\n"); fflush(stdout);}
446 for(i=0; i<dimy; i++) for(j=0; j<dimx; j++) m[j][i]=(char)0;
447 for(i=0; i<n; i++) if(fx[i]>=0 && fx[i]<dimx && fy[i]>=0 && fy[i]<dimy)
448 m[fx[i]][fy[i]]=(char)1;
449
450 /* Fill the inside of ROI */
451 if(verbose>1) {printf("fill matrix\n"); fflush(stdout);}
452 for(i=0; i<dimy; i++) if(m[0][i]==0) m[0][i]=2; else break;
453 for(i=dimy-1; i>=0; i--) if(m[0][i]==0) m[0][i]=2; else break;
454 for(i=0; i<dimy; i++) if(m[dimx-1][i]==0) m[dimx-1][i]=2; else break;
455 for(i=dimy-1; i>=0; i--) if(m[dimx-1][i]==0) m[dimx-1][i]=2; else break;
456 for(i=0; i<dimx; i++) if(m[i][0]==0) m[i][0]=2; else break;
457 for(i=dimx-1; i>=0; i--) if(m[i][0]==0) m[i][0]=2; else break;
458 for(i=0; i<dimx; i++) if(m[i][dimy-1]==0) m[i][dimy-1]=2; else break;
459 for(i=dimx-1; i>=0; i--) if(m[i][dimy-1]==0) m[i][dimy-1]=2; else break;
460 for(i=0; i<dimy; i++) for(j=0; j<dimx; j++) {
461 if(m[j][i]!=2) continue;
462 if(i>0 && m[j][i-1]==0) {m[j][i-1]=2; j=-1; i-=2; continue;}
463 if(j>0 && m[j-1][i]==0) {m[j-1][i]=2; j-=2; continue;}
464 if(j+1<dimx) {if(m[j+1][i]!=1) m[j+1][i]=2;}
465 if(i+1<dimy) {if(m[j][i+1]!=1) m[j][i+1]=2;}
466 m[j][i]=3;
467 }
468 for(i=0; i<dimy; i++) for(j=0; j<dimx; j++)
469 if(m[j][i]==0) m[j][i]=2; else if(m[j][i]>1) m[j][i]=0;
470 //if(m[j][i]<2) m[j][i]=1; else m[j][i]=0;
471
472 /* Free memory */
473 if(verbose>1) {printf("clean-up\n"); fflush(stdout);}
474 free((char*)x); free((char*)y);
475 free((char*)fx); free((char*)fy);
476
477 return(0);
478}
int roiFillGaps(int *x, int *y, int nr, int **ffx, int **ffy)

◆ roiOnOffPrint()

void roiOnOffPrint ( int dimx,
int dimy,
char ** m )
extern

Print ROI on-off matrix.

Parameters
dimxOn-off matrix x dimension.
dimyOn-off matrix y dimension.
mPointer to on-off matrix[dimx][dimy].

Definition at line 483 of file ecat_roi_old.c.

490 {
491 printf("\non-off matrix:\n"); fflush(stdout);
492 if(m==NULL || dimx<1 || dimy<1) {printf("empty\n"); return;}
493 for(int yi=0; yi<dimy; yi++) {
494 for(int xi=0; xi<dimx; xi++) printf("%d", m[xi][yi]);
495 printf("\n");
496 }
497}

◆ roiRead()

int roiRead ( const char * fname,
ROI_list * rl )
extern

Adds ROI file contents (all ROIs) to specified data structure.

Returns
Returns 0, if OK.
Parameters
fnameFile name
rlROI list

Definition at line 48 of file ecat_roi_old.c.

53 {
54 int i, j, n, ch;
55 FILE *fp;
56 char imgfile[FILENAME_MAX], roiname[FILENAME_MAX];
57 float zoom, recon_zoom;
58 int matrix, type, status, pos_x, pos_y, w, h, t, roi, npts, x, y;
59
60 /* Open ROI file */
61 if((fp=fopen(fname, "r")) == NULL) {
62 strcpy(roierrmsg, "cannot open file");
63 return(1);
64 }
65
66 n=0;
67 while(1) {
68 /* Read first '*' */
69 ch=fgetc(fp); if(ch==EOF || ch!='*') break;
70
71 /* Read image file name */
72 if(fscanf(fp, "%s ", imgfile)==EOF) {
73 strcpy(roierrmsg, "cannot read image filename");
74 fclose(fp); return(3);
75 }
76
77 /* Read zooms */
78 if(fscanf(fp, "%f %f ", &zoom, &recon_zoom)==EOF) {
79 strcpy(roierrmsg, "cannot read zoom values");
80 fclose(fp); return(4);}
81 if(recon_zoom==0.0) recon_zoom=1.0;
82
83 /* Read matrix -> t */
84 if(fscanf(fp, "%d %d %d %d %d %d %d %d ", &matrix, &type, &status,
85 &pos_x, &pos_y, &w, &h, &t)==EOF) {
86 strcpy(roierrmsg, "cannot read ROI definitions");
87 fclose(fp); return(5);
88 }
89
90 /* Read ROI nr and name */
91 if(fscanf(fp, "%d ", &roi)==EOF) {
92 strcpy(roierrmsg, "cannot read ROI number");
93 fclose(fp); return(6);
94 }
95 for(i=j=0; j<3 && i<FILENAME_MAX-1; i++) {
96 roiname[i]=(char)getc(fp);
97 if(roiname[i]=='/') j++; else j=0;
98 }
99 j=i-3; roiname[j]=(char)0;
100 {
101 char buf[FILENAME_MAX]; strcpy(buf, roiname);
102 int c=strncpyCleanSpaces(roiname, buf, 128);
103 if(c==0) sprintf(roiname, "ROI%d", roi);
104 else strReplaceChar(roiname, ' ', '_');
105 }
106
107 /* Read the number of points */
108 (void)getc(fp); (void)getc(fp); /* skip 0 and space */
109 if(fscanf(fp, "%d ", &npts)==EOF) {
110 strcpy(roierrmsg, "cannot read nr of ROI border points");
111 fclose(fp); return(8);
112 }
113
114 /* Allocate memory for ROI */
115 if(rl->_allocNr==0){ rl->roi=malloc(sizeof(ROI)); rl->_allocNr++;}
116 else if(rl->_allocNr<=rl->nr){
117 rl->roi=(ROI*)realloc((char*)rl->roi, (rl->nr+1)*sizeof(ROI));
118 rl->_allocNr++;
119 }
120 if(rl->roi==NULL) {
121 strcpy(roierrmsg, "out of memory");
122 fclose(fp); rl->nr=0; return(11);
123 }
124
125
126 /* Copy data */
127 strcpy(rl->roi[rl->nr].imgfile, imgfile);
128 rl->roi[rl->nr].zoom = zoom;
129 rl->roi[rl->nr].recon_zoom = recon_zoom;
130 rl->roi[rl->nr].matnum = matrix;
131 rl->roi[rl->nr].type = type;
132 rl->roi[rl->nr].status = status;
133 rl->roi[rl->nr].pos_x = pos_x;
134 rl->roi[rl->nr].pos_y = pos_y;
135 rl->roi[rl->nr].w = w;
136 rl->roi[rl->nr].h = h;
137 rl->roi[rl->nr].t = t;
138 rl->roi[rl->nr].roi = roi;
139 strcpy(rl->roi[rl->nr].roiname, roiname);
140 rl->roi[rl->nr].point_nr = npts;
141 rl->roi[rl->nr].userdata=NULL;
142 rl->nr++;
143 n++;
144
145 /* If TRACE ROI, read points */
146 if(type==ROI_TRACE) {
147 if((rl->roi[rl->nr-1].x=malloc((npts+1)*sizeof(int)))==NULL) {
148 strcpy(roierrmsg, "cannot read ROI border definition");
149 fclose(fp); rl->nr--; return(12);
150 }
151 if((rl->roi[rl->nr-1].y=malloc((npts+1)*sizeof(int)))==NULL) {
152 strcpy(roierrmsg, "cannot read ROI border definition");
153 fclose(fp); rl->nr--; free((char*)rl->roi[rl->nr-1].x); return(13);
154 }
155 for(i=0; i<npts; i++)
156 if(fscanf(fp, "%d %d", &x, &y) == EOF) {
157 strcpy(roierrmsg, "cannot read ROI border definition");
158 free((char*)rl->roi[rl->nr-1].x); free((char*)rl->roi[rl->nr-1].y);
159 fclose(fp); rl->nr--; return(9);
160 } else {
161 rl->roi[rl->nr-1].x[i]=x; rl->roi[rl->nr-1].y[i]=y;
162 }
163 do {ch=getc(fp);} while(ch!=EOF && ch!='\n');
164 /* make sure that ROI start and end are connected (extra memory was allocated previously) */
165 if(rl->roi[rl->nr-1].x[npts-1]!=0 || rl->roi[rl->nr-1].y[npts-1]!=0) {
166 rl->roi[rl->nr-1].x[npts]=0;
167 rl->roi[rl->nr-1].y[npts]=0;
168 npts++; rl->roi[rl->nr-1].point_nr++;
169 }
170 }
171 }
172 fclose(fp);
173 if(n==0) {
174 strcpy(roierrmsg, "error in ROI file");
175 return(2);
176 }
177 /* Make points for others than trace ROIs */
178 for(i=rl->nr-n; i<rl->nr; i++) {
179 switch(rl->roi[i].type) {
180 case ROI_RECTANGULAR: j=roiComputeRect(&rl->roi[i]); break;
181 case ROI_CIRCULAR: j=roiComputeCirc(&rl->roi[i]); break;
182 case ROI_ELLIPSE: j=roiComputeElli(&rl->roi[i]); break;
183 case ROI_TRACE: j=0; break;
184 default: j=-1;
185 }
186 if(j) {
187 strcpy(roierrmsg, "cannot read ROI border definition");
188 rl->nr-=n; return(21);
189 }
190 }
191 return(0);
192}
int roiComputeRect(ROI *roi)
Definition ecat_roi.c:264
int roiComputeElli(ROI *roi)
int roiComputeCirc(ROI *roi)

◆ roiSave()

int roiSave ( const char * fname,
ROI_list * rl )
extern

Save ROI list in file.

Returns
Non-zero in case of an error.
Parameters
fnameFile name
rlROI list

Definition at line 504 of file ecat_roi_old.c.

509 {
510 int i, j;
511 FILE *fp;
512
513 /* Check arguments */
514 if(!fname[0] || rl==NULL || rl->nr<1) {
515 strcpy(roierrmsg, "invalid arguments for roiSave()");
516 return(1);
517 }
518
519 /* Create file */
520 if((fp=fopen(fname, "w"))==NULL) {
521 strcpy(roierrmsg, "cannot open file");
522 return(2);
523 }
524
525 for(i=0; i<rl->nr; i++) {
526 /* Write first line */
527 if(fprintf(fp, "*%s %f %f %d %d %d %d %d %d %d %d %d %s///%d %d\n",
528 rl->roi[i].imgfile, rl->roi[i].zoom, rl->roi[i].recon_zoom,
529 rl->roi[i].matnum, rl->roi[i].type, rl->roi[i].status,
530 rl->roi[i].pos_x, rl->roi[i].pos_y, rl->roi[i].w, rl->roi[i].h,
531 rl->roi[i].t, rl->roi[i].roi, rl->roi[i].roiname,
532 0, rl->roi[i].point_nr ) < 10 ) {
533 strcpy(roierrmsg, "cannot write data");
534 fclose(fp); return(3);
535 }
536 /* Write second line, if trace ROI */
537 if(rl->roi[i].type!=ROI_TRACE) continue;
538 for(j=0; j<rl->roi[i].point_nr; j++)
539 fprintf(fp, "%d %d ", rl->roi[i].x[j], rl->roi[i].y[j]);
540 fprintf(fp, "\n");
541 }
542
543 /* Close file */
544 fclose(fp);
545
546 return(0);
547}

Variable Documentation

◆ roierrmsg

char roierrmsg[128]
extern

The ROI library error message

Definition at line 11 of file ecat_roi.c.

Referenced by roi_append(), roi_onoff(), roi_read(), roi_save(), roiAppend(), roiOnOff(), roiRead(), and roiSave().