GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dspf_header.c
Go to the documentation of this file.
1 #include <string.h>
2 #include "viz.h"
3 
4 
5 /*================= DOCUMENT RETURN VALUES! =================*/
6 
8 {
9  int isize, flsize;
10  cmndln_info *linep;
11  FILE *fp;
12  long Where_dataoff;
13  long Where_lookoff;
14 
15  linep = &(headp->linefax);
16  fp = headp->dspfoutfp;
17 
18  isize = sizeof(int);
19  flsize = sizeof(float);
20  /* print the header code on first line of file */
21  if (!fwrite(DSPF_ID, strlen(DSPF_ID), 1, fp))
22  return (-1);
23  /* the dimensions of the data */
24  if (1 != fwrite(&headp->xdim, isize, 1, fp))
25  return (-1);
26  if (1 != fwrite(&headp->ydim, isize, 1, fp))
27  return (-1);
28  if (1 != fwrite(&headp->zdim, isize, 1, fp))
29  return (-1);
30 
31 
32  /* print out code for min and max values */
33  if (1 != fwrite(&headp->min, flsize, 1, fp))
34  return (-1);
35  if (1 != fwrite(&headp->max, flsize, 1, fp))
36  return (-1);
37 
38  /* the litmodel stored for each polygon */
39  if (1 != fwrite(&linep->litmodel, isize, 1, fp))
40  return (-1);
41 
42  /* write the total number of thresholds to be searched for */
43  if (1 != fwrite(&linep->nthres, isize, 1, fp))
44  return (-1);
45  /* write the array of thresholds out */
46  if ((fwrite(linep->tvalue, flsize, linep->nthres, fp)) != linep->nthres) {
47  fprintf(stderr, "ERROR: fwrite in dspf_header.c\n");
48  return (-1);
49  }
50 
51  /* write the offset to the lookup table */
52  /* the first time this number is set to 0 */
53  /*this information will be overwritten after dspf is done */
54  /* ftell keeps track of where this information is to be placed */
55  Where_lookoff = ftell(fp);
56  headp->Lookoff = 0;
57  if (1 != fwrite(&headp->Lookoff, sizeof(long), 1, fp))
58  return (-1);
59 
60  /* code to determine the length of the binary file header */
61  /* Dataoff = length of the header */
62  /*Dataoff = strlen (DSPF_ID) + 7*isize + 5*flsize + linep->nthres*flsize; */
63  Where_dataoff = ftell(fp);
64  headp->Dataoff = 0;
65  if (1 != fwrite(&headp->Dataoff, sizeof(long), 1, fp))
66  return (-1);
67 
68 
69 
70  /* End of header, now go back and fill in what we can */
71  headp->Dataoff = ftell(fp);
72  fseek(fp, Where_dataoff, 0);
73  if (1 != fwrite(&headp->Dataoff, sizeof(long), 1, fp))
74  return (-1);
75 
76  fseek(fp, headp->Dataoff, 0); /* and return to begin writing data */
77 
78  /* will still have to come back once more to fill in Lookup offset */
79 
80  return (0);
81 }
82 
83 
84 /**************************** dfread_header **********************************/
85 
86 /**************************** dfread_header **********************************/
87 
88 /**************************** dfread_header **********************************/
89 
91 {
92  int isize, flsize;
93  FILE *fp;
94  cmndln_info *linep;
95  char buf[80];
96  int len;
97 
98  fp = headp->dspfinfp;
99 
100 
101  len = strlen(DSPF_ID);
102  fseek(fp, 0L, 0); /* rewind file */
103  /*read in header information and store in File_info struct */
104 
105  if (!fread(buf, 1, len, fp))
106  return (-1);
107  buf[len] = 0;
108  if (strncmp(DSPF_ID, buf, len)) {
109  if (!strncmp("dspf003.01", buf, len))
110  return (dfread_header_old(headp, fp));
111 
112  fprintf(stderr, "Error: header mismatch '%s' - '%s'\n", DSPF_ID, buf);
113  return (-1);
114  }
115  linep = &(headp->linefax);
116  isize = sizeof(int);
117  flsize = sizeof(float);
118 
119  if (!fread(&headp->xdim, isize, 1, fp))
120  return (-1);
121  if (!fread(&headp->ydim, isize, 1, fp))
122  return (-1);
123  if (!fread(&headp->zdim, isize, 1, fp))
124  return (-1);
125  if (!fread(&headp->min, flsize, 1, fp))
126  return (-1);
127  if (!fread(&headp->max, flsize, 1, fp))
128  return (-1);
129  if (!fread(&linep->litmodel, isize, 1, fp))
130  return (-1);
131  if (!fread(&linep->nthres, isize, 1, fp))
132  return (-1);
133  if (!fread(linep->tvalue, flsize, linep->nthres, fp))
134  return (-1);
135  if (!fread(&headp->Lookoff, isize, 1, fp))
136  return (-1);
137  if (!fread(&headp->Dataoff, isize, 1, fp))
138  return (-1);
139 
140  print_head_info(headp);
141 
142  return (1);
143 }
144 
145 int dfread_header_old(file_info * headp, FILE * fp)
146 {
147  int isize, flsize;
148  cmndln_info *linep;
149  float tmp;
150 
151  linep = &(headp->linefax);
152  isize = sizeof(int);
153  flsize = sizeof(float);
154 
155  if (!fread(&headp->xdim, isize, 1, fp))
156  return (-1);
157  if (!fread(&headp->ydim, isize, 1, fp))
158  return (-1);
159  if (!fread(&headp->zdim, isize, 1, fp))
160  return (-1);
161  if (!fread(&tmp, flsize, 1, fp))
162  return (-1);
163  if (!fread(&tmp, flsize, 1, fp))
164  return (-1);
165  if (!fread(&tmp, flsize, 1, fp))
166  return (-1);
167  if (!fread(&headp->min, flsize, 1, fp))
168  return (-1);
169  if (!fread(&headp->max, flsize, 1, fp))
170  return (-1);
171  if (!fread(&linep->litmodel, isize, 1, fp))
172  return (-1);
173  if (!fread(&linep->nthres, isize, 1, fp))
174  return (-1);
175  if (!fread(linep->tvalue, flsize, linep->nthres, fp))
176  return (-1);
177  if (!fread(&headp->Lookoff, isize, 1, fp))
178  return (-1);
179  if (!fread(&headp->Dataoff, isize, 1, fp))
180  return (-1);
181 
182  print_head_info(headp);
183 
184  return (1);
185 }
int dfread_header_old(file_info *headp, FILE *fp)
Definition: dspf_header.c:145
float tvalue[MAXTHRESH]
Definition: viz.h:24
int dfwrite_header(file_info *headp)
Definition: dspf_header.c:7
int litmodel
Definition: viz.h:25
float min
Definition: viz.h:41
Definition: viz.h:28
int ydim
Definition: viz.h:34
int dfread_header(file_info *headp)
Definition: dspf_header.c:90
cmndln_info linefax
Definition: viz.h:44
int
Definition: g3dcolor.c:48
long Dataoff
Definition: viz.h:42
int zdim
Definition: viz.h:34
long Lookoff
Definition: viz.h:43
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
#define DSPF_ID
Definition: viz.h:6
FILE * dspfinfp
Definition: viz.h:33
int xdim
Definition: viz.h:34
FILE * dspfoutfp
Definition: viz.h:33
float max
Definition: viz.h:41
int nthres
Definition: viz.h:23