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

Functions for reading and writing pixel definition files. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

void pxlInit (IMG_PIXELS *pxl)
void pxlFree (IMG_PIXELS *pxl)
int pxlAllocate (IMG_PIXELS *pxl, long long int pxlNr)
int pxlAllocateMore (IMG_PIXELS *pxl, long long int pxlNr)
int pxlMakeRoom (IMG_PIXELS *list, long long int i, long long int n)
int pxlAdd (IMG_PIXELS *list, IMG_PIXEL *pxl)
int pxlGet (IMG_PIXELS *list, long long int i, IMG_PIXEL *pxl)
long long int pxlAddFromMask (IMG_PIXELS *list, IMG *img)
void pxlMove (IMG_PIXELS *list, long long int from, long long int to)
int pxlRm (IMG_PIXELS *list, long long int index)
long long int pxlRmDuplicates (IMG_PIXELS *list)
int pxlWrite (IMG_PIXELS *pxl, FILE *fp, char *status)
int pxlRead (IMG_PIXELS *pxl, const char *fname, char *status)

Detailed Description

Functions for reading and writing pixel definition files.

Author
Vesa Oikonen

Definition in file pixel.c.

Function Documentation

◆ pxlAdd()

int pxlAdd ( IMG_PIXELS * list,
IMG_PIXEL * pxl )

Add given pixel into IMG_PIXELS data.

Returns
0 if successful.
Author
Vesa Oikonen
See also
pxlInit, pxlFree, pxlAllocate, pxlMakeRoom, pxlRm, pxlRead, pxlWrite
Parameters
listPointer to IMG_PIXELS struct, which must be initiated. Memory is added if needed, and pxlNr increased.
pxlPointer to IMG_PIXEL struct to add.

Definition at line 139 of file pixel.c.

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}
int pxlAllocateMore(IMG_PIXELS *pxl, long long int pxlNr)
Definition pixel.c:74
IMG_PIXEL * p
long long int pxlNr

Referenced by imgMaskFloodFill().

◆ pxlAddFromMask()

long long int pxlAddFromMask ( IMG_PIXELS * list,
IMG * img )

Add pixel(s) from mask image into IMG_PIXELS data.

Returns
the nr of added pixels.
See also
pxlInit, pxlFree, pxlAdd
Author
Vesa Oikonen
Parameters
listPointer to IMG_PIXELS struct, which must be initiated. Memory is added if needed, and pxlNr increased.
imgPointer to mask image.

Definition at line 185 of file pixel.c.

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}
unsigned short int dimx
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy

◆ pxlAllocate()

int pxlAllocate ( IMG_PIXELS * pxl,
long long int pxlNr )

Allocate memory for IMG_PIXELS data. Any previous contents are deleted. Return Returns 0 when successful.

See also
pxlInit, pxlAllocateMore, pxlFree
Parameters
pxlPointer to initiated IMG_PIXELS struct data; any old contents are deleted. pxlNr inside the struct is set to or kept at zero.
pxlNrNr of pixels to allocate

Definition at line 44 of file pixel.c.

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}
void pxlFree(IMG_PIXELS *pxl)
Definition pixel.c:28
long long int _pxlNr

Referenced by pxlAllocateMore().

◆ pxlAllocateMore()

int pxlAllocateMore ( IMG_PIXELS * pxl,
long long int pxlNr )

Allocate memory for more IMG_PIXELS data. Any previous contents are preserved. Return Returns 0 when successful.

See also
pxlInit, pxlFree, pxlAllocate, pxlMakeRoom
Parameters
pxlPointer to initiated IMG_PIXELS struct data; any old contents are preserved, but existing data is not required. pxlNr inside the struct is set to or kept at zero.
pxlNrNr of additional pixels to allocate; if struct contains unused space for requested pixels already, then nothing is done.

Definition at line 74 of file pixel.c.

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}
int pxlAllocate(IMG_PIXELS *pxl, long long int pxlNr)
Definition pixel.c:44

Referenced by pxlAdd(), pxlAddFromMask(), pxlMakeRoom(), and pxlRead().

◆ pxlFree()

void pxlFree ( IMG_PIXELS * pxl)

Free memory allocated for IMG_PIXELS. All data is cleared.

See also
pxlInit, pxlAllocate
Parameters
pxlPointer to IMG_PIXELS struct

Definition at line 28 of file pixel.c.

31 {
32 if(pxl==NULL) return;
33 free(pxl->p);
34 pxlInit(pxl);
35}
void pxlInit(IMG_PIXELS *pxl)
Definition pixel.c:14

Referenced by imgMaskFloodFill(), and pxlAllocate().

◆ pxlGet()

int pxlGet ( IMG_PIXELS * list,
long long int i,
IMG_PIXEL * pxl )

Get a pixel from IMG_PIXELS list.

See also
pxlInit, pxlFree, pxlAllocate, pxlMakeRoom, pxlAdd, pxlRead
Returns
0 if successful.
Parameters
listPointer to IMG_PIXELS struct, containing the list of pixels.
iPixel list index [0..list->pxlNr-1].
pxlPointer to IMG_PIXEL struct into which pixel coordinates are written.

Definition at line 163 of file pixel.c.

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}

Referenced by imgMaskFloodFill().

◆ pxlInit()

void pxlInit ( IMG_PIXELS * pxl)

Initiate the IMG_PIXELS struct before any use.

See also
pxlAllocate, pxlFree
Author
Vesa Oikonen
Parameters
pxlPointer to IMG_PIXELS

Definition at line 14 of file pixel.c.

17 {
18 if(pxl==NULL) return;
19 pxl->pxlNr=pxl->_pxlNr=0;
20 pxl->p=NULL;
21}

Referenced by imgMaskFloodFill(), and pxlFree().

◆ pxlMakeRoom()

int pxlMakeRoom ( IMG_PIXELS * list,
long long int i,
long long int n )

Make room for new pixels in the IMG_PIXELS list, allocating more memory if needed. Previous contents are preserved but moved in the list.

See also
pxlAllocateMore
Returns
0 if successful.
Parameters
listPointer to IMG_PIXELS struct with existing contents.
iIndex [0..pxlNr] of the new room start position.
nNr of empty list items to add.

Definition at line 110 of file pixel.c.

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}

Referenced by imgMaskFloodFill().

◆ pxlMove()

void pxlMove ( IMG_PIXELS * list,
long long int from,
long long int to )

Move pixel from one slot to another inside IMG_PIXELS data, changing the position of others accordingly.

See also
pxlInit, pxlFree, pxlAllocate, pxlRm, pxlMakeRoom
Parameters
listPointer to IMG_PIXELS struct, which must be initiated.
fromIndex [0.._pxlNr-1] of source position.
toIndex [0.._pxlNr-1] of target position.

Definition at line 220 of file pixel.c.

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}

Referenced by pxlRm().

◆ pxlRead()

int pxlRead ( IMG_PIXELS * pxl,
const char * fname,
char * status )

Read IMG_PIXELS data from specified file.

See also
pxlInit, pxlFree, pxlWrite
Returns
0 if successful.
Author
Vesa Oikonen
Parameters
pxlPointer to IMG_PIXELS struct, into which contents of file are to be added; call pxlInit() once before using this function.
fnamePointer to the file name; this string is not modified.
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 331 of file pixel.c.

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}
int atoi_with_check(const char *int_as_string, int *result_value)
Definition decpoint.c:238
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

◆ pxlRm()

int pxlRm ( IMG_PIXELS * list,
long long int index )

Remove specified pixel from IMG_PIXELS data.

Returns
0 if successful.
Author
Vesa Oikonen
See also
pxlInit, pxlFree, pxlRmDuplicates, pxlAdd, pxlAllocate, pxlMakeRoom, pxlGet
Parameters
listPointer to IMG_PIXELS struct, which must be initiated. Allocated memory is not reduced, but pxlNr is decreased.
indexIndex [0..pxlNr-1] of pixel to delete.

Definition at line 249 of file pixel.c.

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}
void pxlMove(IMG_PIXELS *list, long long int from, long long int to)
Definition pixel.c:220

Referenced by pxlRmDuplicates().

◆ pxlRmDuplicates()

long long int pxlRmDuplicates ( IMG_PIXELS * list)

Remove duplicates from IMG_PIXELS data.

Returns
the nr of removed pixels.
Author
Vesa Oikonen
See also
pxlInit, pxlFree, pxlRm, pxlAdd, pxlAllocate, pxlMakeRoom, pxlGet
Parameters
listPointer to IMG_PIXELS struct, which must be initiated. Allocated memory is not reduced, but pxlNr is decreased.

Definition at line 273 of file pixel.c.

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}
int pxlRm(IMG_PIXELS *list, long long int index)
Definition pixel.c:249

◆ pxlWrite()

int pxlWrite ( IMG_PIXELS * pxl,
FILE * fp,
char * status )

Write IMG_PIXELS data into specified file.

Returns
0 if successful.
Author
Vesa Oikonen
See also
pxlRead, pxlFree, pxlGet, pxlAdd
Parameters
pxlPointer to IMG_PIXELS struct, contents of which are to be written
fpOutput file pointer
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 299 of file pixel.c.

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}