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