Using stdlib.h rather than malloc.h
[snark14.git] / src / snark / read_eval_phantom1.c
1 /*
2  ***********************************************************
3  $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4  $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/read_eval_phantom1.c $
5  $LastChangedRevision: 92 $
6  $Date: 2014-07-02 17:46:48 -0400 (Wed, 02 Jul 2014) $
7  $Author: agulati $
8  ***********************************************************
9
10  Previously part of SuperSNARK
11  */
12
13 /* ------------------------- read_eval_phantom1.c -------------------------
14
15  This function reads from the eval file the abnormality index for all
16  the structures in the phantom.
17
18  INPUTS: None.
19
20  OUTPUTS:
21  numstr - number of structures in phantom.
22  phantom - array of length numstr containing the average pixel values (i.e the
23  abnormality index) for each structure in the phantom.
24  regions - array of length numstr containing the structure numbers
25  strarea - array of length numstr containing the area (i.e number of pixels)
26  for each structure.
27  */
28
29 #include <stdlib.h>
30 #include "experimenter.h"
31 #include "errorc.h"
32 #include "read_eval_phantom1.h"
33
34 void read_eval_phantom1(int** regions, int* numstr, double** phantom, int** strarea)
35 {
36         int reg, area, count = 0;
37         double average;
38         char string[MAXLINESIZE];
39         FILE *evalfile;
40
41         if ((evalfile = fopen("eval", "r")) == NULL)
42         {
43                 errorc("in read_eval_phantom1.c: error in opening file ", "eval");
44         }
45
46         while (1)
47         { /* Read eval file until first line of numbers is reached
48          then break from while loop */
49
50                 fgets(string, sizeof(string), evalfile);
51                 if (sscanf(string, "%d %d %lf", &reg, &area, &average) == 3)
52                         break;
53                 else
54                         ;
55         }
56         /* Allocate memory for the arrays phantom, strarea and
57          regions. Store abnormality index, area and structure
58          number for the first line (from previous while loop).
59          Count is a counter for the number of structures read
60          in. */
61
62         *phantom = (double *) malloc(sizeof(double));
63         *regions = (int *) malloc(sizeof(int));
64         *strarea = (int *) malloc(sizeof(int));
65         if ((*phantom == NULL) || (*regions == NULL))
66                 errorc("memory allocation failed in ", "read_eval_phantom.c");
67         (*phantom)[count] = average;
68         (*strarea)[count] = area;
69         (*regions)[count] = reg;
70         count++;
71
72         while (1)
73         { /* Read in abnormality indexes, areas and structure numbers
74          and allocate more memory for the new values. Break from
75          the while loop when finished */
76
77                 fgets(string, sizeof(string), evalfile);
78                 if (sscanf(string, "%d %d %lf", &reg, &area, &average) == 3)
79                 {
80
81                         *phantom = (double *) realloc(*phantom, (count + 1) * sizeof(double));
82                         *strarea = (int *) realloc(*strarea, (count + 1) * sizeof(int));
83                         *regions = (int *) realloc(*regions, (count + 1) * sizeof(int));
84                         if (((*phantom) == NULL) || ((*regions) == NULL) || ((*strarea) == NULL))
85                         {
86                                 errorc("memory reallocation failed in ", "read_eval_phantom.c");
87                         }
88
89                         (*phantom)[count] = average;
90                         (*strarea)[count] = area;
91                         (*regions)[count] = reg;
92                         count++;
93                 }
94                 else
95                 {
96                         break;
97                 }
98         }
99         *numstr = count;
100
101         fclose(evalfile);
102 }