GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cube_io.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include "viz.h"
3 
4 static unsigned char Buffer[10000]; /* buffer for outputting data to file */
5 
6 /*
7  ** Buffer Format:
8  ** n_thresholds // char //
9  ** Jump cnt // short //
10  ** n_polys [n_thresholds] // char (nybble?) //
11  ** thresh_indexes [n_thresholds] // char //
12  ** poly_info [n_thresholds] // char v[3][3];n[3][3]; //
13  **
14  ** if (n_thresholds < 0) then -n_threshlds == number of consecutive cubes
15  ** on current row that do NOT contain any threshold info, and thus any
16  ** data space in draw file.
17  ** If val[ n_thresholds(i) ] < 0 then next byte is
18  ** 'n_thresholds(i+(-n_threholds(i)))'
19  **
20  ** BUT, this code will simply place a 0 in 1st byte, and send it on to
21  * lower routine that writes out compressed data.
22  */
23 
24 int write_cube(Cube_data * Cube, /* array of poly info by threshold */
25  int cur_x, file_info * headfax)
26 {
27  register int i, j;
28  register int size; /* final size of data written */
29  register int offset1; /* pointer to n_polys */
30  register int offset2; /* pointer to thresh_indexes */
31  register int offset3 = 0; /* pointer to poly_info */
32  poly_info *Poly_info;
33  int t_cnt;
34 
35  t_cnt = Cube->n_thresh;
36 
37  Buffer[0] = t_cnt;
38 
39  if (t_cnt) {
40  offset1 = 3; /* pointer to n_polys */
41  offset2 = 3 + t_cnt; /* pointer to thresh_indexes */
42  offset3 = 3 + t_cnt + t_cnt; /* pointer to poly_info */
43 
44  /*poly_size = sizeof (poly_info) * t_cnt; */
45 
46  for (i = 0; i < Cube->n_thresh; i++) { /* n_thresholds loop */
47  Buffer[offset1++] = Cube->data[i].npoly;
48  Buffer[offset2++] = Cube->data[i].t_ndx; /* THRESHOLD INDEX */
49 
50  for (j = 0; j < Cube->data[i].npoly; j++) {
51  Poly_info = &(Cube->data[i].poly[j]);
52  /*memcpy (Buffer[offset3], Cube->data[i].poly_info,poly_size); */
53  Buffer[offset3++] = Poly_info->v1[0];
54  Buffer[offset3++] = Poly_info->v1[1];
55  Buffer[offset3++] = Poly_info->v1[2];
56  Buffer[offset3++] = Poly_info->v2[0];
57  Buffer[offset3++] = Poly_info->v2[1];
58  Buffer[offset3++] = Poly_info->v2[2];
59  Buffer[offset3++] = Poly_info->v3[0];
60  Buffer[offset3++] = Poly_info->v3[1];
61  Buffer[offset3++] = Poly_info->v3[2];
62  Buffer[offset3++] = Poly_info->n1[0];
63  Buffer[offset3++] = Poly_info->n1[1];
64  Buffer[offset3++] = Poly_info->n1[2];
65 
66  /* DEBUG */
67  if (headfax->linefax.litmodel > 1) { /* 3 normals */
68  Buffer[offset3++] = Poly_info->n2[0];
69  Buffer[offset3++] = Poly_info->n2[1];
70  Buffer[offset3++] = Poly_info->n2[2];
71  Buffer[offset3++] = Poly_info->n3[0];
72  Buffer[offset3++] = Poly_info->n3[1];
73  Buffer[offset3++] = Poly_info->n3[2];
74  }
75  }
76  }
77  size = offset3 - 3; /* 3 is 1st 3 bytes header */
78  Buffer[1] = (size >> 8) & 0xff; /* write short Big-endian */
79  Buffer[2] = size & 0xff;
80  }
81 
82  /*fprintf(stderr,"before write_cube_buffer\n"); */
83  write_cube_buffer(Buffer, offset3, cur_x, headfax); /* write it out to file */
84 
85  return 0;
86 }
87 
88 /*
89  ** Still have to add code to build index table
90  ** Also I am going to incorporate this into build_output before we're done
91  */
92 int write_cube_buffer(unsigned char *Buffer, int size,
93  int cur_x, file_info * headfax)
94 {
95  static int num_zero = 0;
96  unsigned char junk;
97 
98  if (!Buffer[0]) {
99  num_zero++;
100  if (num_zero == 126 || cur_x == headfax->xdim - 2) {
101  junk = 0x80 | num_zero;
102  fwrite(&junk, 1, 1, headfax->dspfoutfp);
103  num_zero = 0;
104  }
105  }
106  else {
107  /* first write out zero data */
108  if (num_zero) {
109  junk = 0x80 | num_zero;
110  fwrite(&junk, 1, 1, headfax->dspfoutfp);
111  num_zero = 0;
112  }
113 
114  /* then the current buffer */
115  fwrite(Buffer, 1, size, headfax->dspfoutfp);
116  }
117 
118  return 0;
119 }
120 
121 static long fsize = 0;
122 static char *fptr = NULL;
123 
124 /*
125  ** expects headfax->dspfinfp to be pointing to current cube
126  ** i.e. already searched up to this point (alowing of course
127  ** for 0 data already read in
128  **
129  ** returns num_thresholds or 0 for no data or -1 on error
130  **
131  ** expects linefax and headfax to be filled in.
132  */
133 int read_cube(Cube_data * Cube, file_info * headfax)
134 {
135  register int offset1, offset2, offset3;
136  int t_cnt;
137  int ret;
138  int i, j, size;
139  unsigned char inchar;
140  poly_info *Poly_info;
141  static int first = 1;
142  FILE *fp;
143 
144  static int zeros_left = 0; /* move this out if a seek routine is written */
145 
146  fp = headfax->dspfinfp;
147  first = !fsize;
148  if (first)
149  zeros_left = 0;
150 
151  while (first) { /* use while instead of if to utilize 'break' !! */
152  /* try reading the entire file into memory */
153  long start, stop, i;
154  int ret;
155 
156  first = 0;
157 
158  start = ftell(fp);
159  fseek(fp, 0L, 2);
160  stop = ftell(fp);
161  fsize = stop - start + 1;
162  fseek(fp, start, 0);
163  if (fptr) {
164  free(fptr);
165  fptr = NULL;
166  }
167  if (NULL == (fptr = malloc(fsize))) {
168  /*DEBUG*/ fprintf(stderr, "Malloc failed\n");
169  fsize = 0;
170  break;
171  }
172 
173  for (i = 0; ret = fread(fptr + i, 1, 10240, fp); i += ret) ;
174  }
175 
176  if (zeros_left) {
177  --zeros_left;
178  return Cube->n_thresh = 0;
179  }
180 
181  my_fread(&inchar, 1, 1, fp); /* use signed char */
182  if (inchar & 0x80) {
183  zeros_left = (0x7f & inchar) - 1;
184  return Cube->n_thresh = 0;
185  }
186  else /*read in cubefax data */
187  t_cnt = inchar;
188 
189  /* read in size info */
190  my_fread(&inchar, 1, 1, fp); /* read in size of cube data */
191  size = inchar << 8;
192  my_fread(&inchar, 1, 1, fp);
193  size |= inchar;
194 
195  if (0 >= (ret = my_fread(Buffer, 1, size, fp))) {
196  fprintf(stderr, "Error reading display file offset %ld\n", ftell(fp));
197  return (-1);
198  }
199 
200  if (ret != size) {
201  fprintf(stderr, "Error (size) reading display file offset %ld\n",
202  ftell(fp));
203  return (-1);
204  }
205 
206 
207  {
208  offset1 = 0; /* pointer to n_polys */
209  offset2 = t_cnt; /* pointer to thresh_indexes */
210  offset3 = t_cnt + t_cnt; /* pointer to poly_info */
211 
212  for (i = 0; i < t_cnt; i++) { /* n_thresholds loop */
213  Cube->data[i].npoly = Buffer[offset1++];
214  Cube->data[i].t_ndx = Buffer[offset2++]; /* THRESHOLD INDEX */
215 
216  for (j = 0; j < Cube->data[i].npoly; j++) {
217  Poly_info = &(Cube->data[i].poly[j]);
218  Poly_info->v1[0] = Buffer[offset3++];
219  Poly_info->v1[1] = Buffer[offset3++];
220  Poly_info->v1[2] = Buffer[offset3++];
221  Poly_info->v2[0] = Buffer[offset3++];
222  Poly_info->v2[1] = Buffer[offset3++];
223  Poly_info->v2[2] = Buffer[offset3++];
224  Poly_info->v3[0] = Buffer[offset3++];
225  Poly_info->v3[1] = Buffer[offset3++];
226  Poly_info->v3[2] = Buffer[offset3++];
227  Poly_info->n1[0] = Buffer[offset3++];
228  Poly_info->n1[1] = Buffer[offset3++];
229  Poly_info->n1[2] = Buffer[offset3++];
230  /*
231  fprintf(stderr,"# %f ",Poly_info->v1[0]);
232  fprintf(stderr,"%f ",Poly_info->v1[1]);
233  fprintf(stderr,"%f \n",Poly_info->v1[2]);
234  */
235  if (headfax->linefax.litmodel > 1) { /* 3 normals */
236  Poly_info->n2[0] = Buffer[offset3++];
237  Poly_info->n2[1] = Buffer[offset3++];
238  Poly_info->n2[2] = Buffer[offset3++];
239  Poly_info->n3[0] = Buffer[offset3++];
240  Poly_info->n3[1] = Buffer[offset3++];
241  Poly_info->n3[2] = Buffer[offset3++];
242  }
243  }
244  }
245  }
246  return Cube->n_thresh = t_cnt;
247 }
248 
249 #ifdef NEWCODE
250 int my_fread(char *buf, int size, int cnt, FILE * fp)
251 {
252  static char in_buf[10240];
253  static char *start, *end;
254  char *outp;
255  int ret;
256 
257  if (ret = fread(in_buf, 1, 10240, fp)) ;
258 
259 
260  return 0;
261 }
262 #else
263 
264 static int cptr = 0;
265 
266 int my_fread(char *buf, int size, int cnt, FILE * fp)
267 {
268  if (!fsize)
269  return fread(buf, size, cnt, fp);
270  else {
271  int amt;
272 
273  amt = size * cnt;
274  if (cptr + amt >= fsize)
275  amt = fsize - cptr - 1;
276  struct_copy(buf, fptr + cptr, amt);
277  cptr += amt;
278  return (amt);
279  }
280 
281  return 0;
282 }
283 
284 int reset_reads(file_info * headfax)
285 {
286  if (!fsize)
287  fseek(headfax->dspfinfp, headfax->Dataoff, 0);
288  else
289  cptr = 0;
290 
291  return 0;
292 }
293 
294 int new_dspf(file_info * hfax)
295 {
296  fseek(hfax->dspfinfp, hfax->Dataoff, 0);
297  cptr = fsize = 0;
298 
299  return 0;
300 }
301 #endif
int write_cube(Cube_data *Cube, int cur_x, file_info *headfax)
Definition: cube_io.c:24
float v2[3]
Definition: viz.h:53
float n2[3]
Definition: viz.h:55
int litmodel
Definition: viz.h:25
int struct_copy(char *To, char *From, int size)
Definition: struct_copy.c:2
float n3[3]
Definition: viz.h:55
Definition: viz.h:28
int write_cube_buffer(unsigned char *Buffer, int size, int cur_x, file_info *headfax)
Definition: cube_io.c:92
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int t_ndx
Definition: viz.h:61
Definition: viz.h:50
void * malloc(YYSIZE_T)
Definition: viz.h:65
cmndln_info linefax
Definition: viz.h:44
float v3[3]
Definition: viz.h:54
int my_fread(char *buf, int size, int cnt, FILE *fp)
Definition: cube_io.c:266
cube_info data[MAXTHRESH]
Definition: viz.h:68
long Dataoff
Definition: viz.h:42
int first
Definition: form/open.c:25
int n_thresh
Definition: viz.h:67
int new_dspf(file_info *hfax)
Definition: cube_io.c:294
int read_cube(Cube_data *Cube, file_info *headfax)
Definition: cube_io.c:133
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int cur_x
Definition: driver/init.c:37
float n1[3]
Definition: viz.h:55
return NULL
Definition: dbfopen.c:1394
FILE * dspfinfp
Definition: viz.h:33
void free(void *)
int xdim
Definition: viz.h:34
poly_info poly[MAXPOLY]
Definition: viz.h:62
int npoly
Definition: viz.h:60
float v1[3]
Definition: viz.h:52
FILE * dspfoutfp
Definition: viz.h:33
int reset_reads(file_info *headfax)
Definition: cube_io.c:284