format.c 5.6 KB

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