dspf_header.c 4.9 KB

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