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