GRASS Programmer's Manual  6.4.2(2012)
cube_io.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "viz.h"
00003 
00004 static unsigned char Buffer[10000];     /* buffer for outputting data to file */
00005 
00006 /*
00007  **  Buffer Format:
00008  **    n_thresholds                       //  char                      //
00009  **    Jump cnt                           //  short                     //
00010  **    n_polys        [n_thresholds]      //  char  (nybble?)           //
00011  **    thresh_indexes [n_thresholds]      //  char                      //
00012  **    poly_info      [n_thresholds]      //  char v[3][3];n[3][3];     //
00013  **
00014  **   if (n_thresholds < 0) then -n_threshlds == number of consecutive cubes
00015  **     on current row  that do NOT contain any threshold info, and thus any
00016  **     data space in draw file.
00017  **   If val[ n_thresholds(i) ] < 0  then next byte is  
00018  **       'n_thresholds(i+(-n_threholds(i)))'
00019  **
00020  **   BUT, this code will simply place a 0 in 1st byte, and send it on to
00021  *       lower routine that writes out compressed data.
00022  */
00023 
00024 int write_cube(Cube_data * Cube,        /* array of poly info  by threshold */
00025                int cur_x, file_info * headfax)
00026 {
00027     register int i, j;
00028     register int size;          /* final size of data written */
00029     register int offset1;       /* pointer to n_polys */
00030     register int offset2;       /* pointer to thresh_indexes */
00031     register int offset3 = 0;   /* pointer to poly_info */
00032     poly_info *Poly_info;
00033     int t_cnt;
00034 
00035     t_cnt = Cube->n_thresh;
00036 
00037     Buffer[0] = t_cnt;
00038 
00039     if (t_cnt) {
00040         offset1 = 3;            /* pointer to n_polys */
00041         offset2 = 3 + t_cnt;    /* pointer to thresh_indexes */
00042         offset3 = 3 + t_cnt + t_cnt;    /* pointer to poly_info */
00043 
00044         /*poly_size = sizeof (poly_info) * t_cnt; */
00045 
00046         for (i = 0; i < Cube->n_thresh; i++) {  /* n_thresholds loop */
00047             Buffer[offset1++] = Cube->data[i].npoly;
00048             Buffer[offset2++] = Cube->data[i].t_ndx;    /* THRESHOLD INDEX */
00049 
00050             for (j = 0; j < Cube->data[i].npoly; j++) {
00051                 Poly_info = &(Cube->data[i].poly[j]);
00052                 /*memcpy (Buffer[offset3], Cube->data[i].poly_info,poly_size); */
00053                 Buffer[offset3++] = Poly_info->v1[0];
00054                 Buffer[offset3++] = Poly_info->v1[1];
00055                 Buffer[offset3++] = Poly_info->v1[2];
00056                 Buffer[offset3++] = Poly_info->v2[0];
00057                 Buffer[offset3++] = Poly_info->v2[1];
00058                 Buffer[offset3++] = Poly_info->v2[2];
00059                 Buffer[offset3++] = Poly_info->v3[0];
00060                 Buffer[offset3++] = Poly_info->v3[1];
00061                 Buffer[offset3++] = Poly_info->v3[2];
00062                 Buffer[offset3++] = Poly_info->n1[0];
00063                 Buffer[offset3++] = Poly_info->n1[1];
00064                 Buffer[offset3++] = Poly_info->n1[2];
00065 
00066                 /* DEBUG */
00067                 if (headfax->linefax.litmodel > 1) {    /* 3 normals */
00068                     Buffer[offset3++] = Poly_info->n2[0];
00069                     Buffer[offset3++] = Poly_info->n2[1];
00070                     Buffer[offset3++] = Poly_info->n2[2];
00071                     Buffer[offset3++] = Poly_info->n3[0];
00072                     Buffer[offset3++] = Poly_info->n3[1];
00073                     Buffer[offset3++] = Poly_info->n3[2];
00074                 }
00075             }
00076         }
00077         size = offset3 - 3;     /* 3 is 1st 3 bytes header */
00078         Buffer[1] = (size >> 8) & 0xff; /* write short Big-endian */
00079         Buffer[2] = size & 0xff;
00080     }
00081 
00082     /*fprintf(stderr,"before write_cube_buffer\n"); */
00083     write_cube_buffer(Buffer, offset3, cur_x, headfax); /* write it out to file */
00084 
00085     return 0;
00086 }
00087 
00088 /*
00089  **  Still have to add code to build index table 
00090  **   Also I am going to incorporate this into build_output before we're done
00091  */
00092 int write_cube_buffer(unsigned char *Buffer, int size,
00093                       int cur_x, file_info * headfax)
00094 {
00095     static int num_zero = 0;
00096     unsigned char junk;
00097 
00098     if (!Buffer[0]) {
00099         num_zero++;
00100         if (num_zero == 126 || cur_x == headfax->xdim - 2) {
00101             junk = 0x80 | num_zero;
00102             fwrite(&junk, 1, 1, headfax->dspfoutfp);
00103             num_zero = 0;
00104         }
00105     }
00106     else {
00107         /* first write out zero data */
00108         if (num_zero) {
00109             junk = 0x80 | num_zero;
00110             fwrite(&junk, 1, 1, headfax->dspfoutfp);
00111             num_zero = 0;
00112         }
00113 
00114         /* then the current buffer */
00115         fwrite(Buffer, 1, size, headfax->dspfoutfp);
00116     }
00117 
00118     return 0;
00119 }
00120 
00121 static long fsize = 0;
00122 static char *fptr = NULL;
00123 
00124 /*
00125  ** expects headfax->dspfinfp to be pointing to current cube
00126  **  i.e. already searched up to this point  (alowing of course
00127  **  for 0 data already read in 
00128  **
00129  **  returns num_thresholds  or 0 for no data  or  -1 on error
00130  **
00131  **  expects linefax and headfax to be filled in.
00132  */
00133 int read_cube(Cube_data * Cube, file_info * headfax)
00134 {
00135     register int offset1, offset2, offset3;
00136     int t_cnt;
00137     int ret;
00138     int i, j, size;
00139     unsigned char inchar;
00140     poly_info *Poly_info;
00141     static int first = 1;
00142     FILE *fp;
00143 
00144     static int zeros_left = 0;  /* move this out if a seek routine is written */
00145 
00146     fp = headfax->dspfinfp;
00147     first = !fsize;
00148     if (first)
00149         zeros_left = 0;
00150 
00151     while (first) {             /* use while instead of if to utilize 'break' !! */
00152         /* try reading the entire file into memory */
00153         long start, stop, i;
00154         int ret;
00155 
00156         first = 0;
00157 
00158         start = ftell(fp);
00159         fseek(fp, 0L, 2);
00160         stop = ftell(fp);
00161         fsize = stop - start + 1;
00162         fseek(fp, start, 0);
00163         if (fptr) {
00164             free(fptr);
00165             fptr = NULL;
00166         }
00167         if (NULL == (fptr = malloc(fsize))) {
00168              /*DEBUG*/ fprintf(stderr, "Malloc failed\n");
00169             fsize = 0;
00170             break;
00171         }
00172 
00173         for (i = 0; ret = fread(fptr + i, 1, 10240, fp); i += ret) ;
00174     }
00175 
00176     if (zeros_left) {
00177         --zeros_left;
00178         return Cube->n_thresh = 0;
00179     }
00180 
00181     my_fread(&inchar, 1, 1, fp);        /* use signed char */
00182     if (inchar & 0x80) {
00183         zeros_left = (0x7f & inchar) - 1;
00184         return Cube->n_thresh = 0;
00185     }
00186     else                        /*read in cubefax data */
00187         t_cnt = inchar;
00188 
00189     /* read in size info */
00190     my_fread(&inchar, 1, 1, fp);        /* read in size of cube data */
00191     size = inchar << 8;
00192     my_fread(&inchar, 1, 1, fp);
00193     size |= inchar;
00194 
00195     if (0 >= (ret = my_fread(Buffer, 1, size, fp))) {
00196         fprintf(stderr, "Error reading display file offset %ld\n", ftell(fp));
00197         return (-1);
00198     }
00199 
00200     if (ret != size) {
00201         fprintf(stderr, "Error (size) reading display file offset %ld\n",
00202                 ftell(fp));
00203         return (-1);
00204     }
00205 
00206 
00207     {
00208         offset1 = 0;            /* pointer to n_polys */
00209         offset2 = t_cnt;        /* pointer to thresh_indexes */
00210         offset3 = t_cnt + t_cnt;        /* pointer to poly_info */
00211 
00212         for (i = 0; i < t_cnt; i++) {   /* n_thresholds loop */
00213             Cube->data[i].npoly = Buffer[offset1++];
00214             Cube->data[i].t_ndx = Buffer[offset2++];    /* THRESHOLD INDEX */
00215 
00216             for (j = 0; j < Cube->data[i].npoly; j++) {
00217                 Poly_info = &(Cube->data[i].poly[j]);
00218                 Poly_info->v1[0] = Buffer[offset3++];
00219                 Poly_info->v1[1] = Buffer[offset3++];
00220                 Poly_info->v1[2] = Buffer[offset3++];
00221                 Poly_info->v2[0] = Buffer[offset3++];
00222                 Poly_info->v2[1] = Buffer[offset3++];
00223                 Poly_info->v2[2] = Buffer[offset3++];
00224                 Poly_info->v3[0] = Buffer[offset3++];
00225                 Poly_info->v3[1] = Buffer[offset3++];
00226                 Poly_info->v3[2] = Buffer[offset3++];
00227                 Poly_info->n1[0] = Buffer[offset3++];
00228                 Poly_info->n1[1] = Buffer[offset3++];
00229                 Poly_info->n1[2] = Buffer[offset3++];
00230                 /*
00231                    fprintf(stderr,"# %f ",Poly_info->v1[0]);
00232                    fprintf(stderr,"%f ",Poly_info->v1[1]);
00233                    fprintf(stderr,"%f \n",Poly_info->v1[2]);
00234                  */
00235                 if (headfax->linefax.litmodel > 1) {    /* 3 normals */
00236                     Poly_info->n2[0] = Buffer[offset3++];
00237                     Poly_info->n2[1] = Buffer[offset3++];
00238                     Poly_info->n2[2] = Buffer[offset3++];
00239                     Poly_info->n3[0] = Buffer[offset3++];
00240                     Poly_info->n3[1] = Buffer[offset3++];
00241                     Poly_info->n3[2] = Buffer[offset3++];
00242                 }
00243             }
00244         }
00245     }
00246     return Cube->n_thresh = t_cnt;
00247 }
00248 
00249 #ifdef NEWCODE
00250 int my_fread(char *buf, int size, int cnt, FILE * fp)
00251 {
00252     static char in_buf[10240];
00253     static char *start, *end;
00254     char *outp;
00255     int ret;
00256 
00257     if (ret = fread(in_buf, 1, 10240, fp)) ;
00258 
00259 
00260     return 0;
00261 }
00262 #else
00263 
00264 static int cptr = 0;
00265 
00266 int my_fread(char *buf, int size, int cnt, FILE * fp)
00267 {
00268     if (!fsize)
00269         return fread(buf, size, cnt, fp);
00270     else {
00271         int amt;
00272 
00273         amt = size * cnt;
00274         if (cptr + amt >= fsize)
00275             amt = fsize - cptr - 1;
00276         struct_copy(buf, fptr + cptr, amt);
00277         cptr += amt;
00278         return (amt);
00279     }
00280 
00281     return 0;
00282 }
00283 
00284 int reset_reads(file_info * headfax)
00285 {
00286     if (!fsize)
00287         fseek(headfax->dspfinfp, headfax->Dataoff, 0);
00288     else
00289         cptr = 0;
00290 
00291     return 0;
00292 }
00293 
00294 int new_dspf(file_info * hfax)
00295 {
00296     fseek(hfax->dspfinfp, hfax->Dataoff, 0);
00297     cptr = fsize = 0;
00298 
00299     return 0;
00300 }
00301 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines