format.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <grass/gis.h>
  2. #include <grass/glocale.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <grass/config.h>
  6. #ifdef HAVE_UNISTD_H
  7. #include <unistd.h>
  8. #endif
  9. #include "G.h"
  10. /*!
  11. <h3>GRASS Raster Format</h3>
  12. Small example to illustrate the raster format:
  13. A file may contain the following 3x3 floating point matrix:
  14. \verbatim
  15. 10.000 20.000 30.000
  16. 20.000 40.000 50.000
  17. 30.000 50.000 60.000
  18. \endverbatim
  19. The header is a single byte, equal to sizeof(off_t) (typically 4 on a
  20. 32-bit platform, 8 on a 64-bit platform). Then, NROWS+1 offsets are
  21. written as off_t's (i.e. 4 or 8 bytes, depending upon platform) in
  22. big-endian (Motorola) byte order.
  23. <P>
  24. Thus, above example is actually interpreted as:
  25. \verbatim
  26. 4 sizeof(off_t)
  27. 0 0 0 17 offset of row 0
  28. 0 0 0 36 offset of row 1
  29. 0 0 0 55 offset of row 2
  30. 0 0 0 74 offset of end of data
  31. \endverbatim
  32. See G__write_row_ptrs() below for the code which writes this data.
  33. However, note that the row offsets are initially zero;
  34. they get overwritten later (if you are writing compressed data,
  35. you don't know how much space it will require until you've compressed
  36. it).
  37. As for the format of the actual row data, see put_fp_data() in
  38. src/libes/gis/put_row.c and RFC 1014 (the XDR specification):
  39. http://www.faqs.org/rfcs/rfc1014.html
  40. */
  41. /**********************************************************************
  42. *
  43. * G__check_format(int fd)
  44. *
  45. * Check to see if map with file descriptor "fd" is in compressed
  46. * format. If it is, the offset table at the beginning of the
  47. * file (which gives seek addresses into the file where code for
  48. * each row is found) is read into the File Control Buffer (FCB).
  49. * The compressed flag in the FCB is appropriately set.
  50. *
  51. * returns: 1 if row pointers were read successfully, -1 otherwise
  52. **********************************************************************/
  53. int G__check_format(int fd)
  54. {
  55. struct fileinfo *fcb = &G__.fileinfo[fd];
  56. unsigned char compress[4];
  57. /*
  58. * Check to see if the file is in compress mode
  59. * 4 possibilites
  60. * compressed flag in cellhd is negative (meaning pre 3.0 cell file)
  61. * compression flag is first 3 bytes of cell file
  62. * compression flag is 0 - not compressed
  63. * compression flag is 1 - compressed using RLE (int) or zlib (FP)
  64. * compression flag is 2 - compressed using zlib
  65. */
  66. if (fcb->cellhd.compressed < 0) {
  67. if (read(fd, compress, 3) != 3
  68. || compress[0] != 251 || compress[1] != 255 || compress[2] != 251)
  69. fcb->cellhd.compressed = 0;
  70. }
  71. if (!fcb->cellhd.compressed)
  72. return fd;
  73. /* allocate space to hold the row address array */
  74. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  75. /* read the row address array */
  76. return G__read_row_ptrs(fd);
  77. }
  78. int G__read_row_ptrs(int fd)
  79. {
  80. struct fileinfo *fcb = &G__.fileinfo[fd];
  81. int nrows = fcb->cellhd.rows;
  82. unsigned char nbytes;
  83. unsigned char *buf, *b;
  84. int n;
  85. int row;
  86. /*
  87. * pre3.0 row addresses were written directly from the array of off_t's
  88. * (this makes them machine dependent)
  89. */
  90. if (fcb->cellhd.compressed < 0) {
  91. n = (nrows + 1) * sizeof(off_t);
  92. if (read(fd, fcb->row_ptr, n) != n)
  93. goto badread;
  94. return 1;
  95. }
  96. /*
  97. * 3.0 row address array is in a machine independent format
  98. * (warning - the format will work even if the sizeof(off_t) is
  99. * not the same from machine to machine, as long as the
  100. * actual values do not exceed the capability of the off_t)
  101. */
  102. if (read(fd, &nbytes, 1) != 1)
  103. goto badread;
  104. if (nbytes == 0)
  105. goto badread;
  106. n = (nrows + 1) * nbytes;
  107. buf = G_malloc(n);
  108. if (read(fd, buf, n) != n)
  109. goto badread;
  110. for (row = 0, b = buf; row <= nrows; row++) {
  111. off_t v = 0;
  112. for (n = 0; n < (int)nbytes; n++) {
  113. unsigned char c = *b++;
  114. if (nbytes > sizeof(off_t) && n < nbytes - sizeof(off_t) &&
  115. c != 0)
  116. goto badread;
  117. v <<= 8;
  118. v += c;
  119. }
  120. fcb->row_ptr[row] = v;
  121. }
  122. G_free(buf);
  123. return 1;
  124. badread:
  125. G_warning(_("Fail of initial read of compressed file [%s in %s]"),
  126. fcb->name, fcb->mapset);
  127. return -1;
  128. }
  129. int G__write_row_ptrs(int fd)
  130. {
  131. struct fileinfo *fcb = &G__.fileinfo[fd];
  132. int nrows = fcb->cellhd.rows;
  133. int nbytes = sizeof(off_t);
  134. unsigned char *buf, *b;
  135. int len, row, result;
  136. lseek(fd, 0L, SEEK_SET);
  137. len = (nrows + 1) * nbytes + 1;
  138. b = buf = G_malloc(len);
  139. *b++ = nbytes;
  140. for (row = 0; row <= nrows; row++) {
  141. off_t v = fcb->row_ptr[row];
  142. int i;
  143. for (i = nbytes - 1; i >= 0; i--) {
  144. b[i] = v & 0xff;
  145. v >>= 8;
  146. }
  147. b += nbytes;
  148. }
  149. result = (write(fd, buf, len) == len);
  150. G_free(buf);
  151. return result;
  152. }