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);
897 return get_map_row(fd, buf, row, data_type, 0, 1);
976 static int open_null_read(
int fd)
1011 static int read_null_bits(
int null_fd,
unsigned char *flags,
int row,
1018 if (compute_window_row(fd, row, &R) <= 0) {
1027 offset = (off_t) size *R;
1029 if (lseek(null_fd, offset, SEEK_SET) < 0) {
1030 G_warning(_(
"Error reading null row %d"), R);
1034 if (read(null_fd, flags, size) != size) {
1035 G_warning(_(
"Error reading null row %d"), R);
1042 static void get_null_value_row_nomask(
int fd,
char *flags,
int row)
1048 G_warning(_(
"Reading raster map <%s@%s> request for row %d is outside region"),
1062 null_fd = open_null_read(fd);
1114 _(
"Unable to realloc buffer"));
1134 static void get_null_value_row_gdal(
int fd,
char *flags,
int row)
1140 if (get_map_row_nomask(fd, tmp_buf, row, DCELL_TYPE) <= 0) {
1148 flags[i] = !fcb->
col_map[i] ||
1149 memcmp(&tmp_buf[i], &fcb->
gdal->
null_val,
sizeof(DCELL)) == 0;
1160 static void embed_mask(
char *flags,
int row)
1178 static void get_null_value_row(
int fd,
char *flags,
int row,
int with_mask)
1183 get_null_value_row_gdal(fd, flags, row);
1186 get_null_value_row_nomask(fd, flags, row);
1189 embed_mask(flags, row);
1192 static int embed_nulls(
int fd,
void *buf,
int row, RASTER_MAP_TYPE
map_type,
1193 int null_is_zero,
int with_mask)
1204 get_null_value_row(fd,
G__.
null_buf, row, with_mask);
1247 get_null_value_row(fd, flags, row, 1);
int G_is_c_null_value(const CELL *cellVal)
Returns 1 if cell is NULL, 0 otherwise. This will test if the value cell is the largest int...
int G_get_c_raster_row_nomask(int fd, CELL *buf, int row)
Read raster row without masking (CELL type)
int G_get_d_raster_row(int fd, DCELL *buf, int row)
Get raster row (DCELL type)
void G_free(void *buf)
Free allocated memory.
void G__set_null_value(void *rast, int numVals, int null_is_zero, RASTER_MAP_TYPE data_type)
CELL * G_allocate_c_raster_buf(void)
Allocates memory for a raster map of type CELL.
int G_get_map_row_nomask(int fd, CELL *buf, int row)
Read raster row without masking (this routine is deprecated)
int G_get_raster_row(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Get raster row.
unsigned char * null_work_buf
int G_zero_raster_buf(void *rast, RASTER_MAP_TYPE data_type)
Zero a raster buffer.
CELL G_quant_get_cell_value(struct Quant *q, DCELL dcellVal)
Returns a CELL category for the floating-point value based on the quantization rules in q...
int G_get_f_raster_row_nomask(int fd, FCELL *buf, int row)
Read raster row without masking (FCELL type)
int G_get_f_raster_row(int fd, FCELL *buf, int row)
Get raster row (FCELL type)
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
int G_get_map_row(int fd, CELL *buf, int row)
Get raster row (this routine is deprecated!)
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
int G_set_raster_value_c(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
Places a CELL raster value.
int G_get_c_raster_row(int fd, CELL *buf, int row)
Get raster row (CELL type)
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
int G_get_null_value_row(int fd, char *flags, int row)
Read or simmulate null value row.
int G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
int G__init_null_bits(unsigned char *flags, int cols)
int G__convert_flags_01(char *zero_ones, const unsigned char *flags, int n)
int G__null_bitstream_size(int cols)
Determines null bitstream size.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
struct fileinfo * fileinfo
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
unsigned char * compressed_buf
void G_set_c_null_value(CELL *cellVals, int numVals)
char * G_find_file_misc(const char *dir, const char *element, char *name, const char *mapset)
int G_get_d_raster_row_nomask(int fd, DCELL *buf, int row)
Read raster row without masking (DCELL type)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G__check_null_bit(const unsigned char *flags, int bit_num, int n)
unsigned char * NULL_ROWS[NULL_ROWS_INMEM]
int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
If the data_type is CELL_TYPE, calls G_is_c_null_value ((CELL *) rast); If the data_type is FCELL_TYP...
int G_get_raster_row_nomask(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
Read raster row without masking.
DCELL * G_allocate_d_raster_buf(void)
Allocates memory for a raster map of type DCELL.