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