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