TPCCLIB
Loading...
Searching...
No Matches
pixel.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcimgio.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
16 IMG_PIXELS *pxl
17) {
18 if(pxl==NULL) return;
19 pxl->pxlNr=pxl->_pxlNr=0;
20 pxl->p=NULL;
21}
22/*****************************************************************************/
23
24/*****************************************************************************/
30 IMG_PIXELS *pxl
31) {
32 if(pxl==NULL) return;
33 free(pxl->p);
34 pxlInit(pxl);
35}
36/*****************************************************************************/
37
38/*****************************************************************************/
47 IMG_PIXELS *pxl,
49 long long int pxlNr
50) {
51 if(pxl==NULL) return(1);
52 /* Delete any previous contents */
53 pxlFree(pxl);
54 /* If no memory is requested, then just return */
55 if(pxlNr<1) return(0);
56
57 /* Allocate memory for IMG_PIXEL data */
58 pxl->p=(IMG_PIXEL*)malloc(pxlNr*sizeof(IMG_PIXEL));
59 if(pxl->p==NULL) return(2);
60 for(long long int i=0; i<pxlNr; i++) {
61 pxl->p[i].x=pxl->p[i].y=pxl->p[i].z=pxl->p[i].f=0;
62 }
63 pxl->_pxlNr=pxlNr;
64 return(0);
65}
66/*****************************************************************************/
67
68/*****************************************************************************/
78 IMG_PIXELS *pxl,
81 long long int pxlNr
82) {
83 if(pxl==NULL) return(1);
84 /* If no memory is requested, then just return */
85 if(pxlNr<1) return(0);
86 /* If none allocated previously then use pxlAllocate */
87 if(pxl->_pxlNr==0) return(pxlAllocate(pxl, pxlNr));
88 /* Check if there is enough space already */
89 long long int newPxlNr, addPxlNr;
90 newPxlNr=pxl->pxlNr+pxlNr; addPxlNr=newPxlNr-pxl->_pxlNr;
91 if(addPxlNr<=0) return(0);
92 /* Reallocate */
93 IMG_PIXEL *pxlPtr;
94 pxlPtr=realloc(pxl->p, sizeof(IMG_PIXEL)*newPxlNr);
95 if(pxlPtr==NULL) return(2);
96 pxl->p=pxlPtr;
97 for(int i=pxl->_pxlNr; i<newPxlNr; i++)
98 pxl->p[i].x=pxl->p[i].y=pxl->p[i].z=pxl->p[i].f=0;
99 pxl->_pxlNr=newPxlNr;
100 return(0);
101}
102/*****************************************************************************/
103
104/*****************************************************************************/
112 IMG_PIXELS *list,
114 long long int i,
116 long long int n
117) {
118 if(list==NULL || i>list->pxlNr) return(1);
119 /* Check whether anything needs to be added */
120 if(n<1) return(0);
121 /* If user wanted room in the end, then just add the memory */
122 if(i==list->pxlNr) return(pxlAllocateMore(list, n));
123 /* Otherwise, first add the space */
124 if(pxlAllocateMore(list, n)!=0) return(2);
125 /* and the move previous data forward in the list */
126 for(long long int li=list->pxlNr-1; li>=i; li--) list->p[li+n]=list->p[li];
127 /* and add pxlNr */
128 list->pxlNr+=n;
129 return(0);
130}
131/*****************************************************************************/
132
133/*****************************************************************************/
142 IMG_PIXELS *list,
144 IMG_PIXEL *pxl
145) {
146 if(list==NULL || pxl==NULL) return(1);
147 int ret;
148 if((ret=pxlAllocateMore(list, 1))!=0) return(ret);
149 list->p[list->pxlNr].x=pxl->x;
150 list->p[list->pxlNr].y=pxl->y;
151 list->p[list->pxlNr].z=pxl->z;
152 list->p[list->pxlNr].f=pxl->f;
153 list->pxlNr++;
154 return(0);
155}
156/*****************************************************************************/
157
158/*****************************************************************************/
165 IMG_PIXELS *list,
167 long long int i,
169 IMG_PIXEL *pxl
170) {
171 if(list==NULL || pxl==NULL || i<0 || i>=list->pxlNr) return(1);
172 pxl->x=list->p[i].x;
173 pxl->y=list->p[i].y;
174 pxl->z=list->p[i].z;
175 return(0);
176}
177/*****************************************************************************/
178
179/*****************************************************************************/
185long long int pxlAddFromMask(
188 IMG_PIXELS *list,
190 IMG *img
191) {
192 if(list==NULL || img==NULL) return(1);
193 if(img->dimz<1 || img->dimy<1 || img->dimx<1 || img->dimt<1) return(0);
194 long long int n=0;
195 for(int zi=0; zi<img->dimz; zi++)
196 for(int yi=0; yi<img->dimy; yi++)
197 for(int xi=0; xi<img->dimx; xi++)
198 if(fabs(img->m[zi][yi][xi][0])>=0.5) n++;
199 if(n==0) return(0);
200 if(pxlAllocateMore(list, n)!=0) return(0);
201 for(int zi=0; zi<img->dimz; zi++)
202 for(int yi=0; yi<img->dimy; yi++)
203 for(int xi=0; xi<img->dimx; xi++)
204 if(fabs(img->m[zi][yi][xi][0])>=0.5) {
205 list->p[list->pxlNr].x=1+xi;
206 list->p[list->pxlNr].y=1+yi;
207 list->p[list->pxlNr].z=1+zi;
208 list->p[list->pxlNr].f=0;
209 list->pxlNr++;
210 }
211 return(n);
212}
213/*****************************************************************************/
214
215/*****************************************************************************/
222 IMG_PIXELS *list,
224 long long int from,
226 long long int to
227) {
228 if(list==NULL || from<0 || to<0) return;
229 if(from>=list->_pxlNr || to>=list->_pxlNr) return;
230 if(from==to) return;
231 long long int i=from;
232 IMG_PIXEL tmp=list->p[from];
233 if(from>to) {
234 for(long long int i=from; i>to; i--) list->p[i]=list->p[i-1];
235 } else {
236 for(long long int i=from; i<to; i++) list->p[i]=list->p[i+1];
237 }
238 list->p[i]=tmp;
239 return;
240}
241/*****************************************************************************/
242
243/*****************************************************************************/
252 IMG_PIXELS *list,
254 long long int index
255) {
256 if(list==NULL || index<0) return(1);
257 if(index>=list->pxlNr) return(0);
258 /* If last one, then just decrease the pxlNr */
259 if(index==list->pxlNr-1) {list->pxlNr--; return(0);}
260 /* Otherwise move it to the last place and then decrease the pxlNr */
261 pxlMove(list, index, list->pxlNr-1);
262 list->pxlNr--;
263 return(0);
264}
265/*****************************************************************************/
266
267/*****************************************************************************/
273long long int pxlRmDuplicates(
276 IMG_PIXELS *list
277) {
278 if(list==NULL || list->pxlNr<2) return(0);
279 long long int i=list->pxlNr-1, j, n=0;
280 while(i>0) {
281 for(j=0; j<i; j++) {
282 if(list->p[i].z!=list->p[j].z) continue;
283 if(list->p[i].x!=list->p[j].x) continue;
284 if(list->p[i].y!=list->p[j].y) continue;
285 pxlRm(list, i); n++; break;
286 }
287 i--;
288 }
289 return(n);
290}
291/*****************************************************************************/
292
293/*****************************************************************************/
301 IMG_PIXELS *pxl,
303 FILE *fp,
306 char *status
307) {
308 if(pxl==NULL || pxl->pxlNr<1 || pxl->p==NULL) {
309 if(status!=NULL) strcpy(status, "no pixels to write");
310 return(1);
311 }
312 int n=7;
313 for(long long i=0; i<pxl->pxlNr && n>6; i++)
314 n=fprintf(fp, "%d,%d,%d,%d\n",
315 pxl->p[i].x, pxl->p[i].y, pxl->p[i].z, pxl->p[i].f);
316 if(n<7) {
317 if(status!=NULL) strcpy(status, "cannot write pixels into file");
318 return(2);
319 }
320 if(status!=NULL) strcpy(status, "ok");
321 return(0);
322}
323/*****************************************************************************/
324
325/*****************************************************************************/
334 IMG_PIXELS *pxl,
336 const char *fname,
339 char *status
340) {
341 if(pxl==NULL) {
342 if(status!=NULL) strcpy(status, "program error");
343 return(1);
344 }
345 int i, c, n, longest, ret;
346
347 /* Try to read the file */
348 FILE *fp;
349 fp=fopen(fname, "r");
350 if(fp==NULL) {
351 if(status!=NULL) strcpy(status, "cannot open file");
352 return(2);
353 }
354 /* Get the length of the longest line */
355 i=longest=0;
356 while((c=fgetc(fp))!=EOF) {
357 if(c==10 || c==13) {if(i>longest) longest=i; i=0;} else i++;
358 }
359 if(i>longest) longest=i;
360 rewind(fp); longest+=1;
361 /* and allocate memory for string of that length */
362 char *line, buf[20];
363 line=(char*)malloc((longest+1)*sizeof(char));
364 if(line==NULL) {
365 if(status!=NULL) strcpy(status, "out of memory");
366 fclose(fp); return(3);
367 }
368 /* Allocate space for a few pixels */
369 if(pxlAllocateMore(pxl, 10)!=0) {
370 if(status!=NULL) strcpy(status, "out of memory");
371 fclose(fp); free(line); return(3);
372 }
373 /* Read data lines */
374 while(fgets(line, longest, fp)!=NULL) {
375 /* forget comment lines */
376 if(line[0]=='#') continue;
377 /* get nr of tokens on this line */
378 n=strTokenNr(line, " ,;\t\n\r"); if(n==0) continue;
379 if(n<3 || n>4) {
380 if(status!=NULL) strcpy(status, "invalid format");
381 fclose(fp); free(line); return(4);
382 }
383 /* Allocate more memory if necessary */
384 if(pxl->pxlNr==pxl->_pxlNr) {
385 if(pxlAllocateMore(pxl, 10)!=0) {
386 if(status!=NULL) strcpy(status, "out of memory");
387 fclose(fp); free(line); return(3);
388 }
389 }
390 /* Read the pixel coordinates */
391 ret=0; //printf("line := %s", line); printf("n := %d\n", n);
392 for(i=0; i<n; i++) {
393 if(strTokenNCpy(line, " ,;\t\n\r", 1+i, buf, 20)<1) {ret=1; break;}
394 //printf(" buf := %s\n", buf);
395 if((ret=atoi_with_check(buf, &c))!=0) break;
396 if(i==0) pxl->p[pxl->pxlNr].x=c;
397 else if(i==1) pxl->p[pxl->pxlNr].y=c;
398 else if(i==2) pxl->p[pxl->pxlNr].z=c;
399 else if(i==3) pxl->p[pxl->pxlNr].f=c;
400 }
401 if(ret) {
402 if(status!=NULL) strcpy(status, "invalid coordinate");
403 fclose(fp); free(line); return(4);
404 }
405 pxl->pxlNr++;
406 }
407 fclose(fp); free(line);
408 return(0);
409}
410/*****************************************************************************/
411
412/*****************************************************************************/
int atoi_with_check(const char *int_as_string, int *result_value)
Definition decpoint.c:238
Header file for libtpcimgio.
int strTokenNCpy(const char *str1, const char *str2, int i, char *str3, int count)
Definition strext.c:45
int strTokenNr(const char *str1, const char *str2)
Definition strext.c:17
int pxlMakeRoom(IMG_PIXELS *list, long long int i, long long int n)
Definition pixel.c:110
void pxlFree(IMG_PIXELS *pxl)
Definition pixel.c:28
int pxlAllocateMore(IMG_PIXELS *pxl, long long int pxlNr)
Definition pixel.c:74
int pxlGet(IMG_PIXELS *list, long long int i, IMG_PIXEL *pxl)
Definition pixel.c:163
int pxlAdd(IMG_PIXELS *list, IMG_PIXEL *pxl)
Definition pixel.c:139
int pxlWrite(IMG_PIXELS *pxl, FILE *fp, char *status)
Definition pixel.c:299
int pxlRead(IMG_PIXELS *pxl, const char *fname, char *status)
Definition pixel.c:331
long long int pxlRmDuplicates(IMG_PIXELS *list)
Definition pixel.c:273
void pxlInit(IMG_PIXELS *pxl)
Definition pixel.c:14
void pxlMove(IMG_PIXELS *list, long long int from, long long int to)
Definition pixel.c:220
int pxlAllocate(IMG_PIXELS *pxl, long long int pxlNr)
Definition pixel.c:44
long long int pxlAddFromMask(IMG_PIXELS *list, IMG *img)
Definition pixel.c:185
int pxlRm(IMG_PIXELS *list, long long int index)
Definition pixel.c:249
long long int _pxlNr
IMG_PIXEL * p
long long int pxlNr
unsigned short int dimx
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy