18 #include <sys/types.h>
20 #include <rpc/types.h>
23 #include <grass/config.h>
24 #include <grass/glocale.h>
30 #define NULL_FILE "null"
34 static int embed_nulls(
int,
void *,
int, RASTER_MAP_TYPE,
int,
int);
38 static int compute_window_row(
int fd,
int row,
int *cellRow)
46 G_warning(_(
"Reading raster map <%s@%s> request for row %d is outside region"),
53 f = row * fcb->
C1 + fcb->
C2;
58 if (r < 0 || r >= fcb->
cellhd.rows)
68 static void do_reclass_int(
int fd,
void *cell,
int null_is_zero)
72 CELL *reclass_table = fcb->
reclass.table;
84 if (c[i] < min || c[i] > max) {
92 c[i] = reclass_table[c[i] -
min];
101 static int read_data_fp_compressed(
int fd,
int row,
unsigned char *data_buf,
106 off_t t2 = fcb->
row_ptr[row + 1];
107 size_t readamount = t2 - t1;
110 if (lseek(fd, t1, SEEK_SET) < 0)
115 if ((
size_t) G_zlib_read(fd, readamount, data_buf, bufsize) != bufsize)
123 static void rle_decompress(
unsigned char *dst,
const unsigned char *src,
124 int nbytes,
int size)
126 int pairs = size / (nbytes + 1);
129 for (i = 0; i < pairs; i++) {
133 for (j = 0; j < repeat; j++) {
134 memcpy(dst, src, nbytes);
142 static int read_data_compressed(
int fd,
int row,
unsigned char *data_buf,
147 off_t t2 = fcb->
row_ptr[row + 1];
148 ssize_t readamount = t2 - t1;
152 if (lseek(fd, t1, SEEK_SET) < 0)
155 if (read(fd, cmp, readamount) != readamount)
159 if (fcb->
cellhd.compressed > 0) {
161 n = *nbytes = *cmp++;
166 n = *nbytes = fcb->
nbytes;
168 if (fcb->
cellhd.compressed < 0 || readamount < n * fcb->
cellhd.cols) {
169 if (fcb->
cellhd.compressed == 2)
170 G_zlib_expand(cmp, readamount, data_buf, n * fcb->
cellhd.cols);
172 rle_decompress(data_buf, cmp, n, readamount);
175 memcpy(data_buf, cmp, readamount);
182 static int read_data_uncompressed(
int fd,
int row,
unsigned char *data_buf,
190 if (lseek(fd, (off_t) row * bufsize, SEEK_SET) == -1)
193 if (read(fd, data_buf, bufsize) != bufsize)
202 static int read_data_gdal(
int fd,
int row,
unsigned char *data_buf,
int *nbytes)
209 err = G_gdal_raster_IO(
210 fcb->
gdal->band, GF_Read, 0, row, fcb->
cellhd.cols, 1, data_buf,
213 return err == CE_None ? 0 : -1;
221 static int read_data(
int fd,
int row,
unsigned char *data_buf,
int *nbytes)
227 return read_data_gdal(fd, row, data_buf, nbytes);
230 if (!fcb->
cellhd.compressed)
231 return read_data_uncompressed(fd, row, data_buf, nbytes);
236 return read_data_compressed(fd, row, data_buf, nbytes);
238 return read_data_fp_compressed(fd, row, data_buf, nbytes);
245 static void cell_values_int(
int fd,
const unsigned char *
data,
251 int big = (size_t) nbytes >=
sizeof(CELL);
254 for (i = 0; i < n; i++) {
255 const unsigned char *d;
265 if (cmap[i] == cmapold) {
270 d = data + (cmap[i] - 1) * nbytes;
272 if (big && (*d & 0x80)) {
281 for (j = 1; j <
nbytes; j++)
292 static void cell_values_float(
int fd,
const unsigned char *data,
306 for (i = 0; i < n; i++) {
312 if (cmap[i] == cmapold) {
317 if (cmap[i] < cmapold) {
322 while (cmapold++ != cmap[i])
323 if (!xdr_float(xdrs, &c[i]))
324 G_fatal_error(_(
"cell_values_float: xdr_float failed for index %d"),
333 static void cell_values_double(
int fd,
const unsigned char *data,
347 for (i = 0; i < n; i++) {
353 if (cmap[i] == cmapold) {
358 if (cmap[i] < cmapold) {
363 while (cmapold++ != cmap[i])
364 if (!xdr_double(xdrs, &c[i]))
365 G_fatal_error(_(
"cell_values_double: xdr_double failed for index %d"),
380 static void gdal_values_int(
int fd,
const unsigned char *data,
385 const unsigned char *d;
389 for (i = 0; i < n; i++) {
395 if (cmap[i] == cmapold) {
400 d = data + (cmap[i] - 1) * nbytes;
402 switch (fcb->
gdal->type) {
403 case GDT_Byte: cell[i] = *(GByte *)d;
break;
404 case GDT_Int16: cell[i] = *(GInt16 *)d;
break;
405 case GDT_UInt16: cell[i] = *(GUInt16 *)d;
break;
406 case GDT_Int32: cell[i] = *(GInt32 *)d;
break;
407 case GDT_UInt32: cell[i] = *(GUInt32 *)d;
break;
420 static void gdal_values_float(
int fd,
const float *data,
427 for (i = 0; i < n; i++) {
433 if (cmap[i] == cmapold) {
438 cell[i] = data[cmap[i] - 1];
446 static void gdal_values_double(
int fd,
const double *data,
453 for (i = 0; i < n; i++) {
459 if (cmap[i] == cmapold) {
464 cell[i] = data[cmap[i] - 1];
488 static void transfer_to_cell_XX(
int fd,
void *cell)
490 static void (*cell_values_type[3]) () = {
491 cell_values_int, cell_values_float, cell_values_double};
493 static void (*gdal_values_type[3]) () = {
494 gdal_values_int, gdal_values_float, gdal_values_double};
512 static void transfer_to_cell_fi(
int fd,
void *cell)
520 ((CELL *) cell)[i] = (fcb->
col_map[i] == 0)
526 static void transfer_to_cell_di(
int fd,
void *cell)
534 ((CELL *) cell)[i] = (fcb->
col_map[i] == 0)
542 static void transfer_to_cell_if(
int fd,
void *cell)
552 static void transfer_to_cell_df(
int fd,
void *cell)
564 static void transfer_to_cell_id(
int fd,
void *cell)
574 static void transfer_to_cell_fd(
int fd,
void *cell)
593 static int get_map_row_nomask(
int fd,
void *rast,
int row,
594 RASTER_MAP_TYPE data_type)
596 static void (*transfer_to_cell_FtypeOtype[3][3]) () = { {
597 transfer_to_cell_XX, transfer_to_cell_if, transfer_to_cell_id}, {
598 transfer_to_cell_fi, transfer_to_cell_XX, transfer_to_cell_fd}, {
599 transfer_to_cell_di, transfer_to_cell_df, transfer_to_cell_XX}};
604 rowStatus = compute_window_row(fd, row, &r);
606 if (rowStatus <= 0) {
620 if (fcb->
cellhd.compressed)
621 G_warning(_(
"Error reading compressed map <%s@%s>, row %d"),
624 G_warning(_(
"Error reading map <%s@%s>, row %d"),
633 (transfer_to_cell_FtypeOtype[fcb->
map_type][data_type]) (fd, rast);
640 static int get_map_row_no_reclass(
int fd,
void *rast,
int row,
641 RASTER_MAP_TYPE data_type,
int null_is_zero,
646 stat = get_map_row_nomask(fd, rast, row, data_type);
650 stat = embed_nulls(fd, rast, row, data_type, null_is_zero, with_mask);
659 static int get_map_row(
int fd,
void *rast,
int row, RASTER_MAP_TYPE data_type,
660 int null_is_zero,
int with_mask)
679 get_map_row_no_reclass(fd, buf, row, type, null_is_zero, with_mask);
689 do_reclass_int(fd, buf, null_is_zero);
691 if (data_type == CELL_TYPE)
742 return get_map_row(fd, buf, row, CELL_TYPE, 1, 0);
761 RASTER_MAP_TYPE data_type)
763 return get_map_row(fd, buf, row, data_type, 0, 0);
850 return get_map_row(fd, buf, row, CELL_TYPE, 1, 1);
898 return get_map_row(fd, buf, row, data_type, 0, 1);
977 static int open_null_read(
int fd)
1012 static int read_null_bits(
int null_fd,
unsigned char *flags,
int row,
1019 if (compute_window_row(fd, row, &R) <= 0) {
1028 offset = (off_t) size *R;
1030 if (lseek(null_fd, offset, SEEK_SET) < 0) {
1031 G_warning(_(
"Error reading null row %d"), R);
1035 if (read(null_fd, flags, size) != size) {
1036 G_warning(_(
"Error reading null row %d"), R);
1043 static void get_null_value_row_nomask(
int fd,
char *flags,
int row)
1049 G_warning(_(
"Reading raster map <%s@%s> request for row %d is outside region"),
1063 null_fd = open_null_read(fd);
1115 _(
"Unable to realloc buffer"));
1135 static void get_null_value_row_gdal(
int fd,
char *flags,
int row)
1141 if (get_map_row_nomask(fd, tmp_buf, row, DCELL_TYPE) <= 0) {
1149 flags[i] = !fcb->
col_map[i] ||
1150 memcmp(&tmp_buf[i], &fcb->
gdal->
null_val,
sizeof(DCELL)) == 0;
1161 static void embed_mask(
char *flags,
int row)
1179 static void get_null_value_row(
int fd,
char *flags,
int row,
int with_mask)
1184 get_null_value_row_gdal(fd, flags, row);
1187 get_null_value_row_nomask(fd, flags, row);
1190 embed_mask(flags, row);
1193 static int embed_nulls(
int fd,
void *buf,
int row, RASTER_MAP_TYPE
map_type,
1194 int null_is_zero,
int with_mask)
1205 get_null_value_row(fd,
G__.
null_buf, row, with_mask);
1248 get_null_value_row(fd, flags, row, 1);