2
#include <grass/glocale.h>
6
#include <grass/config.h>
16
<h3>GRASS Raster Format</h3>
18
Small example to illustrate the raster format:
20
A file may contain the following 3x3 floating point matrix:
27
The header is a single byte, equal to sizeof(off_t) (typically 4 on a
28
32-bit platform, 8 on a 64-bit platform). Then, NROWS+1 offsets are
29
written as off_t's (i.e. 4 or 8 bytes, depending upon platform) in
30
big-endian (Motorola) byte order.
32
Thus, above example is actually interpreted as:
35
0 0 0 17 offset of row 0
36
0 0 0 36 offset of row 1
37
0 0 0 55 offset of row 2
38
0 0 0 74 offset of end of data
41
See G__write_row_ptrs() below for the code which writes this data.
42
However, note that the row offsets are initially zero;
43
they get overwritten later (if you are writing compressed data,
44
you don't know how much space it will require until you've compressed
47
As for the format of the actual row data, see put_fp_data() in
48
src/libes/gis/put_row.c and RFC 1014 (the XDR specification):
49
http://www.faqs.org/rfcs/rfc1014.html
53
/**********************************************************************
55
* G__check_format(int fd)
57
* Check to see if map with file descriptor "fd" is in compressed
58
* format. If it is, the offset table at the beginning of the
59
* file (which gives seek addresses into the file where code for
60
* each row is found) is read into the File Control Buffer (FCB).
61
* The compressed flag in the FCB is appropriately set.
63
* returns: 1 if row pointers were read successfully, -1 otherwise
64
**********************************************************************/
66
int G__check_format(int fd)
68
struct fileinfo *fcb = &G__.fileinfo[fd];
69
unsigned char compress[4];
72
* Check to see if the file is in compress mode
74
* compressed flag in cellhd is negative (meaning pre 3.0 cell file)
75
* compression flag is first 3 bytes of cell file
76
* compression flag is 0 - not compressed
77
* compression flag is 1 - compressed using RLE (int) or zlib (FP)
78
* compression flag is 2 - compressed using zlib
81
if (fcb->cellhd.compressed < 0) {
82
if (read(fd, compress, 3) != 3
83
|| compress[0] != 251 || compress[1] != 255 || compress[2] != 251)
84
fcb->cellhd.compressed = 0;
87
if (!fcb->cellhd.compressed)
90
/* allocate space to hold the row address array */
91
fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
93
/* read the row address array */
94
return G__read_row_ptrs(fd);
97
int G__read_row_ptrs(int fd)
99
struct fileinfo *fcb = &G__.fileinfo[fd];
100
int nrows = fcb->cellhd.rows;
101
unsigned char nbytes;
102
unsigned char *buf, *b;
107
* pre3.0 row addresses were written directly from the array of off_t's
108
* (this makes them machine dependent)
111
if (fcb->cellhd.compressed < 0) {
112
n = (nrows + 1) * sizeof(off_t);
113
if (read(fd, fcb->row_ptr, n) != n)
119
* 3.0 row address array is in a machine independent format
120
* (warning - the format will work even if the sizeof(off_t) is
121
* not the same from machine to machine, as long as the
122
* actual values do not exceed the capability of the off_t)
125
if (read(fd, &nbytes, 1) != 1)
130
n = (nrows + 1) * nbytes;
132
if (read(fd, buf, n) != n)
135
for (row = 0, b = buf; row <= nrows; row++) {
138
for (n = 0; n < (int)nbytes; n++) {
139
unsigned char c = *b++;
141
if (nbytes > sizeof(off_t) && n < nbytes - sizeof(off_t) &&
149
fcb->row_ptr[row] = v;
157
G_warning(_("Fail of initial read of compressed file [%s in %s]"),
158
fcb->name, fcb->mapset);
162
int G__write_row_ptrs(int fd)
164
struct fileinfo *fcb = &G__.fileinfo[fd];
165
int nrows = fcb->cellhd.rows;
166
int nbytes = sizeof(off_t);
167
unsigned char *buf, *b;
168
int len, row, result;
170
lseek(fd, 0L, SEEK_SET);
172
len = (nrows + 1) * nbytes + 1;
173
b = buf = G_malloc(len);
176
for (row = 0; row <= nrows; row++) {
177
off_t v = fcb->row_ptr[row];
180
for (i = nbytes - 1; i >= 0; i--) {
188
result = (write(fd, buf, len) == len);