dspf_header.c 4.8 KB

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